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