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-base: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
70 :accessor employee-company
72 :db-info (:join-class company
74 :foreign-key companyid
79 :accessor employee-manager
81 :db-info (:join-class employee
85 (:base-table employee))
87 (def-view-class company ()
90 :db-constraints :not-null
95 :db-constraints :not-null
106 :db-info (:join-class employee
107 :home-key presidentid
111 :reader company-employees
113 :db-info (:join-class employee
114 :home-key (companyid groupid)
115 :foreign-key (companyid groupid)
117 (:base-table company))
120 (def-view-class address ()
123 :db-constraints :not-null
128 :initarg :street-number)
132 :initarg :street-name)
135 :void-value "no city"
142 :initarg :postal-code)))
144 (defun test-connect-to-database (db-type spec)
145 (when (db-backend-has-create/destroy-db? db-type)
146 (ignore-errors (destroy-database spec :database-type db-type))
147 (ignore-errors (create-database spec :database-type db-type)))
149 (setf *test-database-type* db-type)
150 (when (>= (length spec) 3)
151 (setq *test-database-user* (third spec)))
153 ;; Connect to the database
155 :database-type db-type
159 ;; Ensure database is empty
160 (truncate-database :database *default-database*)
162 (setf *test-database-underlying-type*
163 (clsql-sys:database-underlying-type *default-database*))
167 (defparameter company1 nil)
168 (defparameter employee1 nil)
169 (defparameter employee2 nil)
170 (defparameter employee3 nil)
171 (defparameter employee4 nil)
172 (defparameter employee5 nil)
173 (defparameter employee6 nil)
174 (defparameter employee7 nil)
175 (defparameter employee8 nil)
176 (defparameter employee9 nil)
177 (defparameter employee10 nil)
178 (defparameter address1 nil)
179 (defparameter address2 nil)
181 (defun test-initialise-database ()
182 (test-basic-initialize)
184 (let ((*backend-warning-behavior*
185 (if (member *test-database-type* '(:postgresql :postgresql-socket))
188 (clsql:create-view-from-class 'employee)
189 (clsql:create-view-from-class 'company)
190 (clsql:create-view-from-class 'address))
192 (setf company1 (make-instance 'company
195 :name "Widgets Inc.")
196 employee1 (make-instance 'employee
200 :height (1+ (random 1.00))
201 :birthday (clsql-base:get-time)
202 :first-name "Vladamir"
204 :email "lenin@soviet.org")
205 employee2 (make-instance 'employee
208 :height (1+ (random 1.00))
210 :birthday (clsql-base:get-time)
213 :email "stalin@soviet.org")
214 employee3 (make-instance 'employee
217 :height (1+ (random 1.00))
219 :birthday (clsql-base:get-time)
222 :email "trotsky@soviet.org")
223 employee4 (make-instance 'employee
226 :height (1+ (random 1.00))
228 :birthday (clsql-base:get-time)
230 :last-name "Kruschev"
231 :email "kruschev@soviet.org")
233 employee5 (make-instance 'employee
237 :height (1+ (random 1.00))
238 :birthday (clsql-base:get-time)
240 :last-name "Brezhnev"
241 :email "brezhnev@soviet.org")
243 employee6 (make-instance 'employee
247 :height (1+ (random 1.00))
248 :birthday (clsql-base:get-time)
250 :last-name "Andropov"
251 :email "andropov@soviet.org")
252 employee7 (make-instance 'employee
255 :height (1+ (random 1.00))
257 :birthday (clsql-base:get-time)
258 :first-name "Konstantin"
259 :last-name "Chernenko"
260 :email "chernenko@soviet.org")
261 employee8 (make-instance 'employee
264 :height (1+ (random 1.00))
266 :birthday (clsql-base:get-time)
267 :first-name "Mikhail"
268 :last-name "Gorbachev"
269 :email "gorbachev@soviet.org")
270 employee9 (make-instance 'employee
274 :height (1+ (random 1.00))
275 :birthday (clsql-base:get-time)
278 :email "yeltsin@soviet.org")
279 employee10 (make-instance 'employee
283 :height (1+ (random 1.00))
284 :birthday (clsql-base:get-time)
285 :first-name "Vladamir"
287 :email "putin@soviet.org")
289 address1 (make-instance 'address
292 :street-name "Park Place"
296 address2 (make-instance 'address
299 ;; sleep to ensure birthdays are no longer at current time
302 ;; Lenin manages everyone
303 (clsql:add-to-relation employee2 'manager employee1)
304 (clsql:add-to-relation employee3 'manager employee1)
305 (clsql:add-to-relation employee4 'manager employee1)
306 (clsql:add-to-relation employee5 'manager employee1)
307 (clsql:add-to-relation employee6 'manager employee1)
308 (clsql:add-to-relation employee7 'manager employee1)
309 (clsql:add-to-relation employee8 'manager employee1)
310 (clsql:add-to-relation employee9 'manager employee1)
311 (clsql:add-to-relation employee10 'manager employee1)
312 ;; Everyone works for Widgets Inc.
313 (clsql:add-to-relation company1 'employees employee1)
314 (clsql:add-to-relation company1 'employees employee2)
315 (clsql:add-to-relation company1 'employees employee3)
316 (clsql:add-to-relation company1 'employees employee4)
317 (clsql:add-to-relation company1 'employees employee5)
318 (clsql:add-to-relation company1 'employees employee6)
319 (clsql:add-to-relation company1 'employees employee7)
320 (clsql:add-to-relation company1 'employees employee8)
321 (clsql:add-to-relation company1 'employees employee9)
322 (clsql:add-to-relation company1 'employees employee10)
323 ;; Lenin is president of Widgets Inc.
324 (clsql:add-to-relation company1 'president employee1)
325 ;; store these instances
326 (clsql:update-records-from-instance employee1)
327 (clsql:update-records-from-instance employee2)
328 (clsql:update-records-from-instance employee3)
329 (clsql:update-records-from-instance employee4)
330 (clsql:update-records-from-instance employee5)
331 (clsql:update-records-from-instance employee6)
332 (clsql:update-records-from-instance employee7)
333 (clsql:update-records-from-instance employee8)
334 (clsql:update-records-from-instance employee9)
335 (clsql:update-records-from-instance employee10)
336 (clsql:update-records-from-instance company1)
337 (clsql:update-records-from-instance address1)
338 (clsql:update-records-from-instance address2))
340 (defvar *error-count* 0)
341 (defvar *error-list* nil)
343 (defun run-tests-append-report-file (report-file)
344 (let* ((report-path (etypecase report-file
345 (pathname report-file)
346 (string (parse-namestring report-file))))
347 (sexp-report-path (make-pathname :defaults report-path
349 (with-open-file (rs report-path :direction :output
351 :if-does-not-exist :create)
352 (with-open-file (srs sexp-report-path :direction :output
354 :if-does-not-exist :create)
355 (run-tests :report-stream rs :sexp-report-stream srs)))))
357 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
358 (let ((specs (read-specs))
359 (*report-stream* report-stream)
360 (*sexp-report-stream* sexp-report-stream)
364 (warn "Not running tests because test configuration file is missing")
365 (return-from run-tests :skipped))
366 (load-necessary-systems specs)
367 (dolist (db-type +all-db-types+)
368 (dolist (spec (db-type-spec db-type specs))
369 (do-tests-for-backend db-type spec))))
370 (zerop *error-count*))
372 (defun load-necessary-systems (specs)
373 (dolist (db-type +all-db-types+)
374 (when (db-type-spec db-type specs)
375 (clsql:initialize-database-type :database-type db-type))))
377 (defun do-tests-for-backend (db-type spec)
378 (test-connect-to-database db-type spec)
381 (multiple-value-bind (test-forms skip-tests)
382 (compute-tests-for-backend db-type *test-database-underlying-type*)
384 (format *report-stream*
386 ******************************************************************************
387 *** CLSQL Test Suite begun at ~A
390 *** Database ~A backend~A.
391 ******************************************************************************
393 (clsql-base:format-time
395 (clsql-base:utime->time (get-universal-time)))
396 (lisp-implementation-type)
397 (lisp-implementation-version)
400 (if (not (eq db-type *test-database-underlying-type*))
401 (format nil " with underlying type ~A"
402 *test-database-underlying-type*)
406 (test-initialise-database)
408 (regression-test:rem-all-tests)
409 (dolist (test-form test-forms)
412 (let ((remaining (regression-test:do-tests *report-stream*)))
413 (when (regression-test:pending-tests)
414 (incf *error-count* (length remaining))))
416 (let ((sexp-error (list db-type
417 *test-database-underlying-type*
420 (regression-test:pending-tests)
421 (lisp-implementation-type)
422 (lisp-implementation-version)
424 (when *sexp-report-stream*
425 (write sexp-error :stream *sexp-report-stream*))
426 (push sexp-error *error-list*))
428 (format *report-stream* "~&Tests skipped:")
430 (dolist (skipped skip-tests)
431 (format *report-stream*
432 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
433 (format *report-stream* " None~%")))
437 (defun compute-tests-for-backend (db-type db-underlying-type)
438 (let ((test-forms '())
440 (dolist (test-form (append (test-basic-forms)
441 *rt-connection* *rt-fddl* *rt-fdml*
442 *rt-ooddl* *rt-oodml* *rt-syntax*))
443 (let ((test (second test-form)))
445 ((and (null (db-type-has-views? db-underlying-type))
446 (clsql-base::in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
447 (push (cons test "views not supported") skip-tests))
448 ((and (null (db-type-has-boolean-where? db-underlying-type))
449 (clsql-base::in test :fdml/select/11 :oodml/select/5))
450 (push (cons test "boolean where not supported") skip-tests))
451 ((and (null (db-type-has-subqueries? db-underlying-type))
452 (clsql-base::in test :fdml/select/5 :fdml/select/10))
453 (push (cons test "subqueries not supported") skip-tests))
454 ((and (null (db-type-transaction-capable? db-underlying-type
456 (clsql-base::in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
457 (push (cons test "transactions not supported") skip-tests))
458 ((and (null (db-type-has-fancy-math? db-underlying-type))
459 (clsql-base::in test :fdml/select/1))
460 (push (cons test "fancy math not supported") skip-tests))
461 ((and (eql *test-database-type* :sqlite)
462 (clsql-base::in test :fddl/view/4 :fdml/select/10))
463 (push (cons test "not supported by sqlite") skip-tests))
465 (push test-form test-forms)))))
466 (values (nreverse test-forms) (nreverse skip-tests))))