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