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*))
68 (defun default-suites ()
69 "The default list of tests to run."
70 (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
71 *rt-ooddl* *rt-oodml* *rt-syntax* *rt-time* *rt-i18n*))
74 (defvar *error-count* 0)
75 (defvar *error-list* nil)
77 (defun run-function-append-report-file (function report-file)
78 (let* ((report-path (etypecase report-file
79 (pathname report-file)
80 (string (parse-namestring report-file))))
81 (sexp-report-path (make-pathname :defaults report-path
83 (with-open-file (rs report-path :direction :output
85 :if-does-not-exist :create)
86 (with-open-file (srs sexp-report-path :direction :output
88 :if-does-not-exist :create)
89 (funcall function :report-stream rs :sexp-report-stream srs)))))
91 (defun run-tests-append-report-file (report-file)
92 (run-function-append-report-file 'run-tests report-file))
95 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil)
96 (suites (default-suites)))
97 ;; clear SQL-OUTPUT cache
98 (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
99 (let ((specs (read-specs))
100 (*report-stream* report-stream)
101 (*sexp-report-stream* sexp-report-stream)
105 (warn "Not running tests because test configuration file is missing")
106 (return-from run-tests :skipped))
107 (load-necessary-systems specs)
108 (dolist (db-type +all-db-types+)
109 (dolist (spec (db-type-spec db-type specs))
110 (let ((*test-connection-spec* spec)
111 (*test-connection-db-type* db-type))
112 (do-tests-for-backend db-type spec :suites suites)))))
113 (zerop *error-count*))
115 (defun load-necessary-systems (specs)
116 (dolist (db-type +all-db-types+)
117 (when (db-type-spec db-type specs)
118 (clsql-sys:initialize-database-type :database-type db-type))))
120 (defun write-report-banner (report-type db-type stream db-name)
123 ******************************************************************************
124 *** CLSQL ~A begun at ~A
127 *** Database ~:@(~A~)
128 *** Type: ~:@(~A~) backend~A.
129 ******************************************************************************
134 (clsql:utime->time (get-universal-time)))
135 (lisp-implementation-type)
136 (lisp-implementation-version)
140 (if (not (eq db-type *test-database-underlying-type*))
141 (format nil " with underlying type ~:@(~A~)"
142 *test-database-underlying-type*)
146 (defun do-tests-for-backend (db-type spec &key
147 (suites (default-suites)) )
148 (test-connect-to-database db-type spec)
150 (multiple-value-bind (test-forms skip-tests)
151 (compute-tests-for-backend db-type *test-database-underlying-type* :suites suites)
153 (write-report-banner "Test Suite" db-type *report-stream*
154 (database-name-from-spec spec db-type))
156 (regression-test:rem-all-tests)
157 (dolist (test-form test-forms)
160 (let* ((cl:*print-right-margin* *test-report-width*)
161 (remaining (regression-test:do-tests *report-stream*)))
162 (when (regression-test:pending-tests)
163 (incf *error-count* (length remaining))))
165 (let ((sexp-error (list db-type
166 *test-database-underlying-type*
169 (regression-test:pending-tests)
170 (lisp-implementation-type)
171 (lisp-implementation-version)
173 (when *sexp-report-stream*
174 (write sexp-error :stream *sexp-report-stream* :readably t))
175 (push sexp-error *error-list*))
177 (format *report-stream* "~&Tests skipped:")
179 (let ((max-test-name (length (symbol-name (caar skip-tests)))))
180 (dolist (skipped (cdr skip-tests))
181 (let ((len (length (symbol-name (car skipped)))))
182 (when (> len max-test-name)
183 (setq max-test-name len))))
184 (let ((fmt (format nil "~~& ~~~DA ~~A~~%" max-test-name)))
185 (dolist (skipped skip-tests)
186 ;; word-wrap the reason string field
187 (let* ((test (car skipped))
188 (reason (cdr skipped))
189 (rlen (length reason))
190 (rwidth (max 20 (- (or *test-report-width* 80) max-test-name 3)))
191 (rwords (clsql-sys::delimited-string-to-list reason #\space t))
192 (rformat (format nil "~~{~~<~%~~1,~D:;~~A~~> ~~}" rwidth))
193 (rwrapped (format nil rformat rwords))
194 (rlines (clsql-sys::delimited-string-to-list rwrapped #\Newline t)))
195 (dolist (rline rlines)
196 (format *report-stream* fmt (if test
202 (format *report-stream* " None~%")))
206 (defun compute-tests-for-backend (db-type db-underlying-type
207 &key (suites (default-suites)))
208 (let ((test-forms '())
210 (dolist (test-form (if (listp suites) suites (list suites)))
211 (let ((test (second test-form)))
213 ((and (not (eql db-underlying-type :mysql))
214 (clsql-sys:in test :connection/query-command))
215 (push (cons test "known to work only in MySQL as yet.") skip-tests))
216 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
217 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
218 (push (cons test "views not supported.") skip-tests))
219 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
220 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
221 (push (cons test "boolean where not supported.") skip-tests))
222 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
223 (clsql-sys:in test :fdml/select/5 :fdml/select/10
224 :fdml/select/32 :fdml/select/33))
225 (push (cons test "subqueries not supported.") skip-tests))
226 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
228 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
229 (push (cons test "transactions not supported.") skip-tests))
230 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
231 (clsql-sys:in test :fdml/select/1))
232 (push (cons test "fancy math not supported.") skip-tests))
233 ((and (eql *test-database-type* :sqlite)
234 (clsql-sys:in test :fddl/view/4 :fdml/select/10
235 :fdml/select/21 :fdml/select/32
237 (push (cons test "not supported by sqlite.") skip-tests))
238 ((and (eql *test-database-type* :sqlite3)
239 (clsql-sys:in test :fddl/view/4 :fdml/select/10
240 :fdml/select/21 :fdml/select/32
242 (push (cons test "not supported by sqlite3.") skip-tests))
243 ((and (not (clsql-sys:db-type-has-bigint? db-type))
244 (clsql-sys:in test :basic/bigint/1))
245 (push (cons test "bigint not supported.") skip-tests))
246 ((and (eql *test-database-underlying-type* :mysql)
247 (clsql-sys:in test :fdml/select/26))
248 (push (cons test "string table aliases not supported on all MySQL versions.") skip-tests))
249 ((and (eql *test-database-underlying-type* :mysql)
250 (clsql-sys:in test :fdml/select/22 :fdml/query/5
251 :fdml/query/7 :fdml/query/8))
252 (push (cons test "not supported by mysql.") skip-tests))
253 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
254 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
255 (push (cons test "union not supported") skip-tests))
256 ((and (eq *test-database-type* :oracle)
257 (clsql-sys:in test :fdml/query/8 :fdml/select/21
259 (push (cons test "syntax not supported.") skip-tests))
260 ((and (eq *test-database-type* :odbc)
261 (eq *test-database-underlying-type* :postgresql)
262 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
263 :fddl/owner/attributes
264 :fddl/owner/attribute-types
266 :fddl/owner/sequence))
267 (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
268 ((and (not (member *test-database-underlying-type*
269 '(:postgresql :oracle)))
270 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
271 :fddl/owner/attributes
272 :fddl/owner/attribute-types
274 :fddl/owner/sequence))
275 (push (cons test "table ownership not supported.") skip-tests))
276 ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
277 (clsql-sys:in test :fdml/query/7))
278 (push (cons test "intersect not supported.") skip-tests))
279 ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
280 (clsql-sys:in test :fdml/query/8))
281 (push (cons test "except not supported.") skip-tests))
282 ((and (eq *test-database-underlying-type* :mssql)
283 (clsql-sys:in test :fdml/select/9))
284 (push (cons test "mssql uses integer math for AVG.") skip-tests))
285 ((and (not (member *test-database-underlying-type*
286 '(:postgresql :mysql :sqlite3)))
287 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
288 (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
289 ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
290 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
291 :oodml/select/15 :oodml/select/16 :oodml/select/17
292 :oodml/select/18 :oodml/select/19 :oodml/select/20
293 :oodml/select/21 :oodml/select/22
294 :oodml/update-records/4 :oodml/update-records/4-slots
295 :oodml/update-records/5 :oodml/update-records/5-slots
296 :oodml/update-records/6 :oodml/update-records/7
297 :oodml/update-records/8 :oodml/update-records/9
298 :oodml/update-records/9-slots :oodml/update-instance/3
299 :oodml/update-instance/4 :oodml/update-instance/5
300 :oodml/update-instance/6 :oodml/update-instance/7
301 :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
302 (push (cons test ":auto-increment not supported.") skip-tests))
303 ((and (not (member *test-database-underlying-type*
304 '(:postgresql :postgresql-socket)))
306 :time/pg/fdml/usec :time/pg/oodml/no-usec :time/pg/oodml/usec))
307 (push (cons test "Postgres specific test.")
309 ((and (member *test-database-underlying-type* '(:mysql))
310 (clsql-sys:in test :time/cross-platform/msec
311 :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
312 (push (cons test "MySQL doesn't support fractional seconds on timestamp columns (http://forge.mysql.com/worklog/task.php?id=946).")
314 ((and (member *test-database-underlying-type* '(:mssql))
315 (clsql-sys:in test :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
316 (push (cons test "MSSQL doesn't support micro-seconds on datetime columns.")
319 (push test-form test-forms)))))
320 (values (nreverse test-forms) (nreverse skip-tests))))
322 (defun rapid-load (type &optional (position 0))
323 "Rapid load for interactive testing."
324 (when *default-database*
325 (disconnect :database *default-database*))
326 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
327 ;(test-initialise-database)
331 (rapid-load :postgresql))
337 (rapid-load :oracle))