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
7 ;;;; Initialisation utilities for running regression tests on CLSQL.
9 ;;;; This file is part of CLSQL.
11 ;;;; CLSQL users are granted the rights to distribute and use this software
12 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
13 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
14 ;;;; ======================================================================
16 (in-package #:clsql-tests)
18 (defvar *report-stream* *standard-output* "Stream to send text report.")
19 (defvar *sexp-report-stream* nil "Stream to send sexp report.")
20 (defvar *rt-internal*)
22 (defvar *rt-connection*)
30 (defvar *test-database-type* nil)
31 (defvar *test-database-underlying-type* nil)
32 (defvar *test-database-user* nil)
33 (defvar *test-start-utime* nil)
34 (defvar *test-connection-spec* nil)
35 (defvar *test-connection-db-type* nil)
38 ((extraterrestrial :initform nil :initarg :extraterrestrial)))
40 (def-view-class person (thing)
41 ((height :db-kind :base :accessor height :type float
43 (married :db-kind :base :accessor married :type boolean
45 (birthday :type clsql:wall-time :initarg :birthday)
46 (bd-utime :type clsql:universal-time :initarg :bd-utime)
47 (hobby :db-kind :virtual :initarg :hobby :initform nil)))
49 (def-view-class employee (person)
52 :db-constraints (:not-null :unique)
57 :db-constraints :not-null
69 :accessor employee-email
76 :accessor employee-company
78 :db-info (:join-class company
80 :foreign-key companyid
86 :accessor employee-manager
88 :db-info (:join-class employee
93 :accessor employee-addresses
95 :db-info (:join-class employee-address
100 (:base-table employee))
102 (def-view-class company ()
105 :db-constraints :not-null
110 :db-constraints :not-null
118 :initarg :presidentid)
122 :db-info (:join-class employee
123 :home-key presidentid
127 :reader company-employees
129 :db-info (:join-class employee
130 :home-key (companyid groupid)
131 :foreign-key (ecompanyid groupid)
134 (def-view-class address ()
137 :db-constraints :not-null
142 :initarg :street-number)
146 :initarg :street-name)
149 :void-value "no city"
156 :initarg :postal-code))
159 ;; many employees can reside at many addressess
160 (def-view-class employee-address ()
161 ((aemplid :type integer :initarg :emplid)
162 (aaddressid :type integer :initarg :addressid)
163 (verified :type boolean :initarg :verified)
164 (address :db-kind :join
165 :db-info (:join-class address
167 :foreign-key addressid
168 :retrieval :immediate))
169 (employee :db-kind :join
170 :db-info (:join-class employee
173 :retrieval :immediate)))
174 (:base-table "ea_join"))
176 (def-view-class deferred-employee-address ()
177 ((aemplid :type integer :initarg :emplid)
178 (aaddressid :type integer :initarg :addressid)
179 (verified :type boolean :initarg :verified)
180 (address :db-kind :join
181 :db-info (:join-class address
183 :foreign-key addressid
186 (:base-table "ea_join"))
188 (def-view-class big ()
189 ((i :type integer :initarg :i)
190 (bi :type bigint :initarg :bi)))
192 ;; classes for testing the normalisedp stuff
193 (def-view-class node ()
194 ((node-id :accessor node-id :initarg :node-id
195 :type integer :db-kind :key
196 :db-constraints (:not-null :auto-increment))
197 (title :accessor title :initarg :title :type (varchar 240))
198 (createtime :accessor createtime :initarg :createtime :type wall-time
199 :db-constraints (:not-null) :initform (get-time))
200 (modifiedtime :accessor modifiedtime :initarg :modifiedtime :type wall-time
201 :initform (make-time :year 1900 :month 1 :day 1))))
203 (def-view-class setting (node)
204 ((setting-id :accessor setting-id :initarg :setting-id
205 :type integer :db-kind :key :db-constraints (:not-null))
206 (vars :accessor vars :initarg :vars :type (varchar 240)))
209 (def-view-class user (node)
210 ((user-id :accessor user-id :initarg :user-id
211 :type integer :db-kind :key :db-constraints (:not-null))
212 (nick :accessor nick :initarg :nick :type (varchar 64)))
215 (def-view-class theme (setting)
216 ((theme-id :accessor theme-id :initarg :theme-id
217 :type integer :db-kind :key :db-constraints (:not-null))
218 (doc :accessor doc :initarg :doc :type (varchar 240)))
221 ;; A class that uses only a superclass db table
222 (def-view-class location (node)
227 (def-view-class subloc (location)
228 ((subloc-id :accessor subloc-id :initarg :subloc-id
229 :type integer :db-kind :key :db-constraints (:not-null))
230 (loc :accessor loc :initarg :loc :type (varchar 64)))
234 (defun test-connect-to-database (db-type spec)
235 (when (clsql-sys:db-backend-has-create/destroy-db? db-type)
236 (ignore-errors (destroy-database spec :database-type db-type))
237 (ignore-errors (create-database spec :database-type db-type)))
239 (setf *test-database-type* db-type)
240 (setf *test-database-user*
242 ((eq :oracle db-type) (second spec))
243 ((>= (length spec) 3) (third spec))))
245 ;; Connect to the database
247 :database-type db-type
251 ;; Ensure database is empty
252 (truncate-database :database *default-database*)
254 (setf *test-database-underlying-type*
255 (clsql-sys:database-underlying-type *default-database*))
259 (defparameter company1 nil)
260 (defparameter employee1 nil)
261 (defparameter employee2 nil)
262 (defparameter employee3 nil)
263 (defparameter employee4 nil)
264 (defparameter employee5 nil)
265 (defparameter employee6 nil)
266 (defparameter employee7 nil)
267 (defparameter employee8 nil)
268 (defparameter employee9 nil)
269 (defparameter employee10 nil)
270 (defparameter address1 nil)
271 (defparameter address2 nil)
272 (defparameter employee-address1 nil)
273 (defparameter employee-address2 nil)
274 (defparameter employee-address3 nil)
275 (defparameter employee-address4 nil)
276 (defparameter employee-address5 nil)
277 (defparameter basenode nil)
278 (defparameter derivednode1 nil)
279 (defparameter derivednode2 nil)
280 (defparameter node nil)
281 (defparameter setting1 nil)
282 (defparameter setting2 nil)
283 (defparameter user1 nil)
284 (defparameter user2 nil)
285 (defparameter theme1 nil)
286 (defparameter theme2 nil)
287 (defparameter loc1 nil)
288 (defparameter loc2 nil)
289 (defparameter subloc1 nil)
290 (defparameter subloc2 nil)
293 (defun test-initialise-database ()
294 (test-basic-initialize)
295 ;; (start-sql-recording :type :both)
296 (let ((*backend-warning-behavior*
297 (if (member *test-database-type* '(:postgresql :postgresql-socket))
300 (clsql:create-view-from-class 'employee)
301 (clsql:create-view-from-class 'company)
302 (clsql:create-view-from-class 'address)
303 (clsql:create-view-from-class 'employee-address)
304 (clsql:create-view-from-class 'big)
305 (clsql:create-view-from-class 'node)
306 (clsql:create-view-from-class 'setting)
307 (clsql:create-view-from-class 'user)
308 (clsql:create-view-from-class 'theme)
309 (clsql:create-view-from-class 'location)
310 (clsql:create-view-from-class 'subloc))
312 (setq *test-start-utime* (get-universal-time))
313 (let* ((*db-auto-sync* t)
314 (now-time (clsql:utime->time *test-start-utime*)))
315 (setf company1 (make-instance 'company
319 :name "Widgets Inc.")
320 employee1 (make-instance 'employee
324 :height (1+ (random 1.00))
325 :bd-utime *test-start-utime*
327 :first-name "Vladimir"
329 :email "lenin@soviet.org"
331 employee2 (make-instance 'employee
334 :height (1+ (random 1.00))
336 :bd-utime *test-start-utime*
340 :email "stalin@soviet.org"
343 employee3 (make-instance 'employee
346 :height (1+ (random 1.00))
348 :bd-utime *test-start-utime*
352 :email "trotsky@soviet.org"
355 employee4 (make-instance 'employee
358 :height (1+ (random 1.00))
360 :bd-utime *test-start-utime*
363 :last-name "Kruschev"
364 :email "kruschev@soviet.org"
367 employee5 (make-instance 'employee
371 :height (1+ (random 1.00))
372 :bd-utime *test-start-utime*
375 :last-name "Brezhnev"
376 :email "brezhnev@soviet.org"
379 employee6 (make-instance 'employee
383 :height (1+ (random 1.00))
384 :bd-utime *test-start-utime*
387 :last-name "Andropov"
388 :email "andropov@soviet.org"
391 employee7 (make-instance 'employee
394 :height (1+ (random 1.00))
396 :bd-utime *test-start-utime*
398 :first-name "Konstantin"
399 :last-name "Chernenko"
400 :email "chernenko@soviet.org"
403 employee8 (make-instance 'employee
406 :height (1+ (random 1.00))
408 :bd-utime *test-start-utime*
410 :first-name "Mikhail"
411 :last-name "Gorbachev"
412 :email "gorbachev@soviet.org"
415 employee9 (make-instance 'employee
419 :height (1+ (random 1.00))
420 :bd-utime *test-start-utime*
424 :email "yeltsin@soviet.org"
427 employee10 (make-instance 'employee
431 :height (1+ (random 1.00))
432 :bd-utime *test-start-utime*
434 :first-name "Vladimir"
436 :email "putin@soviet.org"
439 address1 (make-instance 'address
442 :street-name "Park Place"
445 address2 (make-instance 'address
447 employee-address1 (make-instance 'employee-address
451 employee-address2 (make-instance 'employee-address
455 employee-address3 (make-instance 'employee-address
459 employee-address4 (make-instance 'employee-address
463 employee-address5 (make-instance 'employee-address
466 node (make-instance 'node
468 setting1 (make-instance 'setting
471 setting2 (make-instance 'setting
474 user1 (make-instance 'user
477 user2 (make-instance 'user
480 theme1 (make-instance 'theme
484 theme2 (make-instance 'theme
487 loc1 (make-instance 'location
489 loc2 (make-instance 'location
491 subloc1 (make-instance 'subloc
494 subloc2 (make-instance 'subloc
496 :loc "second subloc"))
499 (let ((max (expt 2 60)))
501 (make-instance 'big :i (1+ i) :bi (truncate max (1+ i))))))
503 ;; sleep to ensure birthdays are no longer at current time
507 ;; Lenin manages everyone
508 (clsql:add-to-relation employee2 'manager employee1)
509 (clsql:add-to-relation employee3 'manager employee1)
510 (clsql:add-to-relation employee4 'manager employee1)
511 (clsql:add-to-relation employee5 'manager employee1)
512 (clsql:add-to-relation employee6 'manager employee1)
513 (clsql:add-to-relation employee7 'manager employee1)
514 (clsql:add-to-relation employee8 'manager employee1)
515 (clsql:add-to-relation employee9 'manager employee1)
516 (clsql:add-to-relation employee10 'manager employee1)
517 ;; Everyone works for Widgets Inc.
518 (clsql:add-to-relation company1 'employees employee1)
519 (clsql:add-to-relation company1 'employees employee2)
520 (clsql:add-to-relation company1 'employees employee3)
521 (clsql:add-to-relation company1 'employees employee4)
522 (clsql:add-to-relation company1 'employees employee5)
523 (clsql:add-to-relation company1 'employees employee6)
524 (clsql:add-to-relation company1 'employees employee7)
525 (clsql:add-to-relation company1 'employees employee8)
526 (clsql:add-to-relation company1 'employees employee9)
527 (clsql:add-to-relation company1 'employees employee10)
528 ;; Lenin is president of Widgets Inc.
529 (clsql:add-to-relation company1 'president employee1)
532 ;; store these instances
534 (clsql:update-records-from-instance employee1)
535 (clsql:update-records-from-instance employee2)
536 (clsql:update-records-from-instance employee3)
537 (clsql:update-records-from-instance employee4)
538 (clsql:update-records-from-instance employee5)
539 (clsql:update-records-from-instance employee6)
540 (clsql:update-records-from-instance employee7)
541 (clsql:update-records-from-instance employee8)
542 (clsql:update-records-from-instance employee9)
543 (clsql:update-records-from-instance employee10)
544 (clsql:update-records-from-instance company1)
545 (clsql:update-records-from-instance address1)
546 (clsql:update-records-from-instance address2)
550 (defvar *error-count* 0)
551 (defvar *error-list* nil)
553 (defun run-function-append-report-file (function report-file)
554 (let* ((report-path (etypecase report-file
555 (pathname report-file)
556 (string (parse-namestring report-file))))
557 (sexp-report-path (make-pathname :defaults report-path
559 (with-open-file (rs report-path :direction :output
561 :if-does-not-exist :create)
562 (with-open-file (srs sexp-report-path :direction :output
564 :if-does-not-exist :create)
565 (funcall function :report-stream rs :sexp-report-stream srs)))))
567 (defun run-tests-append-report-file (report-file)
568 (run-function-append-report-file 'run-tests report-file))
571 (defun run-tests (&key (report-stream *standard-output*) (sexp-report-stream nil))
572 ;; clear SQL-OUTPUT cache
573 (setq clsql-sys::*output-hash* (make-hash-table :test #'equal))
574 (let ((specs (read-specs))
575 (*report-stream* report-stream)
576 (*sexp-report-stream* sexp-report-stream)
580 (warn "Not running tests because test configuration file is missing")
581 (return-from run-tests :skipped))
582 (load-necessary-systems specs)
583 (dolist (db-type +all-db-types+)
584 (dolist (spec (db-type-spec db-type specs))
585 (let ((*test-connection-spec* spec)
586 (*test-connection-db-type* db-type))
587 (do-tests-for-backend db-type spec)))))
588 (zerop *error-count*))
590 (defun load-necessary-systems (specs)
591 (dolist (db-type +all-db-types+)
592 (when (db-type-spec db-type specs)
593 (clsql-sys:initialize-database-type :database-type db-type))))
595 (defun write-report-banner (report-type db-type stream)
598 ******************************************************************************
599 *** CLSQL ~A begun at ~A
602 *** Database ~:@(~A~) backend~A.
603 ******************************************************************************
608 (clsql:utime->time (get-universal-time)))
609 (lisp-implementation-type)
610 (lisp-implementation-version)
613 (if (not (eq db-type *test-database-underlying-type*))
614 (format nil " with underlying type ~:@(~A~)"
615 *test-database-underlying-type*)
619 (defun do-tests-for-backend (db-type spec)
620 (test-connect-to-database db-type spec)
623 (multiple-value-bind (test-forms skip-tests)
624 (compute-tests-for-backend db-type *test-database-underlying-type*)
626 (write-report-banner "Test Suite" db-type *report-stream*)
628 (test-initialise-database)
630 (regression-test:rem-all-tests)
631 (dolist (test-form test-forms)
634 (let ((remaining (regression-test:do-tests *report-stream*)))
635 (when (regression-test:pending-tests)
636 (incf *error-count* (length remaining))))
638 (let ((sexp-error (list db-type
639 *test-database-underlying-type*
642 (regression-test:pending-tests)
643 (lisp-implementation-type)
644 (lisp-implementation-version)
646 (when *sexp-report-stream*
647 (write sexp-error :stream *sexp-report-stream* :readably t))
648 (push sexp-error *error-list*))
650 (format *report-stream* "~&Tests skipped:")
652 (dolist (skipped skip-tests)
653 (format *report-stream*
654 "~& ~20A ~A~%" (car skipped) (cdr skipped)))
655 (format *report-stream* " None~%")))
659 (defun compute-tests-for-backend (db-type db-underlying-type)
660 (let ((test-forms '())
662 (dolist (test-form (append *rt-internal* *rt-connection* *rt-basic* *rt-fddl* *rt-fdml*
663 *rt-ooddl* *rt-oodml* *rt-syntax*))
664 (let ((test (second test-form)))
666 ((and (null (clsql-sys:db-type-has-views? db-underlying-type))
667 (clsql-sys:in test :fddl/view/1 :fddl/view/2 :fddl/view/3 :fddl/view/4))
668 (push (cons test "views not supported") skip-tests))
669 ((and (null (clsql-sys:db-type-has-boolean-where? db-underlying-type))
670 (clsql-sys:in test :fdml/select/11 :oodml/select/5))
671 (push (cons test "boolean where not supported") skip-tests))
672 ((and (null (clsql-sys:db-type-has-subqueries? db-underlying-type))
673 (clsql-sys:in test :fdml/select/5 :fdml/select/10
674 :fdml/select/32 :fdml/select/33))
675 (push (cons test "subqueries not supported") skip-tests))
676 ((and (null (clsql-sys:db-type-transaction-capable? db-underlying-type
678 (clsql-sys:in test :fdml/transaction/1 :fdml/transaction/2 :fdml/transaction/3 :fdml/transaction/4))
679 (push (cons test "transactions not supported") skip-tests))
680 ((and (null (clsql-sys:db-type-has-fancy-math? db-underlying-type))
681 (clsql-sys:in test :fdml/select/1))
682 (push (cons test "fancy math not supported") skip-tests))
683 ((and (eql *test-database-type* :sqlite)
684 (clsql-sys:in test :fddl/view/4 :fdml/select/10
685 :fdml/select/21 :fdml/select/32
687 (push (cons test "not supported by sqlite") skip-tests))
688 ((and (eql *test-database-type* :sqlite3)
689 (clsql-sys:in test :fddl/view/4 :fdml/select/10
690 :fdml/select/21 :fdml/select/32
692 (push (cons test "not supported by sqlite3") skip-tests))
693 ((and (not (clsql-sys:db-type-has-bigint? db-type))
694 (clsql-sys:in test :basic/bigint/1))
695 (push (cons test "bigint not supported") skip-tests))
696 ((and (eql *test-database-underlying-type* :mysql)
697 (clsql-sys:in test :fdml/select/26))
698 (push (cons test "string table aliases not supported on all mysql versions") skip-tests))
699 ((and (eql *test-database-underlying-type* :mysql)
700 (clsql-sys:in test :fdml/select/22 :fdml/query/5
701 :fdml/query/7 :fdml/query/8))
702 (push (cons test "not supported by mysql") skip-tests))
703 ((and (null (clsql-sys:db-type-has-union? db-underlying-type))
704 (clsql-sys:in test :fdml/query/6 :fdml/select/31))
705 (push (cons test "union not supported") skip-tests))
706 ((and (eq *test-database-type* :oracle)
707 (clsql-sys:in test :fdml/query/8 :fdml/select/21
709 (push (cons test "syntax not supported") skip-tests))
710 ((and (eq *test-database-type* :odbc)
711 (eq *test-database-underlying-type* :postgresql)
712 (clsql-sys:in test :fddl/owner/1))
713 (push (cons test "table ownership not supported by postgresql odbc driver") skip-tests))
714 ((and (not (member *test-database-underlying-type*
715 '(:postgresql :oracle)))
716 (clsql-sys:in test :fddl/owner/1))
717 (push (cons test "table ownership not supported") skip-tests))
718 ((and (null (clsql-sys:db-type-has-intersect? db-underlying-type))
719 (clsql-sys:in test :fdml/query/7))
720 (push (cons test "intersect not supported") skip-tests))
721 ((and (null (clsql-sys:db-type-has-except? db-underlying-type))
722 (clsql-sys:in test :fdml/query/8))
723 (push (cons test "except not supported") skip-tests))
724 ((and (eq *test-database-underlying-type* :mssql)
725 (clsql-sys:in test :fdml/select/9))
726 (push (cons test "mssql uses integer math for AVG") skip-tests))
727 ((and (not (member *test-database-underlying-type*
728 '(:postgresql :mysql :sqlite3)))
729 (clsql-sys:in test :fdml/select/37 :fdml/select/38))
730 (push (cons test "LIMIT keyword not supported in SELECT") skip-tests))
732 (push test-form test-forms)))))
733 (values (nreverse test-forms) (nreverse skip-tests))))
735 (defun rapid-load (type &optional (position 0))
736 "Rapid load for interactive testing."
737 (when *default-database*
738 (disconnect :database *default-database*))
739 (test-connect-to-database type (nth position (db-type-spec type (read-specs))))
740 (test-initialise-database)
744 (rapid-load :postgresql))
750 (rapid-load :oracle))