factor/basis/db/tuples/tuples-tests.factor

502 lines
14 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: io.files kernel tools.test db db.tuples classes
2008-04-18 13:43:21 -04:00
db.types continuations namespaces math math.ranges
prettyprint calendar sequences db.sqlite math.intervals
2008-05-30 20:09:37 -04:00
db.postgresql accessors random math.bitfields.lib
math.ranges strings sequences.lib urls fry ;
2008-03-01 17:00:45 -05:00
IN: db.tuples.tests
2008-03-11 01:09:49 -04:00
TUPLE: person the-id the-name the-number the-real
ts date time blob factor-blob url ;
: <person> ( name age real ts date time blob factor-blob url -- person )
person new
swap >>url
swap >>factor-blob
swap >>blob
swap >>time
swap >>date
swap >>ts
swap >>the-real
swap >>the-number
swap >>the-name ;
: <user-assigned-person> ( id name age real ts date time blob factor-blob url -- person )
<person>
swap >>the-id ;
2008-03-05 20:08:33 -05:00
SYMBOL: person1
SYMBOL: person2
SYMBOL: person3
SYMBOL: person4
2008-02-11 14:39:43 -05:00
: test-tuples ( -- )
2008-04-28 19:41:53 -04:00
[ ] [ person recreate-table ] unit-test
2008-03-13 00:57:56 -04:00
[ ] [ person ensure-table ] unit-test
[ ] [ person drop-table ] unit-test
[ ] [ person create-table ] unit-test
[ person create-table ] must-fail
2008-03-13 00:57:56 -04:00
[ ] [ person ensure-table ] unit-test
2008-02-11 14:39:43 -05:00
2008-03-05 20:08:33 -05:00
[ ] [ person1 get insert-tuple ] unit-test
2008-09-02 16:08:23 -04:00
[ 1 ] [ person1 get the-id>> ] unit-test
2008-09-02 16:08:23 -04:00
[ ] [ person1 get 200 >>the-number drop ] unit-test
2008-03-05 20:08:33 -05:00
[ ] [ person1 get update-tuple ] unit-test
[ T{ person f 1 "billy" 200 3.14 } ]
[ T{ person f 1 } select-tuple ] unit-test
2008-03-05 20:08:33 -05:00
[ ] [ person2 get insert-tuple ] unit-test
[
{
T{ person f 1 "billy" 200 3.14 }
T{ person f 2 "johnny" 10 3.14 }
}
] [ T{ person f f f f 3.14 } select-tuples ] unit-test
[
{
T{ person f 1 "billy" 200 3.14 }
T{ person f 2 "johnny" 10 3.14 }
}
] [ T{ person f } select-tuples ] unit-test
2008-03-05 21:56:40 -05:00
[
{
T{ person f 2 "johnny" 10 3.14 }
}
] [ T{ person f f f 10 3.14 } select-tuples ] unit-test
2008-04-29 22:03:01 -04:00
[ ] [ person1 get delete-tuples ] unit-test
[ f ] [ T{ person f 1 } select-tuple ] unit-test
2008-03-05 20:08:33 -05:00
[ ] [ person3 get insert-tuple ] unit-test
[
T{
person
f
3
"teddy"
10
3.14
2008-04-23 01:07:26 -04:00
T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
2008-08-30 11:12:57 -04:00
T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
B{ 115 116 111 114 101 105 110 97 98 108 111 98 }
}
2008-03-05 20:08:33 -05:00
] [ T{ person f 3 } select-tuple ] unit-test
2008-03-11 01:09:49 -04:00
[ ] [ person4 get insert-tuple ] unit-test
[
T{
person
f
4
"eddie"
10
3.14
2008-04-23 01:07:26 -04:00
T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
2008-08-30 11:12:57 -04:00
T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
2008-03-11 01:09:49 -04:00
f
H{ { 1 2 } { 3 4 } { 5 "lol" } }
2008-06-12 19:23:46 -04:00
URL" http://www.google.com/search?hl=en&q=trailer+park+boys&btnG=Google+Search"
2008-03-11 01:09:49 -04:00
}
] [ T{ person f 4 } select-tuple ] unit-test
[ ] [ person drop-table ] unit-test ;
: db-assigned-person-schema ( -- )
person "PERSON"
{
{ "the-id" "ID" +db-assigned-id+ }
{ "the-name" "NAME" { VARCHAR 256 } +not-null+ }
{ "the-number" "AGE" INTEGER { +default+ 0 } }
{ "the-real" "REAL" DOUBLE { +default+ 0.3 } }
2008-03-05 20:08:33 -05:00
{ "ts" "TS" TIMESTAMP }
{ "date" "D" DATE }
{ "time" "T" TIME }
{ "blob" "B" BLOB }
2008-03-11 01:09:49 -04:00
{ "factor-blob" "FB" FACTOR-BLOB }
{ "url" "U" URL }
} define-persistent
"billy" 10 3.14 f f f f f f <person> person1 set
"johnny" 10 3.14 f f f f f f <person> person2 set
2008-04-23 23:23:22 -04:00
"teddy" 10 3.14
T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
2008-08-30 11:12:57 -04:00
T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
B{ 115 116 111 114 101 105 110 97 98 108 111 98 } f f <person> person3 set
2008-04-23 23:23:22 -04:00
"eddie" 10 3.14
T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
2008-08-30 11:12:57 -04:00
T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
2008-06-12 19:23:46 -04:00
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 ;
: user-assigned-person-schema ( -- )
person "PERSON"
{
{ "the-id" "ID" INTEGER +user-assigned-id+ }
{ "the-name" "NAME" { VARCHAR 256 } +not-null+ }
{ "the-number" "AGE" INTEGER { +default+ 0 } }
{ "the-real" "REAL" DOUBLE { +default+ 0.3 } }
2008-03-05 20:08:33 -05:00
{ "ts" "TS" TIMESTAMP }
{ "date" "D" DATE }
{ "time" "T" TIME }
{ "blob" "B" BLOB }
2008-03-11 01:09:49 -04:00
{ "factor-blob" "FB" FACTOR-BLOB }
{ "url" "U" URL }
} define-persistent
1 "billy" 10 3.14 f f f f f f <user-assigned-person> person1 set
2 "johnny" 10 3.14 f f f f f f <user-assigned-person> person2 set
2008-04-23 23:23:22 -04:00
3 "teddy" 10 3.14
T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
2008-08-30 11:12:57 -04:00
T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
2008-04-23 23:23:22 -04:00
B{ 115 116 111 114 101 105 110 97 98 108 111 98 }
f f <user-assigned-person> person3 set
2008-04-23 23:23:22 -04:00
4 "eddie" 10 3.14
T{ timestamp f 2008 3 5 16 24 11 T{ duration f 0 0 0 0 0 0 } }
T{ timestamp f 2008 11 22 0 0 0 T{ duration f 0 0 0 0 0 0 } }
2008-08-30 11:12:57 -04:00
T{ timestamp f 0 0 0 12 34 56 T{ duration f 0 0 0 0 0 0 } }
2008-06-12 19:23:46 -04:00
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 ;
TUPLE: paste n summary author channel mode contents timestamp annotations ;
TUPLE: annotation n paste-id summary author mode contents ;
: db-assigned-paste-schema ( -- )
paste "PASTE"
{
{ "n" "ID" +db-assigned-id+ }
{ "summary" "SUMMARY" TEXT }
{ "author" "AUTHOR" TEXT }
{ "channel" "CHANNEL" TEXT }
{ "mode" "MODE" TEXT }
{ "contents" "CONTENTS" TEXT }
{ "date" "DATE" TIMESTAMP }
{ "annotations" { +has-many+ annotation } }
} define-persistent
annotation "ANNOTATION"
{
{ "n" "ID" +db-assigned-id+ }
{ "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "n" } }
{ "summary" "SUMMARY" TEXT }
{ "author" "AUTHOR" TEXT }
{ "mode" "MODE" TEXT }
{ "contents" "CONTENTS" TEXT }
} define-persistent ;
! { "localhost" "postgres" "" "factor-test" } postgresql-db [
! [ paste drop-table ] [ drop ] recover
! [ annotation drop-table ] [ drop ] recover
! [ paste drop-table ] [ drop ] recover
! [ annotation drop-table ] [ drop ] recover
! [ ] [ paste create-table ] unit-test
! [ ] [ annotation create-table ] unit-test
! ] with-db
: test-sqlite ( quot -- )
[ ] swap '[ "tuples-test.db" temp-file sqlite-db , with-db ] unit-test ;
2008-06-01 01:02:24 -04:00
: test-postgresql ( quot -- )
[ ] swap '[ { "localhost" "postgres" "foob" "factor-test" } postgresql-db , with-db ] unit-test ;
: test-repeated-insert
[ ] [ person ensure-table ] unit-test
[ ] [ person1 get insert-tuple ] unit-test
[ person1 get insert-tuple ] must-fail ;
2008-03-05 20:08:33 -05:00
TUPLE: serialize-me id data ;
2008-03-05 21:56:40 -05:00
: test-serialize ( -- )
2008-03-05 20:08:33 -05:00
serialize-me "SERIALIZED"
{
{ "id" "ID" +db-assigned-id+ }
2008-03-05 20:08:33 -05:00
{ "data" "DATA" FACTOR-BLOB }
} define-persistent
[ serialize-me drop-table ] [ drop ] recover
[ ] [ serialize-me create-table ] unit-test
[ ] [ T{ serialize-me f f H{ { 1 2 } } } insert-tuple ] unit-test
[
{ T{ serialize-me f 1 H{ { 1 2 } } } }
2008-03-05 21:56:40 -05:00
] [ T{ serialize-me f 1 } select-tuples ] unit-test ;
TUPLE: exam id name score ;
2008-05-30 20:09:37 -04:00
: random-exam ( -- exam )
f
2008-06-07 11:48:05 -04:00
6 [ CHAR: a CHAR: z [a,b] random ] replicate >string
2008-05-30 20:09:37 -04:00
100 random
exam boa ;
2008-04-18 13:43:21 -04:00
: test-intervals ( -- )
2008-03-05 21:56:40 -05:00
exam "EXAM"
{
{ "id" "ID" +db-assigned-id+ }
2008-03-05 21:56:40 -05:00
{ "name" "NAME" TEXT }
{ "score" "SCORE" INTEGER }
} define-persistent
[ exam drop-table ] [ drop ] recover
[ ] [ exam create-table ] unit-test
[ ] [ T{ exam f f "Kyle" 100 } insert-tuple ] unit-test
[ ] [ T{ exam f f "Stan" 80 } insert-tuple ] unit-test
[ ] [ T{ exam f f "Kenny" 60 } insert-tuple ] unit-test
[ ] [ T{ exam f f "Cartman" 41 } insert-tuple ] unit-test
[
2008-04-17 20:43:07 -04:00
{
T{ exam f 3 "Kenny" 60 }
T{ exam f 4 "Cartman" 41 }
}
] [
T{ exam f f f T{ interval f { 0 t } { 70 t } } } select-tuples
] unit-test
[
{ }
] [
T{ exam f T{ interval f { 3 f } { 4 f } } f } select-tuples
] unit-test
[
{
T{ exam f 4 "Cartman" 41 }
}
] [
T{ exam f T{ interval f { 3 f } { 4 t } } f } select-tuples
] unit-test
[
{
T{ exam f 3 "Kenny" 60 }
}
] [
T{ exam f T{ interval f { 3 t } { 4 f } } f } select-tuples
] unit-test
[
{
T{ exam f 3 "Kenny" 60 }
T{ exam f 4 "Cartman" 41 }
}
] [
T{ exam f T{ interval f { 3 t } { 4 t } } f } select-tuples
2008-04-18 13:43:21 -04:00
] unit-test
[
{
T{ exam f 1 "Kyle" 100 }
T{ exam f 2 "Stan" 80 }
}
] [
T{ exam f f { "Stan" "Kyle" } } select-tuples
] unit-test
[
{
T{ exam f 1 "Kyle" 100 }
T{ exam f 2 "Stan" 80 }
T{ exam f 3 "Kenny" 60 }
}
] [
T{ exam f T{ range f 1 3 1 } } select-tuples
2008-04-28 20:41:35 -04:00
] unit-test
[
{
T{ exam f 2 "Stan" 80 }
T{ exam f 3 "Kenny" 60 }
T{ exam f 4 "Cartman" 41 }
}
] [
T{ exam f T{ interval f { 2 t } { 1.0/0.0 f } } } select-tuples
] unit-test
[
{
T{ exam f 1 "Kyle" 100 }
}
] [
T{ exam f T{ interval f { -1.0/0.0 t } { 2 f } } } select-tuples
] unit-test
[
{
T{ exam f 1 "Kyle" 100 }
T{ exam f 2 "Stan" 80 }
T{ exam f 3 "Kenny" 60 }
T{ exam f 4 "Cartman" 41 }
}
] [
T{ exam f T{ interval f { -1.0/0.0 t } { 1/0. f } } } select-tuples
2008-04-28 21:01:32 -04:00
] unit-test
[
{
T{ exam f 1 "Kyle" 100 }
T{ exam f 2 "Stan" 80 }
T{ exam f 3 "Kenny" 60 }
T{ exam f 4 "Cartman" 41 }
}
] [
T{ exam } select-tuples
2008-06-07 11:48:05 -04:00
] unit-test
2008-06-12 19:04:01 -04:00
[ 4 ] [ T{ exam } f count-tuples ] unit-test ;
TUPLE: bignum-test id m n o ;
: <bignum-test> ( m n o -- obj )
bignum-test new
swap >>o
swap >>n
swap >>m ;
: test-bignum
bignum-test "BIGNUM_TEST"
{
{ "id" "ID" +db-assigned-id+ }
{ "m" "M" BIG-INTEGER }
{ "n" "N" UNSIGNED-BIG-INTEGER }
{ "o" "O" SIGNED-BIG-INTEGER }
} define-persistent
[ bignum-test drop-table ] ignore-errors
[ ] [ bignum-test ensure-table ] unit-test
2008-04-20 17:57:50 -04:00
[ ] [ 63 2^ 1- dup dup <bignum-test> insert-tuple ] unit-test ;
2008-04-20 17:57:50 -04:00
! sqlite only
! [ T{ bignum-test f 1
! -9223372036854775808 9223372036854775808 -9223372036854775808 } ]
! [ T{ bignum-test f 1 } select-tuple ] unit-test ;
2008-03-17 15:14:04 -04:00
TUPLE: secret n message ;
C: <secret> secret
: test-random-id
secret "SECRET"
{
{ "n" "ID" +random-id+ system-random-generator }
2008-03-17 15:14:04 -04:00
{ "message" "MESSAGE" TEXT }
} define-persistent
[ ] [ secret recreate-table ] unit-test
2008-04-28 18:17:19 -04:00
[ t ] [ f "kilroy was here" <secret> [ insert-tuple ] keep n>> integer? ] unit-test
[ ] [ f "kilroy was here2" <secret> insert-tuple ] unit-test
[ ] [ f "kilroy was here3" <secret> insert-tuple ] unit-test
[ t ] [
T{ secret } select-tuples
first message>> "kilroy was here" head?
] unit-test
[ t ] [
T{ secret } select-tuples length 3 =
] unit-test ;
2008-03-17 15:14:04 -04:00
[ db-assigned-person-schema test-tuples ] test-sqlite
[ user-assigned-person-schema test-tuples ] test-sqlite
[ user-assigned-person-schema test-repeated-insert ] test-sqlite
2008-04-20 01:20:21 -04:00
[ test-bignum ] test-sqlite
[ test-serialize ] test-sqlite
[ test-intervals ] test-sqlite
[ test-random-id ] test-sqlite
2008-04-20 00:18:12 -04:00
[ db-assigned-person-schema test-tuples ] test-postgresql
[ user-assigned-person-schema test-tuples ] test-postgresql
[ user-assigned-person-schema test-repeated-insert ] test-postgresql
2008-04-20 17:57:50 -04:00
[ test-bignum ] test-postgresql
2008-04-20 01:20:21 -04:00
[ test-serialize ] test-postgresql
[ test-intervals ] test-postgresql
2008-04-21 14:11:19 -04:00
[ test-random-id ] test-postgresql
2008-04-20 01:20:21 -04:00
TUPLE: does-not-persist ;
2008-04-21 14:11:19 -04:00
[
[ does-not-persist create-sql-statement ]
[ class \ not-persistent = ] must-fail-with
] test-sqlite
2008-04-20 01:20:21 -04:00
[
[ does-not-persist create-sql-statement ]
[ class \ not-persistent = ] must-fail-with
] test-postgresql
2008-03-17 15:14:04 -04:00
2008-05-28 18:02:58 -04:00
2008-05-30 17:13:47 -04:00
TUPLE: suparclass id a ;
2008-05-28 18:02:58 -04:00
suparclass f {
{ "id" "ID" +db-assigned-id+ }
{ "a" "A" INTEGER }
} define-persistent
TUPLE: subbclass < suparclass b ;
subbclass "SUBCLASS" {
{ "b" "B" TEXT }
} define-persistent
2008-05-30 20:05:55 -04:00
TUPLE: fubbclass < subbclass ;
fubbclass "FUBCLASS" { } define-persistent
2008-05-28 18:02:58 -04:00
: test-db-inheritance ( -- )
2008-05-30 17:13:47 -04:00
[ ] [ subbclass ensure-table ] unit-test
2008-05-30 20:05:55 -04:00
[ ] [ fubbclass ensure-table ] unit-test
2008-05-30 17:13:47 -04:00
[ ] [
subbclass new 5 >>a "hi" >>b dup insert-tuple id>> "id" set
] unit-test
[ t "hi" 5 ] [
subbclass new "id" get >>id select-tuple
[ subbclass? ] [ b>> ] [ a>> ] tri
2008-05-30 20:05:55 -04:00
] unit-test
[ ] [ fubbclass new 0 >>a "hi" >>b insert-tuple ] unit-test
[ t ] [ fubbclass new select-tuples [ fubbclass? ] all? ] unit-test ;
2008-05-28 18:02:58 -04:00
[ test-db-inheritance ] test-sqlite
[ test-db-inheritance ] test-postgresql
TUPLE: string-encoding-test id string ;
string-encoding-test "STRING_ENCODING_TEST" {
{ "id" "ID" +db-assigned-id+ }
{ "string" "STRING" TEXT }
} define-persistent
: test-string-encoding ( -- )
[ ] [ string-encoding-test ensure-table ] unit-test
[ ] [
string-encoding-test new
"\u{copyright-sign}\u{bengali-letter-cha}" >>string
[ insert-tuple ] [ id>> "id" set ] bi
] unit-test
[ "\u{copyright-sign}\u{bengali-letter-cha}" ] [
string-encoding-test new "id" get >>id select-tuple string>>
] unit-test ;
[ test-string-encoding ] test-sqlite
[ test-string-encoding ] test-postgresql
2008-05-28 18:02:58 -04:00
2008-04-21 05:42:34 -04:00
! Don't comment these out. These words must infer
\ bind-tuple must-infer
\ insert-tuple must-infer
\ update-tuple must-infer
2008-04-29 22:03:01 -04:00
\ delete-tuples must-infer
2008-04-21 05:42:34 -04:00
\ select-tuple must-infer
\ define-persistent must-infer
2008-04-23 20:40:17 -04:00
\ ensure-table must-infer
\ create-table must-infer
\ drop-table must-infer