-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_pool.py
More file actions
96 lines (82 loc) · 1.79 KB
/
test_pool.py
File metadata and controls
96 lines (82 loc) · 1.79 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
#!/usr/bin/env python
# coding=utf-8
"""
test the db pool
"""
import pytest
from dbpool import get_cursor, get_dict_cursor
@pytest.fixture(scope="session")
def db():
with get_cursor() as cur:
cur.execute(u"""\
DROP TABLE IF EXISTS t1
""")
cur.execute(u"""\
CREATE TABLE t1 (
id uuid DEFAULT uuid_generate_v4(),
data text)
""")
cur.execute(u"""\
INSERT INTO t1
(data)
VALUES
('abc'),
('def'),
('ghi')
""")
def test_get_cursor(db):
with get_cursor() as cur:
cur.execute(u"""\
SELECT 1
""")
assert cur.fetchone()[0] == 1
with get_cursor() as cur:
cur.execute(u"""\
SELECT data FROM t1 ORDER BY data
""")
r = cur.fetchall()
assert r[0][0] == "abc"
assert r[1][0] == "def"
assert r[2][0] == "ghi"
def test_get_dict_cursor(db):
with get_dict_cursor() as cur:
cur.execute(u"""\
SELECT data FROM t1 ORDER BY data
""")
r = cur.fetchall()
# r[0] is of type psycopg2.extras.DictRow, not a raw python dict.
assert r[0]['data'] == "abc"
assert dict(r[0]) == {'data': "abc"}
assert r[1]['data'] == "def"
def test_abort_on_exception(db):
try:
with get_cursor() as cur:
cur.execute(u"""\
INSERT INTO t1
(data)
VALUES
('jkl')
""")
1 / 0 # raise exception
except ZeroDivisionError:
pass
with get_cursor() as cur:
cur.execute(u"""\
SELECT data FROM t1 WHERE data = 'jkl'
""")
r = cur.fetchone()
assert r is None
def test_insert_new_data(db):
with get_cursor() as cur:
cur.execute(u"""\
INSERT INTO t1
(data)
VALUES
('mno')
""")
with get_cursor() as cur:
cur.execute(u"""\
SELECT data FROM t1 WHERE data = 'mno'
""")
r = cur.fetchone()
assert r[0] == 'mno'