1 ! Copyright (C) 2008 Doug Coleman.
2 ! Copyright (C) 2018 Alexander Ilin.
3 ! See https://factorcode.org/license.txt for BSD license.
4 USING: accessors calendar calendar.parser classes continuations
5 db.tester db.tuples db.types kernel math math.intervals ranges
6 namespaces random sequences sorting strings tools.test urls ;
9 TUPLE: person the-id the-name the-number the-real
10 ts date time blob factor-blob url ;
12 : <person> ( name age real ts date time blob factor-blob url -- person )
24 : <user-assigned-person> ( id name age real ts date time blob factor-blob url -- person )
34 [ ] [ person recreate-table ] unit-test
35 [ ] [ person ensure-table ] unit-test
36 [ ] [ person drop-table ] unit-test
37 [ ] [ person create-table ] unit-test
38 [ person create-table ] must-fail
39 [ ] [ person ensure-table ] unit-test
41 [ ] [ person1 get insert-tuple ] unit-test
43 [ 1 ] [ person1 get the-id>> ] unit-test
45 [ ] [ person1 get 200 >>the-number drop ] unit-test
47 [ ] [ person1 get update-tuple ] unit-test
49 [ T{ person f 1 "billy" 200 3.14 } ]
50 [ T{ person f 1 } select-tuple ] unit-test
51 [ ] [ person2 get insert-tuple ] unit-test
54 T{ person f 1 "billy" 200 3.14 }
55 T{ person f 2 "johnny" 10 3.14 }
57 ] [ T{ person f f f f 3.14 } select-tuples ] unit-test
60 T{ person f 1 "billy" 200 3.14 }
61 T{ person f 2 "johnny" 10 3.14 }
63 ] [ T{ person f } select-tuples ] unit-test
67 T{ person f 2 "johnny" 10 3.14 }
69 ] [ T{ person f f f 10 3.14 } select-tuples ] unit-test
72 [ ] [ person1 get delete-tuples ] unit-test
73 [ f ] [ T{ person f 1 } select-tuple ] unit-test
75 [ ] [ person3 get insert-tuple ] unit-test
85 T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
86 T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
87 T{ duration f 0 0 0 12 34 56 }
88 B{ 115 116 111 114 101 105 110 97 98 108 111 98 }
90 ] [ T{ person f 3 } select-tuple ] unit-test
92 [ ] [ person4 get insert-tuple ] unit-test
101 T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
102 T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
103 T{ duration f 0 0 0 12 34 56 }
105 H{ { 1 2 } { 3 4 } { 5 "lol" } }
106 URL" https://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search"
108 ] [ T{ person f 4 } select-tuple ] unit-test
110 [ ] [ person drop-table ] unit-test ;
112 : teddy-data ( -- name age real ts date time blob factor-blob url )
114 "2008-03-05 16:24:11" ymdhms>timestamp
115 "2008-11-22 00:00:00" ymdhms>timestamp
116 "12:34:56" hms>duration
117 B{ 115 116 111 114 101 105 110 97 98 108 111 98 } f f ;
119 : eddie-data ( -- name age real ts date time blob factor-blob url )
121 "2008-03-05 16:24:11" ymdhms>timestamp
122 "2008-11-22 00:00:00" ymdhms>timestamp
123 "12:34:56" hms>duration
124 f H{ { 1 2 } { 3 4 } { 5 "lol" } }
125 URL" https://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search" ;
127 : db-assigned-person-schema ( -- )
130 { "the-id" "ID" +db-assigned-id+ }
131 { "the-name" "NAME" { VARCHAR 256 } +not-null+ }
132 { "the-number" "AGE" INTEGER { +default+ 0 } }
133 { "the-real" "REAL" DOUBLE { +default+ 0.3 } }
134 { "ts" "TS" TIMESTAMP }
138 { "factor-blob" "FB" FACTOR-BLOB }
141 "billy" 10 3.14 f f f f f f <person> person1 set
142 "johnny" 10 3.14 f f f f f f <person> person2 set
143 teddy-data <person> person3 set
144 eddie-data <person> person4 set ;
146 : user-assigned-person-schema ( -- )
149 { "the-id" "ID" INTEGER +user-assigned-id+ }
150 { "the-name" "NAME" { VARCHAR 256 } +not-null+ }
151 { "the-number" "AGE" INTEGER { +default+ 0 } }
152 { "the-real" "REAL" DOUBLE { +default+ 0.3 } }
153 { "ts" "TS" TIMESTAMP }
157 { "factor-blob" "FB" FACTOR-BLOB }
160 1 "billy" 10 3.14 f f f f f f <user-assigned-person> person1 set
161 2 "johnny" 10 3.14 f f f f f f <user-assigned-person> person2 set
162 3 teddy-data <user-assigned-person> person3 set
163 4 eddie-data <user-assigned-person> person4 set ;
165 TUPLE: paste n summary author channel mode contents timestamp annotations ;
166 TUPLE: annotation n paste-id summary author mode contents ;
170 { "n" "ID" +db-assigned-id+ }
171 { "summary" "SUMMARY" TEXT }
172 { "author" "AUTHOR" TEXT }
173 { "channel" "CHANNEL" TEXT }
174 { "mode" "MODE" TEXT }
175 { "contents" "CONTENTS" TEXT }
176 { "timestamp" "DATE" TIMESTAMP }
177 { "annotations" { +has-many+ annotation } }
180 : annotation-schema-foreign-key ( -- )
181 annotation "ANNOTATION"
183 { "n" "ID" +db-assigned-id+ }
184 { "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "ID" } }
185 { "summary" "SUMMARY" TEXT }
186 { "author" "AUTHOR" TEXT }
187 { "mode" "MODE" TEXT }
188 { "contents" "CONTENTS" TEXT }
189 } define-persistent ;
191 : annotation-schema-foreign-key-not-null ( -- )
192 annotation "ANNOTATION"
194 { "n" "ID" +db-assigned-id+ }
195 { "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "ID" } +not-null+ }
196 { "summary" "SUMMARY" TEXT }
197 { "author" "AUTHOR" TEXT }
198 { "mode" "MODE" TEXT }
199 { "contents" "CONTENTS" TEXT }
200 } define-persistent ;
202 : annotation-schema-cascade ( -- )
203 annotation "ANNOTATION"
205 { "n" "ID" +db-assigned-id+ }
206 { "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "ID" }
207 +on-delete+ +cascade+ }
208 { "summary" "SUMMARY" TEXT }
209 { "author" "AUTHOR" TEXT }
210 { "mode" "MODE" TEXT }
211 { "contents" "CONTENTS" TEXT }
212 } define-persistent ;
214 : annotation-schema-restrict ( -- )
215 annotation "ANNOTATION"
217 { "n" "ID" +db-assigned-id+ }
218 { "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "ID" } }
219 { "summary" "SUMMARY" TEXT }
220 { "author" "AUTHOR" TEXT }
221 { "mode" "MODE" TEXT }
222 { "contents" "CONTENTS" TEXT }
223 } define-persistent ;
225 : test-paste-schema ( -- )
226 [ ] [ paste ensure-table ] unit-test
227 [ ] [ annotation ensure-table ] unit-test
228 [ ] [ annotation drop-table ] unit-test
229 [ ] [ paste drop-table ] unit-test
230 [ ] [ paste create-table ] unit-test
231 [ ] [ annotation create-table ] unit-test
238 "contents1" >>contents
246 "annotation1" >>summary
248 "annotation contents" >>contents
252 : test-foreign-key ( -- )
253 [ ] [ annotation-schema-foreign-key ] unit-test
255 [ paste new 1 >>n delete-tuples ] must-fail ;
257 : test-foreign-key-not-null ( -- )
258 [ ] [ annotation-schema-foreign-key-not-null ] unit-test
260 [ paste new 1 >>n delete-tuples ] must-fail ;
262 : test-cascade ( -- )
263 [ ] [ annotation-schema-cascade ] unit-test
265 [ ] [ paste new 1 >>n delete-tuples ] unit-test
266 [ 0 ] [ paste new select-tuples length ] unit-test ;
268 : test-restrict ( -- )
269 [ ] [ annotation-schema-restrict ] unit-test
271 [ paste new 1 >>n delete-tuples ] must-fail ;
273 [ test-foreign-key ] test-sqlite
274 [ test-foreign-key-not-null ] test-sqlite
275 [ test-cascade ] test-sqlite
276 [ test-restrict ] test-sqlite
278 [ test-foreign-key ] test-postgresql
279 [ test-foreign-key-not-null ] test-postgresql
280 [ test-cascade ] test-postgresql
281 [ test-restrict ] test-postgresql
283 : test-repeated-insert ( -- )
284 [ ] [ person ensure-table ] unit-test
285 [ ] [ person1 get insert-tuple ] unit-test
286 [ person1 get insert-tuple ] must-fail ;
288 TUPLE: serialize-me id data ;
290 : test-serialize ( -- )
291 serialize-me "SERIALIZED"
293 { "id" "ID" +db-assigned-id+ }
294 { "data" "DATA" FACTOR-BLOB }
296 [ ] [ serialize-me recreate-table ] unit-test
298 [ ] [ T{ serialize-me f f H{ { 1 2 } } } insert-tuple ] unit-test
300 { T{ serialize-me f 1 H{ { 1 2 } } } }
301 ] [ T{ serialize-me f 1 } select-tuples ] unit-test ;
303 TUPLE: exam id name score ;
305 : random-exam ( -- exam )
307 6 [ CHAR: a CHAR: z [a..b] random ] replicate >string
311 : test-intervals ( -- )
315 { "idd" "ID" +db-assigned-id+ }
316 { "named" "NAME" TEXT }
317 { "score" "SCORE" INTEGER }
320 seq>> { "idd" "named" } =
325 { "id" "ID" +db-assigned-id+ }
326 { "name" "NAME" TEXT }
327 { "score" "SCORE" INTEGER }
329 [ ] [ exam recreate-table ] unit-test
331 [ ] [ T{ exam f f "Kyle" 100 } insert-tuple ] unit-test
332 [ ] [ T{ exam f f "Stan" 80 } insert-tuple ] unit-test
333 [ ] [ T{ exam f f "Kenny" 60 } insert-tuple ] unit-test
334 [ ] [ T{ exam f f "Cartman" 41 } insert-tuple ] unit-test
337 [ T{ exam { name IGNORE } { score IGNORE } } select-tuples length ] unit-test
340 [ T{ exam { name IGNORE } { score IGNORE } } select-tuples first score>> ] unit-test
342 [ T{ exam { name IGNORE } { score IGNORE } { id IGNORE } } select-tuples first score>> ] [ class>> "EXAM" = ] must-fail-with
346 T{ exam f 3 "Kenny" 60 }
347 T{ exam f 4 "Cartman" 41 }
350 T{ exam f f f T{ interval f { 0 t } { 70 t } } } select-tuples
356 T{ exam f T{ interval f { 3 f } { 4 f } } f } select-tuples
360 T{ exam f 4 "Cartman" 41 }
363 T{ exam f T{ interval f { 3 f } { 4 t } } f } select-tuples
367 T{ exam f 3 "Kenny" 60 }
370 T{ exam f T{ interval f { 3 t } { 4 f } } f } select-tuples
374 T{ exam f 3 "Kenny" 60 }
375 T{ exam f 4 "Cartman" 41 }
378 T{ exam f T{ interval f { 3 t } { 4 t } } f } select-tuples
383 T{ exam f 1 "Kyle" 100 }
384 T{ exam f 2 "Stan" 80 }
387 T{ exam f f { "Stan" "Kyle" } } select-tuples
392 T{ exam f 1 "Kyle" 100 }
393 T{ exam f 2 "Stan" 80 }
394 T{ exam f 3 "Kenny" 60 }
397 T{ exam f T{ range f 1 3 1 } } select-tuples
402 T{ exam f 2 "Stan" 80 }
403 T{ exam f 3 "Kenny" 60 }
404 T{ exam f 4 "Cartman" 41 }
407 T{ exam f T{ interval f { 2 t } { 1/0. f } } } select-tuples
412 T{ exam f 1 "Kyle" 100 }
415 T{ exam f T{ interval f { -1/0. t } { 2 f } } } select-tuples
420 T{ exam f 1 "Kyle" 100 }
421 T{ exam f 2 "Stan" 80 }
422 T{ exam f 3 "Kenny" 60 }
423 T{ exam f 4 "Cartman" 41 }
426 T{ exam f T{ interval f { -1/0. t } { 1/0. f } } } select-tuples
431 T{ exam f 1 "Kyle" 100 }
432 T{ exam f 2 "Stan" 80 }
433 T{ exam f 3 "Kenny" 60 }
434 T{ exam f 4 "Cartman" 41 }
437 T{ exam } select-tuples
440 [ 4 ] [ T{ exam } count-tuples ] unit-test
442 [ ] [ T{ exam { score 10 } } insert-tuple ] unit-test
445 [ T{ exam { name NULL } } select-tuples first score>> ] unit-test ;
447 TUPLE: bignum-test id m n o ;
448 : <bignum-test> ( m n o -- obj )
455 bignum-test "BIGNUM_TEST"
457 { "id" "ID" +db-assigned-id+ }
458 { "m" "M" BIG-INTEGER }
459 { "n" "N" UNSIGNED-BIG-INTEGER }
460 { "o" "O" SIGNED-BIG-INTEGER }
462 [ ] [ bignum-test recreate-table ] unit-test
463 [ ] [ 63 2^ 1 - dup dup <bignum-test> insert-tuple ] unit-test ;
466 ! [ T{ bignum-test f 1
467 ! -9223372036854775808 9223372036854775808 -9223372036854775808 } ]
468 ! [ T{ bignum-test f 1 } select-tuple ] unit-test ;
470 TUPLE: secret n message ;
473 : test-random-id ( -- )
476 { "n" "ID" +random-id+ system-random-generator }
477 { "message" "MESSAGE" TEXT }
480 [ ] [ secret recreate-table ] unit-test
482 [ t ] [ f "kilroy was here" <secret> [ insert-tuple ] keep n>> integer? ] unit-test
484 [ ] [ f "kilroy was here2" <secret> insert-tuple ] unit-test
486 [ ] [ f "kilroy was here3" <secret> insert-tuple ] unit-test
489 T{ secret } select-tuples
490 first message>> "kilroy was here" head?
494 T{ secret } select-tuples length 3 =
497 [ db-assigned-person-schema test-tuples ] test-sqlite
498 [ user-assigned-person-schema test-tuples ] test-sqlite
499 [ user-assigned-person-schema test-repeated-insert ] test-sqlite
500 [ test-bignum ] test-sqlite
501 [ test-serialize ] test-sqlite
502 [ test-intervals ] test-sqlite
503 [ test-random-id ] test-sqlite
505 [ db-assigned-person-schema test-tuples ] test-postgresql
506 [ user-assigned-person-schema test-tuples ] test-postgresql
507 [ user-assigned-person-schema test-repeated-insert ] test-postgresql
508 [ test-bignum ] test-postgresql
509 [ test-serialize ] test-postgresql
510 [ test-intervals ] test-postgresql
511 [ test-random-id ] test-postgresql
513 TUPLE: does-not-persist ;
516 [ does-not-persist create-sql-statement ]
517 [ class-of \ not-persistent = ] must-fail-with
521 [ does-not-persist create-sql-statement ]
522 [ class-of \ not-persistent = ] must-fail-with
526 TUPLE: suparclass id a ;
529 { "id" "ID" +db-assigned-id+ }
533 TUPLE: subbclass < suparclass b ;
535 subbclass "SUBCLASS" {
539 TUPLE: fubbclass < subbclass ;
541 fubbclass "FUBCLASS" { } define-persistent
543 : test-db-inheritance ( -- )
544 [ ] [ subbclass ensure-table ] unit-test
545 [ ] [ fubbclass ensure-table ] unit-test
548 subbclass new 5 >>a "hi" >>b dup insert-tuple id>> "id" set
552 subbclass new "id" get >>id select-tuple
553 [ subbclass? ] [ b>> ] [ a>> ] tri
556 [ ] [ fubbclass new 0 >>a "hi" >>b insert-tuple ] unit-test
558 [ t ] [ fubbclass new select-tuples [ fubbclass? ] all? ] unit-test ;
560 [ test-db-inheritance ] test-sqlite
561 [ test-db-inheritance ] test-postgresql
564 TUPLE: string-encoding-test id string ;
566 string-encoding-test "STRING_ENCODING_TEST" {
567 { "id" "ID" +db-assigned-id+ }
568 { "string" "STRING" TEXT }
571 : test-string-encoding ( -- )
572 [ ] [ string-encoding-test ensure-table ] unit-test
575 string-encoding-test new
576 "\u{copyright-sign}\u{bengali-letter-cha}" >>string
577 [ insert-tuple ] [ id>> "id" set ] bi
580 [ "\u{copyright-sign}\u{bengali-letter-cha}" ] [
581 string-encoding-test new "id" get >>id select-tuple string>>
584 [ test-string-encoding ] test-sqlite
585 [ test-string-encoding ] test-postgresql
587 : test-queries ( -- )
588 [ ] [ exam ensure-table ] unit-test
589 [ ] [ 1000 [ random-exam insert-tuple ] times ] unit-test
592 T{ exam { score T{ interval { from { 0 t } } { to { 100 t } } } } }
594 5 >>limit select-tuples length
597 TUPLE: compound-foo a b c ;
599 compound-foo "COMPOUND_FOO"
601 { "a" "A" INTEGER +user-assigned-id+ }
602 { "b" "B" INTEGER +user-assigned-id+ }
606 : test-compound-primary-key ( -- )
607 [ ] [ compound-foo ensure-table ] unit-test
608 [ ] [ compound-foo drop-table ] unit-test
609 [ ] [ compound-foo create-table ] unit-test
610 [ ] [ 1 2 3 compound-foo boa insert-tuple ] unit-test
611 [ 1 2 3 compound-foo boa insert-tuple ] must-fail
612 [ ] [ 2 3 4 compound-foo boa insert-tuple ] unit-test
613 [ T{ compound-foo { a 2 } { b 3 } { c 4 } } ]
614 [ compound-foo new 4 >>c select-tuple ] unit-test ;
616 [ test-compound-primary-key ] test-sqlite
617 [ test-compound-primary-key ] test-postgresql
619 TUPLE: timez id time ;
623 { "id" "ID" +db-assigned-id+ }
624 { "time" "TIME" TIME }
627 : test-time-types ( -- )
629 timez new 3 hours >>time insert-tuple
631 T{ duration f 0 0 0 3 0 0 }
633 timez new 3 hours >>time select-tuple time>>
636 [ test-time-types ] test-sqlite
637 [ test-time-types ] test-postgresql
639 TUPLE: example id data ;
643 { "id" "ID" +db-assigned-id+ }
644 { "data" "DATA" BLOB }
647 : test-blob-select ( -- )
649 [ ] [ example new B{ 1 2 3 4 5 } >>data insert-tuple ] unit-test
651 T{ example { id 1 } { data B{ 1 2 3 4 5 } } }
652 ] [ example new B{ 1 2 3 4 5 } >>data select-tuple ] unit-test ;
654 [ test-blob-select ] test-sqlite
655 [ test-blob-select ] test-postgresql
657 TUPLE: select-me id data ;
659 select-me "select_me"
661 { "id" "ID" +db-assigned-id+ }
662 { "data" "DATA" TEXT }
665 : test-mapping ( -- )
666 [ ] [ select-me recreate-table ] unit-test
667 [ ] [ select-me new insert-tuple ] unit-test
668 [ ] [ select-me new "test2" >>data insert-tuple ] unit-test
671 T{ select-me { id 1 } { data f } }
672 T{ select-me { id 2 } { data "test2" } }
673 ] [ select-me new select-tuples first2 ] unit-test
677 select-me new [ data>> ] collector [ each-tuple ] dip
680 [ { "test" "test2" } ] [
681 select-me new NULL >>data [ "test" >>data ] update-tuples
682 select-me new [ data>> ] collector [ each-tuple ] dip
686 [ { "test1" "test2" } ] [
688 dup data>> "test" = [ "test1" >>data ] [ drop f ] if
690 select-me new [ data>> ] collector [ each-tuple ] dip
695 select-me new [ data>> "test1" = ] reject-tuples
696 select-me new [ data>> ] collector [ each-tuple ] dip
700 [ test-mapping ] test-sqlite
701 [ test-mapping ] test-postgresql