flyql 0.0.31__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- e2e/conftest.py +48 -0
- e2e/test_clickhouse_e2e.py +347 -0
- e2e/test_matcher_e2e.py +86 -0
- e2e/test_postgresql_e2e.py +371 -0
- e2e/test_starrocks_e2e.py +445 -0
- flyql/__init__.py +39 -0
- flyql/columns/__init__.py +79 -0
- flyql/columns/char.py +93 -0
- flyql/columns/column.py +48 -0
- flyql/columns/constants.py +28 -0
- flyql/columns/exceptions.py +11 -0
- flyql/columns/parser.py +518 -0
- flyql/columns/state.py +22 -0
- flyql/columns/validator.py +163 -0
- flyql/core/__init__.py +28 -0
- flyql/core/char.py +82 -0
- flyql/core/column.py +49 -0
- flyql/core/constants.py +85 -0
- flyql/core/exceptions.py +16 -0
- flyql/core/expression.py +76 -0
- flyql/core/key.py +355 -0
- flyql/core/parser.py +1613 -0
- flyql/core/range.py +22 -0
- flyql/core/state.py +28 -0
- flyql/core/tree.py +43 -0
- flyql/core/validator.py +293 -0
- flyql/generators/README.md +2 -0
- flyql/generators/__init__.py +7 -0
- flyql/generators/clickhouse/__init__.py +4 -0
- flyql/generators/clickhouse/column.py +138 -0
- flyql/generators/clickhouse/constants.py +146 -0
- flyql/generators/clickhouse/generator.py +958 -0
- flyql/generators/clickhouse/helpers.py +86 -0
- flyql/generators/postgresql/__init__.py +4 -0
- flyql/generators/postgresql/column.py +90 -0
- flyql/generators/postgresql/constants.py +65 -0
- flyql/generators/postgresql/generator.py +954 -0
- flyql/generators/postgresql/helpers.py +76 -0
- flyql/generators/starrocks/__init__.py +4 -0
- flyql/generators/starrocks/column.py +112 -0
- flyql/generators/starrocks/constants.py +39 -0
- flyql/generators/starrocks/generator.py +1039 -0
- flyql/generators/starrocks/helpers.py +86 -0
- flyql/generators/transformer_helpers.py +76 -0
- flyql/matcher/__init__.py +7 -0
- flyql/matcher/evaluator.py +284 -0
- flyql/matcher/key.py +14 -0
- flyql/matcher/record.py +63 -0
- flyql/transformers/__init__.py +14 -0
- flyql/transformers/base.py +42 -0
- flyql/transformers/builtins.py +112 -0
- flyql/transformers/registry.py +34 -0
- flyql/types.py +12 -0
- flyql-0.0.31.dist-info/METADATA +26 -0
- flyql-0.0.31.dist-info/RECORD +63 -0
- flyql-0.0.31.dist-info/WHEEL +5 -0
- flyql-0.0.31.dist-info/licenses/LICENSE +21 -0
- flyql-0.0.31.dist-info/top_level.txt +3 -0
- snippets/getting_started_columns.py +15 -0
- snippets/getting_started_generate_clickhouse.py +13 -0
- snippets/getting_started_match.py +14 -0
- snippets/getting_started_parse.py +4 -0
- snippets/getting_started_transformers.py +11 -0
e2e/conftest.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def pytest_sessionfinish(session: Any, exitstatus: int) -> None:
|
|
8
|
+
"""Write JSON report if E2E_REPORT_JSON is set."""
|
|
9
|
+
report_path = os.environ.get("E2E_REPORT_JSON", "")
|
|
10
|
+
if not report_path:
|
|
11
|
+
return
|
|
12
|
+
|
|
13
|
+
all_results: list[dict[str, Any]] = []
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from test_clickhouse_e2e import _results as ch_results
|
|
17
|
+
|
|
18
|
+
all_results.extend(ch_results)
|
|
19
|
+
except ImportError:
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
from test_starrocks_e2e import _results as sr_results
|
|
24
|
+
|
|
25
|
+
all_results.extend(sr_results)
|
|
26
|
+
except ImportError:
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
from test_postgresql_e2e import _results as pg_results
|
|
31
|
+
|
|
32
|
+
all_results.extend(pg_results)
|
|
33
|
+
except ImportError:
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
from test_matcher_e2e import _results as matcher_results
|
|
38
|
+
|
|
39
|
+
all_results.extend(matcher_results)
|
|
40
|
+
except ImportError:
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
if all_results:
|
|
44
|
+
report = {"language": "python", "results": all_results}
|
|
45
|
+
try:
|
|
46
|
+
Path(report_path).write_text(json.dumps(report, indent=2))
|
|
47
|
+
except OSError as e:
|
|
48
|
+
print(f"warn: could not write e2e report {report_path}: {e}")
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
"""ClickHouse E2E tests for Python FlyQL generator.
|
|
2
|
+
|
|
3
|
+
Connects to a real ClickHouse instance, generates SQL from FlyQL queries
|
|
4
|
+
using the Python generator, executes the SQL, and validates returned IDs
|
|
5
|
+
match expected results from the shared test cases.
|
|
6
|
+
|
|
7
|
+
Outputs a JSON report compatible with the e2e runner when E2E_REPORT_JSON is set.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import sys
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
import pytest
|
|
17
|
+
|
|
18
|
+
# Add python/ to path so we can import flyql
|
|
19
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
20
|
+
|
|
21
|
+
from flyql.core.parser import parse # noqa: E402
|
|
22
|
+
from flyql.generators.clickhouse.column import Column # noqa: E402
|
|
23
|
+
from flyql.generators.clickhouse.generator import (
|
|
24
|
+
to_sql_where,
|
|
25
|
+
to_sql_select,
|
|
26
|
+
) # noqa: E402
|
|
27
|
+
|
|
28
|
+
REPO_ROOT = Path(__file__).resolve().parent.parent.parent
|
|
29
|
+
TEST_DATA_DIR = REPO_ROOT / "tests-data" / "e2e"
|
|
30
|
+
|
|
31
|
+
CH_HOST = os.environ.get("CLICKHOUSE_HOST", "localhost")
|
|
32
|
+
CH_HTTP_PORT = os.environ.get("CLICKHOUSE_HTTP_PORT", "18123")
|
|
33
|
+
CH_USER = os.environ.get("CLICKHOUSE_USER", "flyql")
|
|
34
|
+
CH_PASS = os.environ.get("CLICKHOUSE_PASSWORD", "flyql")
|
|
35
|
+
|
|
36
|
+
_results: list[dict[str, Any]] = []
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def ch_query(sql: str) -> list[dict[str, Any]]:
|
|
40
|
+
"""Execute a query against ClickHouse via HTTP interface."""
|
|
41
|
+
import urllib.request
|
|
42
|
+
import urllib.parse
|
|
43
|
+
|
|
44
|
+
params = urllib.parse.urlencode(
|
|
45
|
+
{
|
|
46
|
+
"user": CH_USER,
|
|
47
|
+
"password": CH_PASS,
|
|
48
|
+
"default_format": "JSONEachRow",
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
url = f"http://{CH_HOST}:{CH_HTTP_PORT}/?{params}"
|
|
52
|
+
|
|
53
|
+
req = urllib.request.Request(url, data=sql.encode("utf-8"), method="POST")
|
|
54
|
+
try:
|
|
55
|
+
with urllib.request.urlopen(req, timeout=10) as resp:
|
|
56
|
+
text = resp.read().decode("utf-8").strip()
|
|
57
|
+
except Exception as e:
|
|
58
|
+
raise RuntimeError(f"ClickHouse error: {e}") from e
|
|
59
|
+
|
|
60
|
+
if not text:
|
|
61
|
+
return []
|
|
62
|
+
return [json.loads(line) for line in text.split("\n")]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def load_json(path: Path) -> Any:
|
|
66
|
+
return json.loads(path.read_text())
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def build_columns() -> dict[str, Column]:
|
|
70
|
+
col_data = load_json(TEST_DATA_DIR / "clickhouse" / "columns.json")
|
|
71
|
+
columns: dict[str, Column] = {}
|
|
72
|
+
for key, col in col_data["columns"].items():
|
|
73
|
+
columns[key] = Column(
|
|
74
|
+
name=col["name"],
|
|
75
|
+
jsonstring=col["jsonstring"],
|
|
76
|
+
_type=col["type"],
|
|
77
|
+
values=col.get("values"),
|
|
78
|
+
)
|
|
79
|
+
return columns
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def build_join_columns() -> dict[str, Column]:
|
|
83
|
+
col_data = load_json(TEST_DATA_DIR / "clickhouse" / "join_columns.json")
|
|
84
|
+
columns: dict[str, Column] = {}
|
|
85
|
+
for key, col in col_data["columns"].items():
|
|
86
|
+
c = Column(
|
|
87
|
+
name=col["name"],
|
|
88
|
+
jsonstring=col.get("jsonstring", False),
|
|
89
|
+
_type=col["type"],
|
|
90
|
+
values=col.get("values"),
|
|
91
|
+
)
|
|
92
|
+
if col.get("raw_identifier"):
|
|
93
|
+
c.with_raw_identifier(col["raw_identifier"])
|
|
94
|
+
columns[key] = c
|
|
95
|
+
return columns
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def load_test_cases() -> list[dict[str, Any]]:
|
|
99
|
+
data = load_json(TEST_DATA_DIR / "test_cases.json")
|
|
100
|
+
return [tc for tc in data["tests"] if "clickhouse" in tc["databases"]]
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def load_join_test_cases() -> list[dict[str, Any]]:
|
|
104
|
+
data = load_json(TEST_DATA_DIR / "join_test_cases.json")
|
|
105
|
+
return [tc for tc in data["tests"] if "clickhouse" in tc["databases"]]
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@pytest.fixture(scope="module")
|
|
109
|
+
def ch_available() -> bool:
|
|
110
|
+
try:
|
|
111
|
+
rows = ch_query("SELECT 1 AS ok")
|
|
112
|
+
return len(rows) > 0 and rows[0]["ok"] == 1
|
|
113
|
+
except Exception:
|
|
114
|
+
return False
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@pytest.fixture(scope="module")
|
|
118
|
+
def columns() -> dict[str, Column]:
|
|
119
|
+
return build_columns()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def ids_match(expected: list[int], got: list[int]) -> bool:
|
|
123
|
+
return sorted(expected) == sorted(got)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@pytest.mark.parametrize(
|
|
127
|
+
"name,flyql,expected_ids",
|
|
128
|
+
[(tc["name"], tc["flyql"], tc["expected_ids"]) for tc in load_test_cases()],
|
|
129
|
+
ids=[tc["name"] for tc in load_test_cases()],
|
|
130
|
+
)
|
|
131
|
+
def test_clickhouse_where(
|
|
132
|
+
ch_available: bool,
|
|
133
|
+
columns: dict[str, Column],
|
|
134
|
+
name: str,
|
|
135
|
+
flyql: str,
|
|
136
|
+
expected_ids: list[int],
|
|
137
|
+
) -> None:
|
|
138
|
+
result: dict[str, Any] = {
|
|
139
|
+
"kind": "where",
|
|
140
|
+
"database": "clickhouse",
|
|
141
|
+
"name": name,
|
|
142
|
+
"flyql": flyql,
|
|
143
|
+
"sql": "",
|
|
144
|
+
"expected_ids": expected_ids,
|
|
145
|
+
"returned_ids": [],
|
|
146
|
+
"passed": False,
|
|
147
|
+
"error": "",
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if not ch_available:
|
|
151
|
+
result["error"] = "ClickHouse not available"
|
|
152
|
+
_results.append(result)
|
|
153
|
+
pytest.skip("ClickHouse not available")
|
|
154
|
+
return
|
|
155
|
+
|
|
156
|
+
try:
|
|
157
|
+
parsed = parse(flyql)
|
|
158
|
+
sql_where = to_sql_where(parsed.current_node, columns)
|
|
159
|
+
result["sql"] = sql_where
|
|
160
|
+
|
|
161
|
+
query = f"SELECT id FROM flyql_e2e_test WHERE {sql_where} ORDER BY id"
|
|
162
|
+
rows = ch_query(query)
|
|
163
|
+
returned_ids = [r["id"] for r in rows]
|
|
164
|
+
result["returned_ids"] = returned_ids
|
|
165
|
+
result["passed"] = ids_match(expected_ids, returned_ids)
|
|
166
|
+
|
|
167
|
+
_results.append(result)
|
|
168
|
+
assert result["passed"], f"expected {expected_ids}, got {returned_ids}"
|
|
169
|
+
except Exception as e:
|
|
170
|
+
result["error"] = str(e)
|
|
171
|
+
_results.append(result)
|
|
172
|
+
raise
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def load_select_test_cases() -> list[dict[str, Any]]:
|
|
176
|
+
data = load_json(TEST_DATA_DIR / "clickhouse" / "select_test_cases.json")
|
|
177
|
+
return data["tests"]
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def load_join_select_test_cases() -> list[dict[str, Any]]:
|
|
181
|
+
data = load_json(TEST_DATA_DIR / "clickhouse" / "join_select_test_cases.json")
|
|
182
|
+
return data["tests"]
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@pytest.mark.parametrize(
|
|
186
|
+
"name,select_columns,expected_rows",
|
|
187
|
+
[
|
|
188
|
+
(tc["name"], tc["select_columns"], tc["expected_rows"])
|
|
189
|
+
for tc in load_select_test_cases()
|
|
190
|
+
],
|
|
191
|
+
ids=[tc["name"] for tc in load_select_test_cases()],
|
|
192
|
+
)
|
|
193
|
+
def test_clickhouse_select(
|
|
194
|
+
ch_available: bool,
|
|
195
|
+
columns: dict[str, Column],
|
|
196
|
+
name: str,
|
|
197
|
+
select_columns: str,
|
|
198
|
+
expected_rows: list[list[str]],
|
|
199
|
+
) -> None:
|
|
200
|
+
result: dict[str, Any] = {
|
|
201
|
+
"kind": "select",
|
|
202
|
+
"database": "clickhouse",
|
|
203
|
+
"name": name,
|
|
204
|
+
"select_columns": select_columns,
|
|
205
|
+
"sql": "",
|
|
206
|
+
"expected_rows": expected_rows,
|
|
207
|
+
"returned_rows": [],
|
|
208
|
+
"passed": False,
|
|
209
|
+
"error": "",
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if not ch_available:
|
|
213
|
+
result["error"] = "ClickHouse not available"
|
|
214
|
+
_results.append(result)
|
|
215
|
+
pytest.skip("ClickHouse not available")
|
|
216
|
+
return
|
|
217
|
+
|
|
218
|
+
try:
|
|
219
|
+
select_result = to_sql_select(select_columns, columns)
|
|
220
|
+
result["sql"] = select_result.sql
|
|
221
|
+
|
|
222
|
+
query = f"SELECT {select_result.sql} FROM flyql_e2e_test ORDER BY id"
|
|
223
|
+
rows = ch_query(query)
|
|
224
|
+
returned_rows = [
|
|
225
|
+
[str(v) if v is not None else "null" for v in row.values()] for row in rows
|
|
226
|
+
]
|
|
227
|
+
result["returned_rows"] = returned_rows
|
|
228
|
+
result["passed"] = returned_rows == expected_rows
|
|
229
|
+
|
|
230
|
+
_results.append(result)
|
|
231
|
+
assert result["passed"], f"expected {expected_rows}, got {returned_rows}"
|
|
232
|
+
except Exception as e:
|
|
233
|
+
result["error"] = str(e)
|
|
234
|
+
_results.append(result)
|
|
235
|
+
raise
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@pytest.fixture(scope="module")
|
|
239
|
+
def join_columns() -> dict[str, Column]:
|
|
240
|
+
return build_join_columns()
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
@pytest.mark.parametrize(
|
|
244
|
+
"name,flyql,expected_ids",
|
|
245
|
+
[(tc["name"], tc["flyql"], tc["expected_ids"]) for tc in load_join_test_cases()],
|
|
246
|
+
ids=[tc["name"] for tc in load_join_test_cases()],
|
|
247
|
+
)
|
|
248
|
+
def test_clickhouse_join(
|
|
249
|
+
ch_available: bool,
|
|
250
|
+
join_columns: dict[str, Column],
|
|
251
|
+
name: str,
|
|
252
|
+
flyql: str,
|
|
253
|
+
expected_ids: list[int],
|
|
254
|
+
) -> None:
|
|
255
|
+
result: dict[str, Any] = {
|
|
256
|
+
"kind": "where",
|
|
257
|
+
"database": "clickhouse",
|
|
258
|
+
"name": name,
|
|
259
|
+
"flyql": flyql,
|
|
260
|
+
"sql": "",
|
|
261
|
+
"expected_ids": expected_ids,
|
|
262
|
+
"returned_ids": [],
|
|
263
|
+
"passed": False,
|
|
264
|
+
"error": "",
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if not ch_available:
|
|
268
|
+
result["error"] = "ClickHouse not available"
|
|
269
|
+
_results.append(result)
|
|
270
|
+
pytest.skip("ClickHouse not available")
|
|
271
|
+
return
|
|
272
|
+
|
|
273
|
+
try:
|
|
274
|
+
parsed = parse(flyql)
|
|
275
|
+
sql_where = to_sql_where(parsed.current_node, join_columns)
|
|
276
|
+
result["sql"] = sql_where
|
|
277
|
+
|
|
278
|
+
query = f"SELECT t.id FROM flyql_e2e_test t INNER JOIN flyql_e2e_related r ON t.id = r.test_id WHERE {sql_where} ORDER BY t.id"
|
|
279
|
+
rows = ch_query(query)
|
|
280
|
+
returned_ids = [r["id"] for r in rows]
|
|
281
|
+
result["returned_ids"] = returned_ids
|
|
282
|
+
result["passed"] = ids_match(expected_ids, returned_ids)
|
|
283
|
+
|
|
284
|
+
_results.append(result)
|
|
285
|
+
assert result["passed"], f"expected {expected_ids}, got {returned_ids}"
|
|
286
|
+
except Exception as e:
|
|
287
|
+
result["error"] = str(e)
|
|
288
|
+
_results.append(result)
|
|
289
|
+
raise
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
@pytest.fixture(scope="module")
|
|
293
|
+
def join_select_columns() -> dict[str, Column]:
|
|
294
|
+
return build_join_columns()
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
@pytest.mark.parametrize(
|
|
298
|
+
"name,select_columns,expected_rows",
|
|
299
|
+
[
|
|
300
|
+
(tc["name"], tc["select_columns"], tc["expected_rows"])
|
|
301
|
+
for tc in load_join_select_test_cases()
|
|
302
|
+
],
|
|
303
|
+
ids=[tc["name"] for tc in load_join_select_test_cases()],
|
|
304
|
+
)
|
|
305
|
+
def test_clickhouse_join_select(
|
|
306
|
+
ch_available: bool,
|
|
307
|
+
join_select_columns: dict[str, Column],
|
|
308
|
+
name: str,
|
|
309
|
+
select_columns: str,
|
|
310
|
+
expected_rows: list[list[str]],
|
|
311
|
+
) -> None:
|
|
312
|
+
result: dict[str, Any] = {
|
|
313
|
+
"kind": "select",
|
|
314
|
+
"database": "clickhouse",
|
|
315
|
+
"name": name,
|
|
316
|
+
"select_columns": select_columns,
|
|
317
|
+
"sql": "",
|
|
318
|
+
"expected_rows": expected_rows,
|
|
319
|
+
"returned_rows": [],
|
|
320
|
+
"passed": False,
|
|
321
|
+
"error": "",
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if not ch_available:
|
|
325
|
+
result["error"] = "ClickHouse not available"
|
|
326
|
+
_results.append(result)
|
|
327
|
+
pytest.skip("ClickHouse not available")
|
|
328
|
+
return
|
|
329
|
+
|
|
330
|
+
try:
|
|
331
|
+
select_result = to_sql_select(select_columns, join_select_columns)
|
|
332
|
+
result["sql"] = select_result.sql
|
|
333
|
+
|
|
334
|
+
query = f"SELECT {select_result.sql} FROM flyql_e2e_test t INNER JOIN flyql_e2e_related r ON t.id = r.test_id ORDER BY t.id"
|
|
335
|
+
rows = ch_query(query)
|
|
336
|
+
returned_rows = [
|
|
337
|
+
[str(v) if v is not None else "null" for v in row.values()] for row in rows
|
|
338
|
+
]
|
|
339
|
+
result["returned_rows"] = returned_rows
|
|
340
|
+
result["passed"] = returned_rows == expected_rows
|
|
341
|
+
|
|
342
|
+
_results.append(result)
|
|
343
|
+
assert result["passed"], f"expected {expected_rows}, got {returned_rows}"
|
|
344
|
+
except Exception as e:
|
|
345
|
+
result["error"] = str(e)
|
|
346
|
+
_results.append(result)
|
|
347
|
+
raise
|
e2e/test_matcher_e2e.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""Matcher E2E tests for Python FlyQL — in-memory evaluation parity with database results."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
11
|
+
|
|
12
|
+
from flyql.matcher.evaluator import Evaluator # noqa: E402
|
|
13
|
+
from flyql.matcher.record import Record # noqa: E402
|
|
14
|
+
from flyql.core.parser import parse # noqa: E402
|
|
15
|
+
|
|
16
|
+
REPO_ROOT = Path(__file__).resolve().parent.parent.parent
|
|
17
|
+
TEST_DATA_DIR = REPO_ROOT / "tests-data" / "e2e"
|
|
18
|
+
|
|
19
|
+
_results: list[dict[str, Any]] = []
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def load_json(path: Path) -> Any:
|
|
23
|
+
return json.loads(path.read_text())
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
ROWS = load_json(TEST_DATA_DIR / "rows.json")["rows"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def load_test_cases() -> list[dict[str, Any]]:
|
|
30
|
+
data = load_json(TEST_DATA_DIR / "test_cases.json")
|
|
31
|
+
return [
|
|
32
|
+
tc
|
|
33
|
+
for tc in data["tests"]
|
|
34
|
+
if not any(
|
|
35
|
+
x in tc["flyql"]
|
|
36
|
+
for x in [
|
|
37
|
+
"tags.",
|
|
38
|
+
"metadata.",
|
|
39
|
+
"meta_json.",
|
|
40
|
+
"hello*'",
|
|
41
|
+
"'*@",
|
|
42
|
+
"created_at<=",
|
|
43
|
+
]
|
|
44
|
+
)
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def match_row(query: str, data: dict[str, Any]) -> bool:
|
|
49
|
+
result = parse(query)
|
|
50
|
+
evaluator = Evaluator()
|
|
51
|
+
record = Record(data)
|
|
52
|
+
return evaluator.evaluate(result.current_node, record)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def ids_match(expected: list[int], got: list[int]) -> bool:
|
|
56
|
+
return sorted(expected) == sorted(got)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@pytest.mark.parametrize(
|
|
60
|
+
"name,flyql,expected_ids",
|
|
61
|
+
[(tc["name"], tc["flyql"], tc["expected_ids"]) for tc in load_test_cases()],
|
|
62
|
+
ids=[tc["name"] for tc in load_test_cases()],
|
|
63
|
+
)
|
|
64
|
+
def test_matcher_where(name: str, flyql: str, expected_ids: list[int]) -> None:
|
|
65
|
+
result: dict[str, Any] = {
|
|
66
|
+
"kind": "where",
|
|
67
|
+
"database": "matcher",
|
|
68
|
+
"name": name,
|
|
69
|
+
"flyql": flyql,
|
|
70
|
+
"sql": "(in-memory)",
|
|
71
|
+
"expected_ids": expected_ids,
|
|
72
|
+
"returned_ids": [],
|
|
73
|
+
"passed": False,
|
|
74
|
+
"error": "",
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try:
|
|
78
|
+
matched_ids = [row["id"] for row in ROWS if match_row(flyql, row)]
|
|
79
|
+
result["returned_ids"] = matched_ids
|
|
80
|
+
result["passed"] = ids_match(expected_ids, matched_ids)
|
|
81
|
+
_results.append(result)
|
|
82
|
+
assert result["passed"], f"expected {expected_ids}, got {matched_ids}"
|
|
83
|
+
except Exception as e:
|
|
84
|
+
result["error"] = str(e)
|
|
85
|
+
_results.append(result)
|
|
86
|
+
raise
|