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*))
67 (defvar *error-count* 0)
68 (defvar *error-list* nil)
70 (defun run-function-append-report-file (function report-file)
71 (let* ((report-path (etypecase report-file
72 (pathname report-file)
73 (string (parse-namestring report-file))))
74 (sexp-report-path (make-pathname :defaults report-path
76 (with-open-file (rs report-path :direction :output
78 :if-does-not-exist :create)
79 (with-open-file (srs sexp-report-path :direction :output
81 :if-does-not-exist :create)
82 (funcall function :report-stream rs :sexp-report-stream srs)))))
84 (defun run-tests-append-report-file (report-file)
85 (run-function-append-report-file 'run-tests report-file))
88 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
89 ;; clear SQL-OUTPUT cache
90 (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
91 (let ((specs (read-specs))
92 (*report-stream* report-stream)
93 (*sexp-report-stream* sexp-report-stream)
97 (warn "Not running tests because test configuration file is missing")
98 (return-from run-tests :skipped))
99 (load-necessary-systems specs)
100 (dolist (db-type +all-db-types+)
101 (dolist (spec (db-type-spec db-type specs))
102 (let ((*test-connection-spec* spec)
103 (*test-connection-db-type* db-type))
104 (do-tests-for-backend db-type spec)))))
105 (zerop *error-count*))
107 (defun load-necessary-systems (specs)
108 (dolist (db-type +all-db-types+)
109 (when (db-type-spec db-type specs)
110 (clsql-sys:initialize-database-type :database-type db-type))))
112 (defun write-report-banner (report-type db-type stream db-name)
115 ******************************************************************************
116 *** CLSQL ~A begun at ~A
119 *** Database ~:@(~A~)
120 *** Type: ~:@(~A~) backend~A.
121 ******************************************************************************
126 (clsql:utime->time (get-universal-time)))
127 (lisp-implementation-type)
128 (lisp-implementation-version)
132 (if (not (eq db-type *test-database-underlying-type*))
133 (format nil " with underlying type ~:@(~A~)"
134 *test-database-underlying-type*)
138 (defun do-tests-for-backend (db-type spec)
139 (test-connect-to-database db-type spec)
142 (multiple-value-bind (test-forms skip-tests)
143 (compute-tests-for-backend db-type *test-database-underlying-type*)
145 (write-report-banner "Test Suite" db-type *report-stream*
146 (database-name-from-spec spec db-type))
148 ; (test-initialise-database)
150 (regression-test:rem-all-tests)
151 (dolist (test-form test-forms)
154 (let ((remaining (regression-test:do-tests *report-stream*)))
155 (when (regression-test:pending-tests)
156 (incf *error-count* (length remaining))))
158 (let ((sexp-error (list db-type
159 *test-database-underlying-type*
162 (regression-test:pending-tests)
163 (lisp-implementation-type)
164 (lisp-implementation-version)
166 (when *sexp-report-stream*
167 (write sexp-error :stream *sexp-report-stream* :readably t))
168 (push sexp-error *error-list*))
170 (format *report-stream* "~&Tests skipped:")
172 (dolist (skipped skip-tests)
173 (format *report-stream*
174 "~& ~30A ~A~%" (car skipped) (cdr skipped)))
175 (format *report-stream* " None~%")))
179 (defun compute-tests-for-backend (db-type db-underlying-type)
180 (let ((test-forms '())
182 (dolist (test-form (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
183 *rt-ooddl* *rt-oodml* *rt-syntax*))
184 (let ((test (second test-form)))
186 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
187 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
188 (push (cons test "views not supported.") skip-tests))
189 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
190 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
191 (push (cons test "boolean where not supported.") skip-tests))
192 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
193 (clsql-sys:in test :fdml/select/5 :fdml/select/10
194 :fdml/select/32 :fdml/select/33))
195 (push (cons test "subqueries not supported.") skip-tests))
196 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
198 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
199 (push (cons test "transactions not supported.") skip-tests))
200 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
201 (clsql-sys:in test :fdml/select/1))
202 (push (cons test "fancy math not supported.") skip-tests))
203 ((and (eql *test-database-type* :sqlite)
204 (clsql-sys:in test :fddl/view/4 :fdml/select/10
205 :fdml/select/21 :fdml/select/32
207 (push (cons test "not supported by sqlite.") skip-tests))
208 ((and (eql *test-database-type* :sqlite3)
209 (clsql-sys:in test :fddl/view/4 :fdml/select/10
210 :fdml/select/21 :fdml/select/32
212 (push (cons test "not supported by sqlite3.") skip-tests))
213 ((and (not (clsql-sys:db-type-has-bigint? db-type))
214 (clsql-sys:in test :basic/bigint/1))
215 (push (cons test "bigint not supported.") skip-tests))
216 ((and (eql *test-database-underlying-type* :mysql)
217 (clsql-sys:in test :fdml/select/26))
218 (push (cons test "string table aliases not supported on all mysql versions.") skip-tests))
219 ((and (eql *test-database-underlying-type* :mysql)
220 (clsql-sys:in test :fdml/select/22 :fdml/query/5
221 :fdml/query/7 :fdml/query/8))
222 (push (cons test "not supported by mysql.") skip-tests))
223 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
224 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
225 (push (cons test "union not supported") skip-tests))
226 ((and (eq *test-database-type* :oracle)
227 (clsql-sys:in test :fdml/query/8 :fdml/select/21
229 (push (cons test "syntax not supported.") skip-tests))
230 ((and (eq *test-database-type* :odbc)
231 (eq *test-database-underlying-type* :postgresql)
232 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
233 :fddl/owner/attributes
234 :fddl/owner/attribute-types
236 :fddl/owner/sequence))
237 (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
238 ((and (not (member *test-database-underlying-type*
239 '(:postgresql :oracle)))
240 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
241 :fddl/owner/attributes
242 :fddl/owner/attribute-types
244 :fddl/owner/sequence))
245 (push (cons test "table ownership not supported.") skip-tests))
246 ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
247 (clsql-sys:in test :fdml/query/7))
248 (push (cons test "intersect not supported.") skip-tests))
249 ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
250 (clsql-sys:in test :fdml/query/8))
251 (push (cons test "except not supported.") skip-tests))
252 ((and (eq *test-database-underlying-type* :mssql)
253 (clsql-sys:in test :fdml/select/9))
254 (push (cons test "mssql uses integer math for AVG.") skip-tests))
255 ((and (not (member *test-database-underlying-type*
256 '(:postgresql :mysql :sqlite3)))
257 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
258 (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
259 ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
260 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
261 :oodml/select/15 :oodml/select/16 :oodml/select/17
262 :oodml/select/18 :oodml/select/19 :oodml/select/20
263 :oodml/select/21 :oodml/select/22
264 :oodml/update-records/4 :oodml/update-records/4-slots
265 :oodml/update-records/5 :oodml/update-records/5-slots
266 :oodml/update-records/6 :oodml/update-records/7
267 :oodml/update-records/8 :oodml/update-records/9
268 :oodml/update-records/9-slots :oodml/update-instance/3
269 :oodml/update-instance/4 :oodml/update-instance/5
270 :oodml/update-instance/6 :oodml/update-instance/7
271 :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
272 (push (cons test ":auto-increment not supported.") skip-tests))
274 (push test-form test-forms)))))
275 (values (nreverse test-forms) (nreverse skip-tests))))
277 (defun rapid-load (type &optional (position 0))
278 "Rapid load for interactive testing."
279 (when *default-database*
280 (disconnect :database *default-database*))
281 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
282 ;(test-initialise-database)
286 (rapid-load :postgresql))
292 (rapid-load :oracle))