1 ;;;_ tinydb/rtest.el --- Rtest tests for persist
5 ;; Copyright (C) 2010 Tom Breton (Tehom)
7 ;; Author: Tom Breton (Tehom) <tehom@panix.com>
8 ;; Keywords: lisp, maint, internal
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
31 (require 'tinydb
/asynq
)
34 (defun persist:th
:make-usual-tq
(initial)
42 #'(lambda (old-obj obj
)
44 ;;Type predicate that always passes
49 ;;;_ , tinydb-alist-push
50 (rtest:deftest tinydb-alist-push
51 ( "Operation: Push an element"
56 (persist:th
:make-usual-tq
'((b 144)))))
58 (tinydb-alist-push tq
'a
'(12))
59 (tinydb-q-do-pending tq
)
68 ;;;_ , tinydb-alist-assoc
69 (rtest:deftest tinydb-alist-assoc
70 ( "Proves: Can read from it via `tinydb-alist-assoc'."
73 ((list '((a 12)(b 144)))
75 (persist:th
:make-usual-tq list
))
77 (tinydb-alist-assoc tq
'a
)))
91 ;;;_ , tinydb-alist--remove
92 (rtest:deftest tinydb-alist--remove
94 ( "Gives the expected results"
98 (tinydb-alist--remove (list '(1 a
)'(2 b
)) 1)
103 (tinydb-alist--remove (list '(1 a
)'(2 b
)) 2)
108 ;;;_ , tinydb-alist-update
110 (rtest:deftest tinydb-alist-update
111 ( "Proves: It updates an element."
114 ( (list (list (list 'a
12)(list 'b
144)))
116 (persist:th
:make-usual-tq list
)))
117 (tinydb-alist-update tq
'a
118 #'(lambda (old old-p key
)
119 (assert (equal old
'(12)) t
)
120 (assert (equal key
'a
) t
)
122 (tinydb-q-do-pending tq
)
131 (tinydb-alist-assoc tq
'a
)
136 ;;;_ , tinydb-alist-pushnew
137 (rtest:deftest tinydb-alist-pushnew
139 ( "Situation: Element is not already there.
143 ( (list (list (list 'a
12)(list 'b
144)))
145 (persist:th
:make-usual-tq list
)))
146 (tinydb-alist-pushnew tq
'c
'(1728))
147 (tinydb-q-do-pending tq
)
151 '((a 12)(b 144)(c 1728)))
156 (tinydb-alist-assoc tq
'c
)
161 ( "Situation: Element is already there.
162 Response: No change."
165 ( (list (list (list 'a
12)(list 'b
144)))
167 (persist:th
:make-usual-tq list
)))
168 (tinydb-alist-pushnew tq
'a
'(13))
169 (tinydb-q-do-pending tq
)
179 (tinydb-alist-assoc tq
'a
)
183 ;;;_ , tinydb-alist-push-replace
184 (rtest:deftest tinydb-alist-push-replace
186 ( "Situation: Element is not already there.
190 ( (list (list (list 'a
12)(list 'b
144)))
192 (persist:th
:make-usual-tq list
)))
193 (tinydb-alist-push-replace tq
'c
'(1728))
194 (tinydb-q-do-pending tq
)
198 '((a 12)(b 144)(c 1728)))
203 (tinydb-alist-assoc tq
'c
)
208 ( "Situation: Element is already there.
209 Response: It replaces the old element."
212 ( (list (list (list 'a
12)(list 'b
144)))
214 (persist:th
:make-usual-tq list
)))
215 (tinydb-alist-push-replace tq
'a
'(1728))
216 (tinydb-q-do-pending tq
)
226 (tinydb-alist-assoc tq
'a
)
231 ;;;_ . Reentrancy tests
233 ;;These tests are theoretically sensitive to processor speed, but the
234 ;;specified durations are extremely generous so there shouldn't be a
236 (rtest:deftest tinydb
/reentrancy
238 ( "Proves: Timer events can run during sleep-for."
241 (run-with-timer 0.1 nil
243 (push (list 'timed
(current-time)) recorded
)))
245 (push (list 'delayed
(current-time)) recorded
)
247 (assq 'timed recorded
)
250 (assq 'delayed recorded
)
253 ( (timed-ran-at (second (assq 'timed recorded
)))
254 (delayed-ran-at (second (assq 'delayed recorded
)))
255 (time-diff (time-subtract delayed-ran-at timed-ran-at
)))
258 (time-less-p time-diff
(seconds-to-time 0.99))
261 (time-less-p (seconds-to-time 0.80) time-diff
)
265 ( "Proves: Validates the testing mechanism with sit-for vs a
271 (run-with-timer 0.2 nil
273 (push "Start 2" list
)
275 (push "End 2" list
)))
277 (push "Start test" list
)
279 (push "End test" list
)
283 '("Start test" "Start 2" "End 2" "End test"))
287 ( "Proves: Validates the testing mechanism with multiple run-with-timers.
288 (which somehow nests their sit-for calls)"
292 (run-with-timer 0.1 nil
294 (push "Start 1" list
)
297 (push "End 1" list
)))
298 (run-with-timer 0.2 nil
300 (push "Start 2" list
)
303 (push "End 2" list
)))
305 (push "Start test" list
)
307 (push "End test" list
)
311 '("Start test" "Start 1" "Start 2"
312 "End 2" "End 1" "End test"))
317 ( "Proves: Can see the internal object (at all)."
322 (persist:th
:make-usual-tq
'(12)))
341 ( "Proves: Calling `tinydb-q-will-call' recursively in handler
343 FIX ME: This logic has changed. Now it's only reads that require
350 (persist:th
:make-usual-tq
'(12))))
352 (push "Start test" list
)
358 (tinydb-q-will-call tq t
#'identity
))
361 (push "End test" list
)
365 '("Start test" "End test"))
374 ' ;;No longer guaranteed
375 ( "Proves: Can write asynchronously to it."
380 (persist:th
:make-usual-tq
'(12))))
381 (push "Start test" list
)
382 (run-with-timer 0.1 nil
384 (push "Start 1" list
)
390 (throw 'tinydb-q-new-obj
395 (run-with-timer 0.2 nil
397 (push "Start 2" list
)
402 (throw 'tinydb-q-new-obj
408 (tinydb-q-do-pending tq
)
409 (push "End test" list
)
413 '("Start test" "Start 1" "Start 2"
430 (provide 'tinydb
/rtest
)
432 ;;;_ * Local emacs vars.
433 ;;;_ + Local variables:
438 ;;; tinydb/rtest.el ends here