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*)
29 (defvar *test-database-type* nil)
30 (defvar *test-database-underlying-type* nil)
31 (defvar *test-database-user* nil)
34 ((extraterrestrial :initform nil :initarg :extraterrestrial)))
36 (def-view-class person (thing)
37 ((height :db-kind :base :accessor height :type float
39 (married :db-kind :base :accessor married :type boolean
41 (birthday :type clsql:wall-time :initarg :birthday)
42 (hobby :db-kind :virtual :initarg :hobby :initform nil)))
44 (def-view-class employee (person)
47 :db-constraints :not-null
52 :db-constraints :not-null
64 :accessor employee-email
71 :accessor employee-company
73 :db-info (:join-class company
75 :foreign-key companyid
81 :accessor employee-manager
83 :db-info (:join-class employee
88 :accessor employee-addresses
90 :db-info (:join-class employee-address
95 (:base-table employee))
97 (def-view-class company ()
100 :db-constraints :not-null
105 :db-constraints :not-null
113 :initarg :presidentid)
117 :db-info (:join-class employee
118 :home-key presidentid
122 :reader company-employees
124 :db-info (:join-class employee
125 :home-key (companyid groupid)
126 :foreign-key (ecompanyid groupid)
129 (def-view-class address ()
132 :db-constraints :not-null
137 :initarg :street-number)
141 :initarg :street-name)
144 :void-value "no city"
151 :initarg :postal-code))
154 ;; many employees can reside at many addressess
155 (def-view-class employee-address ()
156 ((aemplid :type integer :initarg :emplid)
157 (aaddressid :type integer :initarg :addressid)
158 (verified :type boolean :initarg :verified)
159 (address :db-kind :join
160 :db-info (:join-class address
162 :foreign-key addressid
163 :retrieval :immediate)))
164 (:base-table "ea_join"))
166 (def-view-class deferred-employee-address ()
167 ((aemplid :type integer :initarg :emplid)
168 (aaddressid :type integer :initarg :addressid)
169 (verified :type boolean :initarg :verified)
170 (address :db-kind :join
171 :db-info (:join-class address
173 :foreign-key addressid
176 (:base-table "ea_join"))
178 (defun test-connect-to-database (db-type spec)
179 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
180 (ignore-errors (destroy-database spec :database-type db-type))
181 (ignore-errors (create-database spec :database-type db-type)))
183 (setf *test-database-type* db-type)
184 (when (>= (length spec) 3)
185 (setq *test-database-user* (third spec)))
187 ;; Connect to the database
189 :database-type db-type
193 ;; Ensure database is empty
194 (truncate-database :database *default-database*)
196 (setf *test-database-underlying-type*
197 (clsql-sys:database-underlying-type *default-database*))
201 (defparameter company1 nil)
202 (defparameter employee1 nil)
203 (defparameter employee2 nil)
204 (defparameter employee3 nil)
205 (defparameter employee4 nil)
206 (defparameter employee5 nil)
207 (defparameter employee6 nil)
208 (defparameter employee7 nil)
209 (defparameter employee8 nil)
210 (defparameter employee9 nil)
211 (defparameter employee10 nil)
212 (defparameter address1 nil)
213 (defparameter address2 nil)
214 (defparameter employee-address1 nil)
215 (defparameter employee-address2 nil)
216 (defparameter employee-address3 nil)
217 (defparameter employee-address4 nil)
218 (defparameter employee-address5 nil)
220 (defun test-initialise-database ()
221 (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 (declare (ignorable db-type))
530 (let ((test-forms '())
532 (dolist (test-form (append (test-basic-forms)
533 *rt-connection* *rt-fddl* *rt-fdml*
534 *rt-ooddl* *rt-oodml* *rt-syntax*))
535 (let ((test (second test-form)))
537 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
538 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
539 (push (cons test "views not supported") skip-tests))
540 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
541 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
542 (push (cons test "boolean where not supported") skip-tests))
543 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
544 (clsql-sys:in test :fdml/select/5 :fdml/select/10))
545 (push (cons test "subqueries not supported") skip-tests))
546 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
548 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
549 (push (cons test "transactions not supported") skip-tests))
550 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
551 (clsql-sys:in test :fdml/select/1))
552 (push (cons test "fancy math not supported") skip-tests))
553 ((and (eql *test-database-type* :sqlite)
554 (clsql-sys:in test :fddl/view/4 :fdml/select/10
556 (push (cons test "not supported by sqlite") skip-tests))
557 ((and (eql *test-database-underlying-type* :mysql)
558 (clsql-sys:in test :fdml/select/22 :fdml/query/5
559 :fdml/query/7 :fdml/query/8))
560 (push (cons test "not supported by mysql") skip-tests))
562 (push test-form test-forms)))))
563 (values (nreverse test-forms) (nreverse skip-tests))))
566 (defun rapid-load (type)
567 "Rapid load for interactive testing."
568 (when *default-database*
569 (disconnect :database *default-database*))
570 (test-connect-to-database type (car (db-type-spec type (read-specs))))
571 (test-initialise-database))
574 (rapid-load :postgresql))