Write the database name in the report banner. This is especially useful for testing...
[clsql.git] / tests / test-init.lisp
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
6 ;;;;
7 ;;;; Initialisation utilities for running regression tests on CLSQL.
8 ;;;;
9 ;;;; This file is part of CLSQL.
10 ;;;;
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 ;;;; ======================================================================
15
16 (in-package #:clsql-tests)
17
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*)
21 (defvar *rt-basic*)
22 (defvar *rt-connection*)
23 (defvar *rt-fddl*)
24 (defvar *rt-fdml*)
25 (defvar *rt-ooddl*)
26 (defvar *rt-oodml*)
27 (defvar *rt-syntax*)
28 (defvar *rt-time*)
29
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)
36
37
38
39 (defun test-connect-to-database (db-type spec)
40   (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
41     (ignore-errors (destroy-database spec :database-type db-type))
42     (ignore-errors (create-database spec :database-type db-type)))
43
44   (setf *test-database-type* db-type)
45   (setf *test-database-user*
46     (cond
47      ((eq :oracle db-type) (second spec))
48      ((>= (length spec) 3) (third spec))))
49
50   ;; Connect to the database
51   (clsql:connect spec
52                  :database-type db-type
53                  :make-default t
54                  :if-exists :old)
55
56   ;; Ensure database is empty
57   (truncate-database :database *default-database*)
58
59   (setf *test-database-underlying-type*
60         (clsql-sys:database-underlying-type *default-database*))
61
62   *default-database*)
63
64
65 (defvar *error-count* 0)
66 (defvar *error-list* nil)
67
68 (defun run-function-append-report-file (function report-file)
69     (let* ((report-path (etypecase report-file
70                         (pathname report-file)
71                         (string (parse-namestring report-file))))
72          (sexp-report-path (make-pathname :defaults report-path
73                                           :type "sexp")))
74       (with-open-file (rs report-path :direction :output
75                           :if-exists :append
76                       :if-does-not-exist :create)
77         (with-open-file (srs sexp-report-path :direction :output
78                              :if-exists :append
79                              :if-does-not-exist :create)
80           (funcall function :report-stream rs :sexp-report-stream srs)))))
81
82 (defun run-tests-append-report-file (report-file)
83   (run-function-append-report-file 'run-tests report-file))
84
85
86 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
87   ;; clear SQL-OUTPUT cache
88   (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
89   (let ((specs (read-specs))
90         (*report-stream* report-stream)
91         (*sexp-report-stream* sexp-report-stream)
92         (*error-count* 0)
93         (*error-list* nil))
94     (unless specs
95       (warn "Not running tests because test configuration file is missing")
96       (return-from run-tests :skipped))
97     (load-necessary-systems specs)
98     (dolist (db-type +all-db-types+)
99       (dolist (spec (db-type-spec db-type specs))
100         (let ((*test-connection-spec* spec)
101               (*test-connection-db-type* db-type))
102           (do-tests-for-backend db-type spec)))))
103   (zerop *error-count*))
104
105 (defun load-necessary-systems (specs)
106   (dolist (db-type +all-db-types+)
107     (when (db-type-spec db-type specs)
108       (clsql-sys:initialize-database-type :database-type db-type))))
109
110 (defun write-report-banner (report-type db-type stream db-name)
111   (format stream
112           "~&
113 ******************************************************************************
114 ***     CLSQL ~A begun at ~A
115 ***     ~A
116 ***     ~A on ~A
117 ***     Database ~:@(~A~)
118 ***     Type: ~:@(~A~) backend~A.
119 ******************************************************************************
120 "
121           report-type
122           (clsql:format-time
123            nil
124            (clsql:utime->time (get-universal-time)))
125           (lisp-implementation-type)
126           (lisp-implementation-version)
127           (machine-type)
128           db-name
129           db-type
130           (if (not (eq db-type *test-database-underlying-type*))
131               (format nil " with underlying type ~:@(~A~)"
132                       *test-database-underlying-type*)
133               "")
134           ))
135
136 (defun do-tests-for-backend (db-type spec)
137   (test-connect-to-database db-type spec)
138
139   (unwind-protect
140        (multiple-value-bind (test-forms skip-tests)
141            (compute-tests-for-backend db-type *test-database-underlying-type*)
142
143            (write-report-banner "Test Suite" db-type *report-stream*
144                                 (database-name-from-spec spec db-type))
145
146 ;           (test-initialise-database)
147
148            (regression-test:rem-all-tests)
149            (dolist (test-form test-forms)
150              (eval test-form))
151
152            (let ((remaining (regression-test:do-tests *report-stream*)))
153              (when (regression-test:pending-tests)
154                (incf *error-count* (length remaining))))
155
156            (let ((sexp-error (list db-type
157                                    *test-database-underlying-type*
158                                    (get-universal-time)
159                                    (length test-forms)
160                                    (regression-test:pending-tests)
161                                    (lisp-implementation-type)
162                                    (lisp-implementation-version)
163                                    (machine-type))))
164              (when *sexp-report-stream*
165                (write sexp-error :stream *sexp-report-stream* :readably t))
166              (push sexp-error *error-list*))
167
168            (format *report-stream* "~&Tests skipped:")
169            (if skip-tests
170                (dolist (skipped skip-tests)
171                  (format *report-stream*
172                          "~&   ~20A ~A~%" (car skipped) (cdr skipped)))
173                (format *report-stream* " None~%")))
174     (disconnect)))
175
176
177 (defun compute-tests-for-backend (db-type db-underlying-type)
178   (let ((test-forms '())
179         (skip-tests '()))
180     (dolist (test-form (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
181                                *rt-ooddl* *rt-oodml* *rt-syntax*))
182       (let ((test (second test-form)))
183         (cond
184           ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
185                 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
186            (push (cons test "views not supported") skip-tests))
187           ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
188                 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
189            (push (cons test "boolean where not supported") skip-tests))
190           ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
191                 (clsql-sys:in test :fdml/select/5 :fdml/select/10
192                               :fdml/select/32 :fdml/select/33))
193            (push (cons test "subqueries not supported") skip-tests))
194           ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
195                                                     *default-database*))
196                 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
197            (push (cons test "transactions not supported") skip-tests))
198           ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
199                 (clsql-sys:in test :fdml/select/1))
200            (push (cons test "fancy math not supported") skip-tests))
201           ((and (eql *test-database-type* :sqlite)
202                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
203                                 :fdml/select/21 :fdml/select/32
204                                 :fdml/select/33))
205            (push (cons test "not supported by sqlite") skip-tests))
206           ((and (eql *test-database-type* :sqlite3)
207                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
208                               :fdml/select/21 :fdml/select/32
209                               :fdml/select/33))
210            (push (cons test "not supported by sqlite3") skip-tests))
211           ((and (not (clsql-sys:db-type-has-bigint? db-type))
212                 (clsql-sys:in test :basic/bigint/1))
213            (push (cons test "bigint not supported") skip-tests))
214           ((and (eql *test-database-underlying-type* :mysql)
215                 (clsql-sys:in test :fdml/select/26))
216            (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
217           ((and (eql *test-database-underlying-type* :mysql)
218                 (clsql-sys:in test :fdml/select/22 :fdml/query/5
219                                 :fdml/query/7 :fdml/query/8))
220            (push (cons test "not supported by mysql") skip-tests))
221           ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
222                 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
223            (push (cons test "union not supported") skip-tests))
224           ((and (eq *test-database-type* :oracle)
225                 (clsql-sys:in test :fdml/query/8 :fdml/select/21
226                               :fddl/table/6))
227            (push (cons test "syntax not supported") skip-tests))
228           ((and (eq *test-database-type* :odbc)
229                 (eq *test-database-underlying-type* :postgresql)
230                 (clsql-sys:in test :fddl/owner/1))
231            (push (cons test "table ownership not supported by postgresql odbc driver") skip-tests))
232           ((and (not (member *test-database-underlying-type*
233                              '(:postgresql :oracle)))
234                 (clsql-sys:in test :fddl/owner/1))
235            (push (cons test "table ownership not supported") skip-tests))
236           ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
237                 (clsql-sys:in test :fdml/query/7))
238            (push (cons test "intersect not supported") skip-tests))
239           ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
240                 (clsql-sys:in test :fdml/query/8))
241            (push (cons test "except not supported") skip-tests))
242           ((and (eq *test-database-underlying-type* :mssql)
243                 (clsql-sys:in test :fdml/select/9))
244            (push (cons test "mssql uses integer math for AVG") skip-tests))
245           ((and (not (member *test-database-underlying-type*
246                              '(:postgresql :mysql :sqlite3)))
247                 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
248            (push (cons test "LIMIT keyword not supported in SELECT") skip-tests))
249           ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
250                 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
251                               :oodml/select/15 :oodml/select/16 :oodml/select/17
252                               :oodml/select/18 :oodml/select/19 :oodml/select/20
253                               :oodml/select/21 :oodml/select/22
254                               :oodml/update-records/4 :oodml/update-records/4-slots
255                               :oodml/update-records/5 :oodml/update-records/5-slots
256                               :oodml/update-records/6 :oodml/update-records/7
257                               :oodml/update-records/8 :oodml/update-records/9
258                               :oodml/update-records/9-slots :oodml/update-instance/3
259                               :oodml/update-instance/4 :oodml/update-instance/5
260                               :oodml/update-instance/6 :oodml/update-instance/7
261                               :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
262            (push (cons test ":auto-increment not by backend.") skip-tests))
263           (t
264            (push test-form test-forms)))))
265       (values (nreverse test-forms) (nreverse skip-tests))))
266
267 (defun rapid-load (type &optional (position 0))
268   "Rapid load for interactive testing."
269   (when *default-database*
270       (disconnect :database *default-database*))
271   (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
272   ;(test-initialise-database)
273   *default-database*)
274
275 (defun rl ()
276   (rapid-load :postgresql))
277
278 (defun rlm ()
279   (rapid-load :mysql))
280
281 (defun rlo ()
282   (rapid-load :oracle))