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
8 ;;;; Initialisation utilities for running regression tests on CLSQL.
10 ;;;; This file is part of CLSQL.
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*)
23 (defvar *rt-connection*)
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)
37 ((extraterrestrial :initform nil :initarg :extraterrestrial)))
39 (def-view-class person (thing)
40 ((height :db-kind :base :accessor height :type float
42 (married :db-kind :base :accessor married :type boolean
44 (birthday :type clsql:wall-time :initarg :birthday)
45 (bd-utime :type clsql:universal-time :initarg :bd-utime)
46 (hobby :db-kind :virtual :initarg :hobby :initform nil)))
48 (def-view-class employee (person)
51 :db-constraints (:not-null :unique)
56 :db-constraints :not-null
68 :accessor employee-email
75 :accessor employee-company
77 :db-info (:join-class company
79 :foreign-key companyid
85 :accessor employee-manager
87 :db-info (:join-class employee
92 :accessor employee-addresses
94 :db-info (:join-class employee-address
99 (:base-table employee))
101 (def-view-class company ()
104 :db-constraints :not-null
109 :db-constraints :not-null
117 :initarg :presidentid)
121 :db-info (:join-class employee
122 :home-key presidentid
126 :reader company-employees
128 :db-info (:join-class employee
129 :home-key (companyid groupid)
130 :foreign-key (ecompanyid groupid)
133 (def-view-class address ()
136 :db-constraints :not-null
141 :initarg :street-number)
145 :initarg :street-name)
148 :void-value "no city"
155 :initarg :postal-code))
158 ;; many employees can reside at many addressess
159 (def-view-class employee-address ()
160 ((aemplid :type integer :initarg :emplid)
161 (aaddressid :type integer :initarg :addressid)
162 (verified :type boolean :initarg :verified)
163 (address :db-kind :join
164 :db-info (:join-class address
166 :foreign-key addressid
167 :retrieval :immediate)))
168 (:base-table "ea_join"))
170 (def-view-class deferred-employee-address ()
171 ((aemplid :type integer :initarg :emplid)
172 (aaddressid :type integer :initarg :addressid)
173 (verified :type boolean :initarg :verified)
174 (address :db-kind :join
175 :db-info (:join-class address
177 :foreign-key addressid
180 (:base-table "ea_join"))
182 (def-view-class big ()
183 ((i :type integer :initarg :i)
184 (bi :type bigint :initarg :bi)))
186 (defun test-connect-to-database (db-type spec)
187 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
188 (ignore-errors (destroy-database spec :database-type db-type))
189 (ignore-errors (create-database spec :database-type db-type)))
191 (setf *test-database-type* db-type)
192 (setf *test-database-user*
194 ((eq :oracle db-type) (second spec))
195 ((>= (length spec) 3) (third spec))))
197 ;; Connect to the database
199 :database-type db-type
203 ;; Ensure database is empty
204 (truncate-database :database *default-database*)
206 (setf *test-database-underlying-type*
207 (clsql-sys:database-underlying-type *default-database*))
211 (defparameter company1 nil)
212 (defparameter employee1 nil)
213 (defparameter employee2 nil)
214 (defparameter employee3 nil)
215 (defparameter employee4 nil)
216 (defparameter employee5 nil)
217 (defparameter employee6 nil)
218 (defparameter employee7 nil)
219 (defparameter employee8 nil)
220 (defparameter employee9 nil)
221 (defparameter employee10 nil)
222 (defparameter address1 nil)
223 (defparameter address2 nil)
224 (defparameter employee-address1 nil)
225 (defparameter employee-address2 nil)
226 (defparameter employee-address3 nil)
227 (defparameter employee-address4 nil)
228 (defparameter employee-address5 nil)
230 (defun test-initialise-database ()
231 (test-basic-initialize)
232 (let ((*backend-warning-behavior*
233 (if (member *test-database-type* '(:postgresql :postgresql-socket))
236 (clsql:create-view-from-class 'employee)
237 (clsql:create-view-from-class 'company)
238 (clsql:create-view-from-class 'address)
239 (clsql:create-view-from-class 'employee-address)
240 (clsql:create-view-from-class 'big))
242 (setq *test-start-utime* (get-universal-time))
243 (let* ((*db-auto-sync* t)
244 (now-time (clsql:utime->time *test-start-utime*)))
245 (setf company1 (make-instance 'company
249 :name "Widgets Inc.")
250 employee1 (make-instance 'employee
254 :height (1+ (random 1.00))
255 :bd-utime *test-start-utime*
257 :first-name "Vladamir"
259 :email "lenin@soviet.org"
261 employee2 (make-instance 'employee
264 :height (1+ (random 1.00))
266 :bd-utime *test-start-utime*
270 :email "stalin@soviet.org"
273 employee3 (make-instance 'employee
276 :height (1+ (random 1.00))
278 :bd-utime *test-start-utime*
282 :email "trotsky@soviet.org"
285 employee4 (make-instance 'employee
288 :height (1+ (random 1.00))
290 :bd-utime *test-start-utime*
293 :last-name "Kruschev"
294 :email "kruschev@soviet.org"
297 employee5 (make-instance 'employee
301 :height (1+ (random 1.00))
302 :bd-utime *test-start-utime*
305 :last-name "Brezhnev"
306 :email "brezhnev@soviet.org"
309 employee6 (make-instance 'employee
313 :height (1+ (random 1.00))
314 :bd-utime *test-start-utime*
317 :last-name "Andropov"
318 :email "andropov@soviet.org"
321 employee7 (make-instance 'employee
324 :height (1+ (random 1.00))
326 :bd-utime *test-start-utime*
328 :first-name "Konstantin"
329 :last-name "Chernenko"
330 :email "chernenko@soviet.org"
333 employee8 (make-instance 'employee
336 :height (1+ (random 1.00))
338 :bd-utime *test-start-utime*
340 :first-name "Mikhail"
341 :last-name "Gorbachev"
342 :email "gorbachev@soviet.org"
345 employee9 (make-instance 'employee
349 :height (1+ (random 1.00))
350 :bd-utime *test-start-utime*
354 :email "yeltsin@soviet.org"
357 employee10 (make-instance 'employee
361 :height (1+ (random 1.00))
362 :bd-utime *test-start-utime*
364 :first-name "Vladamir"
366 :email "putin@soviet.org"
369 address1 (make-instance 'address
372 :street-name "Park Place"
375 address2 (make-instance 'address
377 employee-address1 (make-instance 'employee-address
381 employee-address2 (make-instance 'employee-address
385 employee-address3 (make-instance 'employee-address
389 employee-address4 (make-instance 'employee-address
393 employee-address5 (make-instance 'employee-address
397 (let ((max (expt 2 60)))
399 (make-instance 'big :i (1+ i) :bi (truncate max (1+ i))))))
401 ;; sleep to ensure birthdays are no longer at current time
405 ;; Lenin manages everyone
406 (clsql:add-to-relation employee2 'manager employee1)
407 (clsql:add-to-relation employee3 'manager employee1)
408 (clsql:add-to-relation employee4 'manager employee1)
409 (clsql:add-to-relation employee5 'manager employee1)
410 (clsql:add-to-relation employee6 'manager employee1)
411 (clsql:add-to-relation employee7 'manager employee1)
412 (clsql:add-to-relation employee8 'manager employee1)
413 (clsql:add-to-relation employee9 'manager employee1)
414 (clsql:add-to-relation employee10 'manager employee1)
415 ;; Everyone works for Widgets Inc.
416 (clsql:add-to-relation company1 'employees employee1)
417 (clsql:add-to-relation company1 'employees employee2)
418 (clsql:add-to-relation company1 'employees employee3)
419 (clsql:add-to-relation company1 'employees employee4)
420 (clsql:add-to-relation company1 'employees employee5)
421 (clsql:add-to-relation company1 'employees employee6)
422 (clsql:add-to-relation company1 'employees employee7)
423 (clsql:add-to-relation company1 'employees employee8)
424 (clsql:add-to-relation company1 'employees employee9)
425 (clsql:add-to-relation company1 'employees employee10)
426 ;; Lenin is president of Widgets Inc.
427 (clsql:add-to-relation company1 'president employee1)
430 ;; store these instances
432 (clsql:update-records-from-instance employee1)
433 (clsql:update-records-from-instance employee2)
434 (clsql:update-records-from-instance employee3)
435 (clsql:update-records-from-instance employee4)
436 (clsql:update-records-from-instance employee5)
437 (clsql:update-records-from-instance employee6)
438 (clsql:update-records-from-instance employee7)
439 (clsql:update-records-from-instance employee8)
440 (clsql:update-records-from-instance employee9)
441 (clsql:update-records-from-instance employee10)
442 (clsql:update-records-from-instance company1)
443 (clsql:update-records-from-instance address1)
444 (clsql:update-records-from-instance address2)
448 (defvar *error-count* 0)
449 (defvar *error-list* nil)
451 (defun run-function-append-report-file (function report-file)
452 (let* ((report-path (etypecase report-file
453 (pathname report-file)
454 (string (parse-namestring report-file))))
455 (sexp-report-path (make-pathname :defaults report-path
457 (with-open-file (rs report-path :direction :output
459 :if-does-not-exist :create)
460 (with-open-file (srs sexp-report-path :direction :output
462 :if-does-not-exist :create)
463 (funcall function :report-stream rs :sexp-report-stream srs)))))
465 (defun run-tests-append-report-file (report-file)
466 (run-function-append-report-file 'run-tests report-file))
469 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
470 (let ((specs (read-specs))
471 (*report-stream* report-stream)
472 (*sexp-report-stream* sexp-report-stream)
476 (warn "Not running tests because test configuration file is missing")
477 (return-from run-tests :skipped))
478 (load-necessary-systems specs)
479 (dolist (db-type +all-db-types+)
480 (dolist (spec (db-type-spec db-type specs))
481 (do-tests-for-backend db-type spec))))
482 (zerop *error-count*))
484 (defun load-necessary-systems (specs)
485 (dolist (db-type +all-db-types+)
486 (when (db-type-spec db-type specs)
487 (clsql-sys:initialize-database-type :database-type db-type))))
489 (defun write-report-banner (report-type db-type stream)
492 ******************************************************************************
493 *** CLSQL ~A begun at ~A
496 *** Database ~:@(~A~) backend~A.
497 ******************************************************************************
502 (clsql:utime->time (get-universal-time)))
503 (lisp-implementation-type)
504 (lisp-implementation-version)
507 (if (not (eq db-type *test-database-underlying-type*))
508 (format nil " with underlying type ~:@(~A~)"
509 *test-database-underlying-type*)
513 (defun do-tests-for-backend (db-type spec)
514 (test-connect-to-database db-type spec)
517 (multiple-value-bind (test-forms skip-tests)
518 (compute-tests-for-backend db-type *test-database-underlying-type*)
520 (write-report-banner "Test Suite" db-type *report-stream*)
522 (test-initialise-database)
524 (regression-test:rem-all-tests)
525 (dolist (test-form test-forms)
528 (let ((remaining (regression-test:do-tests *report-stream*)))
529 (when (regression-test:pending-tests)
530 (incf *error-count* (length remaining))))
532 (let ((sexp-error (list db-type
533 *test-database-underlying-type*
536 (regression-test:pending-tests)
537 (lisp-implementation-type)
538 (lisp-implementation-version)
540 (when *sexp-report-stream*
541 (write sexp-error :stream *sexp-report-stream* :readably t))
542 (push sexp-error *error-list*))
544 (format *report-stream* "~&Tests skipped:")
546 (dolist (skipped skip-tests)
547 (format *report-stream*
548 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
549 (format *report-stream* " None~%")))
553 (defun compute-tests-for-backend (db-type db-underlying-type)
554 (let ((test-forms '())
556 (dolist (test-form (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
557 *rt-ooddl* *rt-oodml* *rt-syntax*))
558 (let ((test (second test-form)))
560 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
561 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
562 (push (cons test "views not supported") skip-tests))
563 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
564 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
565 (push (cons test "boolean where not supported") skip-tests))
566 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
567 (clsql-sys:in test :fdml/select/5 :fdml/select/10
568 :fdml/select/32 :fdml/select/33))
569 (push (cons test "subqueries not supported") skip-tests))
570 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
572 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
573 (push (cons test "transactions not supported") skip-tests))
574 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
575 (clsql-sys:in test :fdml/select/1))
576 (push (cons test "fancy math not supported") skip-tests))
577 ((and (eql *test-database-type* :sqlite)
578 (clsql-sys:in test :fddl/view/4 :fdml/select/10
579 :fdml/select/21 :fdml/select/32
581 (push (cons test "not supported by sqlite") skip-tests))
582 ((and (not (clsql-sys:db-type-has-bigint? db-type))
583 (clsql-sys:in test :basic/bigint/1))
584 (push (cons test "bigint not supported") skip-tests))
585 ((and (eql *test-database-underlying-type* :mysql)
586 (clsql-sys:in test :fdml/select/26))
587 (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
588 ((and (eql *test-database-underlying-type* :mysql)
589 (clsql-sys:in test :fdml/select/22 :fdml/query/5
590 :fdml/query/7 :fdml/query/8))
591 (push (cons test "not supported by mysql") skip-tests))
592 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
593 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
594 (push (cons test "union not supported") skip-tests))
595 ((and (eq *test-database-type* :oracle)
596 (clsql-sys:in test :fdml/query/8 :fdml/select/21
598 (push (cons test "syntax not supported") skip-tests))
599 ((and (not (member *test-database-underlying-type*
600 '(:postgresql :oracle)))
601 (clsql-sys:in test :fddl/owner/1))
602 (push (cons test "table ownership not supported") skip-tests))
604 (push test-form test-forms)))))
605 (values (nreverse test-forms) (nreverse skip-tests))))
608 (defun rapid-load (type &optional (position 0))
609 "Rapid load for interactive testing."
610 (when *default-database*
611 (disconnect :database *default-database*))
612 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
613 (test-initialise-database)
617 (rapid-load :postgresql))
623 (rapid-load :oracle))