2008-02-12 18:10:56 -05:00
|
|
|
! Copyright (C) 2008 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-12-15 04:33:04 -05:00
|
|
|
USING: io.files io.files.temp kernel tools.test db db.tuples classes
|
2008-04-18 13:43:21 -04:00
|
|
|
db.types continuations namespaces math math.ranges
|
2008-04-28 19:21:45 -04:00
|
|
|
prettyprint calendar sequences db.sqlite math.intervals
|
2008-11-24 13:57:26 -05:00
|
|
|
db.postgresql accessors random math.bitwise system
|
2008-12-17 22:04:17 -05:00
|
|
|
math.ranges strings urls fry db.tuples.private db.private ;
|
2008-03-01 17:00:45 -05:00
|
|
|
IN: db.tuples.tests
|
2008-02-11 00:11:16 -05:00
|
|
|
|
2008-10-02 13:11:56 -04:00
|
|
|
: sqlite-db ( -- sqlite-db )
|
|
|
|
"tuples-test.db" temp-file <sqlite-db> ;
|
|
|
|
|
2008-09-27 15:25:36 -04:00
|
|
|
: test-sqlite ( quot -- )
|
2008-10-02 13:11:56 -04:00
|
|
|
'[
|
|
|
|
[ ] [
|
|
|
|
"tuples-test.db" temp-file <sqlite-db> _ with-db
|
|
|
|
] unit-test
|
|
|
|
] call ; inline
|
|
|
|
|
|
|
|
: postgresql-db ( -- postgresql-db )
|
|
|
|
<postgresql-db>
|
|
|
|
"localhost" >>host
|
|
|
|
"postgres" >>username
|
|
|
|
"thepasswordistrust" >>password
|
|
|
|
"factor-test" >>database ;
|
2008-09-27 15:25:36 -04:00
|
|
|
|
|
|
|
: test-postgresql ( quot -- )
|
2008-10-02 13:11:56 -04:00
|
|
|
'[
|
2008-11-24 13:57:26 -05:00
|
|
|
os windows? cpu x86.64? and [
|
|
|
|
[ ] [ postgresql-db _ with-db ] unit-test
|
|
|
|
] unless
|
2008-10-02 13:11:56 -04:00
|
|
|
] call ; inline
|
|
|
|
|
|
|
|
! These words leak resources, but are useful for interactivel testing
|
|
|
|
: sqlite-test-db ( -- )
|
2008-12-17 22:04:17 -05:00
|
|
|
sqlite-db db-open db-connection set ;
|
2008-10-02 13:11:56 -04:00
|
|
|
|
|
|
|
: postgresql-test-db ( -- )
|
2008-12-17 22:04:17 -05:00
|
|
|
postgresql-db db-open db-connection set ;
|
2008-09-27 15:25:36 -04:00
|
|
|
|
2008-03-11 01:09:49 -04:00
|
|
|
TUPLE: person the-id the-name the-number the-real
|
2008-06-12 19:20:15 -04:00
|
|
|
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-02-11 00:11:16 -05:00
|
|
|
|
2008-03-05 20:08:33 -05:00
|
|
|
SYMBOL: person1
|
|
|
|
SYMBOL: person2
|
|
|
|
SYMBOL: person3
|
|
|
|
SYMBOL: person4
|
2008-02-11 14:39:43 -05:00
|
|
|
|
2008-02-11 00:11:16 -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
|
2008-02-12 18:10:56 -05:00
|
|
|
[ ] [ person create-table ] unit-test
|
2008-02-27 19:28:32 -05:00
|
|
|
[ 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-02-11 00:11:16 -05:00
|
|
|
|
2008-09-02 16:08:23 -04:00
|
|
|
[ 1 ] [ person1 get the-id>> ] unit-test
|
2008-02-11 00:11:16 -05:00
|
|
|
|
2008-09-02 16:08:23 -04:00
|
|
|
[ ] [ person1 get 200 >>the-number drop ] unit-test
|
2008-02-11 00:11:16 -05:00
|
|
|
|
2008-03-05 20:08:33 -05:00
|
|
|
[ ] [ person1 get update-tuple ] unit-test
|
2008-02-11 00:11:16 -05:00
|
|
|
|
2008-02-24 13:32:36 -05:00
|
|
|
[ 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
|
2008-02-25 16:31:07 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
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
|
2008-03-03 09:56:06 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
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-02-21 16:57:18 -05:00
|
|
|
|
2008-04-29 22:03:01 -04:00
|
|
|
[ ] [ person1 get delete-tuples ] unit-test
|
2008-02-25 16:13:00 -05:00
|
|
|
[ f ] [ T{ person f 1 } select-tuple ] unit-test
|
2008-03-05 20:08:33 -05:00
|
|
|
|
|
|
|
[ ] [ person3 get insert-tuple ] unit-test
|
|
|
|
|
|
|
|
[
|
2008-03-05 20:59:29 -05:00
|
|
|
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 } }
|
2008-03-05 20:59:29 -05:00
|
|
|
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
|
|
|
|
|
2008-02-25 16:13:00 -05:00
|
|
|
[ ] [ person drop-table ] unit-test ;
|
2008-02-11 00:11:16 -05:00
|
|
|
|
2008-04-28 17:48:55 -04:00
|
|
|
: db-assigned-person-schema ( -- )
|
2008-03-03 09:56:06 -05:00
|
|
|
person "PERSON"
|
|
|
|
{
|
2008-04-28 17:48:55 -04:00
|
|
|
{ "the-id" "ID" +db-assigned-id+ }
|
2008-03-03 09:56:06 -05:00
|
|
|
{ "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 }
|
2008-06-12 19:20:15 -04:00
|
|
|
{ "url" "U" URL }
|
2008-03-03 09:56:06 -05:00
|
|
|
} define-persistent
|
2008-06-12 19:20:15 -04:00
|
|
|
"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 } }
|
2008-06-12 19:20:15 -04:00
|
|
|
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 ;
|
2008-03-03 09:56:06 -05:00
|
|
|
|
2008-04-28 17:48:55 -04:00
|
|
|
: user-assigned-person-schema ( -- )
|
2008-03-03 09:56:06 -05:00
|
|
|
person "PERSON"
|
|
|
|
{
|
2008-04-28 17:48:55 -04:00
|
|
|
{ "the-id" "ID" INTEGER +user-assigned-id+ }
|
2008-03-03 09:56:06 -05:00
|
|
|
{ "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 }
|
2008-06-12 19:20:15 -04:00
|
|
|
{ "url" "U" URL }
|
2008-03-03 09:56:06 -05:00
|
|
|
} define-persistent
|
2008-06-12 19:20:15 -04:00
|
|
|
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 }
|
2008-06-12 19:20:15 -04:00
|
|
|
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 ;
|
2008-02-18 17:52:00 -05:00
|
|
|
|
2008-09-30 00:43:34 -04:00
|
|
|
|
2008-02-18 17:52:00 -05:00
|
|
|
TUPLE: paste n summary author channel mode contents timestamp annotations ;
|
|
|
|
TUPLE: annotation n paste-id summary author mode contents ;
|
|
|
|
|
2008-09-30 00:43:34 -04:00
|
|
|
paste "PASTE"
|
|
|
|
{
|
|
|
|
{ "n" "ID" +db-assigned-id+ }
|
|
|
|
{ "summary" "SUMMARY" TEXT }
|
|
|
|
{ "author" "AUTHOR" TEXT }
|
|
|
|
{ "channel" "CHANNEL" TEXT }
|
|
|
|
{ "mode" "MODE" TEXT }
|
|
|
|
{ "contents" "CONTENTS" TEXT }
|
|
|
|
{ "timestamp" "DATE" TIMESTAMP }
|
|
|
|
{ "annotations" { +has-many+ annotation } }
|
|
|
|
} define-persistent
|
|
|
|
|
|
|
|
: annotation-schema-foreign-key ( -- )
|
|
|
|
annotation "ANNOTATION"
|
2008-03-03 09:56:06 -05:00
|
|
|
{
|
2008-04-28 17:48:55 -04:00
|
|
|
{ "n" "ID" +db-assigned-id+ }
|
2008-09-30 00:43:34 -04:00
|
|
|
{ "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "ID" } }
|
2008-03-03 09:56:06 -05:00
|
|
|
{ "summary" "SUMMARY" TEXT }
|
|
|
|
{ "author" "AUTHOR" TEXT }
|
|
|
|
{ "mode" "MODE" TEXT }
|
|
|
|
{ "contents" "CONTENTS" TEXT }
|
2008-09-30 00:43:34 -04:00
|
|
|
} define-persistent ;
|
|
|
|
|
|
|
|
: annotation-schema-foreign-key-not-null ( -- )
|
|
|
|
annotation "ANNOTATION"
|
|
|
|
{
|
|
|
|
{ "n" "ID" +db-assigned-id+ }
|
|
|
|
{ "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "ID" } +not-null+ }
|
|
|
|
{ "summary" "SUMMARY" TEXT }
|
|
|
|
{ "author" "AUTHOR" TEXT }
|
|
|
|
{ "mode" "MODE" TEXT }
|
|
|
|
{ "contents" "CONTENTS" TEXT }
|
|
|
|
} define-persistent ;
|
2008-03-03 09:56:06 -05:00
|
|
|
|
2008-09-30 00:43:34 -04:00
|
|
|
: annotation-schema-cascade ( -- )
|
2008-03-03 09:56:06 -05:00
|
|
|
annotation "ANNOTATION"
|
|
|
|
{
|
2008-04-28 17:48:55 -04:00
|
|
|
{ "n" "ID" +db-assigned-id+ }
|
2008-09-30 00:43:34 -04:00
|
|
|
{ "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "ID" }
|
2008-09-27 17:26:21 -04:00
|
|
|
+on-delete+ +cascade+ }
|
2008-03-03 09:56:06 -05:00
|
|
|
{ "summary" "SUMMARY" TEXT }
|
|
|
|
{ "author" "AUTHOR" TEXT }
|
|
|
|
{ "mode" "MODE" TEXT }
|
|
|
|
{ "contents" "CONTENTS" TEXT }
|
|
|
|
} define-persistent ;
|
2008-02-18 17:52:00 -05:00
|
|
|
|
2008-09-30 00:43:34 -04:00
|
|
|
: annotation-schema-restrict ( -- )
|
|
|
|
annotation "ANNOTATION"
|
|
|
|
{
|
|
|
|
{ "n" "ID" +db-assigned-id+ }
|
|
|
|
{ "paste-id" "PASTE_ID" INTEGER { +foreign-id+ paste "ID" } }
|
|
|
|
{ "summary" "SUMMARY" TEXT }
|
|
|
|
{ "author" "AUTHOR" TEXT }
|
|
|
|
{ "mode" "MODE" TEXT }
|
|
|
|
{ "contents" "CONTENTS" TEXT }
|
|
|
|
} define-persistent ;
|
|
|
|
|
2008-09-27 15:25:36 -04:00
|
|
|
: test-paste-schema ( -- )
|
|
|
|
[ ] [ paste ensure-table ] unit-test
|
|
|
|
[ ] [ annotation ensure-table ] unit-test
|
|
|
|
[ ] [ annotation drop-table ] unit-test
|
|
|
|
[ ] [ paste drop-table ] unit-test
|
|
|
|
[ ] [ paste create-table ] unit-test
|
|
|
|
[ ] [ annotation create-table ] unit-test
|
2008-09-27 16:56:43 -04:00
|
|
|
|
|
|
|
[ ] [
|
|
|
|
paste new
|
|
|
|
"summary1" >>summary
|
|
|
|
"erg" >>author
|
|
|
|
"#lol" >>channel
|
|
|
|
"contents1" >>contents
|
|
|
|
now >>timestamp
|
|
|
|
insert-tuple
|
|
|
|
] unit-test
|
|
|
|
|
|
|
|
[ ] [
|
|
|
|
annotation new
|
|
|
|
1 >>paste-id
|
|
|
|
"annotation1" >>summary
|
|
|
|
"erg" >>author
|
|
|
|
"annotation contents" >>contents
|
|
|
|
insert-tuple
|
2008-09-30 00:43:34 -04:00
|
|
|
] unit-test ;
|
2008-09-27 15:25:36 -04:00
|
|
|
|
2008-09-30 00:43:34 -04:00
|
|
|
: test-foreign-key ( -- )
|
|
|
|
[ ] [ annotation-schema-foreign-key ] unit-test
|
|
|
|
test-paste-schema
|
|
|
|
[ paste new 1 >>n delete-tuples ] must-fail ;
|
|
|
|
|
|
|
|
: test-foreign-key-not-null ( -- )
|
|
|
|
[ ] [ annotation-schema-foreign-key-not-null ] unit-test
|
|
|
|
test-paste-schema
|
|
|
|
[ paste new 1 >>n delete-tuples ] must-fail ;
|
|
|
|
|
|
|
|
: test-cascade ( -- )
|
|
|
|
[ ] [ annotation-schema-cascade ] unit-test
|
|
|
|
test-paste-schema
|
|
|
|
[ ] [ paste new 1 >>n delete-tuples ] unit-test
|
|
|
|
[ 0 ] [ paste new select-tuples length ] unit-test ;
|
|
|
|
|
|
|
|
: test-restrict ( -- )
|
|
|
|
[ ] [ annotation-schema-restrict ] unit-test
|
|
|
|
test-paste-schema
|
|
|
|
[ paste new 1 >>n delete-tuples ] must-fail ;
|
|
|
|
|
|
|
|
[ test-foreign-key ] test-sqlite
|
|
|
|
[ test-foreign-key-not-null ] test-sqlite
|
|
|
|
[ test-cascade ] test-sqlite
|
|
|
|
[ test-restrict ] test-sqlite
|
|
|
|
|
|
|
|
[ test-foreign-key ] test-postgresql
|
|
|
|
[ test-foreign-key-not-null ] test-postgresql
|
|
|
|
[ test-cascade ] test-postgresql
|
|
|
|
[ test-restrict ] test-postgresql
|
2008-03-03 09:56:06 -05:00
|
|
|
|
2008-03-16 00:21:53 -04:00
|
|
|
: 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"
|
|
|
|
{
|
2008-04-28 17:48:55 -04:00
|
|
|
{ "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
|
2008-03-05 20:59:29 -05:00
|
|
|
[
|
|
|
|
{ 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-09-23 17:55:32 -04:00
|
|
|
[
|
|
|
|
exam "EXAM"
|
|
|
|
{
|
|
|
|
{ "idd" "ID" +db-assigned-id+ }
|
|
|
|
{ "named" "NAME" TEXT }
|
|
|
|
{ "score" "SCORE" INTEGER }
|
|
|
|
} define-persistent
|
|
|
|
] [
|
|
|
|
seq>> { "idd" "named" } =
|
|
|
|
] must-fail-with
|
|
|
|
|
2008-03-05 21:56:40 -05:00
|
|
|
exam "EXAM"
|
|
|
|
{
|
2008-04-28 17:48:55 -04:00
|
|
|
{ "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-09-30 12:00:44 -04:00
|
|
|
[ 4 ]
|
|
|
|
[ T{ exam { name IGNORE } { score IGNORE } } select-tuples length ] unit-test
|
|
|
|
|
|
|
|
[ f ]
|
|
|
|
[ T{ exam { name IGNORE } { score IGNORE } } select-tuples first score>> ] unit-test
|
|
|
|
|
2008-10-02 00:33:57 -04:00
|
|
|
[ T{ exam { name IGNORE } { score IGNORE } { id IGNORE } } select-tuples first score>> ] [ class>> "EXAM" = ] must-fail-with
|
2008-09-30 12:00:44 -04:00
|
|
|
|
2008-03-05 21:56:40 -05:00
|
|
|
[
|
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-10-10 21:52:28 -04:00
|
|
|
[ 4 ] [ T{ exam } count-tuples ] unit-test
|
|
|
|
|
|
|
|
[ ] [ T{ exam { score 10 } } insert-tuple ] unit-test
|
|
|
|
|
|
|
|
[ 10 ]
|
|
|
|
[ T{ exam { name NULL } } select-tuples first score>> ] unit-test ;
|
2008-03-03 09:56:06 -05:00
|
|
|
|
2008-04-19 20:27:54 -04:00
|
|
|
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"
|
|
|
|
{
|
2008-04-28 17:48:55 -04:00
|
|
|
{ "id" "ID" +db-assigned-id+ }
|
2008-04-19 20:27:54 -04:00
|
|
|
{ "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-19 20:27:54 -04:00
|
|
|
|
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-04-19 20:27:54 -04:00
|
|
|
|
2008-03-17 15:14:04 -04:00
|
|
|
TUPLE: secret n message ;
|
|
|
|
C: <secret> secret
|
|
|
|
|
|
|
|
: test-random-id
|
|
|
|
secret "SECRET"
|
|
|
|
{
|
2008-04-19 20:27:54 -04:00
|
|
|
{ "n" "ID" +random-id+ system-random-generator }
|
2008-03-17 15:14:04 -04:00
|
|
|
{ "message" "MESSAGE" TEXT }
|
|
|
|
} define-persistent
|
|
|
|
|
2008-04-28 19:21:45 -04:00
|
|
|
[ ] [ secret recreate-table ] unit-test
|
2008-04-19 20:27:54 -04:00
|
|
|
|
2008-04-28 18:17:19 -04:00
|
|
|
[ t ] [ f "kilroy was here" <secret> [ insert-tuple ] keep n>> integer? ] unit-test
|
2008-04-19 20:27:54 -04:00
|
|
|
|
|
|
|
[ ] [ 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 ] [
|
2008-04-28 19:21:45 -04:00
|
|
|
T{ secret } select-tuples length 3 =
|
2008-04-19 20:27:54 -04:00
|
|
|
] unit-test ;
|
2008-03-17 15:14:04 -04:00
|
|
|
|
2008-04-28 17:48:55 -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
|
|
|
|
2008-04-28 17:48:55 -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
|
2008-04-21 01:45:14 -04:00
|
|
|
[ 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
|
2008-07-08 16:22:03 -04:00
|
|
|
[ 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
|
2008-09-23 16:59:33 -04:00
|
|
|
|
|
|
|
: test-queries ( -- )
|
|
|
|
[ ] [ exam ensure-table ] unit-test
|
2008-09-27 15:07:39 -04:00
|
|
|
[ ] [ 1000 [ random-exam insert-tuple ] times ] unit-test
|
|
|
|
[ 5 ] [
|
|
|
|
<query>
|
|
|
|
T{ exam { score T{ interval { from { 0 t } } { to { 100 t } } } } }
|
|
|
|
>>tuple
|
|
|
|
5 >>limit select-tuples length
|
|
|
|
] unit-test ;
|
|
|
|
|
|
|
|
TUPLE: compound-foo a b c ;
|
|
|
|
|
|
|
|
compound-foo "COMPOUND_FOO"
|
|
|
|
{
|
|
|
|
{ "a" "A" INTEGER +user-assigned-id+ }
|
|
|
|
{ "b" "B" INTEGER +user-assigned-id+ }
|
|
|
|
{ "c" "C" INTEGER }
|
|
|
|
} define-persistent
|
|
|
|
|
|
|
|
: test-compound-primary-key ( -- )
|
|
|
|
[ ] [ compound-foo ensure-table ] unit-test
|
|
|
|
[ ] [ compound-foo drop-table ] unit-test
|
|
|
|
[ ] [ compound-foo create-table ] unit-test
|
|
|
|
[ ] [ 1 2 3 compound-foo boa insert-tuple ] unit-test
|
|
|
|
[ 1 2 3 compound-foo boa insert-tuple ] must-fail
|
|
|
|
[ ] [ 2 3 4 compound-foo boa insert-tuple ] unit-test
|
|
|
|
[ T{ compound-foo { a 2 } { b 3 } { c 4 } } ]
|
|
|
|
[ compound-foo new 4 >>c select-tuple ] unit-test ;
|
|
|
|
|
|
|
|
[ test-compound-primary-key ] test-sqlite
|
|
|
|
[ test-compound-primary-key ] test-postgresql
|