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
63 lines (49 loc) · 1.8 KB
/
Copy pathtest.rs
File metadata and controls
63 lines (49 loc) · 1.8 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
extern mod postgres;
use postgres::PostgresConnection;
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_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)", [~"101"]));
let res = chk!(conn.query("SELECT id from basic WHERE id = $1", [~"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, NULL), ($2, $3)",
[~"101", ~"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>(~"")
};
}