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)
35 ((extraterrestrial :initform nil :initarg :extraterrestrial)))
37 (def-view-class person (thing)
38 ((height :db-kind :base :accessor height :type float
40 (married :db-kind :base :accessor married :type boolean
42 (birthday :type clsql:wall-time :initarg :birthday)
43 (hobby :db-kind :virtual :initarg :hobby :initform nil)))
45 (def-view-class employee (person)
48 :db-constraints :not-null
53 :db-constraints :not-null
65 :accessor employee-email
72 :accessor employee-company
74 :db-info (:join-class company
76 :foreign-key companyid
82 :accessor employee-manager
84 :db-info (:join-class employee
89 :accessor employee-addresses
91 :db-info (:join-class employee-address
96 (:base-table employee))
98 (def-view-class company ()
101 :db-constraints :not-null
106 :db-constraints :not-null
114 :initarg :presidentid)
118 :db-info (:join-class employee
119 :home-key presidentid
123 :reader company-employees
125 :db-info (:join-class employee
126 :home-key (companyid groupid)
127 :foreign-key (ecompanyid groupid)
130 (def-view-class address ()
133 :db-constraints :not-null
138 :initarg :street-number)
142 :initarg :street-name)
145 :void-value "no city"
152 :initarg :postal-code))
155 ;; many employees can reside at many addressess
156 (def-view-class employee-address ()
157 ((aemplid :type integer :initarg :emplid)
158 (aaddressid :type integer :initarg :addressid)
159 (verified :type boolean :initarg :verified)
160 (address :db-kind :join
161 :db-info (:join-class address
163 :foreign-key addressid
164 :retrieval :immediate)))
165 (:base-table "ea_join"))
167 (def-view-class deferred-employee-address ()
168 ((aemplid :type integer :initarg :emplid)
169 (aaddressid :type integer :initarg :addressid)
170 (verified :type boolean :initarg :verified)
171 (address :db-kind :join
172 :db-info (:join-class address
174 :foreign-key addressid
177 (:base-table "ea_join"))
179 (defun test-connect-to-database (db-type spec)
180 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
181 (ignore-errors (destroy-database spec :database-type db-type))
182 (ignore-errors (create-database spec :database-type db-type)))
184 (setf *test-database-type* db-type)
185 (when (>= (length spec) 3)
186 (setq *test-database-user* (third spec)))
188 ;; Connect to the database
190 :database-type db-type
194 ;; Ensure database is empty
195 (truncate-database :database *default-database*)
197 (setf *test-database-underlying-type*
198 (clsql-sys:database-underlying-type *default-database*))
202 (defparameter company1 nil)
203 (defparameter employee1 nil)
204 (defparameter employee2 nil)
205 (defparameter employee3 nil)
206 (defparameter employee4 nil)
207 (defparameter employee5 nil)
208 (defparameter employee6 nil)
209 (defparameter employee7 nil)
210 (defparameter employee8 nil)
211 (defparameter employee9 nil)
212 (defparameter employee10 nil)
213 (defparameter address1 nil)
214 (defparameter address2 nil)
215 (defparameter employee-address1 nil)
216 (defparameter employee-address2 nil)
217 (defparameter employee-address3 nil)
218 (defparameter employee-address4 nil)
219 (defparameter employee-address5 nil)
221 (defun test-initialise-database ()
222 (test-basic-initialize)
223 (let ((*backend-warning-behavior*
224 (if (member *test-database-type* '(:postgresql :postgresql-socket))
227 (clsql:create-view-from-class 'employee)
228 (clsql:create-view-from-class 'company)
229 (clsql:create-view-from-class 'address)
230 (clsql:create-view-from-class 'employee-address))
232 (let ((*db-auto-sync* t))
233 (setf company1 (make-instance 'company
237 :name "Widgets Inc.")
238 employee1 (make-instance 'employee
242 :height (1+ (random 1.00))
243 :birthday (clsql:get-time)
244 :first-name "Vladamir"
246 :email "lenin@soviet.org"
248 employee2 (make-instance 'employee
251 :height (1+ (random 1.00))
253 :birthday (clsql:get-time)
256 :email "stalin@soviet.org"
259 employee3 (make-instance 'employee
262 :height (1+ (random 1.00))
264 :birthday (clsql:get-time)
267 :email "trotsky@soviet.org"
270 employee4 (make-instance 'employee
273 :height (1+ (random 1.00))
275 :birthday (clsql:get-time)
277 :last-name "Kruschev"
278 :email "kruschev@soviet.org"
281 employee5 (make-instance 'employee
285 :height (1+ (random 1.00))
286 :birthday (clsql:get-time)
288 :last-name "Brezhnev"
289 :email "brezhnev@soviet.org"
292 employee6 (make-instance 'employee
296 :height (1+ (random 1.00))
297 :birthday (clsql:get-time)
299 :last-name "Andropov"
300 :email "andropov@soviet.org"
303 employee7 (make-instance 'employee
306 :height (1+ (random 1.00))
308 :birthday (clsql:get-time)
309 :first-name "Konstantin"
310 :last-name "Chernenko"
311 :email "chernenko@soviet.org"
314 employee8 (make-instance 'employee
317 :height (1+ (random 1.00))
319 :birthday (clsql:get-time)
320 :first-name "Mikhail"
321 :last-name "Gorbachev"
322 :email "gorbachev@soviet.org"
325 employee9 (make-instance 'employee
329 :height (1+ (random 1.00))
330 :birthday (clsql:get-time)
333 :email "yeltsin@soviet.org"
336 employee10 (make-instance 'employee
340 :height (1+ (random 1.00))
341 :birthday (clsql:get-time)
342 :first-name "Vladamir"
344 :email "putin@soviet.org"
347 address1 (make-instance 'address
350 :street-name "Park Place"
353 address2 (make-instance 'address
355 employee-address1 (make-instance 'employee-address
359 employee-address2 (make-instance 'employee-address
363 employee-address3 (make-instance 'employee-address
367 employee-address4 (make-instance 'employee-address
371 employee-address5 (make-instance 'employee-address
376 ;; sleep to ensure birthdays are no longer at current time
380 ;; Lenin manages everyone
381 (clsql:add-to-relation employee2 'manager employee1)
382 (clsql:add-to-relation employee3 'manager employee1)
383 (clsql:add-to-relation employee4 'manager employee1)
384 (clsql:add-to-relation employee5 'manager employee1)
385 (clsql:add-to-relation employee6 'manager employee1)
386 (clsql:add-to-relation employee7 'manager employee1)
387 (clsql:add-to-relation employee8 'manager employee1)
388 (clsql:add-to-relation employee9 'manager employee1)
389 (clsql:add-to-relation employee10 'manager employee1)
390 ;; Everyone works for Widgets Inc.
391 (clsql:add-to-relation company1 'employees employee1)
392 (clsql:add-to-relation company1 'employees employee2)
393 (clsql:add-to-relation company1 'employees employee3)
394 (clsql:add-to-relation company1 'employees employee4)
395 (clsql:add-to-relation company1 'employees employee5)
396 (clsql:add-to-relation company1 'employees employee6)
397 (clsql:add-to-relation company1 'employees employee7)
398 (clsql:add-to-relation company1 'employees employee8)
399 (clsql:add-to-relation company1 'employees employee9)
400 (clsql:add-to-relation company1 'employees employee10)
401 ;; Lenin is president of Widgets Inc.
402 (clsql:add-to-relation company1 'president employee1)
405 ;; store these instances
407 (clsql:update-records-from-instance employee1)
408 (clsql:update-records-from-instance employee2)
409 (clsql:update-records-from-instance employee3)
410 (clsql:update-records-from-instance employee4)
411 (clsql:update-records-from-instance employee5)
412 (clsql:update-records-from-instance employee6)
413 (clsql:update-records-from-instance employee7)
414 (clsql:update-records-from-instance employee8)
415 (clsql:update-records-from-instance employee9)
416 (clsql:update-records-from-instance employee10)
417 (clsql:update-records-from-instance company1)
418 (clsql:update-records-from-instance address1)
419 (clsql:update-records-from-instance address2)
423 (defvar *error-count* 0)
424 (defvar *error-list* nil)
426 (defun run-function-append-report-file (function report-file)
427 (let* ((report-path (etypecase report-file
428 (pathname report-file)
429 (string (parse-namestring report-file))))
430 (sexp-report-path (make-pathname :defaults report-path
432 (with-open-file (rs report-path :direction :output
434 :if-does-not-exist :create)
435 (with-open-file (srs sexp-report-path :direction :output
437 :if-does-not-exist :create)
438 (funcall function :report-stream rs :sexp-report-stream srs)))))
440 (defun run-tests-append-report-file (report-file)
441 (run-function-append-report-file 'run-tests report-file))
444 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
445 (let ((specs (read-specs))
446 (*report-stream* report-stream)
447 (*sexp-report-stream* sexp-report-stream)
451 (warn "Not running tests because test configuration file is missing")
452 (return-from run-tests :skipped))
453 (load-necessary-systems specs)
454 (dolist (db-type +all-db-types+)
455 (dolist (spec (db-type-spec db-type specs))
456 (do-tests-for-backend db-type spec))))
457 (zerop *error-count*))
459 (defun load-necessary-systems (specs)
460 (dolist (db-type +all-db-types+)
461 (when (db-type-spec db-type specs)
462 (clsql-sys:initialize-database-type :database-type db-type))))
464 (defun write-report-banner (report-type db-type stream)
467 ******************************************************************************
468 *** CLSQL ~A begun at ~A
471 *** Database ~A backend~A.
472 ******************************************************************************
477 (clsql:utime->time (get-universal-time)))
478 (lisp-implementation-type)
479 (lisp-implementation-version)
482 (if (not (eq db-type *test-database-underlying-type*))
483 (format nil " with underlying type ~A"
484 *test-database-underlying-type*)
488 (defun do-tests-for-backend (db-type spec)
489 (test-connect-to-database db-type spec)
492 (multiple-value-bind (test-forms skip-tests)
493 (compute-tests-for-backend db-type *test-database-underlying-type*)
495 (write-report-banner "Test Suite" db-type *report-stream*)
497 (test-initialise-database)
499 (regression-test:rem-all-tests)
500 (dolist (test-form test-forms)
503 (let ((remaining (regression-test:do-tests *report-stream*)))
504 (when (regression-test:pending-tests)
505 (incf *error-count* (length remaining))))
507 (let ((sexp-error (list db-type
508 *test-database-underlying-type*
511 (regression-test:pending-tests)
512 (lisp-implementation-type)
513 (lisp-implementation-version)
515 (when *sexp-report-stream*
516 (write sexp-error :stream *sexp-report-stream* :readably t))
517 (push sexp-error *error-list*))
519 (format *report-stream* "~&Tests skipped:")
521 (dolist (skipped skip-tests)
522 (format *report-stream*
523 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
524 (format *report-stream* " None~%")))
528 (defun compute-tests-for-backend (db-type db-underlying-type)
529 (let ((test-forms '())
531 (dolist (test-form (append *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
532 *rt-ooddl* *rt-oodml* *rt-syntax*))
533 (let ((test (second test-form)))
535 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
536 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
537 (push (cons test "views not supported") skip-tests))
538 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
539 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
540 (push (cons test "boolean where not supported") skip-tests))
541 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
542 (clsql-sys:in test :fdml/select/5 :fdml/select/10
543 :fdml/select/32 :fdml/select/33))
544 (push (cons test "subqueries not supported") skip-tests))
545 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
547 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
548 (push (cons test "transactions not supported") skip-tests))
549 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
550 (clsql-sys:in test :fdml/select/1))
551 (push (cons test "fancy math not supported") skip-tests))
552 ((and (eql *test-database-type* :sqlite)
553 (clsql-sys:in test :fddl/view/4 :fdml/select/10
554 :fdml/select/21 :fdml/select/32
556 (push (cons test "not supported by sqlite") skip-tests))
557 ((and (not (clsql-sys:db-type-has-bigint? db-type))
558 (clsql-sys:in test :basic/bigint/1))
559 (push (cons test "bigint not supported") skip-tests))
560 ((and (eql *test-database-underlying-type* :mysql)
561 (clsql-sys:in test :fdml/select/26))
562 (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
563 ((and (eql *test-database-underlying-type* :mysql)
564 (clsql-sys:in test :fdml/select/22 :fdml/query/5
565 :fdml/query/7 :fdml/query/8))
566 (push (cons test "not supported by mysql") skip-tests))
567 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
568 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
569 (push (cons test "union not supported") skip-tests))
571 (push test-form test-forms)))))
572 (values (nreverse test-forms) (nreverse skip-tests))))
575 (defun rapid-load (type)
576 "Rapid load for interactive testing."
577 (when *default-database*
578 (disconnect :database *default-database*))
579 (test-connect-to-database type (car (db-type-spec type (read-specs))))
580 (test-initialise-database))
583 (rapid-load :postgresql))
589 (rapid-load :oracle))