diff --git a/djaosdb/sql2mongo/operators.py b/djaosdb/sql2mongo/operators.py index 59d37fd5935cf50033d678d3fc355f8811e6e309..1c3e412d42d735b283f9688fe248cd0c47853f66 100644 --- a/djaosdb/sql2mongo/operators.py +++ b/djaosdb/sql2mongo/operators.py @@ -547,8 +547,6 @@ class CmpOp(_Op): ref = self._identifier.table field = self._identifier.column - if field.endswith("_id"): - field = field[:-3] result = {"type": "pov", "negation": self.is_negated, "p": field, diff --git a/tests/test_new.py b/tests/test_new.py index 4cb203efc8b2984107d191a7257d31a19ff20e75..b9cc26ee9c08b29fdf34390f9e2530d8e2e1269a 100644 --- a/tests/test_new.py +++ b/tests/test_new.py @@ -99,7 +99,6 @@ def test_merge_joins_and_where_no_where_one_join(): "p": "p1", "v": "rt1", "negation": False, - "str": "REFERENCES rt1 AS p1", }] where = None @@ -110,7 +109,6 @@ def test_merge_joins_and_where_no_where_one_join(): "p": "p1", "v": "rt1", "negation": False, - "str": "REFERENCES rt1 AS p1", } def test_merge_joins_and_where_no_where_two_join(): @@ -119,13 +117,11 @@ def test_merge_joins_and_where_no_where_two_join(): "p": "p1", "v": "rt1", "negation": False, - "str": "REFERENCES rt1 AS p1", },{ "type": "reference", "p": "p2", "v": "rt2", "negation": False, - "str": "REFERENCES rt2 AS p2", }] where = None @@ -139,13 +135,11 @@ def test_merge_joins_and_where_no_where_two_join(): "p": "p1", "v": "rt1", "negation": False, - "str": "REFERENCES rt1 AS p1", },{ "type": "reference", "p": "p2", "v": "rt2", "negation": False, - "str": "REFERENCES rt2 AS p2", } ] } @@ -156,7 +150,6 @@ def test_merge_joins_and_where_independent_where(): "p": "p1", "v": "rt1", "negation": False, - "str": "REFERENCES rt1 AS p1", }] where = { "type": "pov", @@ -182,7 +175,6 @@ def test_merge_joins_and_where_independent_where(): "p": "p1", "v": "rt1", "negation": False, - "str": "REFERENCES rt1 AS p1", } ] } @@ -193,7 +185,6 @@ def test_merge_joins_and_where_backref_sub_query_id(): "p": "p1", "v": "rt1", "negation": False, - "str": "IS REFERENCED BY rt1 AS p1", }] where = { "type": "pov", @@ -212,7 +203,6 @@ def test_merge_joins_and_where_backref_sub_query_id(): "p": "p1", "v": "rt1", "negation": False, - "str": "IS REFERENCED BY rt1 AS p1", "sub": { "type": "pov", "ref": "rt1", @@ -231,7 +221,6 @@ def test_merge_joins_and_where_ref_sub_query_id(): "p": "p1", "v": "rt1", "negation": False, - "str": "REFERENCES rt1 AS p1", }] where = { "type": "in", @@ -246,7 +235,6 @@ def test_merge_joins_and_where_ref_sub_query_id(): "p": "p1", "v": "rt1", "negation": False, - "str": "REFERENCES rt1 AS p1", "sub": { "type": "in", "p": "ID", @@ -281,7 +269,6 @@ def test_parse_select_join(): "p": "content_type_id", "v": "django_content_type", "negation": False, - "str": "REFERENCES django_content_type AS content_type_id", } assert select_query._needs_aggregation() is True @@ -292,7 +279,6 @@ def test_parse_select_join(): "type": "reference", "p": "content_type_id", "v": "django_content_type", - "str": "REFERENCES django_content_type AS content_type_id", "negation": False, "sub": {"type": "in", "p": "ID", @@ -413,7 +399,6 @@ def test_inner_and_outer_join(): assert inner_join.to_mongo() == { "type": "reference", 'negation': False, 'p': 'b_id', - 'str': 'REFERENCES B AS b_id', 'v': 'B'} assert left_outer_join.to_mongo() == {} @@ -421,11 +406,11 @@ def test_inner_and_outer_join(): callback, pipeline = select_query._to_caosdb() assert callback == connection.aggregate assert "joins" not in pipeline + print(pipeline["filter"]) assert pipeline["filter"] == { "type": "reference", 'negation': False, 'p': 'b_id', - 'str': 'REFERENCES B AS b_id', 'v': 'B', "sub": { "type": "pov", @@ -455,21 +440,19 @@ def test_query_generation_conjuction(): 'p': 'auth_group_permissions.group_id', 'o': '=', 'v': 226 }, {'type': 'reference', 'p': 'content_type_id', - 'v': 'django_content_type', 'negation': False, - 'str': 'REFERENCES django_content_type AS content_type_id'}]}, + 'v': 'django_content_type', 'negation': False, }]}, count=False) assert query == ('SELECT id, django_content_type.app_label, ' 'django_content_type.model, codename FROM RECORD ' '"auth_permission" WITH auth_group_permissions.' - 'group_id="226" AND REFERENCES django_content_type ' - 'AS content_type_id') + 'group_id="226" AND REFERENCES django_content_type') def test_parse_select_join_with_reverse_on_clause(): sql = """ SELECT "A"."id", "A"."p1" FROM "A" INNER JOIN "B" - ON ("A"."id" = "B"."a_id") + ON ("A"."id" = "B"."a") WHERE "B"."p2" = %(0)s """ cached_record_types = [ @@ -499,8 +482,7 @@ def test_parse_select_join_with_reverse_on_clause(): assert inner_join.to_mongo() == { "type": "back_reference", - 'negation': False, 'p': 'a_id', - 'str': 'IS REFERENCED BY B AS a_id', + 'negation': False, 'p': 'a', 'v': 'B'} callback, pipeline = select_query._to_caosdb() @@ -510,8 +492,7 @@ def test_parse_select_join_with_reverse_on_clause(): assert pipeline["filter"] == { "type": "back_reference", 'negation': False, - 'p': 'a_id', - 'str': 'IS REFERENCED BY B AS a_id', + 'p': 'a', 'v': 'B', "sub": { "type": "pov", @@ -528,3 +509,69 @@ def test_parse_select_join_with_reverse_on_clause(): # excute select_query._get_cursor() + +def test_inner_join_with_bad_names(): + sql = """SELECT "A"."id", "A"."p1", "A"."p2" FROM + "A" INNER JOIN "B" ON ("A"."id" = + "B"."language_id") WHERE + "B"."material_id" = %(0)s""" + cached_record_types = [ + "A", + "B", + ] + params = ["227"] + connection = _MockConnection(cached_record_types=cached_record_types) + q = Query(connection=connection, sql=sql, params=params) + + assert isinstance(q._query, SelectQuery) + select_query = q._query + assert select_query.left_table == "A" + + assert select_query.where is not None + assert select_query.where.to_mongo() == { + "type": "pov", 'negation': False, + # TODO + 'o': '=', 'p': 'material_id', 'ref': "B", 'v': '227'} + + assert select_query._needs_aggregation() is True + + assert len(select_query.joins) == 1 + + inner_join = select_query.joins[0] + + assert isinstance(inner_join, InnerJoinConverter) + + assert inner_join.to_mongo() == { + "type": "back_reference", + # TODO + 'negation': False, 'p': 'language_id', + # TODO + 'v': 'B'} + + callback, pipeline = select_query._to_caosdb() + assert callback == connection.aggregate + assert "joins" not in pipeline + print(pipeline["filter"]) + assert pipeline["filter"] == { + "type": "back_reference", + 'negation': False, + # TODO + 'p': 'language_id', + # TODO + 'v': 'B', + "sub": { + "type": "pov", + 'negation': False, + 'ref': "B", + # TODO + 'p': 'material_id', + 'o': '=', + 'v': '227' + }, + } + + assert "sort" not in pipeline + assert pipeline["projection"] == ["id", "p1", "p2"] + + # excute + select_query._get_cursor()