1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File: test-init.lisp
4 ;;;; Authors: Marcus Pearce <m.t.pearce@city.ac.uk>, Kevin Rosenberg
5 ;;;; Created: 30/03/2004
7 ;;;; Initialisation utilities for running regression tests on CLSQL.
9 ;;;; This file is part of CLSQL.
11 ;;;; CLSQL users are granted the rights to distribute and use this software
12 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
13 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
14 ;;;; ======================================================================
16 (in-package #:clsql-tests)
18 (defvar *report-stream* *standard-output* "Stream to send text report.")
19 (defvar *sexp-report-stream* nil "Stream to send sexp report.")
20 (defvar *rt-internal*)
22 (defvar *rt-connection*)
29 ;; Below must be set as nil since test-i18n.lisp is not loaded on all platforms.
30 (defvar *rt-i18n* nil)
32 (defvar *test-database-type* nil)
33 (defvar *test-database-underlying-type* nil)
34 (defvar *test-database-user* nil)
35 (defvar *test-false-database-user* "adsfjalsdkfjlakjsdfl"
36 "For testing ownership, a user that isn't the owner.")
37 (defvar *test-start-utime* nil)
38 (defvar *test-connection-spec* nil)
39 (defvar *test-connection-db-type* nil)
40 (defvar *test-report-width* 80 "Width of test report in ems.")
43 (defun test-connect-to-database (db-type spec)
44 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
45 (ignore-errors (destroy-database spec :database-type db-type))
46 (ignore-errors (create-database spec :database-type db-type)))
48 (setf *test-database-type* db-type)
49 (setf *test-database-user*
51 ((member db-type '(:oracle :odbc :aodbc)) (second spec))
52 ((>= (length spec) 3) (third spec))))
54 ;; Connect to the database
56 :database-type db-type
60 ;; Ensure database is empty
61 (truncate-database :database *default-database*)
63 (setf *test-database-underlying-type*
64 (clsql-sys:database-underlying-type *default-database*))
66 ;; If Postgres, turn off notices to console
67 (when (eql db-type :postgresql)
68 (clsql:execute-command "SET client_min_messages = WARNING"))
72 (defun default-suites ()
73 "The default list of tests to run."
74 (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
75 *rt-ooddl* *rt-oodml* *rt-syntax* *rt-time* *rt-i18n*))
78 (defvar *error-count* 0)
79 (defvar *error-list* nil)
81 (defun run-function-append-report-file (function report-file)
82 (let* ((report-path (etypecase report-file
83 (pathname report-file)
84 (string (parse-namestring report-file))))
85 (sexp-report-path (make-pathname :defaults report-path
87 (with-open-file (rs report-path :direction :output
89 :if-does-not-exist :create)
90 (with-open-file (srs sexp-report-path :direction :output
92 :if-does-not-exist :create)
93 (funcall function :report-stream rs :sexp-report-stream srs)))))
95 (defun run-tests-append-report-file (report-file)
96 (run-function-append-report-file 'run-tests report-file))
99 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil)
100 (suites (default-suites)))
101 ;; clear SQL-OUTPUT cache
102 (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
103 (let ((specs (read-specs))
104 (*report-stream* report-stream)
105 (*sexp-report-stream* sexp-report-stream)
109 (warn "Not running tests because test configuration file is missing")
110 (return-from run-tests :skipped))
111 (load-necessary-systems specs)
112 (dolist (db-type +all-db-types+)
113 (dolist (spec (db-type-spec db-type specs))
114 (let ((*test-connection-spec* spec)
115 (*test-connection-db-type* db-type))
116 (do-tests-for-backend db-type spec :suites suites)))))
117 (zerop *error-count*))
119 (defun load-necessary-systems (specs)
120 (dolist (db-type +all-db-types+)
121 (when (db-type-spec db-type specs)
122 (clsql-sys:initialize-database-type :database-type db-type))))
124 (defun write-report-banner (report-type db-type stream db-name)
127 ******************************************************************************
128 *** CLSQL ~A begun at ~A
131 *** Database ~:@(~A~)
132 *** Type: ~:@(~A~) backend~A.
133 ******************************************************************************
138 (clsql:utime->time (get-universal-time)))
139 (lisp-implementation-type)
140 (lisp-implementation-version)
144 (if (not (eq db-type *test-database-underlying-type*))
145 (format nil " with underlying type ~:@(~A~)"
146 *test-database-underlying-type*)
150 (defun do-tests-for-backend (db-type spec &key
151 (suites (default-suites)) )
152 (test-connect-to-database db-type spec)
154 (multiple-value-bind (test-forms skip-tests)
155 (compute-tests-for-backend db-type *test-database-underlying-type* :suites suites)
157 (write-report-banner "Test Suite" db-type *report-stream*
158 (database-name-from-spec spec db-type))
160 (regression-test:rem-all-tests)
161 (dolist (test-form test-forms)
164 (let* ((cl:*print-right-margin* *test-report-width*)
165 (remaining (regression-test:do-tests *report-stream*)))
166 (when (regression-test:pending-tests)
167 (incf *error-count* (length remaining))))
169 (let ((sexp-error (list db-type
170 *test-database-underlying-type*
173 (regression-test:pending-tests)
174 (lisp-implementation-type)
175 (lisp-implementation-version)
177 (when *sexp-report-stream*
178 (write sexp-error :stream *sexp-report-stream* :readably t))
179 (push sexp-error *error-list*))
181 (format *report-stream* "~&Tests skipped:")
183 (let ((max-test-name (length (symbol-name (caar skip-tests)))))
184 (dolist (skipped (cdr skip-tests))
185 (let ((len (length (symbol-name (car skipped)))))
186 (when (> len max-test-name)
187 (setq max-test-name len))))
188 (let ((fmt (format nil "~~& ~~~DA ~~A~~%" max-test-name)))
189 (dolist (skipped skip-tests)
190 ;; word-wrap the reason string field
191 (let* ((test (car skipped))
192 (reason (cdr skipped))
193 (rlen (length reason))
194 (rwidth (max 20 (- (or *test-report-width* 80) max-test-name 3)))
195 (rwords (clsql-sys::delimited-string-to-list reason #\space t))
196 (rformat (format nil "~~{~~<~%~~1,~D:;~~A~~> ~~}" rwidth))
197 (rwrapped (format nil rformat rwords))
198 (rlines (clsql-sys::delimited-string-to-list rwrapped #\Newline t)))
199 (dolist (rline rlines)
200 (format *report-stream* fmt (if test
206 (format *report-stream* " None~%")))
210 (defun compute-tests-for-backend (db-type db-underlying-type
211 &key (suites (default-suites)))
212 (let ((test-forms '())
214 (dolist (test-form (if (listp suites) suites (list suites)))
215 (let ((test (second test-form)))
217 ((and (not (eql db-underlying-type :mysql))
218 (clsql-sys:in test :connection/query-command))
219 (push (cons test "known to work only in MySQL as yet.") skip-tests))
220 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
221 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
222 (push (cons test "views not supported.") skip-tests))
223 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
224 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
225 (push (cons test "boolean where not supported.") skip-tests))
226 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
227 (clsql-sys:in test :fdml/select/5 :fdml/select/10
228 :fdml/select/32 :fdml/select/33))
229 (push (cons test "subqueries not supported.") skip-tests))
230 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
232 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
233 (push (cons test "transactions not supported.") skip-tests))
234 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
235 (clsql-sys:in test :fdml/select/1))
236 (push (cons test "fancy math not supported.") skip-tests))
237 ((and (eql *test-database-type* :sqlite)
238 (clsql-sys:in test :fddl/view/4 :fdml/select/10
239 :fdml/select/21 :fdml/select/32
241 (push (cons test "not supported by sqlite.") skip-tests))
242 ((and (eql *test-database-type* :sqlite3)
243 (clsql-sys:in test :fddl/view/4 :fdml/select/10
244 :fdml/select/21 :fdml/select/32
246 (push (cons test "not supported by sqlite3.") skip-tests))
247 ((and (not (clsql-sys:db-type-has-bigint? db-type))
248 (clsql-sys:in test :basic/bigint/1))
249 (push (cons test "bigint not supported.") skip-tests))
250 ((and (eql *test-database-underlying-type* :mysql)
251 (clsql-sys:in test :fdml/select/26))
252 (push (cons test "string table aliases not supported on all MySQL versions.") skip-tests))
253 ((and (eql *test-database-underlying-type* :mysql)
254 (clsql-sys:in test :fdml/select/22 :fdml/query/5
255 :fdml/query/7 :fdml/query/8))
256 (push (cons test "not supported by mysql.") skip-tests))
257 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
258 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
259 (push (cons test "union not supported") skip-tests))
260 ((and (eq *test-database-type* :oracle)
261 (clsql-sys:in test :fdml/query/8 :fdml/select/21
263 (push (cons test "syntax not supported.") skip-tests))
264 ((and (eq *test-database-type* :odbc)
265 (eq *test-database-underlying-type* :postgresql)
266 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
267 :fddl/owner/attributes
268 :fddl/owner/attribute-types
270 :fddl/owner/sequence))
271 (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
272 ((and (not (member *test-database-underlying-type*
273 '(:postgresql :oracle)))
274 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
275 :fddl/owner/attributes
276 :fddl/owner/attribute-types
278 :fddl/owner/sequence))
279 (push (cons test "table ownership not supported.") skip-tests))
280 ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
281 (clsql-sys:in test :fdml/query/7))
282 (push (cons test "intersect not supported.") skip-tests))
283 ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
284 (clsql-sys:in test :fdml/query/8))
285 (push (cons test "except not supported.") skip-tests))
286 ((and (eq *test-database-underlying-type* :mssql)
287 (clsql-sys:in test :fdml/select/9))
288 (push (cons test "mssql uses integer math for AVG.") skip-tests))
289 ((and (not (member *test-database-underlying-type*
290 '(:postgresql :mysql :sqlite3 )))
291 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
292 (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
293 ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
294 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
295 :oodml/select/15 :oodml/select/16 :oodml/select/17
296 :oodml/select/18 :oodml/select/19 :oodml/select/20
297 :oodml/select/21 :oodml/select/22 :oodml/select/23
298 :oodml/update-records/4 :oodml/update-records/4-slots
299 :oodml/update-records/5 :oodml/update-records/5-slots
300 :oodml/update-records/6 :oodml/update-records/7
301 :oodml/update-records/8 :oodml/update-records/9
302 :oodml/update-records/9-slots :oodml/update-records/10
303 :oodml/update-records/11 :oodml/update-instance/3
304 :oodml/update-instance/4 :oodml/update-instance/5
305 :oodml/update-instance/6 :oodml/update-instance/7
306 :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
307 (push (cons test ":auto-increment not supported.") skip-tests))
308 ((and (not (member *test-database-underlying-type*
309 '(:postgresql :postgresql-socket)))
311 :time/pg/fdml/usec :time/pg/oodml/no-usec :time/pg/oodml/usec))
312 (push (cons test "Postgres specific test.")
314 ((and (member *test-database-underlying-type* '(:mysql))
315 (clsql-sys:in test :time/cross-platform/msec
316 :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
317 (push (cons test "MySQL doesn't support fractional seconds on timestamp columns (http://forge.mysql.com/worklog/task.php?id=946).")
319 ((and (member *test-database-underlying-type* '(:mssql))
320 (clsql-sys:in test :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
321 (push (cons test "MSSQL doesn't support micro-seconds on datetime columns.")
324 (push test-form test-forms)))))
325 (values (nreverse test-forms) (nreverse skip-tests))))
327 (defun rapid-load (type &optional (position 0))
328 "Rapid load for interactive testing."
329 (when *default-database*
330 (disconnect :database *default-database*))
331 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
332 ;(test-initialise-database)
336 (rapid-load :postgresql))
342 (rapid-load :oracle))