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)
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 ((remaining (regression-test:do-tests *report-stream*)))
159 (when (regression-test:pending-tests)
160 (incf *error-count* (length remaining))))
162 (let ((sexp-error (list db-type
163 *test-database-underlying-type*
166 (regression-test:pending-tests)
167 (lisp-implementation-type)
168 (lisp-implementation-version)
170 (when *sexp-report-stream*
171 (write sexp-error :stream *sexp-report-stream* :readably t))
172 (push sexp-error *error-list*))
174 (format *report-stream* "~&Tests skipped:")
176 (let ((max-test-name 20))
177 (dolist (skipped skip-tests)
178 (let ((len (length (symbol-name (car skipped)))))
179 (when (> len max-test-name)
180 (setq max-test-name len))))
181 (let ((fmt (format nil "~~& ~~~DA ~~A~~%" max-test-name)))
182 (dolist (skipped skip-tests)
183 (format *report-stream* fmt (car skipped) (cdr skipped)))))
184 (format *report-stream* " None~%")))
188 (defun compute-tests-for-backend (db-type db-underlying-type
189 &key (suites (default-suites)))
190 (let ((test-forms '())
192 (dolist (test-form (if (listp suites) suites (list suites)))
193 (let ((test (second test-form)))
195 ((and (not (eql db-underlying-type :mysql))
196 (clsql-sys:in test :connection/query-command))
197 (push (cons test "Known to only work in mysql as yet.") skip-tests))
198 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
199 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
200 (push (cons test "views not supported.") skip-tests))
201 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
202 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
203 (push (cons test "boolean where not supported.") skip-tests))
204 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
205 (clsql-sys:in test :fdml/select/5 :fdml/select/10
206 :fdml/select/32 :fdml/select/33))
207 (push (cons test "subqueries not supported.") skip-tests))
208 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
210 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
211 (push (cons test "transactions not supported.") skip-tests))
212 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
213 (clsql-sys:in test :fdml/select/1))
214 (push (cons test "fancy math not supported.") skip-tests))
215 ((and (eql *test-database-type* :sqlite)
216 (clsql-sys:in test :fddl/view/4 :fdml/select/10
217 :fdml/select/21 :fdml/select/32
219 (push (cons test "not supported by sqlite.") skip-tests))
220 ((and (eql *test-database-type* :sqlite3)
221 (clsql-sys:in test :fddl/view/4 :fdml/select/10
222 :fdml/select/21 :fdml/select/32
224 (push (cons test "not supported by sqlite3.") skip-tests))
225 ((and (not (clsql-sys:db-type-has-bigint? db-type))
226 (clsql-sys:in test :basic/bigint/1))
227 (push (cons test "bigint not supported.") skip-tests))
228 ((and (eql *test-database-underlying-type* :mysql)
229 (clsql-sys:in test :fdml/select/26))
230 (push (cons test "string table aliases not supported on all mysql versions.") skip-tests))
231 ((and (eql *test-database-underlying-type* :mysql)
232 (clsql-sys:in test :fdml/select/22 :fdml/query/5
233 :fdml/query/7 :fdml/query/8))
234 (push (cons test "not supported by mysql.") skip-tests))
235 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
236 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
237 (push (cons test "union not supported") skip-tests))
238 ((and (eq *test-database-type* :oracle)
239 (clsql-sys:in test :fdml/query/8 :fdml/select/21
241 (push (cons test "syntax not supported.") skip-tests))
242 ((and (eq *test-database-type* :odbc)
243 (eq *test-database-underlying-type* :postgresql)
244 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
245 :fddl/owner/attributes
246 :fddl/owner/attribute-types
248 :fddl/owner/sequence))
249 (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
250 ((and (not (member *test-database-underlying-type*
251 '(:postgresql :oracle)))
252 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
253 :fddl/owner/attributes
254 :fddl/owner/attribute-types
256 :fddl/owner/sequence))
257 (push (cons test "table ownership not supported.") skip-tests))
258 ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
259 (clsql-sys:in test :fdml/query/7))
260 (push (cons test "intersect not supported.") skip-tests))
261 ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
262 (clsql-sys:in test :fdml/query/8))
263 (push (cons test "except not supported.") skip-tests))
264 ((and (eq *test-database-underlying-type* :mssql)
265 (clsql-sys:in test :fdml/select/9))
266 (push (cons test "mssql uses integer math for AVG.") skip-tests))
267 ((and (not (member *test-database-underlying-type*
268 '(:postgresql :mysql :sqlite3)))
269 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
270 (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
271 ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
272 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
273 :oodml/select/15 :oodml/select/16 :oodml/select/17
274 :oodml/select/18 :oodml/select/19 :oodml/select/20
275 :oodml/select/21 :oodml/select/22
276 :oodml/update-records/4 :oodml/update-records/4-slots
277 :oodml/update-records/5 :oodml/update-records/5-slots
278 :oodml/update-records/6 :oodml/update-records/7
279 :oodml/update-records/8 :oodml/update-records/9
280 :oodml/update-records/9-slots :oodml/update-instance/3
281 :oodml/update-instance/4 :oodml/update-instance/5
282 :oodml/update-instance/6 :oodml/update-instance/7
283 :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
284 (push (cons test ":auto-increment not supported.") skip-tests))
285 ((and (not (member *test-database-underlying-type*
286 '(:postgresql :postgresql-socket)))
288 :time/pg/fdml/usec :time/pg/oodml/no-usec :time/pg/oodml/usec))
289 (push (cons test "Postgres specific test.")
291 ((and (member *test-database-underlying-type* '(:mysql))
292 (clsql-sys:in test :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
293 (push (cons test "MySQL does not support fractional seconds on timestamp columns (http://forge.mysql.com/worklog/task.php?id=946).")
297 (push test-form test-forms)))))
298 (values (nreverse test-forms) (nreverse skip-tests))))
300 (defun rapid-load (type &optional (position 0))
301 "Rapid load for interactive testing."
302 (when *default-database*
303 (disconnect :database *default-database*))
304 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
305 ;(test-initialise-database)
309 (rapid-load :postgresql))
315 (rapid-load :oracle))