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 (setf *test-database-user*
187 ((eq :oracle db-type) (second spec))
188 ((>= (length spec) 3) (third spec))))
190 ;; Connect to the database
192 :database-type db-type
196 ;; Ensure database is empty
197 (truncate-database :database *default-database*)
199 (setf *test-database-underlying-type*
200 (clsql-sys:database-underlying-type *default-database*))
204 (defparameter company1 nil)
205 (defparameter employee1 nil)
206 (defparameter employee2 nil)
207 (defparameter employee3 nil)
208 (defparameter employee4 nil)
209 (defparameter employee5 nil)
210 (defparameter employee6 nil)
211 (defparameter employee7 nil)
212 (defparameter employee8 nil)
213 (defparameter employee9 nil)
214 (defparameter employee10 nil)
215 (defparameter address1 nil)
216 (defparameter address2 nil)
217 (defparameter employee-address1 nil)
218 (defparameter employee-address2 nil)
219 (defparameter employee-address3 nil)
220 (defparameter employee-address4 nil)
221 (defparameter employee-address5 nil)
223 (defun test-initialise-database ()
224 (test-basic-initialize)
225 (let ((*backend-warning-behavior*
226 (if (member *test-database-type* '(:postgresql :postgresql-socket))
229 (clsql:create-view-from-class 'employee)
230 (clsql:create-view-from-class 'company)
231 (clsql:create-view-from-class 'address)
232 (clsql:create-view-from-class 'employee-address))
234 (let ((*db-auto-sync* t))
235 (setf company1 (make-instance 'company
239 :name "Widgets Inc.")
240 employee1 (make-instance 'employee
244 :height (1+ (random 1.00))
245 :birthday (clsql:get-time)
246 :first-name "Vladamir"
248 :email "lenin@soviet.org"
250 employee2 (make-instance 'employee
253 :height (1+ (random 1.00))
255 :birthday (clsql:get-time)
258 :email "stalin@soviet.org"
261 employee3 (make-instance 'employee
264 :height (1+ (random 1.00))
266 :birthday (clsql:get-time)
269 :email "trotsky@soviet.org"
272 employee4 (make-instance 'employee
275 :height (1+ (random 1.00))
277 :birthday (clsql:get-time)
279 :last-name "Kruschev"
280 :email "kruschev@soviet.org"
283 employee5 (make-instance 'employee
287 :height (1+ (random 1.00))
288 :birthday (clsql:get-time)
290 :last-name "Brezhnev"
291 :email "brezhnev@soviet.org"
294 employee6 (make-instance 'employee
298 :height (1+ (random 1.00))
299 :birthday (clsql:get-time)
301 :last-name "Andropov"
302 :email "andropov@soviet.org"
305 employee7 (make-instance 'employee
308 :height (1+ (random 1.00))
310 :birthday (clsql:get-time)
311 :first-name "Konstantin"
312 :last-name "Chernenko"
313 :email "chernenko@soviet.org"
316 employee8 (make-instance 'employee
319 :height (1+ (random 1.00))
321 :birthday (clsql:get-time)
322 :first-name "Mikhail"
323 :last-name "Gorbachev"
324 :email "gorbachev@soviet.org"
327 employee9 (make-instance 'employee
331 :height (1+ (random 1.00))
332 :birthday (clsql:get-time)
335 :email "yeltsin@soviet.org"
338 employee10 (make-instance 'employee
342 :height (1+ (random 1.00))
343 :birthday (clsql:get-time)
344 :first-name "Vladamir"
346 :email "putin@soviet.org"
349 address1 (make-instance 'address
352 :street-name "Park Place"
355 address2 (make-instance 'address
357 employee-address1 (make-instance 'employee-address
361 employee-address2 (make-instance 'employee-address
365 employee-address3 (make-instance 'employee-address
369 employee-address4 (make-instance 'employee-address
373 employee-address5 (make-instance 'employee-address
378 ;; sleep to ensure birthdays are no longer at current time
382 ;; Lenin manages everyone
383 (clsql:add-to-relation employee2 'manager employee1)
384 (clsql:add-to-relation employee3 'manager employee1)
385 (clsql:add-to-relation employee4 'manager employee1)
386 (clsql:add-to-relation employee5 'manager employee1)
387 (clsql:add-to-relation employee6 'manager employee1)
388 (clsql:add-to-relation employee7 'manager employee1)
389 (clsql:add-to-relation employee8 'manager employee1)
390 (clsql:add-to-relation employee9 'manager employee1)
391 (clsql:add-to-relation employee10 'manager employee1)
392 ;; Everyone works for Widgets Inc.
393 (clsql:add-to-relation company1 'employees employee1)
394 (clsql:add-to-relation company1 'employees employee2)
395 (clsql:add-to-relation company1 'employees employee3)
396 (clsql:add-to-relation company1 'employees employee4)
397 (clsql:add-to-relation company1 'employees employee5)
398 (clsql:add-to-relation company1 'employees employee6)
399 (clsql:add-to-relation company1 'employees employee7)
400 (clsql:add-to-relation company1 'employees employee8)
401 (clsql:add-to-relation company1 'employees employee9)
402 (clsql:add-to-relation company1 'employees employee10)
403 ;; Lenin is president of Widgets Inc.
404 (clsql:add-to-relation company1 'president employee1)
407 ;; store these instances
409 (clsql:update-records-from-instance employee1)
410 (clsql:update-records-from-instance employee2)
411 (clsql:update-records-from-instance employee3)
412 (clsql:update-records-from-instance employee4)
413 (clsql:update-records-from-instance employee5)
414 (clsql:update-records-from-instance employee6)
415 (clsql:update-records-from-instance employee7)
416 (clsql:update-records-from-instance employee8)
417 (clsql:update-records-from-instance employee9)
418 (clsql:update-records-from-instance employee10)
419 (clsql:update-records-from-instance company1)
420 (clsql:update-records-from-instance address1)
421 (clsql:update-records-from-instance address2)
425 (defvar *error-count* 0)
426 (defvar *error-list* nil)
428 (defun run-function-append-report-file (function report-file)
429 (let* ((report-path (etypecase report-file
430 (pathname report-file)
431 (string (parse-namestring report-file))))
432 (sexp-report-path (make-pathname :defaults report-path
434 (with-open-file (rs report-path :direction :output
436 :if-does-not-exist :create)
437 (with-open-file (srs sexp-report-path :direction :output
439 :if-does-not-exist :create)
440 (funcall function :report-stream rs :sexp-report-stream srs)))))
442 (defun run-tests-append-report-file (report-file)
443 (run-function-append-report-file 'run-tests report-file))
446 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
447 (let ((specs (read-specs))
448 (*report-stream* report-stream)
449 (*sexp-report-stream* sexp-report-stream)
453 (warn "Not running tests because test configuration file is missing")
454 (return-from run-tests :skipped))
455 (load-necessary-systems specs)
456 (dolist (db-type +all-db-types+)
457 (dolist (spec (db-type-spec db-type specs))
458 (do-tests-for-backend db-type spec))))
459 (zerop *error-count*))
461 (defun load-necessary-systems (specs)
462 (dolist (db-type +all-db-types+)
463 (when (db-type-spec db-type specs)
464 (clsql-sys:initialize-database-type :database-type db-type))))
466 (defun write-report-banner (report-type db-type stream)
469 ******************************************************************************
470 *** CLSQL ~A begun at ~A
473 *** Database ~:@(~A~) backend~A.
474 ******************************************************************************
479 (clsql:utime->time (get-universal-time)))
480 (lisp-implementation-type)
481 (lisp-implementation-version)
484 (if (not (eq db-type *test-database-underlying-type*))
485 (format nil " with underlying type ~:@(~A~)"
486 *test-database-underlying-type*)
490 (defun do-tests-for-backend (db-type spec)
491 (test-connect-to-database db-type spec)
494 (multiple-value-bind (test-forms skip-tests)
495 (compute-tests-for-backend db-type *test-database-underlying-type*)
497 (write-report-banner "Test Suite" db-type *report-stream*)
499 (test-initialise-database)
501 (regression-test:rem-all-tests)
502 (dolist (test-form test-forms)
505 (let ((remaining (regression-test:do-tests *report-stream*)))
506 (when (regression-test:pending-tests)
507 (incf *error-count* (length remaining))))
509 (let ((sexp-error (list db-type
510 *test-database-underlying-type*
513 (regression-test:pending-tests)
514 (lisp-implementation-type)
515 (lisp-implementation-version)
517 (when *sexp-report-stream*
518 (write sexp-error :stream *sexp-report-stream* :readably t))
519 (push sexp-error *error-list*))
521 (format *report-stream* "~&Tests skipped:")
523 (dolist (skipped skip-tests)
524 (format *report-stream*
525 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
526 (format *report-stream* " None~%")))
530 (defun compute-tests-for-backend (db-type db-underlying-type)
531 (let ((test-forms '())
533 (dolist (test-form (append *rt-connection* *rt-basic* *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 :fdml/select/32 :fdml/select/33))
546 (push (cons test "subqueries not supported") skip-tests))
547 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
549 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
550 (push (cons test "transactions not supported") skip-tests))
551 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
552 (clsql-sys:in test :fdml/select/1))
553 (push (cons test "fancy math not supported") skip-tests))
554 ((and (eql *test-database-type* :sqlite)
555 (clsql-sys:in test :fddl/view/4 :fdml/select/10
556 :fdml/select/21 :fdml/select/32
558 (push (cons test "not supported by sqlite") skip-tests))
559 ((and (not (clsql-sys:db-type-has-bigint? db-type))
560 (clsql-sys:in test :basic/bigint/1))
561 (push (cons test "bigint not supported") skip-tests))
562 ((and (eql *test-database-underlying-type* :mysql)
563 (clsql-sys:in test :fdml/select/26))
564 (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
565 ((and (eql *test-database-underlying-type* :mysql)
566 (clsql-sys:in test :fdml/select/22 :fdml/query/5
567 :fdml/query/7 :fdml/query/8))
568 (push (cons test "not supported by mysql") skip-tests))
569 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
570 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
571 (push (cons test "union not supported") skip-tests))
573 (push test-form test-forms)))))
574 (values (nreverse test-forms) (nreverse skip-tests))))
577 (defun rapid-load (type &optional (position 0))
578 "Rapid load for interactive testing."
579 (when *default-database*
580 (disconnect :database *default-database*))
581 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
582 (test-initialise-database)
586 (rapid-load :postgresql))
592 (rapid-load :oracle))