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 (def-view-class big ()
180 ((i :type integer :initarg :i)
181 (bi :type bigint :initarg :bi)))
183 (defun test-connect-to-database (db-type spec)
184 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
185 (ignore-errors (destroy-database spec :database-type db-type))
186 (ignore-errors (create-database spec :database-type db-type)))
188 (setf *test-database-type* db-type)
189 (setf *test-database-user*
191 ((eq :oracle db-type) (second spec))
192 ((>= (length spec) 3) (third spec))))
194 ;; Connect to the database
196 :database-type db-type
200 ;; Ensure database is empty
201 (truncate-database :database *default-database*)
203 (setf *test-database-underlying-type*
204 (clsql-sys:database-underlying-type *default-database*))
208 (defparameter company1 nil)
209 (defparameter employee1 nil)
210 (defparameter employee2 nil)
211 (defparameter employee3 nil)
212 (defparameter employee4 nil)
213 (defparameter employee5 nil)
214 (defparameter employee6 nil)
215 (defparameter employee7 nil)
216 (defparameter employee8 nil)
217 (defparameter employee9 nil)
218 (defparameter employee10 nil)
219 (defparameter address1 nil)
220 (defparameter address2 nil)
221 (defparameter employee-address1 nil)
222 (defparameter employee-address2 nil)
223 (defparameter employee-address3 nil)
224 (defparameter employee-address4 nil)
225 (defparameter employee-address5 nil)
227 (defun test-initialise-database ()
228 (test-basic-initialize)
229 (let ((*backend-warning-behavior*
230 (if (member *test-database-type* '(:postgresql :postgresql-socket))
233 (clsql:create-view-from-class 'employee)
234 (clsql:create-view-from-class 'company)
235 (clsql:create-view-from-class 'address)
236 (clsql:create-view-from-class 'employee-address)
237 (clsql:create-view-from-class 'big))
239 (let ((*db-auto-sync* t))
240 (setf company1 (make-instance 'company
244 :name "Widgets Inc.")
245 employee1 (make-instance 'employee
249 :height (1+ (random 1.00))
250 :birthday (clsql:get-time)
251 :first-name "Vladamir"
253 :email "lenin@soviet.org"
255 employee2 (make-instance 'employee
258 :height (1+ (random 1.00))
260 :birthday (clsql:get-time)
263 :email "stalin@soviet.org"
266 employee3 (make-instance 'employee
269 :height (1+ (random 1.00))
271 :birthday (clsql:get-time)
274 :email "trotsky@soviet.org"
277 employee4 (make-instance 'employee
280 :height (1+ (random 1.00))
282 :birthday (clsql:get-time)
284 :last-name "Kruschev"
285 :email "kruschev@soviet.org"
288 employee5 (make-instance 'employee
292 :height (1+ (random 1.00))
293 :birthday (clsql:get-time)
295 :last-name "Brezhnev"
296 :email "brezhnev@soviet.org"
299 employee6 (make-instance 'employee
303 :height (1+ (random 1.00))
304 :birthday (clsql:get-time)
306 :last-name "Andropov"
307 :email "andropov@soviet.org"
310 employee7 (make-instance 'employee
313 :height (1+ (random 1.00))
315 :birthday (clsql:get-time)
316 :first-name "Konstantin"
317 :last-name "Chernenko"
318 :email "chernenko@soviet.org"
321 employee8 (make-instance 'employee
324 :height (1+ (random 1.00))
326 :birthday (clsql:get-time)
327 :first-name "Mikhail"
328 :last-name "Gorbachev"
329 :email "gorbachev@soviet.org"
332 employee9 (make-instance 'employee
336 :height (1+ (random 1.00))
337 :birthday (clsql:get-time)
340 :email "yeltsin@soviet.org"
343 employee10 (make-instance 'employee
347 :height (1+ (random 1.00))
348 :birthday (clsql:get-time)
349 :first-name "Vladamir"
351 :email "putin@soviet.org"
354 address1 (make-instance 'address
357 :street-name "Park Place"
360 address2 (make-instance 'address
362 employee-address1 (make-instance 'employee-address
366 employee-address2 (make-instance 'employee-address
370 employee-address3 (make-instance 'employee-address
374 employee-address4 (make-instance 'employee-address
378 employee-address5 (make-instance 'employee-address
382 (let ((max (expt 2 60)))
384 (make-instance 'big :i (1+ i) :bi (truncate max (1+ i))))))
386 ;; sleep to ensure birthdays are no longer at current time
390 ;; Lenin manages everyone
391 (clsql:add-to-relation employee2 'manager employee1)
392 (clsql:add-to-relation employee3 'manager employee1)
393 (clsql:add-to-relation employee4 'manager employee1)
394 (clsql:add-to-relation employee5 'manager employee1)
395 (clsql:add-to-relation employee6 'manager employee1)
396 (clsql:add-to-relation employee7 'manager employee1)
397 (clsql:add-to-relation employee8 'manager employee1)
398 (clsql:add-to-relation employee9 'manager employee1)
399 (clsql:add-to-relation employee10 'manager employee1)
400 ;; Everyone works for Widgets Inc.
401 (clsql:add-to-relation company1 'employees employee1)
402 (clsql:add-to-relation company1 'employees employee2)
403 (clsql:add-to-relation company1 'employees employee3)
404 (clsql:add-to-relation company1 'employees employee4)
405 (clsql:add-to-relation company1 'employees employee5)
406 (clsql:add-to-relation company1 'employees employee6)
407 (clsql:add-to-relation company1 'employees employee7)
408 (clsql:add-to-relation company1 'employees employee8)
409 (clsql:add-to-relation company1 'employees employee9)
410 (clsql:add-to-relation company1 'employees employee10)
411 ;; Lenin is president of Widgets Inc.
412 (clsql:add-to-relation company1 'president employee1)
415 ;; store these instances
417 (clsql:update-records-from-instance employee1)
418 (clsql:update-records-from-instance employee2)
419 (clsql:update-records-from-instance employee3)
420 (clsql:update-records-from-instance employee4)
421 (clsql:update-records-from-instance employee5)
422 (clsql:update-records-from-instance employee6)
423 (clsql:update-records-from-instance employee7)
424 (clsql:update-records-from-instance employee8)
425 (clsql:update-records-from-instance employee9)
426 (clsql:update-records-from-instance employee10)
427 (clsql:update-records-from-instance company1)
428 (clsql:update-records-from-instance address1)
429 (clsql:update-records-from-instance address2)
433 (defvar *error-count* 0)
434 (defvar *error-list* nil)
436 (defun run-function-append-report-file (function report-file)
437 (let* ((report-path (etypecase report-file
438 (pathname report-file)
439 (string (parse-namestring report-file))))
440 (sexp-report-path (make-pathname :defaults report-path
442 (with-open-file (rs report-path :direction :output
444 :if-does-not-exist :create)
445 (with-open-file (srs sexp-report-path :direction :output
447 :if-does-not-exist :create)
448 (funcall function :report-stream rs :sexp-report-stream srs)))))
450 (defun run-tests-append-report-file (report-file)
451 (run-function-append-report-file 'run-tests report-file))
454 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
455 (let ((specs (read-specs))
456 (*report-stream* report-stream)
457 (*sexp-report-stream* sexp-report-stream)
461 (warn "Not running tests because test configuration file is missing")
462 (return-from run-tests :skipped))
463 (load-necessary-systems specs)
464 (dolist (db-type +all-db-types+)
465 (dolist (spec (db-type-spec db-type specs))
466 (do-tests-for-backend db-type spec))))
467 (zerop *error-count*))
469 (defun load-necessary-systems (specs)
470 (dolist (db-type +all-db-types+)
471 (when (db-type-spec db-type specs)
472 (clsql-sys:initialize-database-type :database-type db-type))))
474 (defun write-report-banner (report-type db-type stream)
477 ******************************************************************************
478 *** CLSQL ~A begun at ~A
481 *** Database ~:@(~A~) backend~A.
482 ******************************************************************************
487 (clsql:utime->time (get-universal-time)))
488 (lisp-implementation-type)
489 (lisp-implementation-version)
492 (if (not (eq db-type *test-database-underlying-type*))
493 (format nil " with underlying type ~:@(~A~)"
494 *test-database-underlying-type*)
498 (defun do-tests-for-backend (db-type spec)
499 (test-connect-to-database db-type spec)
502 (multiple-value-bind (test-forms skip-tests)
503 (compute-tests-for-backend db-type *test-database-underlying-type*)
505 (write-report-banner "Test Suite" db-type *report-stream*)
507 (test-initialise-database)
509 (regression-test:rem-all-tests)
510 (dolist (test-form test-forms)
513 (let ((remaining (regression-test:do-tests *report-stream*)))
514 (when (regression-test:pending-tests)
515 (incf *error-count* (length remaining))))
517 (let ((sexp-error (list db-type
518 *test-database-underlying-type*
521 (regression-test:pending-tests)
522 (lisp-implementation-type)
523 (lisp-implementation-version)
525 (when *sexp-report-stream*
526 (write sexp-error :stream *sexp-report-stream* :readably t))
527 (push sexp-error *error-list*))
529 (format *report-stream* "~&Tests skipped:")
531 (dolist (skipped skip-tests)
532 (format *report-stream*
533 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
534 (format *report-stream* " None~%")))
538 (defun compute-tests-for-backend (db-type db-underlying-type)
539 (let ((test-forms '())
541 (dolist (test-form (append *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
542 *rt-ooddl* *rt-oodml* *rt-syntax*))
543 (let ((test (second test-form)))
545 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
546 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
547 (push (cons test "views not supported") skip-tests))
548 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
549 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
550 (push (cons test "boolean where not supported") skip-tests))
551 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
552 (clsql-sys:in test :fdml/select/5 :fdml/select/10
553 :fdml/select/32 :fdml/select/33))
554 (push (cons test "subqueries not supported") skip-tests))
555 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
557 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
558 (push (cons test "transactions not supported") skip-tests))
559 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
560 (clsql-sys:in test :fdml/select/1))
561 (push (cons test "fancy math not supported") skip-tests))
562 ((and (eql *test-database-type* :sqlite)
563 (clsql-sys:in test :fddl/view/4 :fdml/select/10
564 :fdml/select/21 :fdml/select/32
566 (push (cons test "not supported by sqlite") skip-tests))
567 ((and (not (clsql-sys:db-type-has-bigint? db-type))
568 (clsql-sys:in test :basic/bigint/1))
569 (push (cons test "bigint not supported") skip-tests))
570 ((and (eql *test-database-underlying-type* :mysql)
571 (clsql-sys:in test :fdml/select/26))
572 (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
573 ((and (eql *test-database-underlying-type* :mysql)
574 (clsql-sys:in test :fdml/select/22 :fdml/query/5
575 :fdml/query/7 :fdml/query/8))
576 (push (cons test "not supported by mysql") skip-tests))
577 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
578 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
579 (push (cons test "union not supported") skip-tests))
580 ((and (eq *test-database-type* :oracle)
581 (clsql-sys:in test :fdml/query/8 :fdml/select/21))
582 (push (cons test "syntax not supported") skip-tests))
584 (push test-form test-forms)))))
585 (values (nreverse test-forms) (nreverse skip-tests))))
588 (defun rapid-load (type &optional (position 0))
589 "Rapid load for interactive testing."
590 (when *default-database*
591 (disconnect :database *default-database*))
592 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
593 (test-initialise-database)
597 (rapid-load :postgresql))
603 (rapid-load :oracle))