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 (ecompanyid 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 ((aemplid :type integer
159 (aaddressid :type integer
161 (address :db-kind :join
162 :db-info (:join-class address
164 :foreign-key addressid
165 :retrieval :immediate)))
166 (:base-table "ea_join"))
168 (defun test-connect-to-database (db-type spec)
169 (when (db-backend-has-create/destroy-db? db-type)
170 (ignore-errors (destroy-database spec :database-type db-type))
171 (ignore-errors (create-database spec :database-type db-type)))
173 (setf *test-database-type* db-type)
174 (when (>= (length spec) 3)
175 (setq *test-database-user* (third spec)))
177 ;; Connect to the database
179 :database-type db-type
183 ;; Ensure database is empty
184 (truncate-database :database *default-database*)
186 (setf *test-database-underlying-type*
187 (clsql:database-underlying-type *default-database*))
191 (defparameter company1 nil)
192 (defparameter employee1 nil)
193 (defparameter employee2 nil)
194 (defparameter employee3 nil)
195 (defparameter employee4 nil)
196 (defparameter employee5 nil)
197 (defparameter employee6 nil)
198 (defparameter employee7 nil)
199 (defparameter employee8 nil)
200 (defparameter employee9 nil)
201 (defparameter employee10 nil)
202 (defparameter address1 nil)
203 (defparameter address2 nil)
204 (defparameter employee-address1 nil)
205 (defparameter employee-address2 nil)
206 (defparameter employee-address3 nil)
207 (defparameter employee-address4 nil)
208 (defparameter employee-address5 nil)
210 (defun test-initialise-database ()
211 (test-basic-initialize)
213 (let ((*backend-warning-behavior*
214 (if (member *test-database-type* '(:postgresql :postgresql-socket))
217 (clsql:create-view-from-class 'employee)
218 (clsql:create-view-from-class 'company)
219 (clsql:create-view-from-class 'address)
220 (clsql:create-view-from-class 'employee-address))
222 (let ((*db-auto-sync* t))
223 (setf company1 (make-instance 'company
227 :name "Widgets Inc.")
228 employee1 (make-instance 'employee
232 :height (1+ (random 1.00))
233 :birthday (clsql:get-time)
234 :first-name "Vladamir"
236 :email "lenin@soviet.org"
238 employee2 (make-instance 'employee
241 :height (1+ (random 1.00))
243 :birthday (clsql:get-time)
246 :email "stalin@soviet.org"
249 employee3 (make-instance 'employee
252 :height (1+ (random 1.00))
254 :birthday (clsql:get-time)
257 :email "trotsky@soviet.org"
260 employee4 (make-instance 'employee
263 :height (1+ (random 1.00))
265 :birthday (clsql:get-time)
267 :last-name "Kruschev"
268 :email "kruschev@soviet.org"
271 employee5 (make-instance 'employee
275 :height (1+ (random 1.00))
276 :birthday (clsql:get-time)
278 :last-name "Brezhnev"
279 :email "brezhnev@soviet.org"
282 employee6 (make-instance 'employee
286 :height (1+ (random 1.00))
287 :birthday (clsql:get-time)
289 :last-name "Andropov"
290 :email "andropov@soviet.org"
293 employee7 (make-instance 'employee
296 :height (1+ (random 1.00))
298 :birthday (clsql:get-time)
299 :first-name "Konstantin"
300 :last-name "Chernenko"
301 :email "chernenko@soviet.org"
304 employee8 (make-instance 'employee
307 :height (1+ (random 1.00))
309 :birthday (clsql:get-time)
310 :first-name "Mikhail"
311 :last-name "Gorbachev"
312 :email "gorbachev@soviet.org"
315 employee9 (make-instance 'employee
319 :height (1+ (random 1.00))
320 :birthday (clsql:get-time)
323 :email "yeltsin@soviet.org"
326 employee10 (make-instance 'employee
330 :height (1+ (random 1.00))
331 :birthday (clsql:get-time)
332 :first-name "Vladamir"
334 :email "putin@soviet.org"
337 address1 (make-instance 'address
340 :street-name "Park Place"
343 address2 (make-instance 'address
345 employee-address1 (make-instance 'employee-address
348 employee-address2 (make-instance 'employee-address
351 employee-address3 (make-instance 'employee-address
354 employee-address4 (make-instance 'employee-address
357 employee-address5 (make-instance 'employee-address
362 ;; sleep to ensure birthdays are no longer at current time
366 ;; Lenin manages everyone
367 (clsql:add-to-relation employee2 'manager employee1)
368 (clsql:add-to-relation employee3 'manager employee1)
369 (clsql:add-to-relation employee4 'manager employee1)
370 (clsql:add-to-relation employee5 'manager employee1)
371 (clsql:add-to-relation employee6 'manager employee1)
372 (clsql:add-to-relation employee7 'manager employee1)
373 (clsql:add-to-relation employee8 'manager employee1)
374 (clsql:add-to-relation employee9 'manager employee1)
375 (clsql:add-to-relation employee10 'manager employee1)
376 ;; Everyone works for Widgets Inc.
377 (clsql:add-to-relation company1 'employees employee1)
378 (clsql:add-to-relation company1 'employees employee2)
379 (clsql:add-to-relation company1 'employees employee3)
380 (clsql:add-to-relation company1 'employees employee4)
381 (clsql:add-to-relation company1 'employees employee5)
382 (clsql:add-to-relation company1 'employees employee6)
383 (clsql:add-to-relation company1 'employees employee7)
384 (clsql:add-to-relation company1 'employees employee8)
385 (clsql:add-to-relation company1 'employees employee9)
386 (clsql:add-to-relation company1 'employees employee10)
387 ;; Lenin is president of Widgets Inc.
388 (clsql:add-to-relation company1 'president employee1)
391 ;; store these instances
393 (clsql:update-records-from-instance employee1)
394 (clsql:update-records-from-instance employee2)
395 (clsql:update-records-from-instance employee3)
396 (clsql:update-records-from-instance employee4)
397 (clsql:update-records-from-instance employee5)
398 (clsql:update-records-from-instance employee6)
399 (clsql:update-records-from-instance employee7)
400 (clsql:update-records-from-instance employee8)
401 (clsql:update-records-from-instance employee9)
402 (clsql:update-records-from-instance employee10)
403 (clsql:update-records-from-instance company1)
404 (clsql:update-records-from-instance address1)
405 (clsql:update-records-from-instance address2)
409 (defvar *error-count* 0)
410 (defvar *error-list* nil)
412 (defun run-function-append-report-file (function report-file)
413 (let* ((report-path (etypecase report-file
414 (pathname report-file)
415 (string (parse-namestring report-file))))
416 (sexp-report-path (make-pathname :defaults report-path
418 (with-open-file (rs report-path :direction :output
420 :if-does-not-exist :create)
421 (with-open-file (srs sexp-report-path :direction :output
423 :if-does-not-exist :create)
424 (funcall function :report-stream rs :sexp-report-stream srs)))))
426 (defun run-tests-append-report-file (report-file)
427 (run-function-append-report-file 'run-tests report-file))
430 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
431 (let ((specs (read-specs))
432 (*report-stream* report-stream)
433 (*sexp-report-stream* sexp-report-stream)
437 (warn "Not running tests because test configuration file is missing")
438 (return-from run-tests :skipped))
439 (load-necessary-systems specs)
440 (dolist (db-type +all-db-types+)
441 (dolist (spec (db-type-spec db-type specs))
442 (do-tests-for-backend db-type spec))))
443 (zerop *error-count*))
445 (defun load-necessary-systems (specs)
446 (dolist (db-type +all-db-types+)
447 (when (db-type-spec db-type specs)
448 (clsql:initialize-database-type :database-type db-type))))
450 (defun write-report-banner (report-type db-type stream)
453 ******************************************************************************
454 *** CLSQL ~A begun at ~A
457 *** Database ~A backend~A.
458 ******************************************************************************
463 (clsql:utime->time (get-universal-time)))
464 (lisp-implementation-type)
465 (lisp-implementation-version)
468 (if (not (eq db-type *test-database-underlying-type*))
469 (format nil " with underlying type ~A"
470 *test-database-underlying-type*)
474 (defun do-tests-for-backend (db-type spec)
475 (test-connect-to-database db-type spec)
478 (multiple-value-bind (test-forms skip-tests)
479 (compute-tests-for-backend db-type *test-database-underlying-type*)
481 (write-report-banner "Test Suite" db-type *report-stream*)
483 (test-initialise-database)
485 (regression-test:rem-all-tests)
486 (dolist (test-form test-forms)
489 (let ((remaining (regression-test:do-tests *report-stream*)))
490 (when (regression-test:pending-tests)
491 (incf *error-count* (length remaining))))
493 (let ((sexp-error (list db-type
494 *test-database-underlying-type*
497 (regression-test:pending-tests)
498 (lisp-implementation-type)
499 (lisp-implementation-version)
501 (when *sexp-report-stream*
502 (write sexp-error :stream *sexp-report-stream*))
503 (push sexp-error *error-list*))
505 (format *report-stream* "~&Tests skipped:")
507 (dolist (skipped skip-tests)
508 (format *report-stream*
509 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
510 (format *report-stream* " None~%")))
514 (defun compute-tests-for-backend (db-type db-underlying-type)
515 (declare (ignorable db-type))
516 (let ((test-forms '())
518 (dolist (test-form (append (test-basic-forms)
519 *rt-connection* *rt-fddl* *rt-fdml*
520 *rt-ooddl* *rt-oodml* *rt-syntax*))
521 (let ((test (second test-form)))
523 ((and (null (db-type-has-views? db-underlying-type))
524 (clsql-base::in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
525 (push (cons test "views not supported") skip-tests))
526 ((and (null (db-type-has-boolean-where? db-underlying-type))
527 (clsql-base::in test :fdml/select/11 :oodml/select/5))
528 (push (cons test "boolean where not supported") skip-tests))
529 ((and (null (db-type-has-subqueries? db-underlying-type))
530 (clsql-base::in test :fdml/select/5 :fdml/select/10))
531 (push (cons test "subqueries not supported") skip-tests))
532 ((and (null (db-type-transaction-capable? db-underlying-type
534 (clsql-base::in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
535 (push (cons test "transactions not supported") skip-tests))
536 ((and (null (db-type-has-fancy-math? db-underlying-type))
537 (clsql-base::in test :fdml/select/1))
538 (push (cons test "fancy math not supported") skip-tests))
539 ((and (eql *test-database-type* :sqlite)
540 (clsql-base::in test :fddl/view/4 :fdml/select/10))
541 (push (cons test "not supported by sqlite") skip-tests))
543 (push test-form test-forms)))))
544 (values (nreverse test-forms) (nreverse skip-tests))))
548 "Rapid load for interactive testing."
549 (when *default-database*
550 (disconnect :database *default-database*))
551 (test-connect-to-database :postgresql (car (postgresql-spec (read-specs))))
552 (test-initialise-database))