-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtest_relationship_valid.py
163 lines (142 loc) · 4.83 KB
/
test_relationship_valid.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import base64
import orjson
import pytest
from testcontainers.postgres import PostgresContainer
base_url = "/v2/connector/postgres"
manifest = {
"catalog": "wrenai",
"schema": "public",
"models": [
{
"name": "t1",
"refSql": "select * from (values (1, 2), (2, 3), (3, 3)) as t1(id, many_col)",
"columns": [
{"name": "id", "type": "integer"},
{"name": "many_col", "type": "integer"},
],
"primaryKey": ["id"],
},
{
"name": "t2",
"refSql": "select * from (values (1, 2), (2, 3), (3, 3)) as t2(id, many_col)",
"columns": [
{"name": "id", "type": "integer"},
{"name": "many_col", "type": "integer"},
],
},
],
"relationships": [
{
"name": "t1_id_t2_id",
"joinType": "ONE_TO_ONE",
"models": ["t1", "t2"],
"condition": "t1.id = t2.id",
},
{
"name": "t1_id_t2_many",
"joinType": "ONE_TO_MANY",
"models": ["t1", "t2"],
"condition": "t1.id = t2.many_col",
},
{
"name": "t1_many_t2_id",
"joinType": "MANY_TO_ONE",
"models": ["t1", "t2"],
"condition": "t1.many_col = t2.id",
},
{
"name": "invalid_t1_many_t2_id",
"joinType": "ONE_TO_ONE",
"models": ["t1", "t2"],
"condition": "t1.many_col = t2.id",
},
],
}
@pytest.fixture(scope="module")
def manifest_str():
return base64.b64encode(orjson.dumps(manifest)).decode("utf-8")
@pytest.fixture(scope="module")
def postgres(request) -> PostgresContainer:
pg = PostgresContainer("postgres:16-alpine").start()
request.addfinalizer(pg.stop)
return pg
async def test_validation_relationship(
client, manifest_str, postgres: PostgresContainer
):
connection_info = _to_connection_info(postgres)
response = await client.post(
url=f"{base_url}/validate/relationship_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"relationshipName": "t1_id_t2_id"},
},
)
assert response.status_code == 204
response = await client.post(
url=f"{base_url}/validate/relationship_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"relationshipName": "t1_id_t2_many"},
},
)
assert response.status_code == 204
response = await client.post(
url=f"{base_url}/validate/relationship_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"relationshipName": "t1_many_t2_id"},
},
)
assert response.status_code == 204
async def test_validation_relationship_not_found(
client, manifest_str, postgres: PostgresContainer
):
connection_info = _to_connection_info(postgres)
response = await client.post(
url=f"{base_url}/validate/relationship_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"relationshipName": "not_found"},
},
)
assert response.status_code == 422
assert response.text == "Relationship not_found not found in manifest"
connection_info = _to_connection_info(postgres)
response = await client.post(
url=f"{base_url}/validate/relationship_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {},
},
)
assert response.status_code == 422
assert response.text == "Missing required parameter: `relationship`"
async def test_validation_failure(client, manifest_str, postgres: PostgresContainer):
connection_info = _to_connection_info(postgres)
response = await client.post(
url=f"{base_url}/validate/relationship_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"relationshipName": "invalid_t1_many_t2_id"},
},
)
assert response.status_code == 422
assert (
response.text
== "Exception: <class 'app.model.validator.ValidationError'>, message: Relationship invalid_t1_many_t2_id is not valid: {'result': 'False', 'is_related': 'True', "
"'left_table_unique': 'False', 'right_table_unique': 'True'}"
)
def _to_connection_info(pg: PostgresContainer):
return {
"host": pg.get_container_host_ip(),
"port": pg.get_exposed_port(pg.port),
"user": pg.username,
"password": pg.password,
"database": pg.dbname,
}