1 ! Copyright (C) 2008 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: concurrency.combinators db db.pools db.sqlite db.tuples
4 db.types destructors kernel math random threads tools.test sequences
5 io io.pools prettyprint db.postgresql accessors io.files.temp
6 namespaces fry system math.parser db.queries assocs ;
9 : postgresql-test-db-name ( -- string )
10 cpu name>> "-" "factor-test" 3append
11 H{ { CHAR: - CHAR: _ } { CHAR: . CHAR: _ } } substitute ;
13 : postgresql-test-db ( -- postgresql-db )
14 \ postgresql-db get-global clone postgresql-test-db-name >>database ;
16 : postgresql-template1-db ( -- postgresql-db )
17 \ postgresql-db get-global clone "template1" >>database ;
19 : sqlite-test-db ( -- sqlite-db )
20 cpu name>> "tuples-test." ".db" surround
21 temp-file <sqlite-db> ;
23 ! These words leak resources, but are useful for interactive testing
24 : set-sqlite-db ( -- )
25 sqlite-db db-open db-connection set ;
27 : set-postgresql-db ( -- )
28 postgresql-db db-open db-connection set ;
31 : test-sqlite ( quot -- )
33 [ ] [ sqlite-test-db _ with-db ] unit-test
36 : test-postgresql ( quot -- )
39 os windows? cpu x86.64? and [
40 postgresql-template1-db [
41 postgresql-test-db-name ensure-database
43 [ ] [ postgresql-test-db _ with-db ] unit-test
48 TUPLE: test-1 id a b c ;
51 { "id" "ID" INTEGER +db-assigned-id+ }
52 { "a" "A" { VARCHAR 256 } +not-null+ }
53 { "b" "B" { VARCHAR 256 } +not-null+ }
54 { "c" "C" { VARCHAR 256 } +not-null+ }
57 TUPLE: test-2 id x y z ;
60 { "id" "ID" INTEGER +db-assigned-id+ }
61 { "x" "X" { VARCHAR 256 } +not-null+ }
62 { "y" "Y" { VARCHAR 256 } +not-null+ }
63 { "z" "Z" { VARCHAR 256 } +not-null+ }
66 : test-1-tuple ( -- tuple )
67 f 100 random 100 random 100 random [ number>string ] tri@
70 : db-tester ( test-db -- )
81 test-1-tuple insert-tuple yield
87 : db-tester2 ( test-db -- )
98 test-1-tuple insert-tuple yield