Moving object data out of init into separate files and each one made a dataset.
[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
40
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)))
45
46   (setf *test-database-type* db-type)
47   (setf *test-database-user*
48     (cond
49      ((eq :oracle db-type) (second spec))
50      ((>= (length spec) 3) (third spec))))
51
52   ;; Connect to the database
53   (clsql:connect spec
54                  :database-type db-type
55                  :make-default t
56                  :if-exists :old)
57
58   ;; Ensure database is empty
59   (truncate-database :database *default-database*)
60
61   (setf *test-database-underlying-type*
62         (clsql-sys:database-underlying-type *default-database*))
63
64   *default-database*)
65
66
67 (defvar *error-count* 0)
68 (defvar *error-list* nil)
69
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
75                                           :type "sexp")))
76       (with-open-file (rs report-path :direction :output
77                           :if-exists :append
78                       :if-does-not-exist :create)
79         (with-open-file (srs sexp-report-path :direction :output
80                              :if-exists :append
81                              :if-does-not-exist :create)
82           (funcall function :report-stream rs :sexp-report-stream srs)))))
83
84 (defun run-tests-append-report-file (report-file)
85   (run-function-append-report-file 'run-tests report-file))
86
87
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)
94         (*error-count* 0)
95         (*error-list* nil))
96     (unless specs
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*))
106
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))))
111
112 (defun write-report-banner (report-type db-type stream)
113   (format stream
114           "~&
115 ******************************************************************************
116 ***     CLSQL ~A begun at ~A
117 ***     ~A
118 ***     ~A on ~A
119 ***     Database ~:@(~A~) backend~A.
120 ******************************************************************************
121 "
122           report-type
123           (clsql:format-time
124            nil
125            (clsql:utime->time (get-universal-time)))
126           (lisp-implementation-type)
127           (lisp-implementation-version)
128           (machine-type)
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
145 ;           (test-initialise-database)
146
147            (regression-test:rem-all-tests)
148            (dolist (test-form test-forms)
149              (eval test-form))
150
151            (let ((remaining (regression-test:do-tests *report-stream*)))
152              (when (regression-test:pending-tests)
153                (incf *error-count* (length remaining))))
154
155            (let ((sexp-error (list db-type
156                                    *test-database-underlying-type*
157                                    (get-universal-time)
158                                    (length test-forms)
159                                    (regression-test:pending-tests)
160                                    (lisp-implementation-type)
161                                    (lisp-implementation-version)
162                                    (machine-type))))
163              (when *sexp-report-stream*
164                (write sexp-error :stream *sexp-report-stream* :readably t))
165              (push sexp-error *error-list*))
166
167            (format *report-stream* "~&Tests skipped:")
168            (if skip-tests
169                (dolist (skipped skip-tests)
170                  (format *report-stream*
171                          "~&   ~20A ~A~%" (car skipped) (cdr skipped)))
172                (format *report-stream* " None~%")))
173     (disconnect)))
174
175
176 (defun compute-tests-for-backend (db-type db-underlying-type)
177   (let ((test-forms '())
178         (skip-tests '()))
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)))
182         (cond
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
194                                                     *default-database*))
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
203                                 :fdml/select/33))
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
208                               :fdml/select/33))
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
225                               :fddl/table/6))
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))
248           (t
249            (push test-form test-forms)))))
250       (values (nreverse test-forms) (nreverse skip-tests))))
251
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)
258   *default-database*)
259
260 (defun rl ()
261   (rapid-load :postgresql))
262
263 (defun rlm ()
264   (rapid-load :mysql))
265
266 (defun rlo ()
267   (rapid-load :oracle))