test_connections.py
1.44 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
from etl_to_crawler.connections import _NulSafePgConnection
class _Cursor:
def __init__(self):
self.executed = []
self.executed_many = []
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
return False
def execute(self, operation, parameters=None):
self.executed.append((operation, parameters))
def executemany(self, operation, parameter_sets):
self.executed_many.append((operation, parameter_sets))
class _Connection:
def __init__(self):
self.raw_cursor = _Cursor()
def cursor(self):
return self.raw_cursor
def test_pg_connection_removes_nul_from_execute_parameters():
raw_connection = _Connection()
connection = _NulSafePgConnection(raw_connection)
with connection.cursor() as cur:
cur.execute('INSERT INTO example VALUES (%s, %s)', ('歌\x00词', {'text': '介\x00绍'}))
assert raw_connection.raw_cursor.executed == [
('INSERT INTO example VALUES (%s, %s)', ('歌词', {'text': '介绍'})),
]
def test_pg_connection_removes_nul_from_every_executemany_row():
raw_connection = _Connection()
connection = _NulSafePgConnection(raw_connection)
with connection.cursor() as cur:
cur.executemany('INSERT INTO example VALUES (%s)', [('A\x00',), ('B\x00',)])
assert raw_connection.raw_cursor.executed_many == [
('INSERT INTO example VALUES (%s)', [('A',), ('B',)]),
]