]> gitweb.factorcode.org Git - factor.git/blob - basis/db/postgresql/postgresql-tests.factor
65b75a63dcb6e8d7e8314e3cefb23a3aace1aef1
[factor.git] / basis / db / postgresql / postgresql-tests.factor
1 ! You will need to run  'createdb factor-test' to create the database.
2 ! Set username and password in  the 'connect' word.
3
4 USING: kernel db.postgresql alien continuations io classes
5 prettyprint sequences namespaces tools.test db
6 db.tuples db.types unicode.case ;
7 IN: db.postgresql.tests
8
9 : test-db ( -- postgresql-db )
10     { "localhost" "postgres" "foob" "factor-test" } postgresql-db ;
11
12 [ ] [ test-db [ ] with-db ] unit-test
13
14 [ ] [
15     test-db [
16         [ "drop table person;" sql-command ] ignore-errors
17         "create table person (name varchar(30), country varchar(30));"
18             sql-command
19
20         "insert into person values('John', 'America');" sql-command
21         "insert into person values('Jane', 'New Zealand');" sql-command
22     ] with-db
23 ] unit-test
24
25 [
26     {
27         { "John" "America" }
28         { "Jane" "New Zealand" }
29     }
30 ] [
31     test-db [
32         "select * from person" sql-query
33     ] with-db
34 ] unit-test
35
36 [
37     {
38         { "John" "America" }
39         { "Jane" "New Zealand" }
40     }
41 ] [ test-db [ "select * from person" sql-query ] with-db ] unit-test
42
43 [
44 ] [
45     test-db [
46         "insert into person(name, country) values('Jimmy', 'Canada')"
47         sql-command
48     ] with-db
49 ] unit-test
50
51 [
52     {
53         { "John" "America" }
54         { "Jane" "New Zealand" }
55         { "Jimmy" "Canada" }
56     }
57 ] [ test-db [ "select * from person" sql-query ] with-db ] unit-test
58
59 [
60     test-db [
61         [
62             "insert into person(name, country) values('Jose', 'Mexico')" sql-command
63             "insert into person(name, country) values('Jose', 'Mexico')" sql-command
64             "oops" throw
65         ] with-transaction
66     ] with-db
67 ] must-fail
68
69 [ 3 ] [
70     test-db [
71         "select * from person" sql-query length
72     ] with-db
73 ] unit-test
74
75 [
76 ] [
77     test-db [
78         [
79             "insert into person(name, country) values('Jose', 'Mexico')"
80             sql-command
81             "insert into person(name, country) values('Jose', 'Mexico')"
82             sql-command
83         ] with-transaction
84     ] with-db
85 ] unit-test
86
87 [ 5 ] [
88     test-db [
89         "select * from person" sql-query length
90     ] with-db
91 ] unit-test
92
93
94 : with-dummy-db ( quot -- )
95     >r T{ postgresql-db } db r> with-variable ;