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*)
30 (defvar *test-database-type* nil)
31 (defvar *test-database-underlying-type* nil)
32 (defvar *test-database-user* nil)
33 (defvar *test-false-database-user* "adsfjalsdkfjlakjsdfl"
34 "For testing ownership, a user that isn't the owner.")
35 (defvar *test-start-utime* nil)
36 (defvar *test-connection-spec* nil)
37 (defvar *test-connection-db-type* nil)
38 (defvar *test-report-width* 80 "Width of test report in ems.")
41 (defun test-connect-to-database (db-type spec)
42 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
43 (ignore-errors (destroy-database spec :database-type db-type))
44 (ignore-errors (create-database spec :database-type db-type)))
46 (setf *test-database-type* db-type)
47 (setf *test-database-user*
49 ((member db-type '(:oracle :odbc :aodbc)) (second spec))
50 ((>= (length spec) 3) (third spec))))
52 ;; Connect to the database
54 :database-type db-type
58 ;; Ensure database is empty
59 (truncate-database :database *default-database*)
61 (setf *test-database-underlying-type*
62 (clsql-sys:database-underlying-type *default-database*))
66 (defun default-suites ()
67 "The default list of tests to run."
68 (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
69 *rt-ooddl* *rt-oodml* *rt-syntax* *rt-time*))
72 (defvar *error-count* 0)
73 (defvar *error-list* nil)
75 (defun run-function-append-report-file (function report-file)
76 (let* ((report-path (etypecase report-file
77 (pathname report-file)
78 (string (parse-namestring report-file))))
79 (sexp-report-path (make-pathname :defaults report-path
81 (with-open-file (rs report-path :direction :output
83 :if-does-not-exist :create)
84 (with-open-file (srs sexp-report-path :direction :output
86 :if-does-not-exist :create)
87 (funcall function :report-stream rs :sexp-report-stream srs)))))
89 (defun run-tests-append-report-file (report-file)
90 (run-function-append-report-file 'run-tests report-file))
93 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil)
94 (suites (default-suites)))
95 ;; clear SQL-OUTPUT cache
96 (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
97 (let ((specs (read-specs))
98 (*report-stream* report-stream)
99 (*sexp-report-stream* sexp-report-stream)
103 (warn "Not running tests because test configuration file is missing")
104 (return-from run-tests :skipped))
105 (load-necessary-systems specs)
106 (dolist (db-type +all-db-types+)
107 (dolist (spec (db-type-spec db-type specs))
108 (let ((*test-connection-spec* spec)
109 (*test-connection-db-type* db-type))
110 (do-tests-for-backend db-type spec :suites suites)))))
111 (zerop *error-count*))
113 (defun load-necessary-systems (specs)
114 (dolist (db-type +all-db-types+)
115 (when (db-type-spec db-type specs)
116 (clsql-sys:initialize-database-type :database-type db-type))))
118 (defun write-report-banner (report-type db-type stream db-name)
121 ******************************************************************************
122 *** CLSQL ~A begun at ~A
125 *** Database ~:@(~A~)
126 *** Type: ~:@(~A~) backend~A.
127 ******************************************************************************
132 (clsql:utime->time (get-universal-time)))
133 (lisp-implementation-type)
134 (lisp-implementation-version)
138 (if (not (eq db-type *test-database-underlying-type*))
139 (format nil " with underlying type ~:@(~A~)"
140 *test-database-underlying-type*)
144 (defun do-tests-for-backend (db-type spec &key
145 (suites (default-suites)) )
146 (test-connect-to-database db-type spec)
148 (multiple-value-bind (test-forms skip-tests)
149 (compute-tests-for-backend db-type *test-database-underlying-type* :suites suites)
151 (write-report-banner "Test Suite" db-type *report-stream*
152 (database-name-from-spec spec db-type))
154 (regression-test:rem-all-tests)
155 (dolist (test-form test-forms)
158 (let* ((cl:*print-right-margin* *test-report-width*)
159 (remaining (regression-test:do-tests *report-stream*)))
160 (when (regression-test:pending-tests)
161 (incf *error-count* (length remaining))))
163 (let ((sexp-error (list db-type
164 *test-database-underlying-type*
167 (regression-test:pending-tests)
168 (lisp-implementation-type)
169 (lisp-implementation-version)
171 (when *sexp-report-stream*
172 (write sexp-error :stream *sexp-report-stream* :readably t))
173 (push sexp-error *error-list*))
175 (format *report-stream* "~&Tests skipped:")
177 (let ((max-test-name (length (symbol-name (caar skip-tests)))))
178 (dolist (skipped (cdr skip-tests))
179 (let ((len (length (symbol-name (car skipped)))))
180 (when (> len max-test-name)
181 (setq max-test-name len))))
182 (let ((fmt (format nil "~~& ~~~DA ~~A~~%" max-test-name)))
183 (dolist (skipped skip-tests)
184 ;; word-wrap the reason string field
185 (let* ((test (car skipped))
186 (reason (cdr skipped))
187 (rlen (length reason))
188 (rwidth (max 20 (- (or *test-report-width* 80) max-test-name 3)))
189 (rwords (clsql-sys::delimited-string-to-list reason #\space t))
190 (rformat (format nil "~~{~~<~%~~1,~D:;~~A~~> ~~}" rwidth))
191 (rwrapped (format nil rformat rwords))
192 (rlines (clsql-sys::delimited-string-to-list rwrapped #\Newline t)))
193 (dolist (rline rlines)
194 (format *report-stream* fmt (if test
200 (format *report-stream* " None~%")))
204 (defun compute-tests-for-backend (db-type db-underlying-type
205 &key (suites (default-suites)))
206 (let ((test-forms '())
208 (dolist (test-form (if (listp suites) suites (list suites)))
209 (let ((test (second test-form)))
211 ((and (not (eql db-underlying-type :mysql))
212 (clsql-sys:in test :connection/query-command))
213 (push (cons test "known to work only in MySQL as yet.") skip-tests))
214 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
215 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
216 (push (cons test "views not supported.") skip-tests))
217 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
218 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
219 (push (cons test "boolean where not supported.") skip-tests))
220 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
221 (clsql-sys:in test :fdml/select/5 :fdml/select/10
222 :fdml/select/32 :fdml/select/33))
223 (push (cons test "subqueries not supported.") skip-tests))
224 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
226 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
227 (push (cons test "transactions not supported.") skip-tests))
228 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
229 (clsql-sys:in test :fdml/select/1))
230 (push (cons test "fancy math not supported.") skip-tests))
231 ((and (eql *test-database-type* :sqlite)
232 (clsql-sys:in test :fddl/view/4 :fdml/select/10
233 :fdml/select/21 :fdml/select/32
235 (push (cons test "not supported by sqlite.") skip-tests))
236 ((and (eql *test-database-type* :sqlite3)
237 (clsql-sys:in test :fddl/view/4 :fdml/select/10
238 :fdml/select/21 :fdml/select/32
240 (push (cons test "not supported by sqlite3.") skip-tests))
241 ((and (not (clsql-sys:db-type-has-bigint? db-type))
242 (clsql-sys:in test :basic/bigint/1))
243 (push (cons test "bigint not supported.") skip-tests))
244 ((and (eql *test-database-underlying-type* :mysql)
245 (clsql-sys:in test :fdml/select/26))
246 (push (cons test "string table aliases not supported on all MySQL versions.") skip-tests))
247 ((and (eql *test-database-underlying-type* :mysql)
248 (clsql-sys:in test :fdml/select/22 :fdml/query/5
249 :fdml/query/7 :fdml/query/8))
250 (push (cons test "not supported by mysql.") skip-tests))
251 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
252 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
253 (push (cons test "union not supported") skip-tests))
254 ((and (eq *test-database-type* :oracle)
255 (clsql-sys:in test :fdml/query/8 :fdml/select/21
257 (push (cons test "syntax not supported.") skip-tests))
258 ((and (eq *test-database-type* :odbc)
259 (eq *test-database-underlying-type* :postgresql)
260 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
261 :fddl/owner/attributes
262 :fddl/owner/attribute-types
264 :fddl/owner/sequence))
265 (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
266 ((and (not (member *test-database-underlying-type*
267 '(:postgresql :oracle)))
268 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
269 :fddl/owner/attributes
270 :fddl/owner/attribute-types
272 :fddl/owner/sequence))
273 (push (cons test "table ownership not supported.") skip-tests))
274 ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
275 (clsql-sys:in test :fdml/query/7))
276 (push (cons test "intersect not supported.") skip-tests))
277 ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
278 (clsql-sys:in test :fdml/query/8))
279 (push (cons test "except not supported.") skip-tests))
280 ((and (eq *test-database-underlying-type* :mssql)
281 (clsql-sys:in test :fdml/select/9))
282 (push (cons test "mssql uses integer math for AVG.") skip-tests))
283 ((and (not (member *test-database-underlying-type*
284 '(:postgresql :mysql :sqlite3)))
285 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
286 (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
287 ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
288 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
289 :oodml/select/15 :oodml/select/16 :oodml/select/17
290 :oodml/select/18 :oodml/select/19 :oodml/select/20
291 :oodml/select/21 :oodml/select/22
292 :oodml/update-records/4 :oodml/update-records/4-slots
293 :oodml/update-records/5 :oodml/update-records/5-slots
294 :oodml/update-records/6 :oodml/update-records/7
295 :oodml/update-records/8 :oodml/update-records/9
296 :oodml/update-records/9-slots :oodml/update-instance/3
297 :oodml/update-instance/4 :oodml/update-instance/5
298 :oodml/update-instance/6 :oodml/update-instance/7
299 :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
300 (push (cons test ":auto-increment not supported.") skip-tests))
301 ((and (not (member *test-database-underlying-type*
302 '(:postgresql :postgresql-socket)))
304 :time/pg/fdml/usec :time/pg/oodml/no-usec :time/pg/oodml/usec))
305 (push (cons test "Postgres specific test.")
307 ((and (member *test-database-underlying-type* '(:mysql))
308 (clsql-sys:in test :time/cross-platform/msec
309 :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
310 (push (cons test "MySQL doesn't support fractional seconds on timestamp columns (http://forge.mysql.com/worklog/task.php?id=946).")
312 ((and (member *test-database-underlying-type* '(:mssql))
313 (clsql-sys:in test :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
314 (push (cons test "MSSQL doesn't support micro-seconds on datetime columns.")
317 (push test-form test-forms)))))
318 (values (nreverse test-forms) (nreverse skip-tests))))
320 (defun rapid-load (type &optional (position 0))
321 "Rapid load for interactive testing."
322 (when *default-database*
323 (disconnect :database *default-database*))
324 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
325 ;(test-initialise-database)
329 (rapid-load :postgresql))
335 (rapid-load :oracle))