Beispiel #1
0
def test_prints_minimal_ast():
    ast = Field(name=Name(loc=None, value='foo'),
                loc=None,
                alias=None,
                arguments=None,
                directives=None,
                selection_set=None)
    assert print_ast(ast) == 'foo'
def test_it_concatenates_two_acts_together():
    source_a = Source('{ a, b, ... Frag }')
    source_b = Source('''
        fragment Frag on T {
            c
        }
    ''')

    ast_a = parse(source_a)
    ast_b = parse(source_b)
    ast_c = concat_ast([ast_a, ast_b])

    assert print_ast(ast_c) == '''{
Beispiel #3
0
def test_prints_kitchen_sink():
    ast = parse(SCHEMA_KITCHEN_SINK)
    printed = print_ast(ast)

    expected = '''type Foo implements Bar {
  one: Type
  two(argument: InputType!): Type
  three(argument: InputType, other: String): Int
  four(argument: String = "string"): String
  five(argument: [String] = ["string", "string"]): String
  six(argument: InputType = {key: "value"}): Type
}

interface Bar {
  one: Type
  four(argument: String = "string"): String
}

union Feed = Story | Article | Advert

scalar CustomScalar

enum Site {
  DESKTOP
  MOBILE
}

input InputType {
  key: String!
  answer: Int = 42
}

extend type Foo {
  seven(argument: [String]): Type
}
'''

    assert printed == expected
def test_prints_kitchen_sink():
    ast = parse(SCHEMA_KITCHEN_SINK)
    printed = print_ast(ast)

    expected = '''type Foo implements Bar {
  one: Type
  two(argument: InputType!): Type
  three(argument: InputType, other: String): Int
  four(argument: String = "string"): String
  five(argument: [String] = ["string", "string"]): String
  six(argument: InputType = {key: "value"}): Type
}

interface Bar {
  one: Type
  four(argument: String = "string"): String
}

union Feed = Story | Article | Advert

scalar CustomScalar

enum Site {
  DESKTOP
  MOBILE
}

input InputType {
  key: String!
  answer: Int = 42
}

extend type Foo {
  seven(argument: [String]): Type
}
'''

    assert printed == expected
def test_does_not_alter_ast():
    ast = parse(SCHEMA_KITCHEN_SINK)
    ast_copy = deepcopy(ast)
    print_ast(ast)
    assert ast == ast_copy
def test_print_produces_helpful_error_messages():
    bad_ast = {'random': 'Data'}
    with raises(AssertionError) as excinfo:
        print_ast(bad_ast)

    assert "Invalid AST Node: {'random': 'Data'}" in str(excinfo.value)
Beispiel #7
0
def test_prints_minimal_ast():
    node = ast.ScalarTypeDefinition(name=ast.Name('foo'))

    assert print_ast(node) == 'scalar foo'
Beispiel #8
0
def test_does_not_alter_ast():
    ast = parse(SCHEMA_KITCHEN_SINK)
    ast_copy = deepcopy(ast)
    print_ast(ast)
    assert ast == ast_copy
Beispiel #9
0
def test_print_produces_helpful_error_messages():
    bad_ast = {'random': 'Data'}
    with raises(AssertionError) as excinfo:
        print_ast(bad_ast)

    assert "Invalid AST Node: {'random': 'Data'}" in str(excinfo.value)
def test_correctly_prints_mutation_operation_without_name():
    mutation_ast = parse('mutation { id, name }')
    assert print_ast(mutation_ast) == '''mutation {
Beispiel #11
0
def test_prints_kitchen_sink():
    ast = parse(KITCHEN_SINK)
    printed = print_ast(ast)
    assert printed == '''query queryName($foo: ComplexType, $site: Site = MOBILE) {
Beispiel #12
0
def test_produces_helpful_error_messages():
    bad_ast = {'random': 'Data'}
    with raises(Exception) as excinfo:
        print_ast(bad_ast)
    assert 'Invalid AST Node' in str(excinfo.value)
Beispiel #13
0
def test_prints_minimal_ast():
    ast = Field(name=Name(loc=None, value='foo'))
    assert print_ast(ast) == 'foo'
def test_correctly_prints_mutation_with_artifacts():
    query_ast_shorthanded = parse(
      'mutation ($foo: TestType) @testDirective { id, name }'
    )
    assert print_ast(query_ast_shorthanded) == '''mutation ($foo: TestType) @testDirective {
def test_visits_with_typeinfo_maintains_type_info_during_edit():
    visited = []
    ast = parse('{ human(id: 4) { name, pets }, alien }')

    type_info = TypeInfo(test_schema)

    class TestVisitor(Visitor):

        def enter(self, node, key, parent, *args):
            parent_type = type_info.get_parent_type()
            _type = type_info.get_type()
            input_type = type_info.get_input_type()
            visited.append([
                'enter',
                type(node).__name__,
                node.value if type(node).__name__ == "Name" else None,
                str(parent_type) if parent_type else None,
                str(_type) if _type else None,
                str(input_type) if input_type else None
            ])

            # Make a query valid by adding missing selection sets.
            if type(node).__name__ == "Field" and not node.selection_set and is_composite_type(get_named_type(_type)):
                return Field(
                    alias=node.alias,
                    name=node.name,
                    arguments=node.arguments,
                    directives=node.directives,
                    selection_set=SelectionSet(
                        [Field(name=Name(value='__typename'))]
                    )
                )

        def leave(self, node, key, parent, *args):
            parent_type = type_info.get_parent_type()
            _type = type_info.get_type()
            input_type = type_info.get_input_type()
            visited.append([
                'leave',
                type(node).__name__,
                node.value if type(node).__name__ == "Name" else None,
                str(parent_type) if parent_type else None,
                str(_type) if _type else None,
                str(input_type) if input_type else None
            ])

    edited_ast = visit(ast, TypeInfoVisitor(type_info, TestVisitor()))

    # assert print_ast(ast) == print_ast(parse(
    #     '{ human(id: 4) { name, pets }, alien }'
    # ))
    assert print_ast(edited_ast) == print_ast(parse(
        '{ human(id: 4) { name, pets { __typename } }, alien { __typename } }'
    ))
    assert visited == [
        ['enter', 'Document', None, None, None, None],
        ['enter', 'OperationDefinition', None, None, 'QueryRoot', None],
        ['enter', 'SelectionSet', None, 'QueryRoot', 'QueryRoot', None],
        ['enter', 'Field', None, 'QueryRoot', 'Human', None],
        ['enter', 'Name', 'human', 'QueryRoot', 'Human', None],
        ['leave', 'Name', 'human', 'QueryRoot', 'Human', None],
        ['enter', 'Argument', None, 'QueryRoot', 'Human', 'ID'],
        ['enter', 'Name', 'id', 'QueryRoot', 'Human', 'ID'],
        ['leave', 'Name', 'id', 'QueryRoot', 'Human', 'ID'],
        ['enter', 'IntValue', None, 'QueryRoot', 'Human', 'ID'],
        ['leave', 'IntValue', None, 'QueryRoot', 'Human', 'ID'],
        ['leave', 'Argument', None, 'QueryRoot', 'Human', 'ID'],
        ['enter', 'SelectionSet', None, 'Human', 'Human', None],
        ['enter', 'Field', None, 'Human', 'String', None],
        ['enter', 'Name', 'name', 'Human', 'String', None],
        ['leave', 'Name', 'name', 'Human', 'String', None],
        ['leave', 'Field', None, 'Human', 'String', None],
        ['enter', 'Field', None, 'Human', '[Pet]', None],
        ['enter', 'Name', 'pets', 'Human', '[Pet]', None],
        ['leave', 'Name', 'pets', 'Human', '[Pet]', None],
        ['enter', 'SelectionSet', None, 'Pet', '[Pet]', None],
        ['enter', 'Field', None, 'Pet', 'String!', None],
        ['enter', 'Name', '__typename', 'Pet', 'String!', None],
        ['leave', 'Name', '__typename', 'Pet', 'String!', None],
        ['leave', 'Field', None, 'Pet', 'String!', None],
        ['leave', 'SelectionSet', None, 'Pet', '[Pet]', None],
        ['leave', 'Field', None, 'Human', '[Pet]', None],
        ['leave', 'SelectionSet', None, 'Human', 'Human', None],
        ['leave', 'Field', None, 'QueryRoot', 'Human', None],
        ['enter', 'Field', None, 'QueryRoot', 'Alien', None],
        ['enter', 'Name', 'alien', 'QueryRoot', 'Alien', None],
        ['leave', 'Name', 'alien', 'QueryRoot', 'Alien', None],
        ['enter', 'SelectionSet', None, 'Alien', 'Alien', None],
        ['enter', 'Field', None, 'Alien', 'String!', None],
        ['enter', 'Name', '__typename', 'Alien', 'String!', None],
        ['leave', 'Name', '__typename', 'Alien', 'String!', None],
        ['leave', 'Field', None, 'Alien', 'String!', None],
        ['leave', 'SelectionSet', None, 'Alien', 'Alien', None],
        ['leave', 'Field', None, 'QueryRoot', 'Alien', None],
        ['leave', 'SelectionSet', None, 'QueryRoot', 'QueryRoot', None],
        ['leave', 'OperationDefinition', None, None, 'QueryRoot', None],
        ['leave', 'Document', None, None, None, None]
    ]
def test_prints_minimal_ast():
    node = ast.ScalarTypeDefinition(
        name=ast.Name('foo')
    )

    assert print_ast(node) == 'scalar foo'
def test_correctly_prints_query_operation_without_name():
    query_ast_shorthanded = parse('query { id, name }')
    assert print_ast(query_ast_shorthanded) == '''{