-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathtest__json.py
64 lines (45 loc) · 1.95 KB
/
test__json.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
55
56
57
58
59
60
61
62
63
64
import json
from unittest import mock
import pytest
import sqlalchemy
@pytest.fixture
def json_table(metadata):
from sqlalchemy_bigquery import JSON
return sqlalchemy.Table("json_table", metadata, sqlalchemy.Column("json", JSON))
@pytest.fixture
def json_data():
return {"foo": "bar"}
def test_set_json_serde(faux_conn, metadata, json_table, json_data):
from sqlalchemy_bigquery import JSON
json_serializer = mock.Mock(side_effect=json.dumps)
json_deserializer = mock.Mock(side_effect=json.loads)
engine = sqlalchemy.create_engine(
"bigquery://myproject/mydataset",
json_serializer=json_serializer,
json_deserializer=json_deserializer,
)
json_column = json_table.c.json
process_bind = json_column.type.bind_processor(engine.dialect)
process_bind(json_data)
assert json_serializer.mock_calls == [mock.call(json_data)]
process_result = json_column.type.result_processor(engine.dialect, JSON)
process_result(json.dumps(json_data))
assert json_deserializer.mock_calls == [mock.call(json.dumps(json_data))]
def test_json_create(faux_conn, metadata, json_table, json_data):
expr = sqlalchemy.schema.CreateTable(json_table)
sql = expr.compile(faux_conn.engine).string
assert sql == "\nCREATE TABLE `json_table` (\n\t`json` JSON\n) \n\n"
def test_json_insert(faux_conn, metadata, json_table, json_data):
expr = sqlalchemy.insert(json_table).values(json=json_data)
sql = expr.compile(faux_conn.engine).string
assert (
sql == "INSERT INTO `json_table` (`json`) VALUES (PARSE_JSON(%(json:STRING)s))"
)
def test_json_where(faux_conn, metadata, json_table, json_data):
expr = sqlalchemy.select(json_table.c.json).where(json_table.c.json == json_data)
sql = expr.compile(faux_conn.engine).string
assert sql == (
"SELECT `json_table`.`json` \n"
"FROM `json_table` \n"
"WHERE `json_table`.`json` = PARSE_JSON(%(json_1:STRING)s)"
)