submarine-tests.lisp 8.72 KB
Newer Older
ryszard.szopa's avatar
tests    
ryszard.szopa committed
1
2
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; -*-

ryszard.szopa's avatar
ryszard.szopa committed
3

ryszard.szopa's avatar
tests    
ryszard.szopa committed
4
5
6
(in-package :submarine-tests)


ryszard.szopa's avatar
ryszard.szopa committed
7
8
(def-fixture db-fixture ()
  (progn (set-up)
9
10
11
12
         (unwind-protect
              (handler-bind ((warning #'muffle-warning))
                (&body))
             (clean))))
ryszard.szopa's avatar
ryszard.szopa committed
13
14
15

(defmacro db-test (name &body body)
  `(test ,(append (fiveam::ensure-list name) '(:fixture db-fixture)) ,@body))
ryszard.szopa's avatar
tests    
ryszard.szopa committed
16

ryszard.szopa's avatar
ryszard.szopa committed
17
18
19
20
21
22
(defun set-up ()
  (postmodern:with-connection ( "test" "richard" "dupa" "localhost")
    (postmodern:execute "CREATE DATABASE \"submarine-test\";")))

(defun clean ()
  (pomo:clear-connection-pool)
23
  (sleep .1)
ryszard.szopa's avatar
ryszard.szopa committed
24
25
26
  (pomo:with-connection ( "test" "richard" "dupa" "localhost")
    (postmodern:execute "DROP DATABASE \"submarine-test\";")))

ryszard.szopa's avatar
tests    
ryszard.szopa committed
27
28
29
30
31
32
33
34
(defmacro defdao-example (name supers &body body)
  "Wrapper macro for defining a class inheriting from DAO with the
metaclass set to HTML and the right connection-spec."
  `(defclass ,name (dao ,@supers) 
    ,@body
    (:connection-spec "submarine-test" "richard" "dupa" "localhost")
    (:metaclass db-class)))

ryszard.szopa's avatar
ryszard.szopa committed
35
36
37
(defmacro defmany (x y)
  `(def-many-to-many ,x ,y ("submarine-test" "richard" "dupa" "localhost")))

38

ryszard.szopa's avatar
ryszard.szopa committed
39
40
41
42
43
44
45
46
(in-suite submarine-tests)

(db-test class-connection 
  (defdao-example class-connection ()
    ())
  (finishes  (with-class-connection ((find-class 'class-connection))
               (pomo::query "SELECT 1;"))))

47
(db-test (dao-definition :depends-on class-connection) 
ryszard.szopa's avatar
ryszard.szopa committed
48
49
50
51
  (finishes
    (defdao-example foo ()
      ((foo :type integer)))))

52
(db-test (dao-redefinition :depends-on dao-definition) 
ryszard.szopa's avatar
ryszard.szopa committed
53
54
55
56
57
58
  (finishes
    (defdao-example foo ()
      ((foo :type integer)))
    (defdao-example foo ()
      ((foo :type integer)))))

59
60
61
62
63
64
65
66
67
68
69
(db-test (dao-definition-with-foreign-key :depends-on dao-definition)
  (defdao-example foo ()
      ((foo :type integer)))
  (finishes
    (defdao-example bar ()
      ((foo :type foo :foreign t)))))

(db-test (dao-definition-with-foreign-key-wrong-order :depends-on dao-definition-with-foreign-key)
  (finishes
    (defdao-example bar ()
      ((foo :type foo :foreign t))))
ryszard.szopa's avatar
ryszard.szopa committed
70
71
72
73
74
75
76
77
78
79
80
81
82
   (let ((class (find-class 'bar)))
     (is (member class (submarine::db-class-unfinished-classes class)))
     (defdao-example foo ()
       ((foo :type integer)))
     (is-false (member class (submarine::db-class-unfinished-classes class)))))

(db-test (unresolved-references :depends-on dao-definition-with-foreign-key)
  (defdao-example wrong-foo ()
      ((foreign-key :foreign t :accessor foreign-key :type bar :initarg :foreign :initform nil)))
  (signals unresolved-foreign-key
    (make-instance 'wrong-foo :foreign 1))
  (signals unresolved-foreign-key
    (make-and-save 'wrong-foo :foreign "foo"))
ryszard.szopa's avatar
ryszard.szopa committed
83
  (setf (submarine::db-class-unfinished-classes (find-class 'wrong-foo)) nil))
ryszard.szopa's avatar
ryszard.szopa committed
84
85

(db-test (get-all :depends-on dao-definition-with-foreign-key)
86
  (defdao-example foo ()
ryszard.szopa's avatar
ryszard.szopa committed
87
88
89
90
91
92
93
94
      ((foo :type integer :initform 0)))
  (defdao-example bar ()
    ((foo :type foo :foreign t :initarg :foo :accessor foo)))
  (let ((the-foo (make-and-save 'foo :foo 1)))
    (iter (for i from 1 to 10)
          (make-and-save 'bar :foo the-foo))
    (is (= 10 (length (get-all 'bar the-foo))))))

95
96

(db-test (many-to-many-definition :depends-on dao-definition-with-foreign-key) 
ryszard.szopa's avatar
ryszard.szopa committed
97
98
99
100
101
102
103
  (finishes
    (defdao-example foo ()
      ((foo :type integer)))
    (defdao-example  bar ()
      ((bar :type integer)))
    (defmany foo bar)))

104
(db-test (many-to-many-redefinition :depends-on many-to-many-definition)
ryszard.szopa's avatar
ryszard.szopa committed
105
106
107
108
109
110
111
112
  (finishes
    (defdao-example foo ()
      ((foo :type integer)))
    (defdao-example bar ()
      ((bar :type integer)))
    (defmany foo bar)
    (defmany foo bar)))

ryszard.szopa's avatar
ryszard.szopa committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(db-test (many-to-many :depends-on many-to-many-definition)
  (defdao-example uno ()
    ((foo :type integer :accessor foo :initarg :foo)))
  (defdao-example  dos ()
    ((bar :type integer :accessor bar :initarg :bar)))

  (defmany uno dos)

  (let* ((uno (save-dao (make-instance 'uno :foo 1)))
         (dos (make-and-save 'dos :bar 1)))
    
    (finishes (relate uno dos))
    
    (is (= 1 (length (uno dos))))
    (is (= 1 (length (dos uno))))

    (is (= 1 (bar (car (dos uno)))))))

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(db-test (make-dao :depends-on dao-definition)
  (defdao-example foo ()
    ((foo :type integer :initarg :foo :accessor foo)))
  (finishes (make-instance 'foo :foo 1)))

(db-test (get-dao-from-db :depends-on make-dao)
  (defdao-example foo ()
    ((foo :type integer :initarg :foo :accessor foo)))
  (let ((instance (make-instance 'foo :foo 3)))
    (finishes (save-dao instance))
    (is (= (foo instance) (foo (make-instance 'foo :id (get-id instance)))))))

(db-test (select-dao :depends-on get-dao-from-db)
  (defdao-example foo ()
    ((foo :type integer :initarg :foo :accessor foo)))
  (iter (for i from 1 to 10)
        (make-and-save 'foo :foo 3))
  (let ((select (select-dao 'foo)))
    (is (= (length select) 10))))

(db-test (select-dao-with-test :depends-on select-dao)
  (defdao-example foo ()
    ((foo :type integer :initarg :foo :accessor foo)))
  (iter (for i from 1 to 17)
        (make-and-save 'foo :foo 17))
  (iter (for i from 1 to 13)
        (make-and-save 'foo :foo 13))
  (is (= 13 (length (select-dao 'foo (:= 13 foo)))))
  (is (= 17 (length (select-dao 'foo (:= 17 foo))))))


ryszard.szopa's avatar
ryszard.szopa committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(db-test (save-dao :depends-on dao-definition-with-foreign-key)
  (defdao-example baz ()
    ((baz :accessor baz :initarg :baz :type integer)))

  (defdao-example quux ()
    ((baz :accessor baz :initarg :baz :type baz :foreign t)))
  
  (let*  ((baz (submarine::make-and-save 'baz :baz 1))
          (quux (make-instance 'quux :baz baz)))
    (finishes (save-dao quux))
    (is (equal 1 (baz (baz quux))))
    ))

(db-test (save-dao-recursively :depends-on save-dao)
  (defdao-example baz ()
    ((baz :accessor baz :initarg :baz :type integer)))

  (defdao-example quux ()
    ((baz :accessor baz :initarg :baz :type baz :foreign t)))
  
  (let*  ((baz (make-instance 'baz :baz 1))
          (quux (make-instance 'quux :baz baz)))
    
    (setf (baz baz) 2)
    (finishes (save-dao quux))
    (is (equal 2 (baz (baz quux))))

    (let ((new-quux (make-instance 'quux :id (get-id quux))))
      (is (equal 2 (baz (baz new-quux)))))))

(db-test (save-dao-wrong-id :depends-on save-dao)
  (defdao-example rabe ()
    ((rabe :accessor rabe :initarg :rabe :type integer)))

  (defdao-example tike ()
    ((rabe :accessor rabe :initarg :rabe :type rabe :foreign t)))
  (signals dao-nonexistent-id (make-instance 'rabe :id 666 :rabe 1))  )

200
201
202
203
204
205
206
207
(db-test (save-existing-dao :depends-on make-dao)
  (defdao-example foo ()
    ((foo :type integer :initarg :foo :accessor foo)))
  (let ((foo (make-and-save 'foo :foo 1)))
    (setf (foo foo) 777)
    (finishes (save-dao foo))
    (is (= 777 (foo (make-instance 'foo :id (get-id foo)))))))

ryszard.szopa's avatar
ryszard.szopa committed
208
209
210
211
212
213
214
215
216
217
218
219
(db-test (save-dao-wrong-id-in-reference :depends-on save-dao)
  (defdao-example rabe ()
    ((rabe :accessor rabe :initarg :rabe :type integer)))

  (defdao-example tike ()
    ((rabe :accessor rabe :initarg :rabe :type rabe :foreign t)))

  (let* ((rabe (make-instance 'rabe :rabe 1))
         (tike (make-instance 'tike :rabe rabe)))
    (setf (get-id rabe) 666) ;we set id to a random number
    (save-dao tike)
    (is (= (rabe rabe) (rabe (rabe (make-instance 'tike :id (get-id tike))))))))
ryszard.szopa's avatar
tests    
ryszard.szopa committed
220

ryszard.szopa's avatar
ryszard.szopa committed
221
222
223
224
225
226
227
228
229
230
(db-test (save-dao-transaction :depends-on save-dao-recursively)
  (defdao-example rabe ()
    ((rabe :accessor rabe :initarg :rabe :type integer)))

  (defdao-example tike ()
    ((rabe :accessor rabe :initarg :rabe :type rabe :foreign t)
     (tike :accessor tike :initarg :tike :type integer)))

  (let* ((rabe (make-and-save 'rabe :rabe 777))
         (tike (make-and-save 'tike :rabe rabe :tike 113)))
231
    (setf (rabe (rabe tike)) 666)
ryszard.szopa's avatar
ryszard.szopa committed
232

233
234
235
236
237
238
    (let ((method (defmethod save-dao :after ((tike tike))
                    (error "This had to happen."))))
      (signals error (save-dao tike))
      (remove-method #'save-dao method))

    (is (= 777 (rabe (rabe (make-instance 'tike :id (get-id tike))))))))
ryszard.szopa's avatar
ryszard.szopa committed
239

ryszard.szopa's avatar
tests    
ryszard.szopa committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
;;; Copyright (C) 2007
;;; Ryszard Szopa <ryszard.szopa@gmail.com> & 
;;; Streamtech http://streamtech.nl/
;;;
;;; This software is provided 'as-is', without any express or implied
;;; warranty. In no event will the authors be held liable for any
;;; damages arising from the use of this software.
;;;
;;; Permission is granted to anyone to use this software for any
;;; purpose, including commercial applications, and to alter it and
;;; redistribute it freely, subject to the following restrictions:
;;;
;;; 1. The origin of this software must not be misrepresented; you must
;;;    not claim that you wrote the original software. If you use this
;;;    software in a product, an acknowledgment in the product
;;;    documentation would be appreciated but is not required.
;;;
;;; 2. Altered source versions must be plainly marked as such, and must
;;;    not be misrepresented as being the original software.
;;;
;;; 3. This notice may not be removed or altered from any source
ryszard.szopa's avatar
ryszard.szopa committed
261
262
;;;    distribution.