Adding a test that is just micro-seconds for sqlserver, marking usec ones as skip.
[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
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 ((remaining (regression-test:do-tests *report-stream*)))
159              (when (regression-test:pending-tests)
160                (incf *error-count* (length remaining))))
161
162            (let ((sexp-error (list db-type
163                                    *test-database-underlying-type*
164                                    (get-universal-time)
165                                    (length test-forms)
166                                    (regression-test:pending-tests)
167                                    (lisp-implementation-type)
168                                    (lisp-implementation-version)
169                                    (machine-type))))
170              (when *sexp-report-stream*
171                (write sexp-error :stream *sexp-report-stream* :readably t))
172              (push sexp-error *error-list*))
173
174            (format *report-stream* "~&Tests skipped:")
175            (if skip-tests
176                (let ((max-test-name 20))
177                  (dolist (skipped skip-tests)
178                    (let ((len (length (symbol-name (car skipped)))))
179                      (when (> len max-test-name)
180                        (setq max-test-name len))))
181                  (let ((fmt (format nil "~~&  ~~~DA ~~A~~%" max-test-name)))
182                    (dolist (skipped skip-tests)
183                      (format *report-stream* fmt (car skipped) (cdr skipped)))))
184                (format *report-stream* " None~%")))
185     (disconnect)))
186
187
188 (defun compute-tests-for-backend (db-type db-underlying-type
189                                   &key (suites (default-suites)))
190   (let ((test-forms '())
191         (skip-tests '()))
192     (dolist (test-form (if (listp suites) suites (list suites)))
193       (let ((test (second test-form)))
194         (cond
195           ((and (not (eql db-underlying-type :mysql))
196                 (clsql-sys:in test :connection/query-command))
197            (push (cons test "Known to only work in mysql as yet.") skip-tests))
198           ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
199                 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
200            (push (cons test "views not supported.") skip-tests))
201           ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
202                 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
203            (push (cons test "boolean where not supported.") skip-tests))
204           ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
205                 (clsql-sys:in test :fdml/select/5 :fdml/select/10
206                               :fdml/select/32 :fdml/select/33))
207            (push (cons test "subqueries not supported.") skip-tests))
208           ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
209                                                     *default-database*))
210                 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
211            (push (cons test "transactions not supported.") skip-tests))
212           ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
213                 (clsql-sys:in test :fdml/select/1))
214            (push (cons test "fancy math not supported.") skip-tests))
215           ((and (eql *test-database-type* :sqlite)
216                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
217                                 :fdml/select/21 :fdml/select/32
218                                 :fdml/select/33))
219            (push (cons test "not supported by sqlite.") skip-tests))
220           ((and (eql *test-database-type* :sqlite3)
221                 (clsql-sys:in test :fddl/view/4 :fdml/select/10
222                               :fdml/select/21 :fdml/select/32
223                               :fdml/select/33))
224            (push (cons test "not supported by sqlite3.") skip-tests))
225           ((and (not (clsql-sys:db-type-has-bigint? db-type))
226                 (clsql-sys:in test :basic/bigint/1))
227            (push (cons test "bigint not supported.") skip-tests))
228           ((and (eql *test-database-underlying-type* :mysql)
229                 (clsql-sys:in test :fdml/select/26))
230            (push (cons test "string table aliases not supported on all mysql versions.") skip-tests))
231           ((and (eql *test-database-underlying-type* :mysql)
232                 (clsql-sys:in test :fdml/select/22 :fdml/query/5
233                                 :fdml/query/7 :fdml/query/8))
234            (push (cons test "not supported by mysql.") skip-tests))
235           ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
236                 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
237            (push (cons test "union not supported") skip-tests))
238           ((and (eq *test-database-type* :oracle)
239                 (clsql-sys:in test :fdml/query/8 :fdml/select/21
240                               :fddl/table/6))
241            (push (cons test "syntax not supported.") skip-tests))
242           ((and (eq *test-database-type* :odbc)
243                 (eq *test-database-underlying-type* :postgresql)
244                 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
245                               :fddl/owner/attributes
246                               :fddl/owner/attribute-types
247                               :fddl/owner/index
248                               :fddl/owner/sequence))
249           (push (cons test "table ownership not supported by postgresql odbc driver.") skip-tests))
250           ((and (not (member *test-database-underlying-type*
251                              '(:postgresql :oracle)))
252                 (clsql-sys:in test :fddl/owner/1 :fddl/owner/table
253                               :fddl/owner/attributes
254                               :fddl/owner/attribute-types
255                               :fddl/owner/index
256                               :fddl/owner/sequence))
257            (push (cons test "table ownership not supported.") skip-tests))
258           ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
259                 (clsql-sys:in test :fdml/query/7))
260            (push (cons test "intersect not supported.") skip-tests))
261           ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
262                 (clsql-sys:in test :fdml/query/8))
263            (push (cons test "except not supported.") skip-tests))
264           ((and (eq *test-database-underlying-type* :mssql)
265                 (clsql-sys:in test :fdml/select/9))
266            (push (cons test "mssql uses integer math for AVG.") skip-tests))
267           ((and (not (member *test-database-underlying-type*
268                              '(:postgresql :mysql :sqlite3)))
269                 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
270            (push (cons test "LIMIT keyword not supported in SELECT.") skip-tests))
271           ((and (not (clsql-sys:db-type-has-auto-increment? db-underlying-type))
272                 (clsql-sys:in test :oodml/select/12 :oodml/select/13 :oodml/select/14
273                               :oodml/select/15 :oodml/select/16 :oodml/select/17
274                               :oodml/select/18 :oodml/select/19 :oodml/select/20
275                               :oodml/select/21 :oodml/select/22
276                               :oodml/update-records/4 :oodml/update-records/4-slots
277                               :oodml/update-records/5 :oodml/update-records/5-slots
278                               :oodml/update-records/6 :oodml/update-records/7
279                               :oodml/update-records/8 :oodml/update-records/9
280                               :oodml/update-records/9-slots :oodml/update-instance/3
281                               :oodml/update-instance/4 :oodml/update-instance/5
282                               :oodml/update-instance/6 :oodml/update-instance/7
283                               :oodml/db-auto-sync/3 :oodml/db-auto-sync/4))
284            (push (cons test ":auto-increment not supported.") skip-tests))
285          ((and (not (member *test-database-underlying-type*
286                             '(:postgresql :postgresql-socket)))
287                (clsql-sys:in test
288                              :time/pg/fdml/usec :time/pg/oodml/no-usec :time/pg/oodml/usec))
289           (push (cons test "Postgres specific test.")
290                 skip-tests))
291          ((and (member *test-database-underlying-type* '(:mysql))
292                (clsql-sys:in test :time/cross-platform/msec
293                              :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
294           (push (cons test "MySQL doesn't support fractional seconds on timestamp columns (http://forge.mysql.com/worklog/task.php?id=946).")
295                 skip-tests))
296           ((and (member *test-database-underlying-type* '(:mssql))
297                (clsql-sys:in test :time/cross-platform/usec/no-tz :time/cross-platform/usec/tz))
298           (push (cons test "MSSQL doesn't support micro-seconds on datetime columns.")
299                 skip-tests))
300           (t
301            (push test-form test-forms)))))
302       (values (nreverse test-forms) (nreverse skip-tests))))
303
304 (defun rapid-load (type &optional (position 0))
305   "Rapid load for interactive testing."
306   (when *default-database*
307       (disconnect :database *default-database*))
308   (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
309   ;(test-initialise-database)
310   *default-database*)
311
312 (defun rl ()
313   (rapid-load :postgresql))
314
315 (defun rlm ()
316   (rapid-load :mysql))
317
318 (defun rlo ()
319   (rapid-load :oracle))