Skip to content

Commit

Permalink
Test macro edge expansion (#190)
Browse files Browse the repository at this point in the history
* Add tests

* Separate macro expansion tests that expect errors

* Fix broken @output directives

* Add docstring for test_helpers method

* Add tests

* Separate macro expansion tests that expect errors

* Fix broken @output directives

* Add docstring for test_helpers method

* Fix linter errors

* Fix test

* Add tests, fix tests

* Add tests

* Add tests

* Add test docstrin

* Add tests

* Add tests

* Add expected answers

* Add test with inline fragment directive merging

* More tests
  • Loading branch information
bojanserafimov committed Feb 13, 2019
1 parent 15c5c49 commit 8026ed5
Show file tree
Hide file tree
Showing 5 changed files with 1,049 additions and 13 deletions.
2 changes: 1 addition & 1 deletion graphql_compiler/macros/macro_edge/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def get_and_validate_macro_edge_info(schema, ast, macro_edge_args,
_validate_class_selection_ast(
get_only_selection_from_ast(ast, GraphQLInvalidMacroError), macro_defn_ast)
class_name = get_ast_field_name(macro_defn_ast)
macro_edge_name = macro_defn_directive.arguments['name'].value
macro_edge_name = macro_defn_directive.arguments[0].value.value

_validate_macro_edge_name_for_class_name(schema, class_name, macro_edge_name)

Expand Down
121 changes: 121 additions & 0 deletions graphql_compiler/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import six

from ..debugging_utils import pretty_print_gremlin, pretty_print_match
from ..macros import create_macro_registry, register_macro_edge
from ..query_formatting.graphql_formatting import pretty_print_graphql
from ..schema import insert_meta_fields_into_existing_schema


Expand Down Expand Up @@ -47,6 +49,14 @@ def compare_ir_blocks(test_case, expected_blocks, received_blocks):
u'{}'.format(i, expected, received, mismatch_message))


def compare_graphql(test_case, expected, received):
"""Compare the expected and received GraphQL code, ignoring whitespace."""
msg = '\n{}\n\n!=\n\n{}'.format(
pretty_print_graphql(expected),
pretty_print_graphql(received))
compare_ignoring_whitespace(test_case, expected, received, msg)


def compare_match(test_case, expected, received, parameterized=True):
"""Compare the expected and received MATCH code, ignoring whitespace."""
msg = '\n{}\n\n!=\n\n{}'.format(
Expand Down Expand Up @@ -236,3 +246,114 @@ def construct_location_types(location_types_as_strings):
location: schema.get_type(type_name)
for location, type_name in six.iteritems(location_types_as_strings)
}


def get_test_macro_registry():
"""Return a MacroRegistry object containing macros used in tests."""
schema = get_schema()
macro_registry = create_macro_registry()
type_equivalence_hints = {
schema.get_type('Event'): schema.get_type('EventOrBirthEvent'),
}

valid_macros = [
('''{
Animal @macro_edge_definition(name: "out_Animal_GrandparentOf") {
out_Animal_ParentOf {
out_Animal_ParentOf @macro_edge_target {
uuid
}
}
}
}''', {}),
('''{
Animal @macro_edge_definition(name: "out_Animal_GrandchildrenCalledNate") {
out_Animal_ParentOf {
out_Animal_ParentOf @filter(op_name: "name_or_alias", value: ["$wanted"])
@macro_edge_target {
uuid
}
}
}
}''', {
'name': 'Nate',
}),
('''{
Animal @macro_edge_definition(name: "out_Animal_RichSiblings") {
in_Animal_ParentOf {
net_worth @tag(tag_name: "parent_net_worth")
out_Animal_ParentOf @macro_edge_target {
net_worth @filter(op_name: ">", value: ["%parent_net_worth"])
out_Animal_BornAt {
event_date @filter(op_name: "<", value: ["%birthday"])
}
}
}
}
}''', {}),
('''{
Location @macro_edge_definition(name: "out_Location_Orphans") {
in_Animal_LivesIn @macro_edge_target {
in_Animal_ParentOf @filter(op_name: "has_edge_degree", value: ["$num_parents"])
@optional {
uuid
}
}
}
}''', {
'num_parents': 0,
}),
('''{
Animal @macro_edge_definition(name: "out_Animal_RichYoungerSiblings") {
net_worth @tag(tag_name: "net_worth")
out_Animal_BornAt {
event_date @tag(tag_name: "birthday")
}
in_Animal_ParentOf {
out_Animal_ParentOf @macro_edge_target {
net_worth @filter(op_name: ">", value: ["%net_worth"])
out_Animal_BornAt {
event_date @filter(op_name: "<", value: ["%birthday"])
}
}
}
}
}''', {}),
('''{
Animal @macro_edge_definition(name: "out_Animal_AvailableFood") {
out_Animal_LivesIn {
in_Entity_Related {
... on Food @macro_edge_target {
uuid
}
}
}
}
}''', {}),
('''{
Animal @macro_edge_definition(name: "out_Animal_NearbyEvents") {
out_Animal_LivesIn {
in_Entity_Related @macro_edge_target {
... on Event {
uuid
}
}
}
}
}''', {}),
('''{
Animal @macro_edge_definition(name: "out_Animal_NearbyEntities") {
out_Animal_LivesIn {
in_Entity_Related {
... on Entity @macro_edge_target {
uuid
}
}
}
}
}''', {}),
]

for graphql, args in valid_macros:
register_macro_edge(macro_registry, schema, graphql, args, type_equivalence_hints)
return macro_registry

0 comments on commit 8026ed5

Please sign in to comment.