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-connection*)
30 (defvar *test-database-type* nil)
31 (defvar *test-database-underlying-type* nil)
32 (defvar *test-database-user* nil)
33 (defvar *test-start-utime* nil)
36 ((extraterrestrial :initform nil :initarg :extraterrestrial)))
38 (def-view-class person (thing)
39 ((height :db-kind :base :accessor height :type float
41 (married :db-kind :base :accessor married :type boolean
43 (birthday :type clsql:wall-time :initarg :birthday)
44 (bd-utime :type clsql:universal-time :initarg :bd-utime)
45 (hobby :db-kind :virtual :initarg :hobby :initform nil)))
47 (def-view-class employee (person)
50 :db-constraints :not-null
55 :db-constraints :not-null
67 :accessor employee-email
74 :accessor employee-company
76 :db-info (:join-class company
78 :foreign-key companyid
84 :accessor employee-manager
86 :db-info (:join-class employee
91 :accessor employee-addresses
93 :db-info (:join-class employee-address
98 (:base-table employee))
100 (def-view-class company ()
103 :db-constraints :not-null
108 :db-constraints :not-null
116 :initarg :presidentid)
120 :db-info (:join-class employee
121 :home-key presidentid
125 :reader company-employees
127 :db-info (:join-class employee
128 :home-key (companyid groupid)
129 :foreign-key (ecompanyid groupid)
132 (def-view-class address ()
135 :db-constraints :not-null
140 :initarg :street-number)
144 :initarg :street-name)
147 :void-value "no city"
154 :initarg :postal-code))
157 ;; many employees can reside at many addressess
158 (def-view-class employee-address ()
159 ((aemplid :type integer :initarg :emplid)
160 (aaddressid :type integer :initarg :addressid)
161 (verified :type boolean :initarg :verified)
162 (address :db-kind :join
163 :db-info (:join-class address
165 :foreign-key addressid
166 :retrieval :immediate)))
167 (:base-table "ea_join"))
169 (def-view-class deferred-employee-address ()
170 ((aemplid :type integer :initarg :emplid)
171 (aaddressid :type integer :initarg :addressid)
172 (verified :type boolean :initarg :verified)
173 (address :db-kind :join
174 :db-info (:join-class address
176 :foreign-key addressid
179 (:base-table "ea_join"))
181 (def-view-class big ()
182 ((i :type integer :initarg :i)
183 (bi :type bigint :initarg :bi)))
185 (defun test-connect-to-database (db-type spec)
186 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
187 (ignore-errors (destroy-database spec :database-type db-type))
188 (ignore-errors (create-database spec :database-type db-type)))
190 (setf *test-database-type* db-type)
191 (setf *test-database-user*
193 ((eq :oracle db-type) (second spec))
194 ((>= (length spec) 3) (third spec))))
196 ;; Connect to the database
198 :database-type db-type
202 ;; Ensure database is empty
203 (truncate-database :database *default-database*)
205 (setf *test-database-underlying-type*
206 (clsql-sys:database-underlying-type *default-database*))
210 (defparameter company1 nil)
211 (defparameter employee1 nil)
212 (defparameter employee2 nil)
213 (defparameter employee3 nil)
214 (defparameter employee4 nil)
215 (defparameter employee5 nil)
216 (defparameter employee6 nil)
217 (defparameter employee7 nil)
218 (defparameter employee8 nil)
219 (defparameter employee9 nil)
220 (defparameter employee10 nil)
221 (defparameter address1 nil)
222 (defparameter address2 nil)
223 (defparameter employee-address1 nil)
224 (defparameter employee-address2 nil)
225 (defparameter employee-address3 nil)
226 (defparameter employee-address4 nil)
227 (defparameter employee-address5 nil)
229 (defun test-initialise-database ()
230 (test-basic-initialize)
231 (let ((*backend-warning-behavior*
232 (if (member *test-database-type* '(:postgresql :postgresql-socket))
235 (clsql:create-view-from-class 'employee)
236 (clsql:create-view-from-class 'company)
237 (clsql:create-view-from-class 'address)
238 (clsql:create-view-from-class 'employee-address)
239 (clsql:create-view-from-class 'big))
241 (setq *test-start-utime* (get-universal-time))
242 (let* ((*db-auto-sync* t)
243 (now-time (clsql:utime->time *test-start-utime*)))
244 (setf company1 (make-instance 'company
248 :name "Widgets Inc.")
249 employee1 (make-instance 'employee
253 :height (1+ (random 1.00))
254 :bd-utime *test-start-utime*
256 :first-name "Vladamir"
258 :email "lenin@soviet.org"
260 employee2 (make-instance 'employee
263 :height (1+ (random 1.00))
265 :bd-utime *test-start-utime*
269 :email "stalin@soviet.org"
272 employee3 (make-instance 'employee
275 :height (1+ (random 1.00))
277 :bd-utime *test-start-utime*
281 :email "trotsky@soviet.org"
284 employee4 (make-instance 'employee
287 :height (1+ (random 1.00))
289 :bd-utime *test-start-utime*
292 :last-name "Kruschev"
293 :email "kruschev@soviet.org"
296 employee5 (make-instance 'employee
300 :height (1+ (random 1.00))
301 :bd-utime *test-start-utime*
304 :last-name "Brezhnev"
305 :email "brezhnev@soviet.org"
308 employee6 (make-instance 'employee
312 :height (1+ (random 1.00))
313 :bd-utime *test-start-utime*
316 :last-name "Andropov"
317 :email "andropov@soviet.org"
320 employee7 (make-instance 'employee
323 :height (1+ (random 1.00))
325 :bd-utime *test-start-utime*
327 :first-name "Konstantin"
328 :last-name "Chernenko"
329 :email "chernenko@soviet.org"
332 employee8 (make-instance 'employee
335 :height (1+ (random 1.00))
337 :bd-utime *test-start-utime*
339 :first-name "Mikhail"
340 :last-name "Gorbachev"
341 :email "gorbachev@soviet.org"
344 employee9 (make-instance 'employee
348 :height (1+ (random 1.00))
349 :bd-utime *test-start-utime*
353 :email "yeltsin@soviet.org"
356 employee10 (make-instance 'employee
360 :height (1+ (random 1.00))
361 :bd-utime *test-start-utime*
363 :first-name "Vladamir"
365 :email "putin@soviet.org"
368 address1 (make-instance 'address
371 :street-name "Park Place"
374 address2 (make-instance 'address
376 employee-address1 (make-instance 'employee-address
380 employee-address2 (make-instance 'employee-address
384 employee-address3 (make-instance 'employee-address
388 employee-address4 (make-instance 'employee-address
392 employee-address5 (make-instance 'employee-address
396 (let ((max (expt 2 60)))
398 (make-instance 'big :i (1+ i) :bi (truncate max (1+ i))))))
400 ;; sleep to ensure birthdays are no longer at current time
404 ;; Lenin manages everyone
405 (clsql:add-to-relation employee2 'manager employee1)
406 (clsql:add-to-relation employee3 'manager employee1)
407 (clsql:add-to-relation employee4 'manager employee1)
408 (clsql:add-to-relation employee5 'manager employee1)
409 (clsql:add-to-relation employee6 'manager employee1)
410 (clsql:add-to-relation employee7 'manager employee1)
411 (clsql:add-to-relation employee8 'manager employee1)
412 (clsql:add-to-relation employee9 'manager employee1)
413 (clsql:add-to-relation employee10 'manager employee1)
414 ;; Everyone works for Widgets Inc.
415 (clsql:add-to-relation company1 'employees employee1)
416 (clsql:add-to-relation company1 'employees employee2)
417 (clsql:add-to-relation company1 'employees employee3)
418 (clsql:add-to-relation company1 'employees employee4)
419 (clsql:add-to-relation company1 'employees employee5)
420 (clsql:add-to-relation company1 'employees employee6)
421 (clsql:add-to-relation company1 'employees employee7)
422 (clsql:add-to-relation company1 'employees employee8)
423 (clsql:add-to-relation company1 'employees employee9)
424 (clsql:add-to-relation company1 'employees employee10)
425 ;; Lenin is president of Widgets Inc.
426 (clsql:add-to-relation company1 'president employee1)
429 ;; store these instances
431 (clsql:update-records-from-instance employee1)
432 (clsql:update-records-from-instance employee2)
433 (clsql:update-records-from-instance employee3)
434 (clsql:update-records-from-instance employee4)
435 (clsql:update-records-from-instance employee5)
436 (clsql:update-records-from-instance employee6)
437 (clsql:update-records-from-instance employee7)
438 (clsql:update-records-from-instance employee8)
439 (clsql:update-records-from-instance employee9)
440 (clsql:update-records-from-instance employee10)
441 (clsql:update-records-from-instance company1)
442 (clsql:update-records-from-instance address1)
443 (clsql:update-records-from-instance address2)
447 (defvar *error-count* 0)
448 (defvar *error-list* nil)
450 (defun run-function-append-report-file (function report-file)
451 (let* ((report-path (etypecase report-file
452 (pathname report-file)
453 (string (parse-namestring report-file))))
454 (sexp-report-path (make-pathname :defaults report-path
456 (with-open-file (rs report-path :direction :output
458 :if-does-not-exist :create)
459 (with-open-file (srs sexp-report-path :direction :output
461 :if-does-not-exist :create)
462 (funcall function :report-stream rs :sexp-report-stream srs)))))
464 (defun run-tests-append-report-file (report-file)
465 (run-function-append-report-file 'run-tests report-file))
468 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
469 (let ((specs (read-specs))
470 (*report-stream* report-stream)
471 (*sexp-report-stream* sexp-report-stream)
475 (warn "Not running tests because test configuration file is missing")
476 (return-from run-tests :skipped))
477 (load-necessary-systems specs)
478 (dolist (db-type +all-db-types+)
479 (dolist (spec (db-type-spec db-type specs))
480 (do-tests-for-backend db-type spec))))
481 (zerop *error-count*))
483 (defun load-necessary-systems (specs)
484 (dolist (db-type +all-db-types+)
485 (when (db-type-spec db-type specs)
486 (clsql-sys:initialize-database-type :database-type db-type))))
488 (defun write-report-banner (report-type db-type stream)
491 ******************************************************************************
492 *** CLSQL ~A begun at ~A
495 *** Database ~:@(~A~) backend~A.
496 ******************************************************************************
501 (clsql:utime->time (get-universal-time)))
502 (lisp-implementation-type)
503 (lisp-implementation-version)
506 (if (not (eq db-type *test-database-underlying-type*))
507 (format nil " with underlying type ~:@(~A~)"
508 *test-database-underlying-type*)
512 (defun do-tests-for-backend (db-type spec)
513 (test-connect-to-database db-type spec)
516 (multiple-value-bind (test-forms skip-tests)
517 (compute-tests-for-backend db-type *test-database-underlying-type*)
519 (write-report-banner "Test Suite" db-type *report-stream*)
521 (test-initialise-database)
523 (regression-test:rem-all-tests)
524 (dolist (test-form test-forms)
527 (let ((remaining (regression-test:do-tests *report-stream*)))
528 (when (regression-test:pending-tests)
529 (incf *error-count* (length remaining))))
531 (let ((sexp-error (list db-type
532 *test-database-underlying-type*
535 (regression-test:pending-tests)
536 (lisp-implementation-type)
537 (lisp-implementation-version)
539 (when *sexp-report-stream*
540 (write sexp-error :stream *sexp-report-stream* :readably t))
541 (push sexp-error *error-list*))
543 (format *report-stream* "~&Tests skipped:")
545 (dolist (skipped skip-tests)
546 (format *report-stream*
547 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
548 (format *report-stream* " None~%")))
552 (defun compute-tests-for-backend (db-type db-underlying-type)
553 (let ((test-forms '())
555 (dolist (test-form (append *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
556 *rt-ooddl* *rt-oodml* *rt-syntax*))
557 (let ((test (second test-form)))
559 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
560 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
561 (push (cons test "views not supported") skip-tests))
562 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
563 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
564 (push (cons test "boolean where not supported") skip-tests))
565 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
566 (clsql-sys:in test :fdml/select/5 :fdml/select/10
567 :fdml/select/32 :fdml/select/33))
568 (push (cons test "subqueries not supported") skip-tests))
569 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
571 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
572 (push (cons test "transactions not supported") skip-tests))
573 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
574 (clsql-sys:in test :fdml/select/1))
575 (push (cons test "fancy math not supported") skip-tests))
576 ((and (eql *test-database-type* :sqlite)
577 (clsql-sys:in test :fddl/view/4 :fdml/select/10
578 :fdml/select/21 :fdml/select/32
580 (push (cons test "not supported by sqlite") skip-tests))
581 ((and (not (clsql-sys:db-type-has-bigint? db-type))
582 (clsql-sys:in test :basic/bigint/1))
583 (push (cons test "bigint not supported") skip-tests))
584 ((and (eql *test-database-underlying-type* :mysql)
585 (clsql-sys:in test :fdml/select/26))
586 (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
587 ((and (eql *test-database-underlying-type* :mysql)
588 (clsql-sys:in test :fdml/select/22 :fdml/query/5
589 :fdml/query/7 :fdml/query/8))
590 (push (cons test "not supported by mysql") skip-tests))
591 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
592 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
593 (push (cons test "union not supported") skip-tests))
594 ((and (eq *test-database-type* :oracle)
595 (clsql-sys:in test :fdml/query/8 :fdml/select/21))
596 (push (cons test "syntax not supported") skip-tests))
598 (push test-form test-forms)))))
599 (values (nreverse test-forms) (nreverse skip-tests))))
602 (defun rapid-load (type &optional (position 0))
603 "Rapid load for interactive testing."
604 (when *default-database*
605 (disconnect :database *default-database*))
606 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
607 (test-initialise-database)
611 (rapid-load :postgresql))
617 (rapid-load :oracle))