]> gitweb.factorcode.org Git - factor-unmaintained.git/blob - forestdb/lib/lib-tests.factor
forestdb: not a maintained db
[factor-unmaintained.git] / forestdb / lib / lib-tests.factor
1 ! Copyright (C) 2014 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.strings arrays forestdb.lib forestdb.utils kernel
4 make multiline sequences tools.test ;
5 IN: forestdb.lib.tests
6
7 ! Get/set by key/value
8 { "val123" } [
9     [
10         "test123" [
11             "key123" "val123" fdb-set-kv
12             "key123" fdb-get-kv
13         ] with-kvs-name
14     ] with-forestdb-test-manual
15 ] unit-test
16
17 { "val12345" } [
18     [
19         "test123" [
20             "key123" "val12345" fdb-set-kv
21             "key123" fdb-get-kv
22         ] with-kvs-name
23     ] with-forestdb-test-manual
24 ] unit-test
25
26 ! Get
27
28 {
29     { "key1" "val" }
30 } [
31     [
32         5 set-kv-n
33         fdb-commit-normal
34         "key1" "meta" "val" [
35             fdb_doc>doc [ key>> ] [ body>> ] bi 2array
36         ] with-create-doc
37     ] with-forestdb-test-manual
38 ] unit-test
39
40
41 {
42     { "key1" f "val1" }
43 } [
44     [
45         5 set-kv-n
46         fdb-commit-normal
47         "key1" "no meta" "going away" [
48             fdb-get
49             fdb_doc>doc [ key>> ] [ meta>> ] [ body>> ] tri 3array
50         ] with-create-doc
51     ] with-forestdb-test-manual
52 ] unit-test
53
54
55 {
56     { "key2" f "val2" }
57 } [
58     [
59         5 set-kv-n
60         fdb-commit-normal
61         2 <seqnum-doc> [
62             fdb-get-byseq fdb_doc>doc
63             [ key>> ] [ meta>> ] [ body>> ] tri 3array
64         ] with-doc
65     ] with-forestdb-test-manual
66 ] unit-test
67
68 {
69     { "key2" f "val2" }
70 } [
71     [
72         5 set-kv-n
73         fdb-commit-normal
74         2 <seqnum-doc> [
75             fdb-get-byseq fdb_doc>doc
76             [ key>> ] [ meta>> ] [ body>> ] tri 3array
77         ] with-doc
78     ] with-forestdb-test-manual
79 ] unit-test
80
81 ! Filename is only valid inside with-forestdb
82 { f } [
83     [
84         fdb-get-info filename>> alien>native-string empty?
85     ] with-forestdb-test-manual
86 ] unit-test
87
88 ! Test fdb_doc_create
89 { 6 9 9 } [
90     [
91        "key123" "meta blah" "some body" [
92             [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri
93         ] with-create-doc
94     ] with-forestdb-test-manual
95 ] unit-test
96
97 { 7 8 15 } [
98     [
99        "key1234" "meta blah" "some body" [
100             [ "new meta" "some other body" fdb-doc-update ]
101             [ [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri ] bi
102         ] with-create-doc
103     ] with-forestdb-test-manual
104 ] unit-test
105
106 { 1 1 } [
107     [
108         1 set-kv-n
109         fdb-commit-normal
110         fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi
111     ] with-forestdb-test-manual
112 ] unit-test
113
114 { 6 5 } [
115     [
116         5 set-kv-n
117         5 set-kv-nth
118         fdb-commit-normal
119         fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi
120     ] with-forestdb-test-manual
121 ] unit-test
122
123 { 5 5 } [
124     [
125         5 set-kv-n
126         fdb-commit-normal
127         fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi
128     ] with-forestdb-test-manual
129 ] unit-test
130
131 ! Snapshots
132
133 /*
134 { 5 5 } [
135     [
136         5 set-kv-n
137         fdb-commit-normal
138         FDB_SNAPSHOT_INMEM [
139             fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi
140         ] with-forestdb-snapshot
141     ] with-forestdb-test-manual
142 ] unit-test
143 */
144
145
146 /*
147 ! Snapshots can only occur on commits. If you commit five keys at once,
148 ! and then try to open a snapshot on the second key, it should fail.
149
150 ! XXX: Buggy, fails in _fdb_open with FDB_RESULT_NO_DB_INSTANCE
151 [
152     delete-test-db-1
153     test-db-1 [
154         5 set-kv-n
155         fdb-commit-normal
156         FDB_SNAPSHOT_INMEM [
157             fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi
158         ] with-forestdb-snapshot
159     ] with-forestdb-tester
160 ] [
161     T{ fdb-error { error FDB_RESULT_NO_DB_INSTANCE } } =
162 ] must-fail-with
163
164 ! Test that we take two snapshots and their seqnums/doc counts are right.
165 ! XXX: Buggy, want to see the first snapshot's document count at 5 too
166 {
167     { 7 7 }
168     { 7 7 }
169 } [
170     delete-test-db-1
171     test-db-1 [
172         5 set-kv-n
173         fdb-commit-normal
174
175         6 7 set-kv-range
176         fdb-commit-normal
177
178         FDB_SNAPSHOT_INMEM [
179             fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi 2array
180         ] with-forestdb-snapshot
181
182         FDB_SNAPSHOT_INMEM [
183             fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi 2array
184         ] with-forestdb-snapshot
185     ] with-forestdb-tester
186 ] unit-test
187
188
189 ! Same test as above, but with buggy behavior for now so it passes
190 {
191     7
192     7
193 } [
194     delete-test-db-1
195     test-db-1 [
196         5 set-kv-n
197         fdb-commit-normal
198
199         6 7 set-kv-range
200         fdb-commit-normal
201
202         FDB_SNAPSHOT_INMEM [
203             fdb-get-kvs-info last_seqnum>>
204         ] with-forestdb-snapshot
205
206         FDB_SNAPSHOT_INMEM [
207             fdb-get-kvs-info last_seqnum>>
208         ] with-forestdb-snapshot
209     ] with-forestdb-tester
210 ] unit-test
211
212
213
214
215 ! Rollback test
216 ! Make sure the doc_count is correct after a rollback
217 {
218     7
219     { 5 5 }
220 } [
221     delete-test-db-1
222     test-db-1 [
223         5 set-kv-n
224         fdb-commit-normal
225
226         6 7 set-kv-range
227         fdb-commit-normal
228
229         FDB_SNAPSHOT_INMEM [
230             fdb-get-kvs-info last_seqnum>>
231         ] with-forestdb-snapshot
232
233         5 fdb-rollback
234
235         FDB_SNAPSHOT_INMEM [
236             fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi 2array
237         ] with-forestdb-snapshot
238     ] with-forestdb-tester
239 ] unit-test
240
241 */
242
243
244 ! Iterators test
245 ! No matching keys
246 {
247     { }
248 } [
249     [
250         5 set-kv-n
251         fdb-commit-normal
252         [
253             "omg" "nada" [
254                   fdb_doc>doc [ seqnum>> ] [ key>> ] [ body>> ] tri 3array ,
255             ] with-fdb-normal-iterator
256         ] { } make
257     ] with-forestdb-test-manual
258 ] unit-test
259
260 ! All the keys
261 {
262     {
263         { 1 "key1" "val1" }
264         { 2 "key2" "val2" }
265         { 3 "key3" "val3" }
266         { 4 "key4" "val4" }
267         { 5 "key5" "val5" }
268     }
269 } [
270     [
271         5 set-kv-n
272         fdb-commit-normal
273         [
274             "key1" "key5" [
275                   fdb_doc>doc [ seqnum>> ] [ key>> ] [ body>> ] tri 3array ,
276             ] with-fdb-normal-iterator
277         ] { } make
278     ] with-forestdb-test-manual
279 ] unit-test
280
281 ! Test that keys at extremes get returned
282 {
283     {
284         { 1 "key1" "val1" }
285     }
286 } [
287     [
288         5 set-kv-n
289         fdb-commit-normal
290         [
291             "key0" "key1" [
292                   fdb_doc>doc [ seqnum>> ] [ key>> ] [ body>> ] tri 3array ,
293             ] with-fdb-normal-iterator
294         ] { } make
295     ] with-forestdb-test-manual
296 ] unit-test
297
298 {
299     {
300         { 5 "key5" "val5" }
301     }
302 } [
303     [
304         5 set-kv-n
305         fdb-commit-normal
306         [
307             "key5" "key9" [
308                   fdb_doc>doc [ seqnum>> ] [ key>> ] [ body>> ] tri 3array ,
309             ] with-fdb-normal-iterator
310         ] { } make
311     ] with-forestdb-test-manual
312 ] unit-test
313
314
315 ! Test byseq mapping
316 {
317     V{ 1 2 3 4 5 }
318 } [
319     [
320         5 set-kv-n
321         fdb-commit-normal
322         0 10 [
323             fdb_doc>doc
324         ] with-fdb-byseq-map
325         [ seqnum>> ] map
326     ] with-forestdb-test-manual
327 ] unit-test
328
329 ! XXX: Behavior changed here
330 ! No longer makes new docs that are deleted
331 ! Deleting 5 keys gives you 5 new seqnums that are those docs, but deleted
332 ! {
333     ! V{ { 6 t } { 7 t } { 8 t } { 9 t } { 10 t } }
334 ! } [
335     ! [
336         ! 5 set-kv-n
337         ! 5 del-kv-n
338         ! fdb-commit-normal
339         ! 0 10 [
340             ! fdb_doc>doc
341         ! ] with-fdb-byseq-map
342         ! [ [ seqnum>> ] [ deleted?>> ] bi 2array ] map
343     ! ] with-forestdb-test-manual
344 ! ] unit-test
345
346 ! Test new behavior
347 {
348     V{ }
349 } [
350     [
351         5 set-kv-n
352         5 del-kv-n
353         fdb-commit-normal
354         0 10 [
355             fdb_doc>doc
356         ] with-fdb-byseq-map
357         [ [ seqnum>> ] [ deleted?>> ] bi 2array ] map
358     ] with-forestdb-test-manual
359 ] unit-test
360
361 {
362     {
363         { 1 "key1" }
364         { 2 "key2" }
365         { 3 "key3" }
366         { 4 "key4" }
367         { 5 "key5" }
368     }
369 }
370 [
371     [
372         5 set-kv-n
373         fdb-commit-normal
374         [
375            0 10 [
376                 [ seqnum>> ]
377                 [ [ key>> ] [ keylen>> ] bi alien/length>string ] bi 2array ,
378             ] with-fdb-byseq-each
379         ] { } make
380     ] with-forestdb-test-manual
381 ] unit-test