-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdb-tests.el
145 lines (134 loc) · 4.04 KB
/
db-tests.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
;;; tests for the emacs db.
(require 'cl)
(require 'ert)
(require 'db)
(require 'kv)
(ert-deftest db-get ()
"Test the database interface and the hash implementation."
;; Make a hash-db with no filename
(let ((db (db-make '(db-hash))))
(should-not (db-get "test-key" db))
(db-put "test-key" 321 db)
(should
(equal
321
(db-get "test-key" db)))))
(ert-deftest db-put ()
"Test the put interface."
(let ((db (db-make '(db-hash))))
(should-not (db-get "test-key" db))
(should
(equal
'("1" "2" "3")
(db-put "test-key" '("1" "2" "3") db)))))
(ert-deftest db-query ()
"Test the query interfce."
(let ((db (db-make '(db-hash))))
(db-put "test001"
'(("username" . "test001")
("title" . "Miss")
("surname" . "Test")) db)
(db-put "test002"
'(("username" . "test002")
("title" . "Mr")
("surname" . "Test")) db)
(should
(equal
'(("test001"
("username" . "test001")
("title" . "Miss")
("surname" . "Test")))
(db-query db '(= "username" "test001"))))))
(ert-deftest db-map ()
"Test the mapping."
(let (collected
(db (db-make '(db-hash :query-equal kvdotassoc=)))
(data '(("test001"
("username" . "test001")
("title" . "Miss")
("surname" . "Test"))
("test002"
("username" . "test002")
("title" . "Mr")
("surname" . "Test")))))
(loop for (key . value) in data
do (db-put key value db))
(db-map (lambda (key value)
(setq
collected
(acons key value collected))) db)
(should
(equal
(kvalist-sort collected 'kvcmp)
(kvalist-sort data 'kvcmp)))))
(ert-deftest db-query-deep ()
"Test the query interface with a dotted query."
(let ((db (db-make '(db-hash :query-equal kvdotassoc=))))
(db-put "test001"
'(("username" . "test001")
("details" . (("title" . "Miss")
("surname" . "Test")))) db)
(db-put "test002"
'(("username" . "test002")
("details" .(("title" . "Mr")
("surname" . "Tester")))) db)
(should
(equal
'(("test001"
("username" . "test001")
("details" . (("title" . "Miss")
("surname" . "Test")))))
(db-query db '(= "details.surname" "Test"))))))
(ert-deftest db-hash/save ()
"Test the saving of a hash db."
(unwind-protect
(progn
(let ((db (db-make
;; You shouldn't use an extension but let db deal
;; with it.
'(db-hash :filename "/tmp/test-db"))))
;; Override the save so it does nothing from put
(flet ((db-hash/save (db)
t))
(db-put 'test1 "value1" db)
(db-put 'test2 "value2" db))
;; And now save
(db-hash/save db))
;; And now load in a different scope
(let ((db (db-make
'(db-hash :filename "/tmp/test-db"))))
(should
(equal "value1"
(db-get 'test1 db)))))
(delete-file "/tmp/test-db.elc")))
(ert-deftest db-filter ()
"Test the filtering."
(let ((db (db-make
'(db-hash :filename "/tmp/test-db"))))
(db-put
"test001"
'(("uid" . "test001")
("fullname" . "test user 1"))
db)
(db-put
"test002"
'(("uid" . "test002")
("fullname" . "test user 2"))
db)
(db-put
"test003"
'(("uid" . "test001")
("fullname" . "test user 1"))
db)
(flet ((filt (key value)
(cdr (assoc "fullname" value))))
(let ((filtered
(db-make
`(db-filter
:source ,db
:filter filt))))
(plist-get filtered :source)
(should
(equal (db-get "test002" filtered) "test user 2"))))))
(provide 'db-tests)
;;; db-tests.el ends here