]> gitweb.factorcode.org Git - factor.git/blob - basis/db/tuples/tuples-tests.factor
sort of support foreign keys
[factor.git] / basis / db / tuples / tuples-tests.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.files kernel tools.test db db.tuples classes
4 db.types continuations namespaces math math.ranges
5 prettyprint calendar sequences db.sqlite math.intervals
6 db.postgresql accessors random math.bitwise
7 math.ranges strings urls fry db.tuples.private ;
8 IN: db.tuples.tests
9
10 : test-sqlite ( quot -- )
11     [ ] swap '[
12         "tuples-test.db" temp-file sqlite-db _ with-db
13     ] unit-test ;
14
15 : test-postgresql ( quot -- )
16     [ ] swap '[
17         { "localhost" "postgres" "foob" "factor-test" }
18         postgresql-db _ with-db
19     ] unit-test ;
20
21 TUPLE: person the-id the-name the-number the-real
22 ts date time blob factor-blob url ;
23
24 : <person> ( name age real ts date time blob factor-blob url -- person )
25     person new
26         swap >>url
27         swap >>factor-blob
28         swap >>blob
29         swap >>time
30         swap >>date
31         swap >>ts
32         swap >>the-real
33         swap >>the-number
34         swap >>the-name ;
35
36 : <user-assigned-person> ( id name age real ts date time blob factor-blob url -- person )
37     <person>
38         swap >>the-id ;
39
40 SYMBOL: person1
41 SYMBOL: person2
42 SYMBOL: person3
43 SYMBOL: person4
44
45 : test-tuples ( -- )
46     [ ] [ person recreate-table ] unit-test
47     [ ] [ person ensure-table ] unit-test
48     [ ] [ person drop-table ] unit-test
49     [ ] [ person create-table ] unit-test
50     [ person create-table ] must-fail
51     [ ] [ person ensure-table ] unit-test
52     
53     [ ] [ person1 get insert-tuple ] unit-test
54
55     [ 1 ] [ person1 get the-id>> ] unit-test
56
57     [ ] [ person1 get 200 >>the-number drop ] unit-test
58
59     [ ] [ person1 get update-tuple ] unit-test
60
61     [ T{ person f 1 "billy" 200 3.14 } ]
62     [ T{ person f 1 } select-tuple ] unit-test
63     [ ] [ person2 get insert-tuple ] unit-test
64     [
65         {
66             T{ person f 1 "billy" 200 3.14 }
67             T{ person f 2 "johnny" 10 3.14 }
68         }
69     ] [ T{ person f f f f 3.14 } select-tuples ] unit-test
70     [
71         {
72             T{ person f 1 "billy" 200 3.14 }
73             T{ person f 2 "johnny" 10 3.14 }
74         }
75     ] [ T{ person f } select-tuples ] unit-test
76
77     [
78         {
79             T{ person f 2 "johnny" 10 3.14 }
80         }
81     ] [ T{ person f f f 10 3.14 } select-tuples ] unit-test
82
83
84     [ ] [ person1 get delete-tuples ] unit-test
85     [ f ] [ T{ person f 1 } select-tuple ] unit-test
86
87     [ ] [ person3 get insert-tuple ] unit-test
88
89     [
90         T{
91             person
92             f
93             3
94             "teddy"
95             10
96             3.14
97             T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
98             T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
99             T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
100             B{ 115 116 111 114 101 105 110 97 98 108 111 98 }
101         }
102     ] [ T{ person f 3 } select-tuple ] unit-test
103
104     [ ] [ person4 get insert-tuple ] unit-test
105     [
106         T{
107             person
108             f
109             4
110             "eddie"
111             10
112             3.14
113             T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
114             T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
115             T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
116             f
117             H{ { 1 2 } { 3 4 } { 5 "lol" } }
118             URL" http://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search"
119         }
120     ] [ T{ person f 4 } select-tuple ] unit-test
121
122     [ ] [ person drop-table ] unit-test ;
123
124 : db-assigned-person-schema ( -- )
125     person "PERSON"
126     {
127         { "the-id" "ID" +db-assigned-id+ }
128         { "the-name" "NAME" { VARCHAR 256 } +not-null+ }
129         { "the-number" "AGE" INTEGER { +default+ 0 } }
130         { "the-real" "REAL" DOUBLE { +default+ 0.3 } }
131         { "ts" "TS" TIMESTAMP }
132         { "date" "D" DATE }
133         { "time" "T" TIME }
134         { "blob" "B" BLOB }
135         { "factor-blob" "FB" FACTOR-BLOB }
136         { "url" "U" URL }
137     } define-persistent
138     "billy" 10 3.14 f f f f f f <person> person1 set
139     "johnny" 10 3.14 f f f f f f <person> person2 set
140     "teddy" 10 3.14
141         T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
142         T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
143         T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
144         B{ 115 116 111 114 101 105 110 97 98 108 111 98 } f f <person> person3 set
145     "eddie" 10 3.14
146         T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
147         T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
148         T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
149         f H{ { 1 2 } { 3 4 } { 5 "lol" } } URL" http://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search"  <person> person4 set ;
150
151 : user-assigned-person-schema ( -- )
152     person "PERSON"
153     {
154         { "the-id" "ID" INTEGER +user-assigned-id+ }
155         { "the-name" "NAME" { VARCHAR 256 } +not-null+ }
156         { "the-number" "AGE" INTEGER { +default+ 0 } }
157         { "the-real" "REAL" DOUBLE { +default+ 0.3 } }
158         { "ts" "TS" TIMESTAMP }
159         { "date" "D" DATE }
160         { "time" "T" TIME }
161         { "blob" "B" BLOB }
162         { "factor-blob" "FB" FACTOR-BLOB }
163         { "url" "U" URL }
164     } define-persistent
165     1 "billy" 10 3.14 f f f f f f <user-assigned-person> person1 set
166     2 "johnny" 10 3.14 f f f f f f <user-assigned-person> person2 set
167     3 "teddy" 10 3.14
168         T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
169         T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
170         T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
171         B{ 115 116 111 114 101 105 110 97 98 108 111 98 }
172         f f <user-assigned-person> person3 set
173     4 "eddie" 10 3.14
174         T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
175         T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
176         T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
177         f H{ { 1 2 } { 3 4 } { 5 "lol" } } URL" http://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search" <user-assigned-person> person4 set ;
178
179 TUPLE: paste n summary author channel mode contents timestamp annotations ;
180 TUPLE: annotation n paste-id summary author mode contents ;
181
182 : db-assigned-paste-schema ( -- )
183     paste "PASTE"
184     {
185         { "n" "ID" +db-assigned-id+ }
186         { "summary" "SUMMARY" TEXT }
187         { "author" "AUTHOR" TEXT }
188         { "channel" "CHANNEL" TEXT }
189         { "mode" "MODE" TEXT }
190         { "contents" "CONTENTS" TEXT }
191         { "timestamp" "DATE" TIMESTAMP }
192         { "annotations" { +has-many+ annotation } }
193     } define-persistent
194
195     annotation "ANNOTATION"
196     {
197         { "n" "ID" +db-assigned-id+ }
198         { "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "n" } }
199         { "summary" "SUMMARY" TEXT }
200         { "author" "AUTHOR" TEXT }
201         { "mode" "MODE" TEXT }
202         { "contents" "CONTENTS" TEXT }
203     } define-persistent ;
204
205 : test-paste-schema ( -- )
206     [ ] [ db-assigned-paste-schema ] unit-test
207     [ ] [ paste ensure-table ] unit-test
208     [ ] [ annotation ensure-table ] unit-test
209     [ ] [ annotation drop-table ] unit-test
210     [ ] [ paste drop-table ] unit-test
211     [ ] [ paste create-table ] unit-test
212     [ ] [ annotation create-table ] unit-test
213     ;
214
215 [ test-paste-schema ] test-sqlite
216 [ test-paste-schema ] test-postgresql
217
218 : test-repeated-insert
219     [ ] [ person ensure-table ] unit-test
220     [ ] [ person1 get insert-tuple ] unit-test
221     [ person1 get insert-tuple ] must-fail ;
222
223 TUPLE: serialize-me id data ;
224
225 : test-serialize ( -- )
226     serialize-me "SERIALIZED"
227     {
228         { "id" "ID" +db-assigned-id+ }
229         { "data" "DATA" FACTOR-BLOB }
230     } define-persistent
231     [ serialize-me drop-table ] [ drop ] recover
232     [ ] [ serialize-me create-table ] unit-test
233
234     [ ] [ T{ serialize-me f f H{ { 1 2 } } } insert-tuple ] unit-test
235     [
236         { T{ serialize-me f 1 H{ { 1 2 } } } }
237     ] [ T{ serialize-me f 1 } select-tuples ] unit-test ;
238
239 TUPLE: exam id name score ; 
240
241 : random-exam ( -- exam )
242         f
243         6 [ CHAR: a CHAR: z [a,b] random ] replicate >string
244         100 random
245     exam boa ;
246
247 : test-intervals ( -- )
248     [
249         exam "EXAM"
250         {
251             { "idd" "ID" +db-assigned-id+ }
252             { "named" "NAME" TEXT }
253             { "score" "SCORE" INTEGER }
254         } define-persistent
255     ] [
256         seq>> { "idd" "named" } =
257     ] must-fail-with
258
259     exam "EXAM"
260     {
261         { "id" "ID" +db-assigned-id+ }
262         { "name" "NAME" TEXT }
263         { "score" "SCORE" INTEGER }
264     } define-persistent
265     [ exam drop-table ] [ drop ] recover
266     [ ] [ exam create-table ] unit-test
267
268     [ ] [ T{ exam f f "Kyle" 100 } insert-tuple ] unit-test
269     [ ] [ T{ exam f f "Stan" 80 } insert-tuple ] unit-test
270     [ ] [ T{ exam f f "Kenny" 60 } insert-tuple ] unit-test
271     [ ] [ T{ exam f f "Cartman" 41 } insert-tuple ] unit-test
272
273     [
274         {
275             T{ exam f 3 "Kenny" 60 }
276             T{ exam f 4 "Cartman" 41 }
277         }
278     ] [
279         T{ exam f f f T{ interval f { 0 t } { 70 t } } } select-tuples
280     ] unit-test
281
282     [
283         { }
284     ] [
285         T{ exam f T{ interval f { 3 f } { 4 f } } f } select-tuples
286     ] unit-test
287     [
288         {
289             T{ exam f 4 "Cartman" 41 }
290         }
291     ] [
292         T{ exam f T{ interval f { 3 f } { 4 t } } f } select-tuples
293     ] unit-test
294     [
295         {
296             T{ exam f 3 "Kenny" 60 }
297         }
298     ] [
299         T{ exam f T{ interval f { 3 t } { 4 f } } f } select-tuples
300     ] unit-test
301     [
302         {
303             T{ exam f 3 "Kenny" 60 }
304             T{ exam f 4 "Cartman" 41 }
305         }
306     ] [
307         T{ exam f T{ interval f { 3 t } { 4 t } } f } select-tuples
308     ] unit-test
309
310     [
311         {
312             T{ exam f 1 "Kyle" 100 }
313             T{ exam f 2 "Stan" 80 }
314         }
315     ] [
316         T{ exam f f { "Stan" "Kyle" } } select-tuples
317     ] unit-test
318
319     [
320         {
321             T{ exam f 1 "Kyle" 100 }
322             T{ exam f 2 "Stan" 80 }
323             T{ exam f 3 "Kenny" 60 }
324         }
325     ] [
326         T{ exam f T{ range f 1 3 1 } } select-tuples
327     ] unit-test
328
329     [
330         {
331             T{ exam f 2 "Stan" 80 }
332             T{ exam f 3 "Kenny" 60 }
333             T{ exam f 4 "Cartman" 41 }
334         }
335     ] [
336         T{ exam f T{ interval f { 2 t } { 1.0/0.0 f } } } select-tuples
337     ] unit-test
338
339     [
340         {
341             T{ exam f 1 "Kyle" 100 }
342         }
343     ] [
344         T{ exam f T{ interval f { -1.0/0.0 t } { 2 f } } } select-tuples
345     ] unit-test
346
347     [
348         {
349             T{ exam f 1 "Kyle" 100 }
350             T{ exam f 2 "Stan" 80 }
351             T{ exam f 3 "Kenny" 60 }
352             T{ exam f 4 "Cartman" 41 }
353         }
354     ] [
355         T{ exam f T{ interval f { -1.0/0.0 t } { 1/0. f } } } select-tuples
356     ] unit-test
357     
358     [
359         {
360             T{ exam f 1 "Kyle" 100 }
361             T{ exam f 2 "Stan" 80 }
362             T{ exam f 3 "Kenny" 60 }
363             T{ exam f 4 "Cartman" 41 }
364         }
365     ] [
366         T{ exam } select-tuples
367     ] unit-test
368
369     [ 4 ] [ T{ exam } count-tuples ] unit-test ;
370
371 TUPLE: bignum-test id m n o ;
372 : <bignum-test> ( m n o -- obj )
373     bignum-test new
374         swap >>o
375         swap >>n
376         swap >>m ;
377
378 : test-bignum
379     bignum-test "BIGNUM_TEST"
380     {
381         { "id" "ID" +db-assigned-id+ }
382         { "m" "M" BIG-INTEGER }
383         { "n" "N" UNSIGNED-BIG-INTEGER }
384         { "o" "O" SIGNED-BIG-INTEGER }
385     } define-persistent
386     [ bignum-test drop-table ] ignore-errors
387     [ ] [ bignum-test ensure-table ] unit-test
388     [ ] [ 63 2^ 1- dup dup <bignum-test> insert-tuple ] unit-test ;
389
390     ! sqlite only
391     ! [ T{ bignum-test f 1
392         ! -9223372036854775808 9223372036854775808 -9223372036854775808 } ]
393     ! [ T{ bignum-test f 1 } select-tuple ] unit-test ;
394
395 TUPLE: secret n message ;
396 C: <secret> secret
397
398 : test-random-id
399     secret "SECRET"
400     {
401         { "n" "ID" +random-id+ system-random-generator }
402         { "message" "MESSAGE" TEXT }
403     } define-persistent
404
405     [ ] [ secret recreate-table ] unit-test
406
407     [ t ] [ f "kilroy was here" <secret> [ insert-tuple ] keep n>> integer? ] unit-test
408
409     [ ] [ f "kilroy was here2" <secret> insert-tuple ] unit-test
410
411     [ ] [ f "kilroy was here3" <secret> insert-tuple ] unit-test
412
413     [ t ] [
414         T{ secret } select-tuples
415         first message>> "kilroy was here" head?
416     ] unit-test
417
418     [ t ] [
419         T{ secret } select-tuples length 3 =
420     ] unit-test ;
421
422 [ db-assigned-person-schema test-tuples ] test-sqlite
423 [ user-assigned-person-schema test-tuples ] test-sqlite
424 [ user-assigned-person-schema test-repeated-insert ] test-sqlite
425 [ test-bignum ] test-sqlite
426 [ test-serialize ] test-sqlite
427 [ test-intervals ] test-sqlite
428 [ test-random-id ] test-sqlite
429
430 [ db-assigned-person-schema test-tuples ] test-postgresql
431 [ user-assigned-person-schema test-tuples ] test-postgresql
432 [ user-assigned-person-schema test-repeated-insert ] test-postgresql
433 [ test-bignum ] test-postgresql
434 [ test-serialize ] test-postgresql
435 [ test-intervals ] test-postgresql
436 [ test-random-id ] test-postgresql
437
438 TUPLE: does-not-persist ;
439
440 [
441     [ does-not-persist create-sql-statement ]
442     [ class \ not-persistent = ] must-fail-with
443 ] test-sqlite
444
445 [
446     [ does-not-persist create-sql-statement ]
447     [ class \ not-persistent = ] must-fail-with
448 ] test-postgresql
449
450
451 TUPLE: suparclass id a ;
452
453 suparclass f {
454     { "id" "ID" +db-assigned-id+ }
455     { "a" "A" INTEGER }
456 } define-persistent
457
458 TUPLE: subbclass < suparclass b ;
459
460 subbclass "SUBCLASS" {
461     { "b" "B" TEXT }
462 } define-persistent
463
464 TUPLE: fubbclass < subbclass ;
465
466 fubbclass "FUBCLASS" { } define-persistent
467
468 : test-db-inheritance ( -- )
469     [ ] [ subbclass ensure-table ] unit-test
470     [ ] [ fubbclass ensure-table ] unit-test
471     
472     [ ] [
473         subbclass new 5 >>a "hi" >>b dup insert-tuple id>> "id" set
474     ] unit-test
475     
476     [ t "hi" 5 ] [
477         subbclass new "id" get >>id select-tuple
478         [ subbclass? ] [ b>> ] [ a>> ] tri
479     ] unit-test
480     
481     [ ] [ fubbclass new 0 >>a "hi" >>b insert-tuple ] unit-test
482     
483     [ t ] [ fubbclass new select-tuples [ fubbclass? ] all? ] unit-test ;
484
485 [ test-db-inheritance ] test-sqlite
486 [ test-db-inheritance ] test-postgresql
487
488
489 TUPLE: string-encoding-test id string ;
490
491 string-encoding-test "STRING_ENCODING_TEST" {
492     { "id" "ID" +db-assigned-id+ }
493     { "string" "STRING" TEXT }
494 } define-persistent
495
496 : test-string-encoding ( -- )
497     [ ] [ string-encoding-test ensure-table ] unit-test
498
499     [ ] [
500         string-encoding-test new
501             "\u{copyright-sign}\u{bengali-letter-cha}" >>string
502         [ insert-tuple ] [ id>> "id" set ] bi
503     ] unit-test
504     
505     [ "\u{copyright-sign}\u{bengali-letter-cha}" ] [
506         string-encoding-test new "id" get >>id select-tuple string>>
507     ] unit-test ;
508
509 [ test-string-encoding ] test-sqlite
510 [ test-string-encoding ] test-postgresql
511
512 ! Don't comment these out. These words must infer
513 \ bind-tuple must-infer
514 \ insert-tuple must-infer
515 \ update-tuple must-infer
516 \ delete-tuples must-infer
517 \ select-tuple must-infer
518 \ define-persistent must-infer
519 \ ensure-table must-infer
520 \ create-table must-infer
521 \ drop-table must-infer
522
523 : test-queries ( -- )
524     [ ] [ exam ensure-table ] unit-test
525     [ ] [ 1000 [ random-exam insert-tuple ] times ] unit-test
526     [ 5 ] [
527         <query>
528         T{ exam { score T{ interval { from { 0 t } } { to { 100 t } } } } }
529             >>tuple
530         5 >>limit select-tuples length
531     ] unit-test ;
532
533 TUPLE: compound-foo a b c ;
534
535 compound-foo "COMPOUND_FOO" 
536 {
537     { "a" "A" INTEGER +user-assigned-id+ }
538     { "b" "B" INTEGER +user-assigned-id+ }
539     { "c" "C" INTEGER }
540 } define-persistent
541
542 : test-compound-primary-key ( -- )
543     [ ] [ compound-foo ensure-table ] unit-test
544     [ ] [ compound-foo drop-table ] unit-test
545     [ ] [ compound-foo create-table ] unit-test
546     [ ] [ 1 2 3 compound-foo boa insert-tuple ] unit-test
547     [ 1 2 3 compound-foo boa insert-tuple ] must-fail
548     [ ] [ 2 3 4 compound-foo boa insert-tuple ] unit-test
549     [ T{ compound-foo { a 2 } { b 3 } { c 4 } } ]
550     [ compound-foo new 4 >>c select-tuple ] unit-test ;
551
552 [ test-compound-primary-key ] test-sqlite
553 [ test-compound-primary-key ] test-postgresql
554
555 : test-db ( -- )
556     "tuples-test.db" temp-file sqlite-db make-db db-open db set ;