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* nil "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 (companyid groupid)
128 (:base-table company))
131 (def-view-class address ()
134 :db-constraints :not-null
139 :initarg :street-number)
143 :initarg :street-name)
146 :void-value "no city"
153 :initarg :postal-code)))
155 ;; many employees can reside at many addressess
156 (def-view-class employee-address ()
157 ((emplid :type integer
159 (addressid :type integer
161 (address :db-kind :join
162 :db-info (:join-class address
164 :foreign-key addressid
165 :retrieval :immediate))))
167 (defun test-connect-to-database (db-type spec)
168 (when (db-backend-has-create/destroy-db? db-type)
169 (ignore-errors (destroy-database spec :database-type db-type))
170 (ignore-errors (create-database spec :database-type db-type)))
172 (setf *test-database-type* db-type)
173 (when (>= (length spec) 3)
174 (setq *test-database-user* (third spec)))
176 ;; Connect to the database
178 :database-type db-type
182 ;; Ensure database is empty
183 (truncate-database :database *default-database*)
185 (setf *test-database-underlying-type*
186 (clsql:database-underlying-type *default-database*))
190 (defparameter company1 nil)
191 (defparameter employee1 nil)
192 (defparameter employee2 nil)
193 (defparameter employee3 nil)
194 (defparameter employee4 nil)
195 (defparameter employee5 nil)
196 (defparameter employee6 nil)
197 (defparameter employee7 nil)
198 (defparameter employee8 nil)
199 (defparameter employee9 nil)
200 (defparameter employee10 nil)
201 (defparameter address1 nil)
202 (defparameter address2 nil)
203 (defparameter employee-address1 nil)
204 (defparameter employee-address2 nil)
205 (defparameter employee-address3 nil)
206 (defparameter employee-address4 nil)
207 (defparameter employee-address5 nil)
209 (defun test-initialise-database ()
210 (test-basic-initialize)
212 (let ((*backend-warning-behavior*
213 (if (member *test-database-type* '(:postgresql :postgresql-socket))
216 (clsql:create-view-from-class 'employee)
217 (clsql:create-view-from-class 'company)
218 (clsql:create-view-from-class 'address)
219 (clsql:create-view-from-class 'employee-address))
221 (let ((*db-auto-sync* t))
222 (setf company1 (make-instance 'company
226 :name "Widgets Inc.")
227 employee1 (make-instance 'employee
231 :height (1+ (random 1.00))
232 :birthday (clsql:get-time)
233 :first-name "Vladamir"
235 :email "lenin@soviet.org"
237 employee2 (make-instance 'employee
240 :height (1+ (random 1.00))
242 :birthday (clsql:get-time)
245 :email "stalin@soviet.org"
248 employee3 (make-instance 'employee
251 :height (1+ (random 1.00))
253 :birthday (clsql:get-time)
256 :email "trotsky@soviet.org"
259 employee4 (make-instance 'employee
262 :height (1+ (random 1.00))
264 :birthday (clsql:get-time)
266 :last-name "Kruschev"
267 :email "kruschev@soviet.org"
270 employee5 (make-instance 'employee
274 :height (1+ (random 1.00))
275 :birthday (clsql:get-time)
277 :last-name "Brezhnev"
278 :email "brezhnev@soviet.org"
281 employee6 (make-instance 'employee
285 :height (1+ (random 1.00))
286 :birthday (clsql:get-time)
288 :last-name "Andropov"
289 :email "andropov@soviet.org"
292 employee7 (make-instance 'employee
295 :height (1+ (random 1.00))
297 :birthday (clsql:get-time)
298 :first-name "Konstantin"
299 :last-name "Chernenko"
300 :email "chernenko@soviet.org"
303 employee8 (make-instance 'employee
306 :height (1+ (random 1.00))
308 :birthday (clsql:get-time)
309 :first-name "Mikhail"
310 :last-name "Gorbachev"
311 :email "gorbachev@soviet.org"
314 employee9 (make-instance 'employee
318 :height (1+ (random 1.00))
319 :birthday (clsql:get-time)
322 :email "yeltsin@soviet.org"
325 employee10 (make-instance 'employee
329 :height (1+ (random 1.00))
330 :birthday (clsql:get-time)
331 :first-name "Vladamir"
333 :email "putin@soviet.org"
336 address1 (make-instance 'address
339 :street-name "Park Place"
342 address2 (make-instance 'address
344 employee-address1 (make-instance 'employee-address
347 employee-address2 (make-instance 'employee-address
350 employee-address3 (make-instance 'employee-address
353 employee-address4 (make-instance 'employee-address
356 employee-address5 (make-instance 'employee-address
361 ;; sleep to ensure birthdays are no longer at current time
365 ;; Lenin manages everyone
366 (clsql:add-to-relation employee2 'manager employee1)
367 (clsql:add-to-relation employee3 'manager employee1)
368 (clsql:add-to-relation employee4 'manager employee1)
369 (clsql:add-to-relation employee5 'manager employee1)
370 (clsql:add-to-relation employee6 'manager employee1)
371 (clsql:add-to-relation employee7 'manager employee1)
372 (clsql:add-to-relation employee8 'manager employee1)
373 (clsql:add-to-relation employee9 'manager employee1)
374 (clsql:add-to-relation employee10 'manager employee1)
375 ;; Everyone works for Widgets Inc.
376 (clsql:add-to-relation company1 'employees employee1)
377 (clsql:add-to-relation company1 'employees employee2)
378 (clsql:add-to-relation company1 'employees employee3)
379 (clsql:add-to-relation company1 'employees employee4)
380 (clsql:add-to-relation company1 'employees employee5)
381 (clsql:add-to-relation company1 'employees employee6)
382 (clsql:add-to-relation company1 'employees employee7)
383 (clsql:add-to-relation company1 'employees employee8)
384 (clsql:add-to-relation company1 'employees employee9)
385 (clsql:add-to-relation company1 'employees employee10)
386 ;; Lenin is president of Widgets Inc.
387 (clsql:add-to-relation company1 'president employee1)
390 ;; store these instances
392 (clsql:update-records-from-instance employee1)
393 (clsql:update-records-from-instance employee2)
394 (clsql:update-records-from-instance employee3)
395 (clsql:update-records-from-instance employee4)
396 (clsql:update-records-from-instance employee5)
397 (clsql:update-records-from-instance employee6)
398 (clsql:update-records-from-instance employee7)
399 (clsql:update-records-from-instance employee8)
400 (clsql:update-records-from-instance employee9)
401 (clsql:update-records-from-instance employee10)
402 (clsql:update-records-from-instance company1)
403 (clsql:update-records-from-instance address1)
404 (clsql:update-records-from-instance address2)
408 (defvar *error-count* 0)
409 (defvar *error-list* nil)
411 (defun run-function-append-report-file (function report-file)
412 (let* ((report-path (etypecase report-file
413 (pathname report-file)
414 (string (parse-namestring report-file))))
415 (sexp-report-path (make-pathname :defaults report-path
417 (with-open-file (rs report-path :direction :output
419 :if-does-not-exist :create)
420 (with-open-file (srs sexp-report-path :direction :output
422 :if-does-not-exist :create)
423 (funcall function :report-stream rs :sexp-report-stream srs)))))
425 (defun run-tests-append-report-file (report-file)
426 (run-function-append-report-file 'run-tests report-file))
429 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
430 (let ((specs (read-specs))
431 (*report-stream* report-stream)
432 (*sexp-report-stream* sexp-report-stream)
436 (warn "Not running tests because test configuration file is missing")
437 (return-from run-tests :skipped))
438 (load-necessary-systems specs)
439 (dolist (db-type +all-db-types+)
440 (dolist (spec (db-type-spec db-type specs))
441 (do-tests-for-backend db-type spec))))
442 (zerop *error-count*))
444 (defun load-necessary-systems (specs)
445 (dolist (db-type +all-db-types+)
446 (when (db-type-spec db-type specs)
447 (clsql:initialize-database-type :database-type db-type))))
449 (defun write-report-banner (report-type db-type stream)
452 ******************************************************************************
453 *** CLSQL ~A begun at ~A
456 *** Database ~A backend~A.
457 ******************************************************************************
462 (clsql:utime->time (get-universal-time)))
463 (lisp-implementation-type)
464 (lisp-implementation-version)
467 (if (not (eq db-type *test-database-underlying-type*))
468 (format nil " with underlying type ~A"
469 *test-database-underlying-type*)
473 (defun do-tests-for-backend (db-type spec)
474 (test-connect-to-database db-type spec)
477 (multiple-value-bind (test-forms skip-tests)
478 (compute-tests-for-backend db-type *test-database-underlying-type*)
480 (write-report-banner "Test Suite" db-type *report-stream*)
482 (test-initialise-database)
484 (regression-test:rem-all-tests)
485 (dolist (test-form test-forms)
488 (let ((remaining (regression-test:do-tests *report-stream*)))
489 (when (regression-test:pending-tests)
490 (incf *error-count* (length remaining))))
492 (let ((sexp-error (list db-type
493 *test-database-underlying-type*
496 (regression-test:pending-tests)
497 (lisp-implementation-type)
498 (lisp-implementation-version)
500 (when *sexp-report-stream*
501 (write sexp-error :stream *sexp-report-stream*))
502 (push sexp-error *error-list*))
504 (format *report-stream* "~&Tests skipped:")
506 (dolist (skipped skip-tests)
507 (format *report-stream*
508 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
509 (format *report-stream* " None~%")))
513 (defun compute-tests-for-backend (db-type db-underlying-type)
514 (declare (ignorable db-type))
515 (let ((test-forms '())
517 (dolist (test-form (append (test-basic-forms)
518 *rt-connection* *rt-fddl* *rt-fdml*
519 *rt-ooddl* *rt-oodml* *rt-syntax*))
520 (let ((test (second test-form)))
522 ((and (null (db-type-has-views? db-underlying-type))
523 (clsql-base::in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
524 (push (cons test "views not supported") skip-tests))
525 ((and (null (db-type-has-boolean-where? db-underlying-type))
526 (clsql-base::in test :fdml/select/11 :oodml/select/5))
527 (push (cons test "boolean where not supported") skip-tests))
528 ((and (null (db-type-has-subqueries? db-underlying-type))
529 (clsql-base::in test :fdml/select/5 :fdml/select/10))
530 (push (cons test "subqueries not supported") skip-tests))
531 ((and (null (db-type-transaction-capable? db-underlying-type
533 (clsql-base::in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
534 (push (cons test "transactions not supported") skip-tests))
535 ((and (null (db-type-has-fancy-math? db-underlying-type))
536 (clsql-base::in test :fdml/select/1))
537 (push (cons test "fancy math not supported") skip-tests))
538 ((and (eql *test-database-type* :sqlite)
539 (clsql-base::in test :fddl/view/4 :fdml/select/10))
540 (push (cons test "not supported by sqlite") skip-tests))
542 (push test-form test-forms)))))
543 (values (nreverse test-forms) (nreverse skip-tests))))
547 "Rapid load for interactive testing."
548 (when *default-database*
549 (disconnect :database *default-database*))
550 (test-connect-to-database :postgresql (car (postgresql-spec (read-specs))))
551 (test-initialise-database))