print start and stop messages while testing (probably remove, but for now I am having...
[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               (format report-stream "~%~%Start Running Tests Against: ~A ~A~%~%" db-type (ignore-errors (subseq spec 0 2)))
128               (do-tests-for-backend db-type spec :suites suites)
129               (format report-stream  "~%~%Finished Running Tests Against: ~A ~A~%~%" db-type (ignore-errors (subseq spec 0 2))))))))
130     (zerop *error-count*)))
131
132 (defun load-necessary-systems (specs)
133   (dolist (db-type +all-db-types+)
134     (when (db-type-spec db-type specs)
135       (clsql-sys:initialize-database-type :database-type db-type))))
136
137 (defun write-report-banner (report-type db-type stream db-name)
138   (format stream
139           "~&
140 ******************************************************************************
141 ***     CLSQL ~A begun at ~A
142 ***     ~A
143 ***     ~A on ~A
144 ***     Database ~:@(~A~)
145 ***     Type: ~:@(~A~) backend~A.
146 ******************************************************************************
147 "
148           report-type
149           (clsql:format-time
150            nil
151            (clsql:utime->time (get-universal-time)))
152           (lisp-implementation-type)
153           (lisp-implementation-version)
154           (machine-type)
155           db-name
156           db-type
157           (if (not (eq db-type *test-database-underlying-type*))
158               (format nil " with underlying type ~:@(~A~)"
159                       *test-database-underlying-type*)
160               "")
161           ))
162
163 (defun do-tests-for-internals (&key (suites (internal-suites)))
164   (write-report-banner "Test Suite" "CLSQL Internals" *report-stream*
165                        "N/A")
166   (%do-tests suites nil))
167
168 (defun %do-tests (test-forms db-type)
169   (regression-test:rem-all-tests)
170   (dolist (test-form test-forms)
171     (eval test-form))
172
173   (let* ((cl:*print-right-margin* *test-report-width*)
174          (remaining (regression-test:do-tests *report-stream*)))
175     (when (regression-test:pending-tests)
176       (incf *error-count* (length remaining))))
177
178   (let ((sexp-error (list db-type
179                           *test-database-underlying-type*
180                           (get-universal-time)
181                           (length test-forms)
182                           (regression-test:pending-tests)
183                           (lisp-implementation-type)
184                           (lisp-implementation-version)
185                           (machine-type))))
186     (when *sexp-report-stream*
187       (write sexp-error :stream *sexp-report-stream* :readably t))
188     (push sexp-error *error-list*))
189   )
190
191 (defun do-tests-for-backend (db-type spec &key
192                              (suites (default-suites)) )
193   (test-connect-to-database db-type spec)
194   (unwind-protect
195        (multiple-value-bind (test-forms skip-tests)
196            (compute-tests-for-backend db-type *test-database-underlying-type* :suites suites)
197
198            (write-report-banner "Test Suite" db-type *report-stream*
199                                 (database-name-from-spec spec db-type))
200
201          (%do-tests test-forms db-type)
202
203            (format *report-stream* "~&Tests skipped:")
204            (if skip-tests
205                (let ((max-test-name (length (symbol-name (caar skip-tests)))))
206                  (dolist (skipped (cdr skip-tests))
207                    (let ((len (length (symbol-name (car skipped)))))
208                      (when (> len max-test-name)
209                        (setq max-test-name len))))
210                  (let ((fmt (format nil "~~&  ~~~DA ~~A~~%" max-test-name)))
211                    (dolist (skipped skip-tests)
212                      ;; word-wrap the reason string field
213                      (let* ((test (car skipped))
214                             (reason (cdr skipped))
215                             (rlen (length reason))
216                             (rwidth (max 20 (- (or *test-report-width* 80) max-test-name 3)))
217                             (rwords (clsql-sys::delimited-string-to-list reason #\space t))
218                             (rformat (format nil "~~{~~<~%~~1,~D:;~~A~~> ~~}" rwidth))
219                             (rwrapped (format nil rformat rwords))
220                             (rlines (clsql-sys::delimited-string-to-list rwrapped #\Newline t)))
221                        (dolist (rline rlines)
222                          (format *report-stream* fmt (if test
223                                                          (prog1
224                                                              test
225                                                            (setq test nil))
226                                                          "")
227                                  rline))))))
228                (format *report-stream* " None~%")))
229     (disconnect)))
230
231
232 (defun compute-tests-for-backend (db-type db-underlying-type
233                                   &key (suites (default-suites)))
234   (let ((test-forms '())
235         (skip-tests '()))
236     (dolist (test-form (if (listp suites) suites (list suites)))
237       (let ((test (second test-form)))
238         (cond
239           ((and (not (eql db-underlying-type :mysql))
240                 (clsql-sys:in test :connection/query-command
241                               :basic/reallybigintegers/1))
242            (push (cons test "known to work only in MySQL as yet.") skip-tests))
243           ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
244                 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
245            (push (cons test "views not supported.") skip-tests))
246           ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
247                 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
248            (push (cons test "boolean where not supported.") skip-tests))
249           ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
250                 (clsql-sys:in test :fdml/select/5 :fdml/select/10
251                               :fdml/select/32 :fdml/select/33))
252            (push (cons test "subqueries not supported.") skip-tests))
253           ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
254                                                     *default-database*))
255                 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
256            (push (cons test "transactions not supported.") skip-tests))
257           ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
258                 (clsql-sys:in test :fdml/select/1))
259            (push (cons test "fancy math not supported.") skip-tests))
260           ((and (eql *test-database-type* :sqlite)
261                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
262                                 :fdml/select/21 :fdml/select/32
263                                 :fdml/select/33))
264            (push (cons test "not supported by sqlite.") skip-tests))
265           ((and (eql *test-database-type* :sqlite3)
266                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
267                               :fdml/select/21 :fdml/select/32
268                               :fdml/select/33))
269            (push (cons test "not supported by sqlite3.") skip-tests))
270           ((and (not (clsql-sys:db-type-has-bigint? db-type))
271                 (clsql-sys:in test :basic/bigint/1))
272            (push (cons test "bigint not supported.") skip-tests))
273           ((and (eql *test-database-underlying-type* :mysql)
274                 (clsql-sys:in test :fdml/select/26))
275            (push (cons test "string table aliases not supported on all MySQL versions.") skip-tests))
276           ((and (eql *test-database-underlying-type* :mysql)
277                 (clsql-sys:in test :fdml/select/22 :fdml/query/5
278                                 :fdml/query/7 :fdml/query/8))
279            (push (cons test "not supported by mysql.") skip-tests))
280           ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
281                 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
282            (push (cons test "union not supported") skip-tests))
283           ((and (eq *test-database-type* :oracle)
284                 (clsql-sys:in test :fdml/query/8 :fdml/select/21
285                               :fddl/table/6))
286            (push (cons test "syntax not supported.") skip-tests))
287           ((and (eq *test-database-type* :odbc)
288                 (eq *test-database-underlying-type* :postgresql)
289                 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
290                               :fddl/owner/attributes
291                               :fddl/owner/attribute-types
292                               :fddl/owner/index
293                               :fddl/owner/sequence))
294           (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
295           ((and (not (member *test-database-underlying-type*
296                              '(:postgresql :oracle)))
297                 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
298                               :fddl/owner/attributes
299                               :fddl/owner/attribute-types
300                               :fddl/owner/index
301                               :fddl/owner/sequence))
302            (push (cons test "table ownership not supported.") skip-tests))
303           ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
304                 (clsql-sys:in test :fdml/query/7))
305            (push (cons test "intersect not supported.") skip-tests))
306           ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
307                 (clsql-sys:in test :fdml/query/8))
308            (push (cons test "except not supported.") skip-tests))
309           ((and (eq *test-database-underlying-type* :mssql)
310                 (clsql-sys:in test :fdml/select/9))
311            (push (cons test "mssql uses integer math for AVG.") skip-tests))
312           ((and (not (member *test-database-underlying-type*
313                              '(:postgresql :mysql :sqlite3 )))
314                 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
315            (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
316           ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
317                 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
318                               :oodml/select/15 :oodml/select/16 :oodml/select/17
319                               :oodml/select/18 :oodml/select/19 :oodml/select/20
320                               :oodml/select/21 :oodml/select/22 :oodml/select/23
321                               :oodml/update-records/4 :oodml/update-records/4-slots
322                               :oodml/update-records/5 :oodml/update-records/5-slots
323                               :oodml/update-records/6 :oodml/update-records/7
324                               :oodml/update-records/8 :oodml/update-records/9
325                               :oodml/update-records/9-slots :oodml/update-records/10
326                               :oodml/update-records/11 :OODML/UPDATE-RECORDS/12 :oodml/update-instance/3
327                               :oodml/update-instance/4 :oodml/update-instance/5
328                               :oodml/update-instance/6 :oodml/update-instance/7
329                               :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
330            (push (cons test ":auto-increment not supported.") skip-tests))
331          ((and (not (member *test-database-underlying-type*
332                             '(:postgresql :postgresql-socket)))
333                (clsql-sys:in test
334                              :time/pg/fdml/usec :time/pg/oodml/no-usec :time/pg/oodml/usec))
335           (push (cons test "Postgres specific test.")
336                 skip-tests))
337          ((and (eql *test-database-type* :postgresql-socket3)
338                (clsql-sys:in test :BASIC/SELECT/2 :basic/select/3))
339           (push (cons test "Postgres-socket3 always auto types")
340                 skip-tests))
341          ((and (eql *test-database-type* :postgresql-socket3)
342                (clsql-sys:in test :fdml/select/18))
343           (push (cons test "Postgres-socket3 doesnt support attribute based type coersion")
344                 skip-tests))
345          ((and (eql *test-database-type* :postgresql-socket3)
346                (clsql-sys:in test :basic/map/1 :basic/map/2 :basic/map/3 :basic/map/4
347                 :basic/do/1 :basic/do/2 :fdml/do-query/1 :fdml/map-query/1
348                 :fdml/map-query/2 :fdml/map-query/3 :fdml/map-query/4 :fdml/loop/1
349                 :fdml/loop/2 :fdml/loop/3
350                 ))
351           (push (cons test "postgresql-socket3 doesnt support cursoring interface")
352                 skip-tests))
353          ((and (member *test-database-underlying-type* '(:mysql))
354                (clsql-sys:in test :time/cross-platform/msec
355                              :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
356           (push (cons test "MySQL doesn't support fractional seconds on timestamp columns (http://forge.mysql.com/worklog/task.php?id=946).")
357                 skip-tests))
358           ((and (member *test-database-underlying-type* '(:mssql))
359                (clsql-sys:in test :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
360           (push (cons test "MSSQL doesn't support micro-seconds on datetime columns.")
361                 skip-tests))
362           (t
363            (push test-form test-forms)))))
364       (values (nreverse test-forms) (nreverse skip-tests))))
365
366 (defun rapid-load (type &optional (position 0))
367   "Rapid load for interactive testing."
368   (when *default-database*
369       (disconnect :database *default-database*))
370   (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
371   ;(test-initialise-database)
372   *default-database*)
373
374 (defun rl ()
375   (rapid-load :postgresql))
376
377 (defun rlm ()
378   (rapid-load :mysql))
379
380 (defun rlo ()
381   (rapid-load :oracle))