1 ;;;_ tinydb/persist/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.
32 (require 'tinydb
/persist
)
33 (require 'emtest
/testhelp
/tagnames
)
34 (require 'emtest
/testhelp
/mocks
/filebuf
)
35 (require 'emtest
/testhelp
/testpoint
)
41 (defconst persist
:th
:examples-dir
42 (emtb:expand-filename-by-load-file
"examples/")
43 "Directory where examples are" )
45 ;;The roles here are out of sync
46 (defconst tinydb
:thd
:examples
47 (emtg:define
+ ;;xmp:b5d33625-bd1d-49d3-9c56-148b699eba99
48 ((project emtest
)(library persist
))
49 (group ((type filename
))
51 (expand-file-name "1" persist
:th
:examples-dir
))
53 (expand-file-name "2" persist
:th
:examples-dir
)))
55 (group ((creation-time before
))
56 (item ((type data
)) '(1 10 500)))
58 (group ((creation-time now
))
59 (item ((type data
)) '(56 25 17)))))
62 ;;;_ , tehom-persist-buffer-as-const-list
65 (rtest:deftest tehom-persist-buffer-as-const-list
67 ( "Situation: The file exists; we know what data it contains.
68 Operation: Get that data
69 Result: The data is what we expect."
70 (emtg:with tinydb
:thd
:examples
71 ((project emtest
)(library persist
))
72 (tehom-persist-buffer-as-const-list
73 (emtg (type filename
)(role master
))
76 (emtg (type data
)(creation-time before
))))))
78 ( "Situation: The file doesn't exist.
79 Result: It returns the initial value - which is ()
80 This does not attempt to create the file or save the buffer."
81 (emtg:with tinydb
:thd
:examples
82 ((project emtest
)(library persist
))
83 (emtb:with-file-f
(:absent t
) filename
86 ;;Tiny touch of psychic here: we know that the initial
89 (tehom-persist-buffer-as-const-list
93 (equal a initial-value
)
98 ;;;_ , tinydb/buffer Same tests on the alist wrt files
100 (rtest:deftest tinydb
/buffer
102 ( "Situation: The file exists; we know what data it contains.
103 Operation: Get that data
104 Result: The data is what we expect."
105 (emtg:with tinydb
:thd
:examples
106 ((project emtest
)(library persist
))
109 (tinydb-persist-make-q
111 (emtg (type filename
)(role master
))
116 ;;Test that we can read it.
119 (tinydb-get-obj filetq
)
120 (emtg (type data
)(creation-time before
))))
124 ( "Situation: The file doesn't exist.
125 Result: It returns the initial value.
126 This does not attempt to create the file or save the buffer."
127 (emtg:with tinydb
:thd
:examples
128 ((project emtest
)(library persist
))
129 (emtb:with-file-f
(:absent t
) filename
132 (initial-value '(a b c
))
134 (tinydb-persist-make-q
142 (tinydb-get-obj filetq
)
144 ;;The file still doesn't exist, because it was not
145 ;;eager-save and no operation we did should have forced
148 (not (file-exists-p filename
))
154 ;;;_ , tehom-persist-buffer-as-mutable-obj
156 (put 'tehom-persist-buffer-as-mutable-obj
'rtest
:test-thru
157 'tehom-persist-buffer-as-mutable-list
)
158 ;;;_ , tehom-persist-buffer-as-mutable-list
161 (rtest:deftest tehom-persist-buffer-as-mutable-list
163 ( "Proves: Form return value is the same as body return value."
164 (emtg:with tinydb
:thd
:examples
165 ((project emtest
)(library persist
))
168 (emtg (role master
)(type filename
)))
170 (tehom-persist-buffer-as-mutable-list
174 (assert (equal ret-val
135) t
)
177 ( "Situation: Read a persisting object.
178 Operation: Set a different value. Read it as if fresh.
179 Afterwards: It retains the new value."
180 (emtg:with tinydb
:thd
:examples
181 ((project emtest
)(library persist
))
183 (:file
(emtg (role master
)(type filename
)))
185 (tehom-persist-buffer-as-mutable-list
188 ;;Validate: We got the expected original value.
190 (emtg (creation-time before
)(type data
)))
191 ;;Set it to the now value.
193 (emtg (creation-time now
)(type data
))))
194 ;;Read it as if fresh
195 (tehom-persist-buffer-as-const-list
199 (emtg (type data
)(creation-time now
)))))))
202 ( "Situation: The file doesn't exist.
203 Result: It returns the initial value - which is ()
204 File now exists and contains the initial value."
205 (emtg:with tinydb
:thd
:examples
206 ((project emtest
)(library persist
))
207 (emtb:with-file-f
(:absent t
) filename
208 ;;Tiny touch of psychic here: we know that the initial
213 (tehom-persist-buffer-as-mutable-list
217 (equal a initial-value
)
220 (file-exists-p filename
)
223 ((text (emtb:file-contents-absname filename
))
224 (obj (if text
(read text
))))
232 ;;;_ , tinydb/buffer/alist/mutate
234 (rtest:deftest tinydb
/buffer
/alist
/mutate
236 ( "Situation: Read a persisting object.
237 Operation: Set a different value. Read it as if fresh.
238 Afterwards: It retains the new value."
239 (emtg:with tinydb
:thd
:examples
240 ((project emtest
)(library persist
))
242 (:file
(emtg (role master
)(type filename
)))
247 (tinydb-persist-make-q
253 ;;Validate: We got the expected original value.
256 (tinydb-get-obj filetq
)
257 (emtg (creation-time before
)(type data
)))
260 ;;Set it to the new value.
263 (emtg (creation-time now
)(type data
)))
265 ;;The new value is immediately available.
267 ((value (tinydb-get-obj filetq
)))
271 (emtg (creation-time now
)(type data
)))
276 ( "Situation: The file doesn't exist.
277 Result: It returns the initial value - which is ()
278 File now exists and contains the initial value."
279 (emtg:with tinydb
:thd
:examples
280 ((project emtest
)(library persist
))
281 (emtb:with-file-f
(:absent t
) filename
284 (initial-value '(12)) ;;Distinct from nil
286 (tinydb-persist-make-q
294 (tinydb-get-obj filetq
)
296 ;;Eager save causes the file to exist.
298 (file-exists-p filename
)
300 ;;The file's contents are as expected.
302 ((text (emtb:file-contents-absname filename
))
303 (obj (if text
(read text
))))
305 (equal obj initial-value
)
311 ;;;_ , tehom-update-persist-buffer
314 (rtest:deftest tehom-update-persist-buffer
316 ( "Operation: Write a different value.
317 Afterwards: Read it as if fresh. It retains the new value."
318 (emtg:with tinydb
:thd
:examples
319 ((project emtest
)(library persist
))
321 (:file
(emtg (role master
)(type filename
)))
323 ;;Save the new value.
324 (tehom-update-persist-buffer
326 (emtg (creation-time now
)(type data
))
328 ;;Read it as if fresh
329 (tehom-persist-buffer-as-const-list
333 (emtg (type data
)(creation-time now
)))))))
335 ( "Operation: Try to write a different value that fails the
337 Result: Signal an error.
338 Afterwards: Read it as if fresh. It still has the old value."
339 (emtg:with tinydb
:thd
:examples
340 ((project emtest
)(library persist
))
342 (:file
(emtg (role master
)(type filename
))) filename
343 ;;Try to save a value that fails the type predicate.
344 ;;This must signal error.
347 (tehom-update-persist-buffer
352 ;;Read it as if fresh
353 (tehom-persist-buffer-as-const-list
356 ;;It still has the old value.
358 (emtg (type data
)(creation-time before
)))))))
360 ( "Operation: Try to write a value that can't be read - In fact, a
362 Result: Signal an error.
363 Afterwards: Read it as if fresh. It still has the old value."
364 (emtg:with tinydb
:thd
:examples
365 ((project emtest
)(library persist
))
367 (:file
(emtg (role master
)(type filename
))) filename
368 ;;Try to save a value that fails the type predicate.
369 ;;This must signal error.
372 (tehom-update-persist-buffer
376 ;;Read it as if fresh
377 (tehom-persist-buffer-as-const-list
380 ;;It still has the old value.
382 (emtg (type data
)(creation-time before
)))))))
386 ;;;_ , tinydb/buffer/update
388 (rtest:deftest tinydb
/buffer
/update
389 ( "Operation: Try to write a different value that fails the
391 Result: Signal an error.
392 Afterwards: Read it as if fresh. It still has the old value."
393 (emtg:with tinydb
:thd
:examples
394 ((project emtest
)(library persist
))
396 (:file
(emtg (role master
)(type filename
))) filename
400 (tinydb-persist-make-q
403 ;;Initial object (Unused here)
407 ;;Try to save a value that fails the type predicate.
408 (tinydb-set-obj filetq
'not-a-list
)
409 ;;Object still has the old value.
412 (tinydb-get-obj filetq
)
413 (emtg (type data
)(creation-time before
))))
416 ;;The last one remaining to convert
417 ( "Operation: Try to write a value that can't be read - In fact, a
419 Result: Signal an error.
420 Afterwards: Read it as if fresh. It still has the old value."
421 (emtg:with tinydb
:thd
:examples
422 ((project emtest
)(library persist
))
424 (:file
(emtg (role master
)(type filename
))) filename
428 (tinydb-persist-make-q
431 ;;Initial object (Unused here)
435 ;;Try to save a value that can't be read.
436 (tinydb-set-obj filetq
(current-buffer))
437 ;;Object still has the old value.
440 (tinydb-get-obj filetq
)
441 (emtg (type data
)(creation-time before
))))
451 (provide 'tinydb
/persist
/rtest
)
453 ;;;_ * Local emacs vars.
454 ;;;_ + Local variables:
459 ;;; tinydb/persist/rtest.el ends here