Marking tests that depend on the ds-nodes dataset as skipped unless backend implement...
[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)
111   (format stream
112           "~&
113 ******************************************************************************
114 ***     CLSQL ~A begun at ~A
115 ***     ~A
116 ***     ~A on ~A
117 ***     Database ~:@(~A~) backend~A.
118 ******************************************************************************
119 "
120           report-type
121           (clsql:format-time
122            nil
123            (clsql:utime->time (get-universal-time)))
124           (lisp-implementation-type)
125           (lisp-implementation-version)
126           (machine-type)
127           db-type
128           (if (not (eq db-type *test-database-underlying-type*))
129               (format nil " with underlying type ~:@(~A~)"
130                       *test-database-underlying-type*)
131               "")
132           ))
133
134 (defun do-tests-for-backend (db-type spec)
135   (test-connect-to-database db-type spec)
136
137   (unwind-protect
138        (multiple-value-bind (test-forms skip-tests)
139            (compute-tests-for-backend db-type *test-database-underlying-type*)
140
141            (write-report-banner "Test Suite" db-type *report-stream*)
142
143 ;           (test-initialise-database)
144
145            (regression-test:rem-all-tests)
146            (dolist (test-form test-forms)
147              (eval test-form))
148
149            (let ((remaining (regression-test:do-tests *report-stream*)))
150              (when (regression-test:pending-tests)
151                (incf *error-count* (length remaining))))
152
153            (let ((sexp-error (list db-type
154                                    *test-database-underlying-type*
155                                    (get-universal-time)
156                                    (length test-forms)
157                                    (regression-test:pending-tests)
158                                    (lisp-implementation-type)
159                                    (lisp-implementation-version)
160                                    (machine-type))))
161              (when *sexp-report-stream*
162                (write sexp-error :stream *sexp-report-stream* :readably t))
163              (push sexp-error *error-list*))
164
165            (format *report-stream* "~&Tests skipped:")
166            (if skip-tests
167                (dolist (skipped skip-tests)
168                  (format *report-stream*
169                          "~&   ~20A ~A~%" (car skipped) (cdr skipped)))
170                (format *report-stream* " None~%")))
171     (disconnect)))
172
173
174 (defun compute-tests-for-backend (db-type db-underlying-type)
175   (let ((test-forms '())
176         (skip-tests '()))
177     (dolist (test-form (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
178                                *rt-ooddl* *rt-oodml* *rt-syntax*))
179       (let ((test (second test-form)))
180         (cond
181           ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
182                 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
183            (push (cons test "views not supported") skip-tests))
184           ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
185                 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
186            (push (cons test "boolean where not supported") skip-tests))
187           ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
188                 (clsql-sys:in test :fdml/select/5 :fdml/select/10
189                               :fdml/select/32 :fdml/select/33))
190            (push (cons test "subqueries not supported") skip-tests))
191           ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
192                                                     *default-database*))
193                 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
194            (push (cons test "transactions not supported") skip-tests))
195           ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
196                 (clsql-sys:in test :fdml/select/1))
197            (push (cons test "fancy math not supported") skip-tests))
198           ((and (eql *test-database-type* :sqlite)
199                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
200                                 :fdml/select/21 :fdml/select/32
201                                 :fdml/select/33))
202            (push (cons test "not supported by sqlite") skip-tests))
203           ((and (eql *test-database-type* :sqlite3)
204                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
205                               :fdml/select/21 :fdml/select/32
206                               :fdml/select/33))
207            (push (cons test "not supported by sqlite3") skip-tests))
208           ((and (not (clsql-sys:db-type-has-bigint? db-type))
209                 (clsql-sys:in test :basic/bigint/1))
210            (push (cons test "bigint not supported") skip-tests))
211           ((and (eql *test-database-underlying-type* :mysql)
212                 (clsql-sys:in test :fdml/select/26))
213            (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
214           ((and (eql *test-database-underlying-type* :mysql)
215                 (clsql-sys:in test :fdml/select/22 :fdml/query/5
216                                 :fdml/query/7 :fdml/query/8))
217            (push (cons test "not supported by mysql") skip-tests))
218           ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
219                 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
220            (push (cons test "union not supported") skip-tests))
221           ((and (eq *test-database-type* :oracle)
222                 (clsql-sys:in test :fdml/query/8 :fdml/select/21
223                               :fddl/table/6))
224            (push (cons test "syntax not supported") skip-tests))
225           ((and (eq *test-database-type* :odbc)
226                 (eq *test-database-underlying-type* :postgresql)
227                 (clsql-sys:in test :fddl/owner/1))
228            (push (cons test "table ownership not supported by postgresql odbc driver") skip-tests))
229           ((and (not (member *test-database-underlying-type*
230                              '(:postgresql :oracle)))
231                 (clsql-sys:in test :fddl/owner/1))
232            (push (cons test "table ownership not supported") skip-tests))
233           ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
234                 (clsql-sys:in test :fdml/query/7))
235            (push (cons test "intersect not supported") skip-tests))
236           ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
237                 (clsql-sys:in test :fdml/query/8))
238            (push (cons test "except not supported") skip-tests))
239           ((and (eq *test-database-underlying-type* :mssql)
240                 (clsql-sys:in test :fdml/select/9))
241            (push (cons test "mssql uses integer math for AVG") skip-tests))
242           ((and (not (member *test-database-underlying-type*
243                              '(:postgresql :mysql :sqlite3)))
244                 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
245            (push (cons test "LIMIT keyword not supported in SELECT") skip-tests))
246           ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
247                 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
248                               :oodml/select/15 :oodml/select/16 :oodml/select/17
249                               :oodml/select/18 :oodml/select/19 :oodml/select/20
250                               :oodml/select/21 :oodml/select/22
251                               :oodml/update-records/4 :oodml/update-records/4-slots
252                               :oodml/update-records/5 :oodml/update-records/5-slots
253                               :oodml/update-records/6 :oodml/update-records/7
254                               :oodml/update-records/8 :oodml/update-records/9
255                               :oodml/update-records/9-slots :oodml/update-instance/3
256                               :oodml/update-instance/4 :oodml/update-instance/5
257                               :oodml/update-instance/6 :oodml/update-instance/7
258                               :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
259            (push (cons test ":auto-increment not by backend.") skip-tests))
260           (t
261            (push test-form test-forms)))))
262       (values (nreverse test-forms) (nreverse skip-tests))))
263
264 (defun rapid-load (type &optional (position 0))
265   "Rapid load for interactive testing."
266   (when *default-database*
267       (disconnect :database *default-database*))
268   (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
269   ;(test-initialise-database)
270   *default-database*)
271
272 (defun rl ()
273   (rapid-load :postgresql))
274
275 (defun rlm ()
276   (rapid-load :mysql))
277
278 (defun rlo ()
279   (rapid-load :oracle))