Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /TrueSelph_jvspatial /tests /db /test_sqlite_translate.py
| """Unit tests for the SQLite query translator. | |
| These tests verify the SQL generated by ``translate_query`` and | |
| ``translate_sort`` directly -- they don't require an actual SQLite | |
| connection. End-to-end behavior against SQLite is covered by | |
| ``test_sqlite_pushdown.py``. | |
| """ | |
| from jvspatial.db._sqlite_translate import ( | |
| translate_partial_filter_expression, | |
| translate_query, | |
| translate_sort, | |
| ) | |
| class TestEqualityPushdown: | |
| def test_plain_equality(self): | |
| sql, params = translate_query({"context.name": "alpha"}) | |
| assert sql == "json_extract(data, '$.context.name') = ?" | |
| assert params == ["alpha"] | |
| def test_plain_equality_int(self): | |
| sql, params = translate_query({"value": 42}) | |
| assert sql == "json_extract(data, '$.value') = ?" | |
| assert params == [42] | |
| def test_plain_equality_bool_coerced(self): | |
| sql, params = translate_query({"active": True}) | |
| assert sql == "json_extract(data, '$.active') = ?" | |
| assert params == [1] | |
| def test_plain_equality_none_uses_is_null(self): | |
| sql, params = translate_query({"deleted_at": None}) | |
| assert sql == "json_extract(data, '$.deleted_at') IS NULL" | |
| assert params == [] | |
| def test_eq_operator_form(self): | |
| sql, params = translate_query({"x": {"$eq": 5}}) | |
| assert sql == "json_extract(data, '$.x') = ?" | |
| assert params == [5] | |
| def test_ne_treats_null_as_not_equal(self): | |
| """``$ne: 5`` must match rows where x is missing/NULL too.""" | |
| sql, params = translate_query({"x": {"$ne": 5}}) | |
| assert sql == ( | |
| "(json_extract(data, '$.x') IS NULL " "OR json_extract(data, '$.x') <> ?)" | |
| ) | |
| assert params == [5] | |
| class TestComparisonPushdown: | |
| def test_gt_gte_lt_lte(self): | |
| sql, params = translate_query({"age": {"$gte": 18, "$lt": 65}}) | |
| # Order of operators inside one operator-dict is preserved by | |
| # Python's dict insertion order; both clauses ANDed. | |
| assert "json_extract(data, '$.age') >= ?" in sql | |
| assert "json_extract(data, '$.age') < ?" in sql | |
| assert " AND " in sql | |
| assert sorted(params) == [18, 65] | |
| def test_in_operator(self): | |
| sql, params = translate_query({"category": {"$in": ["a", "b", "c"]}}) | |
| assert sql == "json_extract(data, '$.category') IN (?,?,?)" | |
| assert params == ["a", "b", "c"] | |
| def test_in_empty_list_matches_nothing(self): | |
| sql, params = translate_query({"x": {"$in": []}}) | |
| assert sql == "0" | |
| assert params == [] | |
| def test_nin_operator_includes_nulls(self): | |
| sql, params = translate_query({"x": {"$nin": [1, 2]}}) | |
| assert sql == ( | |
| "(json_extract(data, '$.x') IS NULL " | |
| "OR json_extract(data, '$.x') NOT IN (?,?))" | |
| ) | |
| assert params == [1, 2] | |
| def test_nin_empty_list_matches_everything(self): | |
| sql, params = translate_query({"x": {"$nin": []}}) | |
| assert sql == "1" | |
| assert params == [] | |
| def test_exists_true(self): | |
| sql, params = translate_query({"x": {"$exists": True}}) | |
| assert sql == "json_extract(data, '$.x') IS NOT NULL" | |
| assert params == [] | |
| def test_exists_false(self): | |
| sql, params = translate_query({"x": {"$exists": False}}) | |
| assert sql == "json_extract(data, '$.x') IS NULL" | |
| assert params == [] | |
| class TestLogicalCombinators: | |
| def test_implicit_and_across_fields(self): | |
| sql, params = translate_query({"a": 1, "b": "two"}) | |
| assert sql == ( | |
| "json_extract(data, '$.a') = ? AND json_extract(data, '$.b') = ?" | |
| ) | |
| assert params == [1, "two"] | |
| def test_explicit_and(self): | |
| sql, params = translate_query({"$and": [{"a": 1}, {"b": "two"}]}) | |
| assert ( | |
| sql | |
| == "((json_extract(data, '$.a') = ?) AND (json_extract(data, '$.b') = ?))" | |
| ) | |
| assert params == [1, "two"] | |
| def test_or(self): | |
| sql, params = translate_query({"$or": [{"a": 1}, {"b": 2}]}) | |
| assert ( | |
| sql | |
| == "((json_extract(data, '$.a') = ?) OR (json_extract(data, '$.b') = ?))" | |
| ) | |
| assert params == [1, 2] | |
| def test_nested_or_and(self): | |
| sql, params = translate_query( | |
| { | |
| "$and": [ | |
| {"$or": [{"name": "alice"}, {"name": "bob"}]}, | |
| {"age": 25}, | |
| ] | |
| } | |
| ) | |
| # Just check it round-trips through the recursive translator. | |
| assert sql is not None | |
| assert "((((json_extract(data, '$.name') = ?) OR" in sql | |
| assert "json_extract(data, '$.age') = ?" in sql | |
| assert params == ["alice", "bob", 25] | |
| class TestFallbackTriggers: | |
| def test_regex_falls_back(self): | |
| assert translate_query({"name": {"$regex": "^a"}}) is None | |
| def test_elem_match_falls_back(self): | |
| assert translate_query({"tags": {"$elemMatch": {"x": 1}}}) is None | |
| def test_size_falls_back(self): | |
| assert translate_query({"items": {"$size": 3}}) is None | |
| def test_unsafe_field_falls_back(self): | |
| # Spaces in field names are unsafe. | |
| assert translate_query({"bad name": 1}) is None | |
| # Quote injection attempts. | |
| assert translate_query({"x'; DROP TABLE records--": 1}) is None | |
| # Empty key. | |
| assert translate_query({"": 1}) is None | |
| def test_unknown_top_level_op_falls_back(self): | |
| assert translate_query({"$nor": [{"x": 1}]}) is None | |
| def test_mixed_supported_and_unsupported_falls_back(self): | |
| assert translate_query({"x": {"$gt": 1, "$regex": "^a"}}) is None | |
| def test_hint_marker_is_ignored(self): | |
| sql, params = translate_query({"x": 1, "$hint": ["x"]}) | |
| assert sql == "json_extract(data, '$.x') = ?" | |
| assert params == [1] | |
| def test_select_marker_is_ignored(self): | |
| sql, params = translate_query({"x": 1, "$select": ["a"]}) | |
| assert sql == "json_extract(data, '$.x') = ?" | |
| assert params == [1] | |
| class TestSortPushdown: | |
| def test_single_key_asc(self): | |
| order = translate_sort([("name", 1)]) | |
| assert order == ( | |
| "(json_extract(data, '$.name') IS NULL), " | |
| "json_extract(data, '$.name') ASC" | |
| ) | |
| def test_single_key_desc(self): | |
| order = translate_sort([("name", -1)]) | |
| assert order == ( | |
| "(json_extract(data, '$.name') IS NULL), " | |
| "json_extract(data, '$.name') DESC" | |
| ) | |
| def test_multi_key(self): | |
| order = translate_sort([("a", 1), ("b", -1)]) | |
| assert "json_extract(data, '$.a') ASC" in order | |
| assert "json_extract(data, '$.b') DESC" in order | |
| def test_invalid_direction_falls_back(self): | |
| assert translate_sort([("a", 0)]) is None | |
| assert translate_sort([("a", 2)]) is None | |
| def test_unsafe_field_falls_back(self): | |
| assert translate_sort([("bad name", 1)]) is None | |
| def test_empty_sort_returns_none(self): | |
| assert translate_sort(None) is None | |
| assert translate_sort([]) is None | |
| class TestPartialFilterExpression: | |
| def test_conversation_session_id_shape(self): | |
| sql = translate_partial_filter_expression( | |
| { | |
| "context.session_id": {"$gt": ""}, | |
| "context.status": {"$gt": ""}, | |
| } | |
| ) | |
| assert sql == ( | |
| "json_extract(data, '$.context.session_id') > '' AND " | |
| "json_extract(data, '$.context.status') > ''" | |
| ) | |
| def test_eq_and_exists(self): | |
| sql = translate_partial_filter_expression( | |
| { | |
| "context.entity": {"$eq": "Agent"}, | |
| "context.name": {"$exists": True}, | |
| } | |
| ) | |
| assert sql == ( | |
| "json_extract(data, '$.context.entity') = 'Agent' AND " | |
| "json_extract(data, '$.context.name') IS NOT NULL" | |
| ) | |
| def test_plain_equality_and_bool(self): | |
| sql = translate_partial_filter_expression( | |
| {"context.active": True, "context.kind": "session"} | |
| ) | |
| assert sql == ( | |
| "json_extract(data, '$.context.active') = 1 AND " | |
| "json_extract(data, '$.context.kind') = 'session'" | |
| ) | |
| def test_string_literal_escapes_quotes(self): | |
| sql = translate_partial_filter_expression({"context.label": {"$eq": "O'Brien"}}) | |
| assert sql == "json_extract(data, '$.context.label') = 'O''Brien'" | |
| def test_unsupported_op_returns_none(self): | |
| assert ( | |
| translate_partial_filter_expression({"context.name": {"$regex": "^a"}}) | |
| is None | |
| ) | |
| def test_unsafe_path_returns_none(self): | |
| assert ( | |
| translate_partial_filter_expression({"context.bad-name": {"$gt": ""}}) | |
| is None | |
| ) | |
| def test_empty_or_non_dict_returns_none(self): | |
| assert translate_partial_filter_expression({}) is None | |
| assert translate_partial_filter_expression(None) is None # type: ignore[arg-type] | |
Xet Storage Details
- Size:
- 9.01 kB
- Xet hash:
- 722e752de9765887a801d3a9974461d51187d149cc2dc296388731f09deb46bf
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.