1 ;;;; Proposed new file in clsql-tests to enable abstracting datasets for reuse.
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
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.
13 (in-package #:clsql-tests)
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.")
19 (defun generic-error (e)
20 (when (and *dataset-debug-on-error*
23 (print e *error-output*)
24 (throw 'quit-dataset e))
26 (defmacro def-dataset (name &body body)
28 ;;probably just shove this into a param, perhaps a marginal
29 ;; bit of processing first.
30 `(defparameter ,name ',body))
32 (defmacro with-dataset (name &body body)
33 "Use a dataset in a dynamic scope, e.g. a single test.
38 3. :cleanup always happens"
42 (restart-case (%dataset-init ,name)
43 (retry-dataset-init ()
44 :report ,(format nil "Retry dataset('~a) init: (with any dataset changes)"
46 (%dataset-init ,name)))
48 (%dataset-cleanup ,name))))
51 (defun %dataset-dispatch (arg)
52 "For use with def-dataset and with-dataset, tries to DWIM."
54 (string (clsql-sys:execute-command arg)) ;treat it as a sql command.
55 ((or function symbol) (funcall arg)) ;run functions
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
64 (defun %dataset-init (name)
65 "Run initialization code and fill database for given dataset."
67 ((error #'generic-error))
68 ;;find items that looks like '(:setup ...),
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))))
74 (%dataset-dispatch setup))
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
81 "INSERT INTO ~a (~a) VALUES (~a)"
82 table-name columns v)))))
84 ;;presumed to be view-class objects, force them to insert.
86 (setf (slot-value o 'clsql-sys::view-database) nil)
87 (clsql-sys:update-records-from-instance o))))))
89 (defun %dataset-cleanup (name)
90 "Run cleanup code associated with the given dataset."
92 (handler-bind ((error #'generic-error))
93 (let ((cleanup (rest (find :cleanup name :key #'first))))
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)))
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 ;;; Example Test Code
105 ;;incomplete example taken from test-init
106 ;; (def-dataset *employees*
107 ;; (:setup "CREATE TABLE employee
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,
118 ;; birthday timestamp without time zone,
120 ;; CONSTRAINT employeepk PRIMARY KEY (emplid, groupid),
121 ;; CONSTRAINT employee_emplid_key UNIQUE (emplid)
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"))