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 ;
10 : test-sqlite ( quot -- )
12 "tuples-test.db" temp-file sqlite-db _ with-db
15 : test-postgresql ( quot -- )
17 { "localhost" "postgres" "foob" "factor-test" }
18 postgresql-db _ with-db
21 TUPLE: person the-id the-name the-number the-real
22 ts date time blob factor-blob url ;
24 : <person> ( name age real ts date time blob factor-blob url -- person )
36 : <user-assigned-person> ( id name age real ts date time blob factor-blob url -- person )
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
53 [ ] [ person1 get insert-tuple ] unit-test
55 [ 1 ] [ person1 get the-id>> ] unit-test
57 [ ] [ person1 get 200 >>the-number drop ] unit-test
59 [ ] [ person1 get update-tuple ] unit-test
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
66 T{ person f 1 "billy" 200 3.14 }
67 T{ person f 2 "johnny" 10 3.14 }
69 ] [ T{ person f f f f 3.14 } select-tuples ] unit-test
72 T{ person f 1 "billy" 200 3.14 }
73 T{ person f 2 "johnny" 10 3.14 }
75 ] [ T{ person f } select-tuples ] unit-test
79 T{ person f 2 "johnny" 10 3.14 }
81 ] [ T{ person f f f 10 3.14 } select-tuples ] unit-test
84 [ ] [ person1 get delete-tuples ] unit-test
85 [ f ] [ T{ person f 1 } select-tuple ] unit-test
87 [ ] [ person3 get insert-tuple ] unit-test
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 }
102 ] [ T{ person f 3 } select-tuple ] unit-test
104 [ ] [ person4 get insert-tuple ] unit-test
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 } }
117 H{ { 1 2 } { 3 4 } { 5 "lol" } }
118 URL" http://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search"
120 ] [ T{ person f 4 } select-tuple ] unit-test
122 [ ] [ person drop-table ] unit-test ;
124 : db-assigned-person-schema ( -- )
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 }
135 { "factor-blob" "FB" FACTOR-BLOB }
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
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
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 ;
151 : user-assigned-person-schema ( -- )
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 }
162 { "factor-blob" "FB" FACTOR-BLOB }
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
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
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 ;
179 TUPLE: paste n summary author channel mode contents timestamp annotations ;
180 TUPLE: annotation n paste-id summary author mode contents ;
182 : db-assigned-paste-schema ( -- )
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 } }
195 annotation "ANNOTATION"
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 ;
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
216 ! [ test-paste-schema ] test-sqlite
217 ! [ test-paste-schema ] test-postgresql
220 : test-repeated-insert
221 [ ] [ person ensure-table ] unit-test
222 [ ] [ person1 get insert-tuple ] unit-test
223 [ person1 get insert-tuple ] must-fail ;
225 TUPLE: serialize-me id data ;
227 : test-serialize ( -- )
228 serialize-me "SERIALIZED"
230 { "id" "ID" +db-assigned-id+ }
231 { "data" "DATA" FACTOR-BLOB }
233 [ serialize-me drop-table ] [ drop ] recover
234 [ ] [ serialize-me create-table ] unit-test
236 [ ] [ T{ serialize-me f f H{ { 1 2 } } } insert-tuple ] unit-test
238 { T{ serialize-me f 1 H{ { 1 2 } } } }
239 ] [ T{ serialize-me f 1 } select-tuples ] unit-test ;
241 TUPLE: exam id name score ;
243 : random-exam ( -- exam )
245 6 [ CHAR: a CHAR: z [a,b] random ] replicate >string
249 : test-intervals ( -- )
253 { "idd" "ID" +db-assigned-id+ }
254 { "named" "NAME" TEXT }
255 { "score" "SCORE" INTEGER }
258 seq>> { "idd" "named" } =
263 { "id" "ID" +db-assigned-id+ }
264 { "name" "NAME" TEXT }
265 { "score" "SCORE" INTEGER }
267 [ exam drop-table ] [ drop ] recover
268 [ ] [ exam create-table ] unit-test
270 [ ] [ T{ exam f f "Kyle" 100 } insert-tuple ] unit-test
271 [ ] [ T{ exam f f "Stan" 80 } insert-tuple ] unit-test
272 [ ] [ T{ exam f f "Kenny" 60 } insert-tuple ] unit-test
273 [ ] [ T{ exam f f "Cartman" 41 } insert-tuple ] unit-test
277 T{ exam f 3 "Kenny" 60 }
278 T{ exam f 4 "Cartman" 41 }
281 T{ exam f f f T{ interval f { 0 t } { 70 t } } } select-tuples
287 T{ exam f T{ interval f { 3 f } { 4 f } } f } select-tuples
291 T{ exam f 4 "Cartman" 41 }
294 T{ exam f T{ interval f { 3 f } { 4 t } } f } select-tuples
298 T{ exam f 3 "Kenny" 60 }
301 T{ exam f T{ interval f { 3 t } { 4 f } } f } select-tuples
305 T{ exam f 3 "Kenny" 60 }
306 T{ exam f 4 "Cartman" 41 }
309 T{ exam f T{ interval f { 3 t } { 4 t } } f } select-tuples
314 T{ exam f 1 "Kyle" 100 }
315 T{ exam f 2 "Stan" 80 }
318 T{ exam f f { "Stan" "Kyle" } } select-tuples
323 T{ exam f 1 "Kyle" 100 }
324 T{ exam f 2 "Stan" 80 }
325 T{ exam f 3 "Kenny" 60 }
328 T{ exam f T{ range f 1 3 1 } } select-tuples
333 T{ exam f 2 "Stan" 80 }
334 T{ exam f 3 "Kenny" 60 }
335 T{ exam f 4 "Cartman" 41 }
338 T{ exam f T{ interval f { 2 t } { 1.0/0.0 f } } } select-tuples
343 T{ exam f 1 "Kyle" 100 }
346 T{ exam f T{ interval f { -1.0/0.0 t } { 2 f } } } select-tuples
351 T{ exam f 1 "Kyle" 100 }
352 T{ exam f 2 "Stan" 80 }
353 T{ exam f 3 "Kenny" 60 }
354 T{ exam f 4 "Cartman" 41 }
357 T{ exam f T{ interval f { -1.0/0.0 t } { 1/0. f } } } select-tuples
362 T{ exam f 1 "Kyle" 100 }
363 T{ exam f 2 "Stan" 80 }
364 T{ exam f 3 "Kenny" 60 }
365 T{ exam f 4 "Cartman" 41 }
368 T{ exam } select-tuples
371 [ 4 ] [ T{ exam } count-tuples ] unit-test ;
373 TUPLE: bignum-test id m n o ;
374 : <bignum-test> ( m n o -- obj )
381 bignum-test "BIGNUM_TEST"
383 { "id" "ID" +db-assigned-id+ }
384 { "m" "M" BIG-INTEGER }
385 { "n" "N" UNSIGNED-BIG-INTEGER }
386 { "o" "O" SIGNED-BIG-INTEGER }
388 [ bignum-test drop-table ] ignore-errors
389 [ ] [ bignum-test ensure-table ] unit-test
390 [ ] [ 63 2^ 1- dup dup <bignum-test> insert-tuple ] unit-test ;
393 ! [ T{ bignum-test f 1
394 ! -9223372036854775808 9223372036854775808 -9223372036854775808 } ]
395 ! [ T{ bignum-test f 1 } select-tuple ] unit-test ;
397 TUPLE: secret n message ;
403 { "n" "ID" +random-id+ system-random-generator }
404 { "message" "MESSAGE" TEXT }
407 [ ] [ secret recreate-table ] unit-test
409 [ t ] [ f "kilroy was here" <secret> [ insert-tuple ] keep n>> integer? ] unit-test
411 [ ] [ f "kilroy was here2" <secret> insert-tuple ] unit-test
413 [ ] [ f "kilroy was here3" <secret> insert-tuple ] unit-test
416 T{ secret } select-tuples
417 first message>> "kilroy was here" head?
421 T{ secret } select-tuples length 3 =
424 [ db-assigned-person-schema test-tuples ] test-sqlite
425 [ user-assigned-person-schema test-tuples ] test-sqlite
426 [ user-assigned-person-schema test-repeated-insert ] test-sqlite
427 [ test-bignum ] test-sqlite
428 [ test-serialize ] test-sqlite
429 [ test-intervals ] test-sqlite
430 [ test-random-id ] test-sqlite
432 [ db-assigned-person-schema test-tuples ] test-postgresql
433 [ user-assigned-person-schema test-tuples ] test-postgresql
434 [ user-assigned-person-schema test-repeated-insert ] test-postgresql
435 [ test-bignum ] test-postgresql
436 [ test-serialize ] test-postgresql
437 [ test-intervals ] test-postgresql
438 [ test-random-id ] test-postgresql
440 TUPLE: does-not-persist ;
443 [ does-not-persist create-sql-statement ]
444 [ class \ not-persistent = ] must-fail-with
448 [ does-not-persist create-sql-statement ]
449 [ class \ not-persistent = ] must-fail-with
453 TUPLE: suparclass id a ;
456 { "id" "ID" +db-assigned-id+ }
460 TUPLE: subbclass < suparclass b ;
462 subbclass "SUBCLASS" {
466 TUPLE: fubbclass < subbclass ;
468 fubbclass "FUBCLASS" { } define-persistent
470 : test-db-inheritance ( -- )
471 [ ] [ subbclass ensure-table ] unit-test
472 [ ] [ fubbclass ensure-table ] unit-test
475 subbclass new 5 >>a "hi" >>b dup insert-tuple id>> "id" set
479 subbclass new "id" get >>id select-tuple
480 [ subbclass? ] [ b>> ] [ a>> ] tri
483 [ ] [ fubbclass new 0 >>a "hi" >>b insert-tuple ] unit-test
485 [ t ] [ fubbclass new select-tuples [ fubbclass? ] all? ] unit-test ;
487 [ test-db-inheritance ] test-sqlite
488 [ test-db-inheritance ] test-postgresql
491 TUPLE: string-encoding-test id string ;
493 string-encoding-test "STRING_ENCODING_TEST" {
494 { "id" "ID" +db-assigned-id+ }
495 { "string" "STRING" TEXT }
498 : test-string-encoding ( -- )
499 [ ] [ string-encoding-test ensure-table ] unit-test
502 string-encoding-test new
503 "\u{copyright-sign}\u{bengali-letter-cha}" >>string
504 [ insert-tuple ] [ id>> "id" set ] bi
507 [ "\u{copyright-sign}\u{bengali-letter-cha}" ] [
508 string-encoding-test new "id" get >>id select-tuple string>>
511 [ test-string-encoding ] test-sqlite
512 [ test-string-encoding ] test-postgresql
514 ! Don't comment these out. These words must infer
515 \ bind-tuple must-infer
516 \ insert-tuple must-infer
517 \ update-tuple must-infer
518 \ delete-tuples must-infer
519 \ select-tuple must-infer
520 \ define-persistent must-infer
521 \ ensure-table must-infer
522 \ create-table must-infer
523 \ drop-table must-infer
525 : test-queries ( -- )
526 [ ] [ exam ensure-table ] unit-test
527 [ ] [ 1000 [ random-exam insert-tuple ] times ] unit-test
530 T{ exam { score T{ interval { from { 0 t } } { to { 100 t } } } } }
532 5 >>limit select-tuples length
535 TUPLE: compound-foo a b c ;
537 compound-foo "COMPOUND_FOO"
539 { "a" "A" INTEGER +user-assigned-id+ }
540 { "b" "B" INTEGER +user-assigned-id+ }
544 : test-compound-primary-key ( -- )
545 [ ] [ compound-foo ensure-table ] unit-test
546 [ ] [ compound-foo drop-table ] unit-test
547 [ ] [ compound-foo create-table ] unit-test
548 [ ] [ 1 2 3 compound-foo boa insert-tuple ] unit-test
549 [ 1 2 3 compound-foo boa insert-tuple ] must-fail
550 [ ] [ 2 3 4 compound-foo boa insert-tuple ] unit-test
551 [ T{ compound-foo { a 2 } { b 3 } { c 4 } } ]
552 [ compound-foo new 4 >>c select-tuple ] unit-test ;
554 [ test-compound-primary-key ] test-sqlite
555 [ test-compound-primary-key ] test-postgresql
558 "tuples-test.db" temp-file sqlite-db make-db db-open db set ;