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