]> gitweb.factorcode.org Git - factor.git/blob - basis/db/tuples/tuples-tests.factor
references with cascade on delete work
[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             +on-delete+ +cascade+ }
200         { "summary" "SUMMARY" TEXT }
201         { "author" "AUTHOR" TEXT }
202         { "mode" "MODE" TEXT }
203         { "contents" "CONTENTS" TEXT }
204     } define-persistent ;
205
206 : test-paste-schema ( -- )
207     [ ] [ db-assigned-paste-schema ] unit-test
208     [ ] [ paste ensure-table ] unit-test
209     [ ] [ annotation ensure-table ] unit-test
210     [ ] [ annotation drop-table ] unit-test
211     [ ] [ paste drop-table ] unit-test
212     [ ] [ paste create-table ] unit-test
213     [ ] [ annotation create-table ] unit-test
214
215     [ ] [
216         paste new
217             "summary1" >>summary
218             "erg" >>author
219             "#lol" >>channel
220             "contents1" >>contents
221             now >>timestamp
222         insert-tuple
223     ] unit-test
224
225     [ ] [
226         annotation new
227             1 >>paste-id
228             "annotation1" >>summary
229             "erg" >>author
230             "annotation contents" >>contents
231         insert-tuple
232     ] unit-test
233
234     [ ] [
235     ] unit-test
236     ;
237
238 [ test-paste-schema ] test-sqlite
239 [ test-paste-schema ] test-postgresql
240
241 : test-repeated-insert
242     [ ] [ person ensure-table ] unit-test
243     [ ] [ person1 get insert-tuple ] unit-test
244     [ person1 get insert-tuple ] must-fail ;
245
246 TUPLE: serialize-me id data ;
247
248 : test-serialize ( -- )
249     serialize-me "SERIALIZED"
250     {
251         { "id" "ID" +db-assigned-id+ }
252         { "data" "DATA" FACTOR-BLOB }
253     } define-persistent
254     [ serialize-me drop-table ] [ drop ] recover
255     [ ] [ serialize-me create-table ] unit-test
256
257     [ ] [ T{ serialize-me f f H{ { 1 2 } } } insert-tuple ] unit-test
258     [
259         { T{ serialize-me f 1 H{ { 1 2 } } } }
260     ] [ T{ serialize-me f 1 } select-tuples ] unit-test ;
261
262 TUPLE: exam id name score ; 
263
264 : random-exam ( -- exam )
265         f
266         6 [ CHAR: a CHAR: z [a,b] random ] replicate >string
267         100 random
268     exam boa ;
269
270 : test-intervals ( -- )
271     [
272         exam "EXAM"
273         {
274             { "idd" "ID" +db-assigned-id+ }
275             { "named" "NAME" TEXT }
276             { "score" "SCORE" INTEGER }
277         } define-persistent
278     ] [
279         seq>> { "idd" "named" } =
280     ] must-fail-with
281
282     exam "EXAM"
283     {
284         { "id" "ID" +db-assigned-id+ }
285         { "name" "NAME" TEXT }
286         { "score" "SCORE" INTEGER }
287     } define-persistent
288     [ exam drop-table ] [ drop ] recover
289     [ ] [ exam create-table ] unit-test
290
291     [ ] [ T{ exam f f "Kyle" 100 } insert-tuple ] unit-test
292     [ ] [ T{ exam f f "Stan" 80 } insert-tuple ] unit-test
293     [ ] [ T{ exam f f "Kenny" 60 } insert-tuple ] unit-test
294     [ ] [ T{ exam f f "Cartman" 41 } insert-tuple ] unit-test
295
296     [
297         {
298             T{ exam f 3 "Kenny" 60 }
299             T{ exam f 4 "Cartman" 41 }
300         }
301     ] [
302         T{ exam f f f T{ interval f { 0 t } { 70 t } } } select-tuples
303     ] unit-test
304
305     [
306         { }
307     ] [
308         T{ exam f T{ interval f { 3 f } { 4 f } } f } select-tuples
309     ] unit-test
310     [
311         {
312             T{ exam f 4 "Cartman" 41 }
313         }
314     ] [
315         T{ exam f T{ interval f { 3 f } { 4 t } } f } select-tuples
316     ] unit-test
317     [
318         {
319             T{ exam f 3 "Kenny" 60 }
320         }
321     ] [
322         T{ exam f T{ interval f { 3 t } { 4 f } } f } select-tuples
323     ] unit-test
324     [
325         {
326             T{ exam f 3 "Kenny" 60 }
327             T{ exam f 4 "Cartman" 41 }
328         }
329     ] [
330         T{ exam f T{ interval f { 3 t } { 4 t } } f } select-tuples
331     ] unit-test
332
333     [
334         {
335             T{ exam f 1 "Kyle" 100 }
336             T{ exam f 2 "Stan" 80 }
337         }
338     ] [
339         T{ exam f f { "Stan" "Kyle" } } select-tuples
340     ] unit-test
341
342     [
343         {
344             T{ exam f 1 "Kyle" 100 }
345             T{ exam f 2 "Stan" 80 }
346             T{ exam f 3 "Kenny" 60 }
347         }
348     ] [
349         T{ exam f T{ range f 1 3 1 } } select-tuples
350     ] unit-test
351
352     [
353         {
354             T{ exam f 2 "Stan" 80 }
355             T{ exam f 3 "Kenny" 60 }
356             T{ exam f 4 "Cartman" 41 }
357         }
358     ] [
359         T{ exam f T{ interval f { 2 t } { 1.0/0.0 f } } } select-tuples
360     ] unit-test
361
362     [
363         {
364             T{ exam f 1 "Kyle" 100 }
365         }
366     ] [
367         T{ exam f T{ interval f { -1.0/0.0 t } { 2 f } } } select-tuples
368     ] unit-test
369
370     [
371         {
372             T{ exam f 1 "Kyle" 100 }
373             T{ exam f 2 "Stan" 80 }
374             T{ exam f 3 "Kenny" 60 }
375             T{ exam f 4 "Cartman" 41 }
376         }
377     ] [
378         T{ exam f T{ interval f { -1.0/0.0 t } { 1/0. f } } } select-tuples
379     ] unit-test
380     
381     [
382         {
383             T{ exam f 1 "Kyle" 100 }
384             T{ exam f 2 "Stan" 80 }
385             T{ exam f 3 "Kenny" 60 }
386             T{ exam f 4 "Cartman" 41 }
387         }
388     ] [
389         T{ exam } select-tuples
390     ] unit-test
391
392     [ 4 ] [ T{ exam } count-tuples ] unit-test ;
393
394 TUPLE: bignum-test id m n o ;
395 : <bignum-test> ( m n o -- obj )
396     bignum-test new
397         swap >>o
398         swap >>n
399         swap >>m ;
400
401 : test-bignum
402     bignum-test "BIGNUM_TEST"
403     {
404         { "id" "ID" +db-assigned-id+ }
405         { "m" "M" BIG-INTEGER }
406         { "n" "N" UNSIGNED-BIG-INTEGER }
407         { "o" "O" SIGNED-BIG-INTEGER }
408     } define-persistent
409     [ bignum-test drop-table ] ignore-errors
410     [ ] [ bignum-test ensure-table ] unit-test
411     [ ] [ 63 2^ 1- dup dup <bignum-test> insert-tuple ] unit-test ;
412
413     ! sqlite only
414     ! [ T{ bignum-test f 1
415         ! -9223372036854775808 9223372036854775808 -9223372036854775808 } ]
416     ! [ T{ bignum-test f 1 } select-tuple ] unit-test ;
417
418 TUPLE: secret n message ;
419 C: <secret> secret
420
421 : test-random-id
422     secret "SECRET"
423     {
424         { "n" "ID" +random-id+ system-random-generator }
425         { "message" "MESSAGE" TEXT }
426     } define-persistent
427
428     [ ] [ secret recreate-table ] unit-test
429
430     [ t ] [ f "kilroy was here" <secret> [ insert-tuple ] keep n>> integer? ] unit-test
431
432     [ ] [ f "kilroy was here2" <secret> insert-tuple ] unit-test
433
434     [ ] [ f "kilroy was here3" <secret> insert-tuple ] unit-test
435
436     [ t ] [
437         T{ secret } select-tuples
438         first message>> "kilroy was here" head?
439     ] unit-test
440
441     [ t ] [
442         T{ secret } select-tuples length 3 =
443     ] unit-test ;
444
445 [ db-assigned-person-schema test-tuples ] test-sqlite
446 [ user-assigned-person-schema test-tuples ] test-sqlite
447 [ user-assigned-person-schema test-repeated-insert ] test-sqlite
448 [ test-bignum ] test-sqlite
449 [ test-serialize ] test-sqlite
450 [ test-intervals ] test-sqlite
451 [ test-random-id ] test-sqlite
452
453 [ db-assigned-person-schema test-tuples ] test-postgresql
454 [ user-assigned-person-schema test-tuples ] test-postgresql
455 [ user-assigned-person-schema test-repeated-insert ] test-postgresql
456 [ test-bignum ] test-postgresql
457 [ test-serialize ] test-postgresql
458 [ test-intervals ] test-postgresql
459 [ test-random-id ] test-postgresql
460
461 TUPLE: does-not-persist ;
462
463 [
464     [ does-not-persist create-sql-statement ]
465     [ class \ not-persistent = ] must-fail-with
466 ] test-sqlite
467
468 [
469     [ does-not-persist create-sql-statement ]
470     [ class \ not-persistent = ] must-fail-with
471 ] test-postgresql
472
473
474 TUPLE: suparclass id a ;
475
476 suparclass f {
477     { "id" "ID" +db-assigned-id+ }
478     { "a" "A" INTEGER }
479 } define-persistent
480
481 TUPLE: subbclass < suparclass b ;
482
483 subbclass "SUBCLASS" {
484     { "b" "B" TEXT }
485 } define-persistent
486
487 TUPLE: fubbclass < subbclass ;
488
489 fubbclass "FUBCLASS" { } define-persistent
490
491 : test-db-inheritance ( -- )
492     [ ] [ subbclass ensure-table ] unit-test
493     [ ] [ fubbclass ensure-table ] unit-test
494     
495     [ ] [
496         subbclass new 5 >>a "hi" >>b dup insert-tuple id>> "id" set
497     ] unit-test
498     
499     [ t "hi" 5 ] [
500         subbclass new "id" get >>id select-tuple
501         [ subbclass? ] [ b>> ] [ a>> ] tri
502     ] unit-test
503     
504     [ ] [ fubbclass new 0 >>a "hi" >>b insert-tuple ] unit-test
505     
506     [ t ] [ fubbclass new select-tuples [ fubbclass? ] all? ] unit-test ;
507
508 [ test-db-inheritance ] test-sqlite
509 [ test-db-inheritance ] test-postgresql
510
511
512 TUPLE: string-encoding-test id string ;
513
514 string-encoding-test "STRING_ENCODING_TEST" {
515     { "id" "ID" +db-assigned-id+ }
516     { "string" "STRING" TEXT }
517 } define-persistent
518
519 : test-string-encoding ( -- )
520     [ ] [ string-encoding-test ensure-table ] unit-test
521
522     [ ] [
523         string-encoding-test new
524             "\u{copyright-sign}\u{bengali-letter-cha}" >>string
525         [ insert-tuple ] [ id>> "id" set ] bi
526     ] unit-test
527     
528     [ "\u{copyright-sign}\u{bengali-letter-cha}" ] [
529         string-encoding-test new "id" get >>id select-tuple string>>
530     ] unit-test ;
531
532 [ test-string-encoding ] test-sqlite
533 [ test-string-encoding ] test-postgresql
534
535 ! Don't comment these out. These words must infer
536 \ bind-tuple must-infer
537 \ insert-tuple must-infer
538 \ update-tuple must-infer
539 \ delete-tuples must-infer
540 \ select-tuple must-infer
541 \ define-persistent must-infer
542 \ ensure-table must-infer
543 \ create-table must-infer
544 \ drop-table must-infer
545
546 : test-queries ( -- )
547     [ ] [ exam ensure-table ] unit-test
548     [ ] [ 1000 [ random-exam insert-tuple ] times ] unit-test
549     [ 5 ] [
550         <query>
551         T{ exam { score T{ interval { from { 0 t } } { to { 100 t } } } } }
552             >>tuple
553         5 >>limit select-tuples length
554     ] unit-test ;
555
556 TUPLE: compound-foo a b c ;
557
558 compound-foo "COMPOUND_FOO" 
559 {
560     { "a" "A" INTEGER +user-assigned-id+ }
561     { "b" "B" INTEGER +user-assigned-id+ }
562     { "c" "C" INTEGER }
563 } define-persistent
564
565 : test-compound-primary-key ( -- )
566     [ ] [ compound-foo ensure-table ] unit-test
567     [ ] [ compound-foo drop-table ] unit-test
568     [ ] [ compound-foo create-table ] unit-test
569     [ ] [ 1 2 3 compound-foo boa insert-tuple ] unit-test
570     [ 1 2 3 compound-foo boa insert-tuple ] must-fail
571     [ ] [ 2 3 4 compound-foo boa insert-tuple ] unit-test
572     [ T{ compound-foo { a 2 } { b 3 } { c 4 } } ]
573     [ compound-foo new 4 >>c select-tuple ] unit-test ;
574
575 [ test-compound-primary-key ] test-sqlite
576 [ test-compound-primary-key ] test-postgresql
577
578 : sqlite-test-db ( -- )
579     "tuples-test.db" temp-file sqlite-db make-db db-open db set ;
580
581 : postgresql-test-db ( -- )
582     { "localhost" "postgres" "foob" "factor-test" } postgresql-db
583     make-db db-open db set ;