Remove `database-destory' [sic] and all doc references to it
[clsql/s11.git] / tests / test-init.lisp
blobb0a902ca0502c8bd54c13ea1d3747df0f3ebdf84
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 ;;;; Updated: $Id$
7 ;;;;
8 ;;;; Initialisation utilities for running regression tests on CLSQL.
9 ;;;;
10 ;;;; This file is part of CLSQL.
11 ;;;;
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; ======================================================================
17 (in-package #:clsql-tests)
19 (defvar *report-stream* *standard-output* "Stream to send text report.")
20 (defvar *sexp-report-stream* nil "Stream to send sexp report.")
21 (defvar *rt-internal*)
22 (defvar *rt-basic*)
23 (defvar *rt-connection*)
24 (defvar *rt-fddl*)
25 (defvar *rt-fdml*)
26 (defvar *rt-ooddl*)
27 (defvar *rt-oodml*)
28 (defvar *rt-syntax*)
29 (defvar *rt-time*)
31 (defvar *test-database-type* nil)
32 (defvar *test-database-underlying-type* nil)
33 (defvar *test-database-user* nil)
34 (defvar *test-start-utime* nil)
35 (defvar *test-connection-spec* nil)
36 (defvar *test-connection-db-type* nil)
38 (defclass thing ()
39 ((extraterrestrial :initform nil :initarg :extraterrestrial)))
41 (def-view-class person (thing)
42 ((height :db-kind :base :accessor height :type float
43 :initarg :height)
44 (married :db-kind :base :accessor married :type boolean
45 :initarg :married)
46 (birthday :type clsql:wall-time :initarg :birthday)
47 (bd-utime :type clsql:universal-time :initarg :bd-utime)
48 (hobby :db-kind :virtual :initarg :hobby :initform nil)))
50 (def-view-class employee (person)
51 ((emplid
52 :db-kind :key
53 :db-constraints (:not-null :unique)
54 :type integer
55 :initarg :emplid)
56 (groupid
57 :db-kind :key
58 :db-constraints :not-null
59 :type integer
60 :initarg :groupid)
61 (first-name
62 :accessor first-name
63 :type (varchar 30)
64 :initarg :first-name)
65 (last-name
66 :accessor last-name
67 :type (varchar 30)
68 :initarg :last-name)
69 (email
70 :accessor employee-email
71 :type (varchar 100)
72 :initarg :email)
73 (ecompanyid
74 :type integer
75 :initarg :companyid)
76 (company
77 :accessor employee-company
78 :db-kind :join
79 :db-info (:join-class company
80 :home-key ecompanyid
81 :foreign-key companyid
82 :set nil))
83 (managerid
84 :type integer
85 :initarg :managerid)
86 (manager
87 :accessor employee-manager
88 :db-kind :join
89 :db-info (:join-class employee
90 :home-key managerid
91 :foreign-key emplid
92 :set nil))
93 (addresses
94 :accessor employee-addresses
95 :db-kind :join
96 :db-info (:join-class employee-address
97 :home-key emplid
98 :foreign-key aemplid
99 :target-slot address
100 :set t)))
101 (:base-table employee))
103 (def-view-class company ()
104 ((companyid
105 :db-kind :key
106 :db-constraints :not-null
107 :type integer
108 :initarg :companyid)
109 (groupid
110 :db-kind :key
111 :db-constraints :not-null
112 :type integer
113 :initarg :groupid)
114 (name
115 :type (varchar 100)
116 :initarg :name)
117 (presidentid
118 :type integer
119 :initarg :presidentid)
120 (president
121 :reader president
122 :db-kind :join
123 :db-info (:join-class employee
124 :home-key presidentid
125 :foreign-key emplid
126 :set nil))
127 (employees
128 :reader company-employees
129 :db-kind :join
130 :db-info (:join-class employee
131 :home-key (companyid groupid)
132 :foreign-key (ecompanyid groupid)
133 :set t))))
135 (def-view-class address ()
136 ((addressid
137 :db-kind :key
138 :db-constraints :not-null
139 :type integer
140 :initarg :addressid)
141 (street-number
142 :type integer
143 :initarg :street-number)
144 (street-name
145 :type (varchar 30)
146 :void-value ""
147 :initarg :street-name)
148 (city
149 :column "city_field"
150 :void-value "no city"
151 :type (varchar 30)
152 :initarg :city)
153 (postal-code
154 :column zip
155 :type integer
156 :void-value 0
157 :initarg :postal-code))
158 (:base-table addr))
160 ;; many employees can reside at many addressess
161 (def-view-class employee-address ()
162 ((aemplid :type integer :initarg :emplid)
163 (aaddressid :type integer :initarg :addressid)
164 (verified :type boolean :initarg :verified)
165 (address :db-kind :join
166 :db-info (:join-class address
167 :home-key aaddressid
168 :foreign-key addressid
169 :retrieval :immediate))
170 (employee :db-kind :join
171 :db-info (:join-class employee
172 :home-key aemplid
173 :foreign-key emplid
174 :retrieval :immediate)))
175 (:base-table "ea_join"))
177 (def-view-class deferred-employee-address ()
178 ((aemplid :type integer :initarg :emplid)
179 (aaddressid :type integer :initarg :addressid)
180 (verified :type boolean :initarg :verified)
181 (address :db-kind :join
182 :db-info (:join-class address
183 :home-key aaddressid
184 :foreign-key addressid
185 :retrieval :deferred
186 :set nil)))
187 (:base-table "ea_join"))
189 (def-view-class big ()
190 ((i :type integer :initarg :i)
191 (bi :type bigint :initarg :bi)))
193 (defun test-connect-to-database (db-type spec)
194 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
195 (ignore-errors (destroy-database spec :database-type db-type))
196 (ignore-errors (create-database spec :database-type db-type)))
198 (setf *test-database-type* db-type)
199 (setf *test-database-user*
200 (cond
201 ((eq :oracle db-type) (second spec))
202 ((>= (length spec) 3) (third spec))))
204 ;; Connect to the database
205 (clsql:connect spec
206 :database-type db-type
207 :make-default t
208 :if-exists :old)
210 ;; Ensure database is empty
211 (truncate-database :database *default-database*)
213 (setf *test-database-underlying-type*
214 (clsql-sys:database-underlying-type *default-database*))
216 *default-database*)
218 (defparameter company1 nil)
219 (defparameter employee1 nil)
220 (defparameter employee2 nil)
221 (defparameter employee3 nil)
222 (defparameter employee4 nil)
223 (defparameter employee5 nil)
224 (defparameter employee6 nil)
225 (defparameter employee7 nil)
226 (defparameter employee8 nil)
227 (defparameter employee9 nil)
228 (defparameter employee10 nil)
229 (defparameter address1 nil)
230 (defparameter address2 nil)
231 (defparameter employee-address1 nil)
232 (defparameter employee-address2 nil)
233 (defparameter employee-address3 nil)
234 (defparameter employee-address4 nil)
235 (defparameter employee-address5 nil)
237 (defun test-initialise-database ()
238 (test-basic-initialize)
239 (let ((*backend-warning-behavior*
240 (if (member *test-database-type* '(:postgresql :postgresql-socket))
241 :ignore
242 :warn)))
243 (clsql:create-view-from-class 'employee)
244 (clsql:create-view-from-class 'company)
245 (clsql:create-view-from-class 'address)
246 (clsql:create-view-from-class 'employee-address)
247 (clsql:create-view-from-class 'big))
249 (setq *test-start-utime* (get-universal-time))
250 (let* ((*db-auto-sync* t)
251 (now-time (clsql:utime->time *test-start-utime*)))
252 (setf company1 (make-instance 'company
253 :presidentid 1
254 :companyid 1
255 :groupid 1
256 :name "Widgets Inc.")
257 employee1 (make-instance 'employee
258 :emplid 1
259 :groupid 1
260 :married t
261 :height (1+ (random 1.00))
262 :bd-utime *test-start-utime*
263 :birthday now-time
264 :first-name "Vladimir"
265 :last-name "Lenin"
266 :email "lenin@soviet.org"
267 :companyid 1)
268 employee2 (make-instance 'employee
269 :emplid 2
270 :groupid 1
271 :height (1+ (random 1.00))
272 :married t
273 :bd-utime *test-start-utime*
274 :birthday now-time
275 :first-name "Josef"
276 :last-name "Stalin"
277 :email "stalin@soviet.org"
278 :managerid 1
279 :companyid 1)
280 employee3 (make-instance 'employee
281 :emplid 3
282 :groupid 1
283 :height (1+ (random 1.00))
284 :married t
285 :bd-utime *test-start-utime*
286 :birthday now-time
287 :first-name "Leon"
288 :last-name "Trotsky"
289 :email "trotsky@soviet.org"
290 :managerid 1
291 :companyid 1)
292 employee4 (make-instance 'employee
293 :emplid 4
294 :groupid 1
295 :height (1+ (random 1.00))
296 :married nil
297 :bd-utime *test-start-utime*
298 :birthday now-time
299 :first-name "Nikita"
300 :last-name "Kruschev"
301 :email "kruschev@soviet.org"
302 :managerid 1
303 :companyid 1)
304 employee5 (make-instance 'employee
305 :emplid 5
306 :groupid 1
307 :married nil
308 :height (1+ (random 1.00))
309 :bd-utime *test-start-utime*
310 :birthday now-time
311 :first-name "Leonid"
312 :last-name "Brezhnev"
313 :email "brezhnev@soviet.org"
314 :managerid 1
315 :companyid 1)
316 employee6 (make-instance 'employee
317 :emplid 6
318 :groupid 1
319 :married nil
320 :height (1+ (random 1.00))
321 :bd-utime *test-start-utime*
322 :birthday now-time
323 :first-name "Yuri"
324 :last-name "Andropov"
325 :email "andropov@soviet.org"
326 :managerid 1
327 :companyid 1)
328 employee7 (make-instance 'employee
329 :emplid 7
330 :groupid 1
331 :height (1+ (random 1.00))
332 :married nil
333 :bd-utime *test-start-utime*
334 :birthday now-time
335 :first-name "Konstantin"
336 :last-name "Chernenko"
337 :email "chernenko@soviet.org"
338 :managerid 1
339 :companyid 1)
340 employee8 (make-instance 'employee
341 :emplid 8
342 :groupid 1
343 :height (1+ (random 1.00))
344 :married nil
345 :bd-utime *test-start-utime*
346 :birthday now-time
347 :first-name "Mikhail"
348 :last-name "Gorbachev"
349 :email "gorbachev@soviet.org"
350 :managerid 1
351 :companyid 1)
352 employee9 (make-instance 'employee
353 :emplid 9
354 :groupid 1
355 :married nil
356 :height (1+ (random 1.00))
357 :bd-utime *test-start-utime*
358 :birthday now-time
359 :first-name "Boris"
360 :last-name "Yeltsin"
361 :email "yeltsin@soviet.org"
362 :managerid 1
363 :companyid 1)
364 employee10 (make-instance 'employee
365 :emplid 10
366 :groupid 1
367 :married nil
368 :height (1+ (random 1.00))
369 :bd-utime *test-start-utime*
370 :birthday now-time
371 :first-name "Vladimir"
372 :last-name "Putin"
373 :email "putin@soviet.org"
374 :managerid 1
375 :companyid 1)
376 address1 (make-instance 'address
377 :addressid 1
378 :street-number 10
379 :street-name "Park Place"
380 :city "Leningrad"
381 :postal-code 123)
382 address2 (make-instance 'address
383 :addressid 2)
384 employee-address1 (make-instance 'employee-address
385 :emplid 1
386 :addressid 1
387 :verified t)
388 employee-address2 (make-instance 'employee-address
389 :emplid 2
390 :addressid 2
391 :verified t)
392 employee-address3 (make-instance 'employee-address
393 :emplid 3
394 :addressid 1
395 :verified nil)
396 employee-address4 (make-instance 'employee-address
397 :emplid 1
398 :addressid 2
399 :verified nil)
400 employee-address5 (make-instance 'employee-address
401 :emplid 3
402 :addressid 2))
404 (let ((max (expt 2 60)))
405 (dotimes (i 555)
406 (make-instance 'big :i (1+ i) :bi (truncate max (1+ i))))))
408 ;; sleep to ensure birthdays are no longer at current time
409 (sleep 1)
412 ;; Lenin manages everyone
413 (clsql:add-to-relation employee2 'manager employee1)
414 (clsql:add-to-relation employee3 'manager employee1)
415 (clsql:add-to-relation employee4 'manager employee1)
416 (clsql:add-to-relation employee5 'manager employee1)
417 (clsql:add-to-relation employee6 'manager employee1)
418 (clsql:add-to-relation employee7 'manager employee1)
419 (clsql:add-to-relation employee8 'manager employee1)
420 (clsql:add-to-relation employee9 'manager employee1)
421 (clsql:add-to-relation employee10 'manager employee1)
422 ;; Everyone works for Widgets Inc.
423 (clsql:add-to-relation company1 'employees employee1)
424 (clsql:add-to-relation company1 'employees employee2)
425 (clsql:add-to-relation company1 'employees employee3)
426 (clsql:add-to-relation company1 'employees employee4)
427 (clsql:add-to-relation company1 'employees employee5)
428 (clsql:add-to-relation company1 'employees employee6)
429 (clsql:add-to-relation company1 'employees employee7)
430 (clsql:add-to-relation company1 'employees employee8)
431 (clsql:add-to-relation company1 'employees employee9)
432 (clsql:add-to-relation company1 'employees employee10)
433 ;; Lenin is president of Widgets Inc.
434 (clsql:add-to-relation company1 'president employee1)
437 ;; store these instances
439 (clsql:update-records-from-instance employee1)
440 (clsql:update-records-from-instance employee2)
441 (clsql:update-records-from-instance employee3)
442 (clsql:update-records-from-instance employee4)
443 (clsql:update-records-from-instance employee5)
444 (clsql:update-records-from-instance employee6)
445 (clsql:update-records-from-instance employee7)
446 (clsql:update-records-from-instance employee8)
447 (clsql:update-records-from-instance employee9)
448 (clsql:update-records-from-instance employee10)
449 (clsql:update-records-from-instance company1)
450 (clsql:update-records-from-instance address1)
451 (clsql:update-records-from-instance address2)
455 (defvar *error-count* 0)
456 (defvar *error-list* nil)
458 (defun run-function-append-report-file (function report-file)
459 (let* ((report-path (etypecase report-file
460 (pathname report-file)
461 (string (parse-namestring report-file))))
462 (sexp-report-path (make-pathname :defaults report-path
463 :type "sexp")))
464 (with-open-file (rs report-path :direction :output
465 :if-exists :append
466 :if-does-not-exist :create)
467 (with-open-file (srs sexp-report-path :direction :output
468 :if-exists :append
469 :if-does-not-exist :create)
470 (funcall function :report-stream rs :sexp-report-stream srs)))))
472 (defun run-tests-append-report-file (report-file)
473 (run-function-append-report-file 'run-tests report-file))
476 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
477 ;; clear SQL-OUTPUT cache
478 (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
479 (let ((specs (read-specs))
480 (*report-stream* report-stream)
481 (*sexp-report-stream* sexp-report-stream)
482 (*error-count* 0)
483 (*error-list* nil))
484 (unless specs
485 (warn "Not running tests because test configuration file is missing")
486 (return-from run-tests :skipped))
487 (load-necessary-systems specs)
488 (dolist (db-type +all-db-types+)
489 (dolist (spec (db-type-spec db-type specs))
490 (let ((*test-connection-spec* spec)
491 (*test-connection-db-type* db-type))
492 (do-tests-for-backend db-type spec)))))
493 (zerop *error-count*))
495 (defun load-necessary-systems (specs)
496 (dolist (db-type +all-db-types+)
497 (when (db-type-spec db-type specs)
498 (clsql-sys:initialize-database-type :database-type db-type))))
500 (defun write-report-banner (report-type db-type stream)
501 (format stream
503 ******************************************************************************
504 *** CLSQL ~A begun at ~A
505 *** ~A
506 *** ~A on ~A
507 *** Database ~:@(~A~) backend~A.
508 ******************************************************************************
510 report-type
511 (clsql:format-time
513 (clsql:utime->time (get-universal-time)))
514 (lisp-implementation-type)
515 (lisp-implementation-version)
516 (machine-type)
517 db-type
518 (if (not (eq db-type *test-database-underlying-type*))
519 (format nil " with underlying type ~:@(~A~)"
520 *test-database-underlying-type*)
524 (defun do-tests-for-backend (db-type spec)
525 (test-connect-to-database db-type spec)
527 (unwind-protect
528 (multiple-value-bind (test-forms skip-tests)
529 (compute-tests-for-backend db-type *test-database-underlying-type*)
531 (write-report-banner "Test Suite" db-type *report-stream*)
533 (test-initialise-database)
535 (regression-test:rem-all-tests)
536 (dolist (test-form test-forms)
537 (eval test-form))
539 (let ((remaining (regression-test:do-tests *report-stream*)))
540 (when (regression-test:pending-tests)
541 (incf *error-count* (length remaining))))
543 (let ((sexp-error (list db-type
544 *test-database-underlying-type*
545 (get-universal-time)
546 (length test-forms)
547 (regression-test:pending-tests)
548 (lisp-implementation-type)
549 (lisp-implementation-version)
550 (machine-type))))
551 (when *sexp-report-stream*
552 (write sexp-error :stream *sexp-report-stream* :readably t))
553 (push sexp-error *error-list*))
555 (format *report-stream* "~&Tests skipped:")
556 (if skip-tests
557 (dolist (skipped skip-tests)
558 (format *report-stream*
559 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
560 (format *report-stream* " None~%")))
561 (disconnect)))
564 (defun compute-tests-for-backend (db-type db-underlying-type)
565 (let ((test-forms '())
566 (skip-tests '()))
567 (dolist (test-form (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
568 *rt-ooddl* *rt-oodml* *rt-syntax*))
569 (let ((test (second test-form)))
570 (cond
571 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
572 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
573 (push (cons test "views not supported") skip-tests))
574 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
575 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
576 (push (cons test "boolean where not supported") skip-tests))
577 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
578 (clsql-sys:in test :fdml/select/5 :fdml/select/10
579 :fdml/select/32 :fdml/select/33))
580 (push (cons test "subqueries not supported") skip-tests))
581 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
582 *default-database*))
583 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
584 (push (cons test "transactions not supported") skip-tests))
585 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
586 (clsql-sys:in test :fdml/select/1))
587 (push (cons test "fancy math not supported") skip-tests))
588 ((and (eql *test-database-type* :sqlite)
589 (clsql-sys:in test :fddl/view/4 :fdml/select/10
590 :fdml/select/21 :fdml/select/32
591 :fdml/select/33))
592 (push (cons test "not supported by sqlite") skip-tests))
593 ((and (eql *test-database-type* :sqlite3)
594 (clsql-sys:in test :fddl/view/4 :fdml/select/10
595 :fdml/select/21 :fdml/select/32
596 :fdml/select/33))
597 (push (cons test "not supported by sqlite3") skip-tests))
598 ((and (not (clsql-sys:db-type-has-bigint? db-type))
599 (clsql-sys:in test :basic/bigint/1))
600 (push (cons test "bigint not supported") skip-tests))
601 ((and (eql *test-database-underlying-type* :mysql)
602 (clsql-sys:in test :fdml/select/26))
603 (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
604 ((and (eql *test-database-underlying-type* :mysql)
605 (clsql-sys:in test :fdml/select/22 :fdml/query/5
606 :fdml/query/7 :fdml/query/8))
607 (push (cons test "not supported by mysql") skip-tests))
608 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
609 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
610 (push (cons test "union not supported") skip-tests))
611 ((and (eq *test-database-type* :oracle)
612 (clsql-sys:in test :fdml/query/8 :fdml/select/21
613 :fddl/table/6))
614 (push (cons test "syntax not supported") skip-tests))
615 ((and (eq *test-database-type* :odbc)
616 (eq *test-database-underlying-type* :postgresql)
617 (clsql-sys:in test :fddl/owner/1))
618 (push (cons test "table ownership not supported by postgresql odbc driver") skip-tests))
619 ((and (not (member *test-database-underlying-type*
620 '(:postgresql :oracle)))
621 (clsql-sys:in test :fddl/owner/1))
622 (push (cons test "table ownership not supported") skip-tests))
623 ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
624 (clsql-sys:in test :fdml/query/7))
625 (push (cons test "intersect not supported") skip-tests))
626 ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
627 (clsql-sys:in test :fdml/query/8))
628 (push (cons test "except not supported") skip-tests))
629 ((and (eq *test-database-underlying-type* :mssql)
630 (clsql-sys:in test :fdml/select/9))
631 (push (cons test "mssql uses integer math for AVG") skip-tests))
632 ((and (not (member *test-database-underlying-type*
633 '(:postgresql :mysql :sqlite3)))
634 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
635 (push (cons test "LIMIT keyword not supported in SELECT") skip-tests))
637 (push test-form test-forms)))))
638 (values (nreverse test-forms) (nreverse skip-tests))))
640 (defun rapid-load (type &optional (position 0))
641 "Rapid load for interactive testing."
642 (when *default-database*
643 (disconnect :database *default-database*))
644 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
645 (test-initialise-database)
646 *default-database*)
648 (defun rl ()
649 (rapid-load :postgresql))
651 (defun rlm ()
652 (rapid-load :mysql))
654 (defun rlo ()
655 (rapid-load :oracle))