-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_micropg.py
54 lines (42 loc) · 1.25 KB
/
test_micropg.py
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
import micropg
try:
micropg.create_database(
host='127.0.0.1', user='postgres', password='password', database='test_micropg'
)
except Exception:
pass
conn = micropg.connect(
host='127.0.0.1', user='postgres', password='password', database='test_micropg'
)
cur = conn.cursor()
# error
try:
cur.execute("BAD STATEMENT")
except micropg.ProgrammingError as e:
assert e.message == '42601:syntax error at or near "BAD"'
# create table, insert, select
try:
cur.execute("DROP TABLE test_micropg")
except:
pass
cur.execute("""
CREATE TABLE test_micropg(
id integer,
name varchar(20)
)
""")
cur.execute("INSERT INTO test_micropg(id, name) values (1, 'test')")
cur.execute("INSERT INTO test_micropg(id, name) values (%s, %s)", [2, 'test2'])
conn.commit()
cur.execute("SELECT id, name FROM test_micropg")
assert cur.fetchall() == [(1, "test"), (2, "test2")]
conn.close()
if False: # disable ssl connection
# test ssl connection
conn = micropg.connect(
host='127.0.0.1', user='postgres', password='password', database='test_micropg', use_ssl=True
)
cur = conn.cursor()
cur.execute("SELECT id, name FROM test_micropg")
assert cur.fetchall() == [(1, "test"), (2, "test2")]
conn.close()