Print the error in a useful format.
[clsql.git] / tests / datasets.lisp
1 ;;;; Proposed new file in clsql-tests to enable abstracting datasets for reuse.
2 ;;;;
3 ;;;; The core is def-datset and with-dataset that respectively define a set,
4 ;;;; and enable one for a dynamic scope. Datasets will normally be setup and
5 ;;;; torn down in the scope of one test, which may impose a computation
6 ;;;; overhead, but enables simpler tests by not worrying about side-effects
7 ;;;; between tests.
8 ;;;;
9 ;;;; In general datasets should be database agnostic, but because the code
10 ;;;; is only run in the scope of a test, if a test is excluded for a backend
11 ;;;; or some other reason then it is never run hence doesn't cause problems.
12
13 (in-package #:clsql-tests)
14
15 (defparameter *dataset-debug-on-error* nil
16   "If we get an error while loading or cleaning up the dataset,
17 should we debug (T) or just print and quit.")
18
19 (defun generic-error (e)
20   (when (and *dataset-debug-on-error*
21              *debugger-hook*)
22     (invoke-debugger e))
23   (princ e *error-output*)
24   (throw 'quit-dataset e))
25
26 (defmacro def-dataset (name &body body)
27   "Define a dataset"
28   ;;probably just shove this into a param, perhaps a marginal
29   ;; bit of processing first.
30   `(defparameter ,name ',body))
31
32 (defmacro with-dataset (name &body body)
33   "Use a dataset in a dynamic scope, e.g. a single test.
34 1. Before the body:
35   * :setup is run
36   * :data is loaded
37 2. Body
38 3. :cleanup always happens"
39   `(catch 'quit-dataset
40      (unwind-protect
41           (progn 
42             (restart-case (%dataset-init ,name)
43               (retry-dataset-init ()
44                 :report ,(format nil "Retry dataset('~a) init: (with any dataset changes)"
45                                 (symbol-name name))
46                 (%dataset-init ,name)))
47             ,@body)
48        (%dataset-cleanup ,name))))
49
50
51 (defun %dataset-dispatch (arg)
52   "For use with def-dataset and with-dataset, tries to DWIM."
53   (etypecase arg
54     (string (clsql-sys:execute-command arg))  ;treat it as a sql command.
55     ((or function symbol) (funcall arg))       ;run functions
56     (list
57        (case (first arg)
58          ((function lambda) (%dataset-dispatch (eval arg))) ;#' forms, lambdas
59          (progn (mapc #'%dataset-dispatch (rest arg)))    ; (progn "asdf" "ff")
60          (ignore-errors (ignore-errors (mapc #'%dataset-dispatch (rest arg))))
61          (t (mapc #'%dataset-dispatch arg)))    ;otherwise implicit progn
62        )))
63
64 (defun %dataset-init (name)
65   "Run initialization code and fill database for given dataset."
66       (handler-bind
67           ((error #'generic-error))
68         ;;find items that looks like '(:setup ...),
69         ;; dispatch the rest.
70         (let ((setup (rest (find :setup name :key #'first)))
71               (sqldata (rest (find :sqldata name :key #'first)))
72               (objdata (rest (find :objdata name :key #'first))))
73           (when setup
74             (%dataset-dispatch setup))
75           (when sqldata
76             ;;run raw sql insert statements
77             (destructuring-bind (table-name columns &rest values-list) sqldata
78               (dolist (v values-list)
79                 (clsql-sys:execute-command
80                  (format nil
81                          "INSERT INTO ~a (~a) VALUES (~a)"
82                          table-name columns v)))))
83           (when objdata
84             ;;presumed to be view-class objects, force them to insert.
85             (dolist (o objdata)
86               (setf (slot-value o 'clsql-sys::view-database) nil)
87               (clsql-sys:update-records-from-instance o))))))
88
89 (defun %dataset-cleanup (name)
90   "Run cleanup code associated with the given dataset."
91   (restart-case 
92       (handler-bind ((error #'generic-error))
93         (let ((cleanup (rest (find :cleanup name :key #'first))))
94           (when cleanup
95             (%dataset-dispatch cleanup))))
96     (retry-dataset-cleanup ()
97       :report "Retry dataset cleanup (with any dataset changes)"
98       (%dataset-cleanup name))
99     (skip-cleanup () nil)))
100
101
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 ;;; Example Test Code
104
105 ;;incomplete example taken from test-init
106 ;; (def-dataset *employees*
107 ;;   (:setup "CREATE TABLE employee
108 ;; (
109 ;;   emplid integer NOT NULL,
110 ;;   groupid integer NOT NULL,
111 ;;   first_name character varying(30),
112 ;;   last_name character varying(30),
113 ;;   email character varying(100),
114 ;;   ecompanyid integer,
115 ;;   managerid integer,
116 ;;   height double,
117 ;;   married boolean,
118 ;;   birthday timestamp without time zone,
119 ;;   bd_utime bigint,
120 ;;   CONSTRAINT employeepk PRIMARY KEY (emplid, groupid),
121 ;;   CONSTRAINT employee_emplid_key UNIQUE (emplid)
122 ;; )
123 ;; ")
124 ;;   ;;alternatively setup can still be done as
125 ;;   ;;(:setup #'(lambda () (create-view-from-class ...)))
126 ;;   (:sqldata "employees" "emplid,groupid,married,height,first_name,last_name"
127 ;;          "1,1,false,1.5,'Napolean', 'Bonaparte'"
128 ;;          (format nil "1,1,true,~a,'Vladimir','Lenin'" (1+ (random 1.00))))
129 ;;   (:cleanup "DROP TABLE EMPLOYEES"))
130