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