forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
115 lines (92 loc) · 3.38 KB
/
Copy pathtest.rs
File metadata and controls
115 lines (92 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
extern mod postgres;
use postgres::{PostgresConnection, ToSql};
macro_rules! params(
($($e:expr),+) => (
[$(
$e as &ToSql
),+]
)
)
macro_rules! chk(
($e:expr) => (
match $e {
Ok(ok) => ok,
Err(err) => fail!(err)
}
)
)
#[test]
fn test_basic() {
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
do conn.in_transaction |conn| {
chk!(conn.update("CREATE TABLE basic (id INT PRIMARY KEY)", []));
chk!(conn.update("INSERT INTO basic (id) VALUES (101)", []));
let res = chk!(conn.query("SELECT id from basic WHERE id = 101", []));
assert_eq!(1, res.len());
assert_eq!(1, res.get(0).len());
assert_eq!(1, res.get(0).len());
assert_eq!(Some(101), res.get(0)[0]);
Err::<(), ~str>(~"")
};
}
#[test]
fn test_multiple_stmts() {
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
do conn.in_transaction |conn| {
chk!(conn.update("CREATE TABLE foo (id INT PRIMARY KEY)", []));
let stmt1 = chk!(conn.prepare("INSERT INTO foo (id) VALUES (101)"));
let stmt2 = chk!(conn.prepare("INSERT INTO foo (id) VALUES (102)"));
chk!(stmt1.update([]));
chk!(stmt2.update([]));
Err::<(), ~str>(~"")
};
}
#[test]
fn test_params() {
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
do conn.in_transaction |conn| {
chk!(conn.update("CREATE TABLE basic (id INT PRIMARY KEY)", []));
chk!(conn.update("INSERT INTO basic (id) VALUES ($1)",
params!(&101)));
let res = chk!(conn.query("SELECT id from basic WHERE id = $1",
params!(&101)));
assert_eq!(Some(101), res.get(0)[0]);
Err::<(), ~str>(~"")
};
}
#[test]
fn test_null() {
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
do conn.in_transaction |conn| {
chk!(conn.update("CREATE TABLE basic (id INT PRIMARY KEY, foo INT)",
[]));
chk!(conn.update("INSERT INTO basic (id, foo) VALUES ($1, $2), ($3, $4)",
params!(&101, &None::<int>, &102, &0)));
let res = chk!(conn.query("SELECT foo from basic ORDER BY id", []));
assert_eq!(None, res.get(0).get::<Option<int>>(0));
assert_eq!(Some(0), res.get(1).get::<Option<int>>(0));
Err::<(), ~str>(~"")
};
}
#[test]
fn test_types() {
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
do conn.in_transaction |conn| {
chk!(conn.update(
"CREATE TABLE foo (
id INT PRIMARY KEY,
str VARCHAR,
float REAL
)", []));
chk!(conn.update("INSERT INTO foo (id, str, float)
VALUES ($1, $2, $3), ($4, $5, $6)",
params!(&101, & &"foobar", &10.5,
&102, &None::<~str>, &None::<float>)));
let res = chk!(conn.query("SELECT str, float from foo ORDER BY id", []));
assert_eq!(~"foobar", res.get(0).get::<~str>(0));
assert_eq!(10.5, res.get(0).get::<float>(1));
assert_eq!(None::<~str>, res.get(1).get::<Option<~str>>(0));
assert_eq!(None::<float>, res.get(1).get::<Option<float>>(1));
Err::<(), ~str>(~"")
};
}