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