Skip to content

Commit a114ae3

Browse files
authored
Merge pull request kensho-technologies#168 from kensho-technologies/add-sql-unittests
Adding unit-tests for SQL backend.
2 parents eebc64d + 5b2f6d7 commit a114ae3

File tree

7 files changed

+490
-130
lines changed

7 files changed

+490
-130
lines changed

graphql_compiler/compiler/ir_lowering_sql/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def lower_ir(ir_blocks, query_metadata_table, type_equivalence_hints=None):
5050

5151
# perform lowering steps
5252
ir_blocks = lower_unary_transformations(ir_blocks)
53+
ir_blocks = lower_unsupported_metafield_expressions(ir_blocks)
5354

5455
# iteratively construct SqlTree
5556
query_path_to_node = {}
@@ -110,7 +111,7 @@ def _validate_all_blocks_supported(ir_blocks, query_metadata_table):
110111
for field_name, field in six.iteritems(construct_result.fields):
111112
if not isinstance(field, constants.SUPPORTED_OUTPUT_EXPRESSION_TYPES):
112113
unsupported_fields.append((field_name, field))
113-
elif field_name in constants.UNSUPPORTED_META_FIELDS:
114+
elif field.location.field in constants.UNSUPPORTED_META_FIELDS:
114115
unsupported_fields.append((field_name, field))
115116

116117
if len(unsupported_blocks) > 0 or len(unsupported_fields) > 0:
@@ -249,3 +250,23 @@ def visitor_fn(expression):
249250
for block in ir_blocks
250251
]
251252
return new_ir_blocks
253+
254+
255+
def lower_unsupported_metafield_expressions(ir_blocks):
256+
"""Raise exception if an unsupported metafield is encountered in any LocalField expression."""
257+
def visitor_fn(expression):
258+
"""Visitor function raising exception for any unsupported metafield."""
259+
if not isinstance(expression, expressions.LocalField):
260+
return expression
261+
if expression.field_name not in constants.UNSUPPORTED_META_FIELDS:
262+
return expression
263+
raise NotImplementedError(
264+
u'Encountered unsupported metafield {} in LocalField {} during construction of '
265+
u'SQL query tree for IR blocks {}.'.format(
266+
constants.UNSUPPORTED_META_FIELDS[expression.field_name], expression, ir_blocks))
267+
268+
new_ir_blocks = [
269+
block.visit_and_update_expressions(visitor_fn)
270+
for block in ir_blocks
271+
]
272+
return new_ir_blocks

graphql_compiler/compiler/ir_lowering_sql/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
u'&&': Operator(u'and_', CARDINALITY_BINARY),
4949
u'||': Operator(u'or_', CARDINALITY_BINARY),
5050
u'=': Operator(u'__eq__', CARDINALITY_UNARY),
51+
u'!=': Operator(u'__ne__', CARDINALITY_UNARY),
5152
u'<': Operator(u'__lt__', CARDINALITY_UNARY),
5253
u'>': Operator(u'__gt__', CARDINALITY_UNARY),
5354
u'<=': Operator(u'__le__', CARDINALITY_UNARY),

graphql_compiler/tests/integration_tests/test_backends_integration.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ def test_simple_output(self, backend_name):
8888
{
8989
Animal {
9090
name @output(out_name: "animal_name")
91+
uuid @output(out_name: "animal_uuid")
9192
}
9293
}
9394
'''
9495
expected_results = [
95-
{'animal_name': 'Animal 1'},
96-
{'animal_name': 'Animal 2'},
97-
{'animal_name': 'Animal 3'},
98-
{'animal_name': 'Animal 4'},
96+
{'animal_name': 'Animal 1', 'animal_uuid': 'cfc6e625-8594-0927-468f-f53d864a7a51'},
97+
{'animal_name': 'Animal 2', 'animal_uuid': 'cfc6e625-8594-0927-468f-f53d864a7a52'},
98+
{'animal_name': 'Animal 3', 'animal_uuid': 'cfc6e625-8594-0927-468f-f53d864a7a53'},
99+
{'animal_name': 'Animal 4', 'animal_uuid': 'cfc6e625-8594-0927-468f-f53d864a7a54'},
99100
]
100101
self.assertResultsEqual(graphql_query, {}, backend_name, expected_results)
101102

@@ -144,4 +145,27 @@ def test_two_filters(self, backend_name):
144145

145146
self.assertResultsEqual(graphql_query, parameters, backend_name, expected_results)
146147

148+
@all_backends
149+
@integration_fixtures
150+
def test_has_substring_precedence(self, backend_name):
151+
graphql_query = '''
152+
{
153+
Animal {
154+
name @output(out_name: "animal_name")
155+
@filter(op_name: "has_substring", value: ["$wide_substring"])
156+
@filter(op_name: "has_substring", value: ["$narrow_substring"])
157+
}
158+
}
159+
'''
160+
parameters = {
161+
# matches all animal names
162+
'wide_substring': 'Animal',
163+
# narrows set to just ['Animal 3']
164+
'narrow_substring': '3',
165+
}
166+
expected_results = [
167+
{'animal_name': 'Animal 3'},
168+
]
169+
self.assertResultsEqual(graphql_query, parameters, backend_name, expected_results)
170+
147171
# pylint: enable=no-member

0 commit comments

Comments
 (0)