ds-artists: new test dataset ds-artists. clsql-tests.asd: add new filee ds-artists...
[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 ;; Below must be set as nil since test-i18n.lisp is not loaded on all platforms.
30 (defvar *rt-i18n* nil)
31
32 (defvar *test-database-type* nil)
33 (defvar *test-database-underlying-type* nil)
34 (defvar *test-database-user* nil)
35 (defvar *test-false-database-user* "adsfjalsdkfjlakjsdfl"
36   "For testing ownership, a user that isn't the owner.")
37 (defvar *test-start-utime* nil)
38 (defvar *test-connection-spec* nil)
39 (defvar *test-connection-db-type* nil)
40 (defvar *test-report-width* 80 "Width of test report in ems.")
41
42
43 (defun test-connect-to-database (db-type spec)
44   (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
45     (ignore-errors (destroy-database spec :database-type db-type))
46     (ignore-errors (create-database spec :database-type db-type)))
47
48   (setf *test-database-type* db-type)
49   (setf *test-database-user*
50     (cond
51      ((member db-type '(:oracle :odbc :aodbc)) (second spec))
52      ((>= (length spec) 3) (third spec))))
53
54   ;; Connect to the database
55   (clsql:connect spec
56                  :database-type db-type
57                  :make-default t
58                  :if-exists :old)
59
60   ;; Ensure database is empty
61   (truncate-database :database *default-database*)
62
63   (setf *test-database-underlying-type*
64         (clsql-sys:database-underlying-type *default-database*))
65
66   ;; If Postgres, turn off notices to console
67   (when (eql db-type :postgresql)
68     (clsql:execute-command "SET client_min_messages = WARNING"))
69
70   *default-database*)
71
72 (defun default-suites ()
73   "The default list of tests to run."
74   (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
75           *rt-ooddl* *rt-oodml* *rt-syntax* *rt-time* *rt-i18n*))
76
77
78 (defvar *error-count* 0)
79 (defvar *error-list* nil)
80
81 (defun run-function-append-report-file (function report-file)
82     (let* ((report-path (etypecase report-file
83                         (pathname report-file)
84                         (string (parse-namestring report-file))))
85          (sexp-report-path (make-pathname :defaults report-path
86                                           :type "sexp")))
87       (with-open-file (rs report-path :direction :output
88                           :if-exists :append
89                       :if-does-not-exist :create)
90         (with-open-file (srs sexp-report-path :direction :output
91                              :if-exists :append
92                              :if-does-not-exist :create)
93           (funcall function :report-stream rs :sexp-report-stream srs)))))
94
95 (defun run-tests-append-report-file (report-file)
96   (run-function-append-report-file 'run-tests report-file))
97
98
99 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil)
100                   (suites (default-suites)))
101   ;; clear SQL-OUTPUT cache
102   (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
103   (let ((specs (read-specs))
104         (*report-stream* report-stream)
105         (*sexp-report-stream* sexp-report-stream)
106         (*error-count* 0)
107         (*error-list* nil))
108     (unless specs
109       (warn "Not running tests because test configuration file is missing")
110       (return-from run-tests :skipped))
111     (load-necessary-systems specs)
112     (dolist (db-type +all-db-types+)
113       (dolist (spec (db-type-spec db-type specs))
114         (let ((*test-connection-spec* spec)
115               (*test-connection-db-type* db-type))
116           (do-tests-for-backend db-type spec :suites suites)))))
117   (zerop *error-count*))
118
119 (defun load-necessary-systems (specs)
120   (dolist (db-type +all-db-types+)
121     (when (db-type-spec db-type specs)
122       (clsql-sys:initialize-database-type :database-type db-type))))
123
124 (defun write-report-banner (report-type db-type stream db-name)
125   (format stream
126           "~&
127 ******************************************************************************
128 ***     CLSQL ~A begun at ~A
129 ***     ~A
130 ***     ~A on ~A
131 ***     Database ~:@(~A~)
132 ***     Type: ~:@(~A~) backend~A.
133 ******************************************************************************
134 "
135           report-type
136           (clsql:format-time
137            nil
138            (clsql:utime->time (get-universal-time)))
139           (lisp-implementation-type)
140           (lisp-implementation-version)
141           (machine-type)
142           db-name
143           db-type
144           (if (not (eq db-type *test-database-underlying-type*))
145               (format nil " with underlying type ~:@(~A~)"
146                       *test-database-underlying-type*)
147               "")
148           ))
149
150 (defun do-tests-for-backend (db-type spec &key
151                              (suites (default-suites)) )
152   (test-connect-to-database db-type spec)
153   (unwind-protect
154        (multiple-value-bind (test-forms skip-tests)
155            (compute-tests-for-backend db-type *test-database-underlying-type* :suites suites)
156
157            (write-report-banner "Test Suite" db-type *report-stream*
158                                 (database-name-from-spec spec db-type))
159
160            (regression-test:rem-all-tests)
161            (dolist (test-form test-forms)
162              (eval test-form))
163
164            (let* ((cl:*print-right-margin* *test-report-width*)
165                   (remaining (regression-test:do-tests *report-stream*)))
166              (when (regression-test:pending-tests)
167                (incf *error-count* (length remaining))))
168
169            (let ((sexp-error (list db-type
170                                    *test-database-underlying-type*
171                                    (get-universal-time)
172                                    (length test-forms)
173                                    (regression-test:pending-tests)
174                                    (lisp-implementation-type)
175                                    (lisp-implementation-version)
176                                    (machine-type))))
177              (when *sexp-report-stream*
178                (write sexp-error :stream *sexp-report-stream* :readably t))
179              (push sexp-error *error-list*))
180
181            (format *report-stream* "~&Tests skipped:")
182            (if skip-tests
183                (let ((max-test-name (length (symbol-name (caar skip-tests)))))
184                  (dolist (skipped (cdr skip-tests))
185                    (let ((len (length (symbol-name (car skipped)))))
186                      (when (> len max-test-name)
187                        (setq max-test-name len))))
188                  (let ((fmt (format nil "~~&  ~~~DA ~~A~~%" max-test-name)))
189                    (dolist (skipped skip-tests)
190                      ;; word-wrap the reason string field
191                      (let* ((test (car skipped))
192                             (reason (cdr skipped))
193                             (rlen (length reason))
194                             (rwidth (max 20 (- (or *test-report-width* 80) max-test-name 3)))
195                             (rwords (clsql-sys::delimited-string-to-list reason #\space t))
196                             (rformat (format nil "~~{~~<~%~~1,~D:;~~A~~> ~~}" rwidth))
197                             (rwrapped (format nil rformat rwords))
198                             (rlines (clsql-sys::delimited-string-to-list rwrapped #\Newline t)))
199                        (dolist (rline rlines)
200                          (format *report-stream* fmt (if test
201                                                          (prog1
202                                                              test
203                                                            (setq test nil))
204                                                          "")
205                                  rline))))))
206                (format *report-stream* " None~%")))
207     (disconnect)))
208
209
210 (defun compute-tests-for-backend (db-type db-underlying-type
211                                   &key (suites (default-suites)))
212   (let ((test-forms '())
213         (skip-tests '()))
214     (dolist (test-form (if (listp suites) suites (list suites)))
215       (let ((test (second test-form)))
216         (cond
217           ((and (not (eql db-underlying-type :mysql))
218                 (clsql-sys:in test :connection/query-command))
219            (push (cons test "known to work only in MySQL as yet.") skip-tests))
220           ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
221                 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
222            (push (cons test "views not supported.") skip-tests))
223           ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
224                 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
225            (push (cons test "boolean where not supported.") skip-tests))
226           ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
227                 (clsql-sys:in test :fdml/select/5 :fdml/select/10
228                               :fdml/select/32 :fdml/select/33))
229            (push (cons test "subqueries not supported.") skip-tests))
230           ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
231                                                     *default-database*))
232                 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
233            (push (cons test "transactions not supported.") skip-tests))
234           ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
235                 (clsql-sys:in test :fdml/select/1))
236            (push (cons test "fancy math not supported.") skip-tests))
237           ((and (eql *test-database-type* :sqlite)
238                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
239                                 :fdml/select/21 :fdml/select/32
240                                 :fdml/select/33))
241            (push (cons test "not supported by sqlite.") skip-tests))
242           ((and (eql *test-database-type* :sqlite3)
243                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
244                               :fdml/select/21 :fdml/select/32
245                               :fdml/select/33))
246            (push (cons test "not supported by sqlite3.") skip-tests))
247           ((and (not (clsql-sys:db-type-has-bigint? db-type))
248                 (clsql-sys:in test :basic/bigint/1))
249            (push (cons test "bigint not supported.") skip-tests))
250           ((and (eql *test-database-underlying-type* :mysql)
251                 (clsql-sys:in test :fdml/select/26))
252            (push (cons test "string table aliases not supported on all MySQL versions.") skip-tests))
253           ((and (eql *test-database-underlying-type* :mysql)
254                 (clsql-sys:in test :fdml/select/22 :fdml/query/5
255                                 :fdml/query/7 :fdml/query/8))
256            (push (cons test "not supported by mysql.") skip-tests))
257           ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
258                 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
259            (push (cons test "union not supported") skip-tests))
260           ((and (eq *test-database-type* :oracle)
261                 (clsql-sys:in test :fdml/query/8 :fdml/select/21
262                               :fddl/table/6))
263            (push (cons test "syntax not supported.") skip-tests))
264           ((and (eq *test-database-type* :odbc)
265                 (eq *test-database-underlying-type* :postgresql)
266                 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
267                               :fddl/owner/attributes
268                               :fddl/owner/attribute-types
269                               :fddl/owner/index
270                               :fddl/owner/sequence))
271           (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
272           ((and (not (member *test-database-underlying-type*
273                              '(:postgresql :oracle)))
274                 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
275                               :fddl/owner/attributes
276                               :fddl/owner/attribute-types
277                               :fddl/owner/index
278                               :fddl/owner/sequence))
279            (push (cons test "table ownership not supported.") skip-tests))
280           ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
281                 (clsql-sys:in test :fdml/query/7))
282            (push (cons test "intersect not supported.") skip-tests))
283           ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
284                 (clsql-sys:in test :fdml/query/8))
285            (push (cons test "except not supported.") skip-tests))
286           ((and (eq *test-database-underlying-type* :mssql)
287                 (clsql-sys:in test :fdml/select/9))
288            (push (cons test "mssql uses integer math for AVG.") skip-tests))
289           ((and (not (member *test-database-underlying-type*
290                              '(:postgresql :mysql :sqlite3)))
291                 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
292            (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
293           ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
294                 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
295                               :oodml/select/15 :oodml/select/16 :oodml/select/17
296                               :oodml/select/18 :oodml/select/19 :oodml/select/20
297                               :oodml/select/21 :oodml/select/22 :oodml/select/23
298                               :oodml/update-records/4 :oodml/update-records/4-slots
299                               :oodml/update-records/5 :oodml/update-records/5-slots
300                               :oodml/update-records/6 :oodml/update-records/7
301                               :oodml/update-records/8 :oodml/update-records/9
302                               :oodml/update-records/9-slots :oodml/update-records/10
303                               :oodml/update-records/11 :oodml/update-instance/3
304                               :oodml/update-instance/4 :oodml/update-instance/5
305                               :oodml/update-instance/6 :oodml/update-instance/7
306                               :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
307            (push (cons test ":auto-increment not supported.") skip-tests))
308          ((and (not (member *test-database-underlying-type*
309                             '(:postgresql :postgresql-socket)))
310                (clsql-sys:in test
311                              :time/pg/fdml/usec :time/pg/oodml/no-usec :time/pg/oodml/usec))
312           (push (cons test "Postgres specific test.")
313                 skip-tests))
314          ((and (member *test-database-underlying-type* '(:mysql))
315                (clsql-sys:in test :time/cross-platform/msec
316                              :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
317           (push (cons test "MySQL doesn't support fractional seconds on timestamp columns (http://forge.mysql.com/worklog/task.php?id=946).")
318                 skip-tests))
319           ((and (member *test-database-underlying-type* '(:mssql))
320                (clsql-sys:in test :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
321           (push (cons test "MSSQL doesn't support micro-seconds on datetime columns.")
322                 skip-tests))
323           (t
324            (push test-form test-forms)))))
325       (values (nreverse test-forms) (nreverse skip-tests))))
326
327 (defun rapid-load (type &optional (position 0))
328   "Rapid load for interactive testing."
329   (when *default-database*
330       (disconnect :database *default-database*))
331   (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
332   ;(test-initialise-database)
333   *default-database*)
334
335 (defun rl ()
336   (rapid-load :postgresql))
337
338 (defun rlm ()
339   (rapid-load :mysql))
340
341 (defun rlo ()
342   (rapid-load :oracle))