Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 104 lines (94 sloc) 2.764 kB
95aef7c @nicferrier initial checkin
authored
1 ;;; tests for the emacs db.
2
3 (require 'cl)
4 (require 'ert)
5 (require 'db)
6 (require 'kv)
7
8 (ert-deftest db-get ()
9 "Test the database interface and the hash implementation."
10 ;; Make a hash-db with no filename
11 (let ((db (db-make '(db-hash))))
12 (should-not (db-get "test-key" db))
13 (db-put "test-key" 321 db)
14 (should
15 (equal
16 321
17 (db-get "test-key" db)))))
18
19 (ert-deftest db-put ()
20 "Test the put interface."
21 (let ((db (db-make '(db-hash))))
22 (should-not (db-get "test-key" db))
23 (should
24 (equal
25 '("1" "2" "3")
26 (db-put "test-key" '("1" "2" "3") db)))))
27
28 (ert-deftest db-query ()
29 "Test the query interfce."
30 (let ((db (db-make '(db-hash))))
31 (db-put "test001"
32 '(("username" . "test001")
33 ("title" . "Miss")
34 ("surname" . "Test")) db)
35 (db-put "test002"
36 '(("username" . "test002")
37 ("title" . "Mr")
38 ("surname" . "Test")) db)
39 (should
40 (equal
41 '(("test001"
42 ("username" . "test001")
43 ("title" . "Miss")
44 ("surname" . "Test")))
45 (db-map 'kvidentity db '(= "username" "test001"))))))
46
47
48 (ert-deftest db-hash--save ()
49 "Test the saving of a hash db."
50 (unwind-protect
51 (progn
52 (let ((db (db-make
53 ;; You shouldn't use an extension but let elnode deal
54 ;; with it.
55 '(db-hash :filename "/tmp/test-db"))))
56 ;; Override the save so it does nothing from put
57 (flet ((db-hash--save (db)
58 t))
59 (db-put 'test1 "value1" db)
60 (db-put 'test2 "value2" db))
61 ;; And now save
62 (db-hash--save db))
63 ;; And now load in a different scope
64 (let ((db (db-make
65 '(db-hash :filename "/tmp/test-db"))))
66 (should
67 (equal "value1"
68 (db-get 'test1 db)))))
69 (delete-file "/tmp/test-db.elc")))
70
71 (ert-deftest db-filter ()
72 "Test the filtering."
73 (let ((db (db-make
74 '(db-hash :filename "/tmp/test-db"))))
75 (db-put
76 "test001"
77 '(("uid" . "test001")
78 ("fullname" . "test user 1"))
79 db)
80 (db-put
81 "test002"
82 '(("uid" . "test002")
83 ("fullname" . "test user 2"))
84 db)
85 (db-put
86 "test003"
87 '(("uid" . "test001")
88 ("fullname" . "test user 1"))
89 db)
90 (flet ((filt (key value)
91 (cdr (assoc "fullname" value))))
92 (let ((filtered
93 (db-make
94 `(db-filter
95 :source ,db
96 :filter filt))))
97 (plist-get filtered :source)
98 (should
99 (equal (db-get "test002" filtered) "test user 2"))))))
100
101 (provide 'db-tests)
102
103 ;;; db-tests.el ends here
Something went wrong with that request. Please try again.