bd8fc99a86a45f3f4d12e780e083fc2715646843
[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-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)
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      ((member db-type '(:oracle :odbc :aodbc)) (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 (defun default-suites ()
67   "The default list of tests to run."
68   (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
69           *rt-ooddl* *rt-oodml* *rt-syntax* *rt-time*))
70
71
72 (defvar *error-count* 0)
73 (defvar *error-list* nil)
74
75 (defun run-function-append-report-file (function report-file)
76     (let* ((report-path (etypecase report-file
77                         (pathname report-file)
78                         (string (parse-namestring report-file))))
79          (sexp-report-path (make-pathname :defaults report-path
80                                           :type "sexp")))
81       (with-open-file (rs report-path :direction :output
82                           :if-exists :append
83                       :if-does-not-exist :create)
84         (with-open-file (srs sexp-report-path :direction :output
85                              :if-exists :append
86                              :if-does-not-exist :create)
87           (funcall function :report-stream rs :sexp-report-stream srs)))))
88
89 (defun run-tests-append-report-file (report-file)
90   (run-function-append-report-file 'run-tests report-file))
91
92
93 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil)
94                   (suites (default-suites)))
95   ;; clear SQL-OUTPUT cache
96   (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
97   (let ((specs (read-specs))
98         (*report-stream* report-stream)
99         (*sexp-report-stream* sexp-report-stream)
100         (*error-count* 0)
101         (*error-list* nil))
102     (unless specs
103       (warn "Not running tests because test configuration file is missing")
104       (return-from run-tests :skipped))
105     (load-necessary-systems specs)
106     (dolist (db-type +all-db-types+)
107       (dolist (spec (db-type-spec db-type specs))
108         (let ((*test-connection-spec* spec)
109               (*test-connection-db-type* db-type))
110           (do-tests-for-backend db-type spec :suites suites)))))
111   (zerop *error-count*))
112
113 (defun load-necessary-systems (specs)
114   (dolist (db-type +all-db-types+)
115     (when (db-type-spec db-type specs)
116       (clsql-sys:initialize-database-type :database-type db-type))))
117
118 (defun write-report-banner (report-type db-type stream db-name)
119   (format stream
120           "~&
121 ******************************************************************************
122 ***     CLSQL ~A begun at ~A
123 ***     ~A
124 ***     ~A on ~A
125 ***     Database ~:@(~A~)
126 ***     Type: ~:@(~A~) backend~A.
127 ******************************************************************************
128 "
129           report-type
130           (clsql:format-time
131            nil
132            (clsql:utime->time (get-universal-time)))
133           (lisp-implementation-type)
134           (lisp-implementation-version)
135           (machine-type)
136           db-name
137           db-type
138           (if (not (eq db-type *test-database-underlying-type*))
139               (format nil " with underlying type ~:@(~A~)"
140                       *test-database-underlying-type*)
141               "")
142           ))
143
144 (defun do-tests-for-backend (db-type spec &key
145                              (suites (default-suites)) )
146   (test-connect-to-database db-type spec)
147   (unwind-protect
148        (multiple-value-bind (test-forms skip-tests)
149            (compute-tests-for-backend db-type *test-database-underlying-type* :suites suites)
150
151            (write-report-banner "Test Suite" db-type *report-stream*
152                                 (database-name-from-spec spec db-type))
153
154            (regression-test:rem-all-tests)
155            (dolist (test-form test-forms)
156              (eval test-form))
157
158            (let ((remaining (regression-test:do-tests *report-stream*)))
159              (when (regression-test:pending-tests)
160                (incf *error-count* (length remaining))))
161
162            (let ((sexp-error (list db-type
163                                    *test-database-underlying-type*
164                                    (get-universal-time)
165                                    (length test-forms)
166                                    (regression-test:pending-tests)
167                                    (lisp-implementation-type)
168                                    (lisp-implementation-version)
169                                    (machine-type))))
170              (when *sexp-report-stream*
171                (write sexp-error :stream *sexp-report-stream* :readably t))
172              (push sexp-error *error-list*))
173
174            (format *report-stream* "~&Tests skipped:")
175            (if skip-tests
176                (dolist (skipped skip-tests)
177                  (format *report-stream*
178                          "~&   ~30A ~A~%" (car skipped) (cdr skipped)))
179                (format *report-stream* " None~%")))
180     (disconnect)))
181
182
183 (defun compute-tests-for-backend (db-type db-underlying-type
184                                   &key (suites (default-suites)))
185   (let ((test-forms '())
186         (skip-tests '()))
187     (dolist (test-form (if (listp suites) suites (list suites)))
188       (let ((test (second test-form)))
189         (cond
190           ((and (not (eql db-underlying-type :mysql))
191                 (clsql-sys:in test :connection/query-command))
192            (push (cons test "Known to only work in mysql as yet.") skip-tests))
193           ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
194                 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
195            (push (cons test "views not supported.") skip-tests))
196           ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
197                 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
198            (push (cons test "boolean where not supported.") skip-tests))
199           ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
200                 (clsql-sys:in test :fdml/select/5 :fdml/select/10
201                               :fdml/select/32 :fdml/select/33))
202            (push (cons test "subqueries not supported.") skip-tests))
203           ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
204                                                     *default-database*))
205                 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
206            (push (cons test "transactions not supported.") skip-tests))
207           ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
208                 (clsql-sys:in test :fdml/select/1))
209            (push (cons test "fancy math not supported.") skip-tests))
210           ((and (eql *test-database-type* :sqlite)
211                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
212                                 :fdml/select/21 :fdml/select/32
213                                 :fdml/select/33))
214            (push (cons test "not supported by sqlite.") skip-tests))
215           ((and (eql *test-database-type* :sqlite3)
216                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
217                               :fdml/select/21 :fdml/select/32
218                               :fdml/select/33))
219            (push (cons test "not supported by sqlite3.") skip-tests))
220           ((and (not (clsql-sys:db-type-has-bigint? db-type))
221                 (clsql-sys:in test :basic/bigint/1))
222            (push (cons test "bigint not supported.") skip-tests))
223           ((and (eql *test-database-underlying-type* :mysql)
224                 (clsql-sys:in test :fdml/select/26))
225            (push (cons test "string table aliases not supported on all mysql versions.") skip-tests))
226           ((and (eql *test-database-underlying-type* :mysql)
227                 (clsql-sys:in test :fdml/select/22 :fdml/query/5
228                                 :fdml/query/7 :fdml/query/8))
229            (push (cons test "not supported by mysql.") skip-tests))
230           ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
231                 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
232            (push (cons test "union not supported") skip-tests))
233           ((and (eq *test-database-type* :oracle)
234                 (clsql-sys:in test :fdml/query/8 :fdml/select/21
235                               :fddl/table/6))
236            (push (cons test "syntax not supported.") skip-tests))
237           ((and (eq *test-database-type* :odbc)
238                 (eq *test-database-underlying-type* :postgresql)
239                 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
240                               :fddl/owner/attributes
241                               :fddl/owner/attribute-types
242                               :fddl/owner/index
243                               :fddl/owner/sequence))
244           (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
245           ((and (not (member *test-database-underlying-type*
246                              '(:postgresql :oracle)))
247                 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
248                               :fddl/owner/attributes
249                               :fddl/owner/attribute-types
250                               :fddl/owner/index
251                               :fddl/owner/sequence))
252            (push (cons test "table ownership not supported.") skip-tests))
253           ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
254                 (clsql-sys:in test :fdml/query/7))
255            (push (cons test "intersect not supported.") skip-tests))
256           ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
257                 (clsql-sys:in test :fdml/query/8))
258            (push (cons test "except not supported.") skip-tests))
259           ((and (eq *test-database-underlying-type* :mssql)
260                 (clsql-sys:in test :fdml/select/9))
261            (push (cons test "mssql uses integer math for AVG.") skip-tests))
262           ((and (not (member *test-database-underlying-type*
263                              '(:postgresql :mysql :sqlite3)))
264                 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
265            (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
266           ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
267                 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
268                               :oodml/select/15 :oodml/select/16 :oodml/select/17
269                               :oodml/select/18 :oodml/select/19 :oodml/select/20
270                               :oodml/select/21 :oodml/select/22
271                               :oodml/update-records/4 :oodml/update-records/4-slots
272                               :oodml/update-records/5 :oodml/update-records/5-slots
273                               :oodml/update-records/6 :oodml/update-records/7
274                               :oodml/update-records/8 :oodml/update-records/9
275                               :oodml/update-records/9-slots :oodml/update-instance/3
276                               :oodml/update-instance/4 :oodml/update-instance/5
277                               :oodml/update-instance/6 :oodml/update-instance/7
278                               :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
279            (push (cons test ":auto-increment not supported.") skip-tests))
280          ((and (not (member *test-database-underlying-type*
281                             '(:postgresql :postgresql-socket)))
282                (clsql-sys:in test
283                              :time/pg/fdml/usec :time/pg/oodml/no-usec :time/pg/oodml/usec))
284           (push (cons test "cant run the same date/time tests everywhere that we do on postgres because there are not standard datetime database types.")
285                 skip-tests))
286          ((and (member *test-database-underlying-type* '(:mysql))
287                (clsql-sys:in test :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
288           (push (cons test "Mysql does not support fractional seconds (on timestamp columns), see http://forge.mysql.com/worklog/task.php?id=946 perhaps?")
289                 skip-tests))
290
291           (t
292            (push test-form test-forms)))))
293       (values (nreverse test-forms) (nreverse skip-tests))))
294
295 (defun rapid-load (type &optional (position 0))
296   "Rapid load for interactive testing."
297   (when *default-database*
298       (disconnect :database *default-database*))
299   (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
300   ;(test-initialise-database)
301   *default-database*)
302
303 (defun rl ()
304   (rapid-load :postgresql))
305
306 (defun rlm ()
307   (rapid-load :mysql))
308
309 (defun rlo ()
310   (rapid-load :oracle))