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