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-start-utime* nil)
34 (defvar *test-connection-spec* nil)
35 (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 ((eq :oracle db-type) (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)
115 ******************************************************************************
116 *** CLSQL ~A begun at ~A
119 *** Database ~:@(~A~) backend~A.
120 ******************************************************************************
125 (clsql:utime->time (get-universal-time)))
126 (lisp-implementation-type)
127 (lisp-implementation-version)
130 (if (not (eq db-type *test-database-underlying-type*))
131 (format nil " with underlying type ~:@(~A~)"
132 *test-database-underlying-type*)
136 (defun do-tests-for-backend (db-type spec)
137 (test-connect-to-database db-type spec)
140 (multiple-value-bind (test-forms skip-tests)
141 (compute-tests-for-backend db-type *test-database-underlying-type*)
143 (write-report-banner "Test Suite" db-type *report-stream*)
145 ; (test-initialise-database)
147 (regression-test:rem-all-tests)
148 (dolist (test-form test-forms)
151 (let ((remaining (regression-test:do-tests *report-stream*)))
152 (when (regression-test:pending-tests)
153 (incf *error-count* (length remaining))))
155 (let ((sexp-error (list db-type
156 *test-database-underlying-type*
159 (regression-test:pending-tests)
160 (lisp-implementation-type)
161 (lisp-implementation-version)
163 (when *sexp-report-stream*
164 (write sexp-error :stream *sexp-report-stream* :readably t))
165 (push sexp-error *error-list*))
167 (format *report-stream* "~&Tests skipped:")
169 (dolist (skipped skip-tests)
170 (format *report-stream*
171 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
172 (format *report-stream* " None~%")))
176 (defun compute-tests-for-backend (db-type db-underlying-type)
177 (let ((test-forms '())
179 (dolist (test-form (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
180 *rt-ooddl* *rt-oodml* *rt-syntax*))
181 (let ((test (second test-form)))
183 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
184 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
185 (push (cons test "views not supported") skip-tests))
186 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
187 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
188 (push (cons test "boolean where not supported") skip-tests))
189 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
190 (clsql-sys:in test :fdml/select/5 :fdml/select/10
191 :fdml/select/32 :fdml/select/33))
192 (push (cons test "subqueries not supported") skip-tests))
193 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
195 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
196 (push (cons test "transactions not supported") skip-tests))
197 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
198 (clsql-sys:in test :fdml/select/1))
199 (push (cons test "fancy math not supported") skip-tests))
200 ((and (eql *test-database-type* :sqlite)
201 (clsql-sys:in test :fddl/view/4 :fdml/select/10
202 :fdml/select/21 :fdml/select/32
204 (push (cons test "not supported by sqlite") skip-tests))
205 ((and (eql *test-database-type* :sqlite3)
206 (clsql-sys:in test :fddl/view/4 :fdml/select/10
207 :fdml/select/21 :fdml/select/32
209 (push (cons test "not supported by sqlite3") skip-tests))
210 ((and (not (clsql-sys:db-type-has-bigint? db-type))
211 (clsql-sys:in test :basic/bigint/1))
212 (push (cons test "bigint not supported") skip-tests))
213 ((and (eql *test-database-underlying-type* :mysql)
214 (clsql-sys:in test :fdml/select/26))
215 (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
216 ((and (eql *test-database-underlying-type* :mysql)
217 (clsql-sys:in test :fdml/select/22 :fdml/query/5
218 :fdml/query/7 :fdml/query/8))
219 (push (cons test "not supported by mysql") skip-tests))
220 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
221 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
222 (push (cons test "union not supported") skip-tests))
223 ((and (eq *test-database-type* :oracle)
224 (clsql-sys:in test :fdml/query/8 :fdml/select/21
226 (push (cons test "syntax not supported") skip-tests))
227 ((and (eq *test-database-type* :odbc)
228 (eq *test-database-underlying-type* :postgresql)
229 (clsql-sys:in test :fddl/owner/1))
230 (push (cons test "table ownership not supported by postgresql odbc driver") skip-tests))
231 ((and (not (member *test-database-underlying-type*
232 '(:postgresql :oracle)))
233 (clsql-sys:in test :fddl/owner/1))
234 (push (cons test "table ownership not supported") skip-tests))
235 ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
236 (clsql-sys:in test :fdml/query/7))
237 (push (cons test "intersect not supported") skip-tests))
238 ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
239 (clsql-sys:in test :fdml/query/8))
240 (push (cons test "except not supported") skip-tests))
241 ((and (eq *test-database-underlying-type* :mssql)
242 (clsql-sys:in test :fdml/select/9))
243 (push (cons test "mssql uses integer math for AVG") skip-tests))
244 ((and (not (member *test-database-underlying-type*
245 '(:postgresql :mysql :sqlite3)))
246 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
247 (push (cons test "LIMIT keyword not supported in SELECT") skip-tests))
249 (push test-form test-forms)))))
250 (values (nreverse test-forms) (nreverse skip-tests))))
252 (defun rapid-load (type &optional (position 0))
253 "Rapid load for interactive testing."
254 (when *default-database*
255 (disconnect :database *default-database*))
256 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
257 ;(test-initialise-database)
261 (rapid-load :postgresql))
267 (rapid-load :oracle))