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