Ejemplo n.º 1
0
 def test_object_query_string_includes_type_and_field_queries(self):
     Book = schema.ObjectType("Book", fields=(
         schema.field("title", schema.String),
         schema.field("publication_year", schema.Int),
     ))
     
     query = Book(
         schema.key("title", Book.fields.title()),
         schema.key("year", Book.fields.publication_year()),
     )
     
     assert_that(str(query), equal_to(dedent("""
         ObjectQuery(
             type=Book,
             fields=(
                 FieldQuery(
                     key="title",
                     field=Book.fields.title,
                     type_query=scalar_query,
                     args=(),
                 ),
                 FieldQuery(
                     key="year",
                     field=Book.fields.publication_year,
                     type_query=scalar_query,
                     args=(),
                 ),
             ),
         )
     """)))
Ejemplo n.º 2
0
 def test_object_type_has_interface_types_in_child_types(self):
     Person = schema.InterfaceType("Person", fields=lambda: (
         schema.field("name", type=schema.String),
     ))
     User = schema.ObjectType("User", fields=lambda: (
         schema.field("name", type=schema.String),
     ), interfaces=(Person, ))
     collected_types = schema.collect_types((User, ))
     assert_that(collected_types, includes(Person))
Ejemplo n.º 3
0
 def test_merged_object_query_has_fields_from_operand_queries(self):
     Song = schema.ObjectType("Song", fields=(
         schema.field("title", type=schema.String),
         schema.field("length", type=schema.Int),
     ))
     query = Song(schema.key("title", Song.fields.title())) + Song(schema.key("length", Song.fields.length()))
     assert_that(query, is_query(Song(
         schema.key("title", Song.fields.title()),
         schema.key("length", Song.fields.length()),
     )))
Ejemplo n.º 4
0
 def test_list_query_merges_element_queries(self):
     Song = schema.ObjectType("Song", fields=(
         schema.field("title", type=schema.String),
         schema.field("length", type=schema.Int),
     ))
     query = (
         schema.ListType(Song)(schema.key("title", Song.fields.title())) +
         schema.ListType(Song)(schema.key("length", Song.fields.length()))
     )
     assert_that(query, is_query(schema.ListType(Song)(
         schema.key("title", Song.fields.title()),
         schema.key("length", Song.fields.length()),
     )))
Ejemplo n.º 5
0
    def test_cannot_convert_query_for_object_to_unrelated_interface(self):
        Item = schema.InterfaceType("Item", fields=(
            schema.field("title", type=schema.String),
        ))
        Song = schema.ObjectType("Song", fields=(
            schema.field("title", type=schema.String),
        ))
        query = Song(
            schema.key("title", Song.fields.title()),
        )

        error = pytest.raises(TypeError, lambda: query.for_type(Item))
        assert_that(str(error.value), equal_to("cannot coerce query for Song to query for Item"))
Ejemplo n.º 6
0
    def test_cannot_convert_query_for_interface_to_unrelated_object(self):
        Item = schema.InterfaceType("Item", fields=(
            schema.field("title", type=schema.String),
        ))
        Book = schema.ObjectType("Book", fields=(
            schema.field("title", type=schema.String),
        ))
        query = Item(
            schema.key("title", Item.fields.title()),
        )

        error = pytest.raises(TypeError, lambda: query.for_type(Book))
        assert_that(str(error.value), equal_to("cannot coerce query for Item to query for Book"))
Ejemplo n.º 7
0
 def test_when_value_is_none_then_nullable_value_is_converted_to_none(self):
     Book = schema.ObjectType("Book", fields=(
         schema.field("title", schema.String),
     ))
     NullableBook = schema.NullableType(Book)
     query = NullableBook(schema.key("book_title", Book.fields.title()))
     assert_that(query.to_json_value(None), equal_to(None))
Ejemplo n.º 8
0
 def test_fields_with_same_key_for_different_types_are_kept_separate(self):
     Item = schema.InterfaceType("Item", fields=())
     Song = schema.ObjectType("Song", interfaces=(Item, ), fields=(
         schema.field("title", type=schema.String),
     ))
     Book = schema.ObjectType("Book", interfaces=(Item, ), fields=(
         schema.field("title", type=schema.String),
     ))
     query = (
         Item(schema.key("title", Song.fields.title())) +
         Item(schema.key("title", Book.fields.title()))
     )
     assert_that(query, is_query(Item(
         schema.key("title", Song.fields.title()),
         schema.key("title", Book.fields.title()),
     )))
Ejemplo n.º 9
0
 def test_object_type_has_field_param_types_in_child_types(self):
     User = schema.ObjectType("User", fields=lambda: (
         schema.field("name", type=schema.String, params=(
             schema.param("long", type=schema.Boolean),
         )),
     ))
     collected_types = schema.collect_types((User, ))
     assert_that(collected_types, includes(schema.Boolean))
Ejemplo n.º 10
0
    def test_converting_query_for_object_to_interface_keeps_fields_for_object(self):
        Item = schema.InterfaceType("Item", fields=(
            schema.field("title", type=schema.String),
        ))
        Song = schema.ObjectType("Song", interfaces=(Item, ), fields=(
            schema.field("length", type=schema.Int),
            schema.field("title", type=schema.String),
        ))
        query = Song(
            schema.key("title", Song.fields.title()),
            schema.key("length", Song.fields.length()),
        )

        assert_that(query.for_type(Item), is_query(Item(
            schema.key("title", Song.fields.title()),
            schema.key("length", Song.fields.length()),
        )))
Ejemplo n.º 11
0
 def test_nullable_type_for_type_calls_for_type_on_element_query(self):
     Item = schema.InterfaceType("Item", fields=(
     ))
     Song = schema.ObjectType("Song", interfaces=(Item, ), fields=(
         schema.field("length", type=schema.Int),
     ))
     Book = schema.ObjectType("Book", interfaces=(Item, ), fields=(
         schema.field("length", type=schema.Int),
     ))
     query = schema.NullableType(Item)(
         schema.key("length", Song.fields.length()),
         schema.key("length", Book.fields.length()),
     ).for_type(schema.NullableType(Song))
     
     assert_that(query, is_query(schema.NullableType(Song)(
         schema.key("length", Song.fields.length()),
     )))
Ejemplo n.º 12
0
def test_when_field_does_not_exist_on_object_type_then_error_is_raised():
    book = schema.ObjectType("Book", fields=(
        schema.field("title", schema.String),
    ))
    
    error = pytest.raises(ValueError, lambda: book.fields.author)
    
    assert_that(str(error.value), equal_to("Book has no field author"))
Ejemplo n.º 13
0
 def test_objects_are_converted_to_dicts(self):
     Book = schema.ObjectType("Book", fields=(
         schema.field("title", schema.String),
     ))
     query = Book(schema.key("book_title", Book.fields.title()))
     value = Object(dict(book_title="Orbiting the Giant Hairball"))
     assert_that(query.to_json_value(value), equal_to({
         "book_title": "Orbiting the Giant Hairball",
     }))
Ejemplo n.º 14
0
 def test_creating_field_query_specialises_type_of_type_query(self):
     Root = schema.ObjectType("Root", fields=lambda: (
         schema.field("song", type=Song),
     ))
     Item = schema.InterfaceType("Item", fields=(
         schema.field("title", type=schema.String),
     ))
     Song = schema.ObjectType("Song", interfaces=(Item, ), fields=(
         schema.field("title", type=schema.String),
     ))
     field_query = Root.fields.song.query(key="song", args=(), type_query=Item(
         schema.key("title", Item.fields.title()),
     ))
     
     assert_that(field_query, is_query(
         Root.fields.song.query(key="song", args=(), type_query=Song(
             schema.key("title", Song.fields.title()),
         )),
     ))
Ejemplo n.º 15
0
 def test_when_value_is_not_none_then_nullable_value_is_converted_using_element_query(self):
     Book = schema.ObjectType("Book", fields=(
         schema.field("title", schema.String),
     ))
     NullableBook = schema.NullableType(Book)
     query = NullableBook(schema.key("book_title", Book.fields.title()))
     value = Object(dict(book_title="Orbiting the Giant Hairball"))
     assert_that(query.to_json_value(value), equal_to({
         "book_title": "Orbiting the Giant Hairball",
     }))
Ejemplo n.º 16
0
def test_arguments_are_coerced():
    Root = schema.ObjectType(
        "Root",
        fields=(
            schema.field("one", type=schema.Int, params=[
                schema.param("arg", type=schema.Int),
            ]),
        ),
    )

    error = pytest.raises(GraphError, lambda: Root.fields.one(Root.fields.one.params.arg(None)))
    assert_that(str(error.value), equal_to("cannot coerce None to Int"))
Ejemplo n.º 17
0
 def test_lists_convert_elements_to_json_values(self):
     Book = schema.ObjectType("Book", fields=(
         schema.field("title", schema.String),
     ))
     BookList = schema.ListType(Book)
     query = BookList(schema.key("book_title", Book.fields.title()))
     value = Object(dict(book_title="Orbiting the Giant Hairball"))
     assert_that(query.to_json_value([value]), equal_to([
         {
             "book_title": "Orbiting the Giant Hairball",
         },
     ]))
Ejemplo n.º 18
0
def test_given_field_arg_has_no_default_when_field_arg_is_not_set_then_error_is_raised():
    Root = schema.ObjectType(
        "Root",
        fields=(
            schema.field("one", type=schema.Int, params=[
                schema.param("arg0", type=schema.Int),
            ]),
        ),
    )
    
    error = pytest.raises(ValueError, lambda: Root.fields.one())
    assert_that(str(error.value), equal_to("missing value for arg0"))
Ejemplo n.º 19
0
 def test_fields_are_recursively_merged(self):
     User = schema.ObjectType(
         "User",
         fields=lambda: (
             schema.field("address", type=Address),
         ),
     )
     
     Address = schema.ObjectType(
         "Address",
         fields=lambda: (
             schema.field("first_line", type=schema.String),
             schema.field("city", type=schema.String),
             schema.field("postcode", type=schema.String),
         ),
     )
     
     left_query = User(
         schema.key("address", User.fields.address(
             schema.key("first_line", Address.fields.first_line()),
             schema.key("city", Address.fields.city()),
         )),
     )
     right_query = User(
         schema.key("address", User.fields.address(
             schema.key("city", Address.fields.city()),
             schema.key("postcode", Address.fields.postcode()),
         )),
     )
     
     assert_that(left_query + right_query, is_query(
         User(
             schema.key("address", User.fields.address(
                 schema.key("first_line", Address.fields.first_line()),
                 schema.key("city", Address.fields.city()),
                 schema.key("postcode", Address.fields.postcode()),
             )),
         ),
     ))
Ejemplo n.º 20
0
 def test_object_type_for_type_filters_fields_to_those_for_type(self):
     Item = schema.InterfaceType("Item", fields=(
         schema.field("title", type=schema.String),
     ))
     Song = schema.ObjectType("Song", interfaces=(Item, ), fields=(
         schema.field("length", type=schema.Int),
         schema.field("title", type=schema.String),
     ))
     Book = schema.ObjectType("Book", interfaces=(Item, ), fields=(
         schema.field("length", type=schema.Int),
         schema.field("title", type=schema.String),
     ))
     query = Item(
         schema.key("title", Item.fields.title()),
         schema.key("length", Song.fields.length()),
         schema.key("length", Book.fields.length()),
     )
     
     assert_that(query.for_type(Song), is_query(Song(
         schema.key("title", Song.fields.title()),
         schema.key("length", Song.fields.length()),
     )))
Ejemplo n.º 21
0
 def test_objects_convert_fields_to_json_values(self):
     Author = schema.ObjectType("Author", fields=(
         schema.field("name", schema.String),
     ))
     Book = schema.ObjectType("Book", fields=(
         schema.field("author", Author),
     ))
     query = Book(
         schema.key("author", Book.fields.author(
             schema.key("name", Author.fields.name()),
         )),
     )
     value = Object(dict(
         author=Object(dict(
             name="Gordon A. Mackenzie",
         )),
     ))
     assert_that(query.to_json_value(value), equal_to({
         "author": {
             "name": "Gordon A. Mackenzie",
         },
     }))
Ejemplo n.º 22
0
 def test_field_query_string_includes_key_and_field_and_type_query(self):
     Book = schema.ObjectType("Book", fields=(
         schema.field("title", schema.String),
     ))
     
     query = schema.key("title", Book.fields.title())
     
     assert_that(query.to_string(Book), equal_to(dedent("""
         FieldQuery(
             key="title",
             field=Book.fields.title,
             type_query=scalar_query,
             args=(),
         )
     """)))
Ejemplo n.º 23
0
def test_given_field_arg_has_default_when_field_arg_is_not_set_then_default_is_used():
    Root = schema.ObjectType(
        "Root",
        fields=(
            schema.field("one", type=schema.Int, params=[
                schema.param("arg0", type=schema.Int, default=None),
                schema.param("arg1", type=schema.Int, default=42),
            ]),
        ),
    )
    
    field_query = Root.fields.one()
    assert_that(field_query.args, has_attrs(
        arg0=None,
        arg1=42,
    ))
Ejemplo n.º 24
0
 def test_field_can_be_from_subtype(self):
     Item = schema.InterfaceType("Item", fields=())
     
     Book = schema.ObjectType("Book", fields=(
         schema.field("title", schema.String),
     ), interfaces=(Item, ))
     
     query = schema.key("title", Book.fields.title())
     
     assert_that(query.to_string(Item), equal_to(dedent("""
         FieldQuery(
             key="title",
             field=Book.fields.title,
             type_query=scalar_query,
             args=(),
         )
     """)))
Ejemplo n.º 25
0
 def test_field_query_string_includes_args(self):
     Book = schema.ObjectType("Book", fields=(
         schema.field("title", schema.String, params=(
             schema.param("truncate", schema.Int),
         )),
     ))
     
     query = schema.key("title", Book.fields.title(Book.fields.title.params.truncate(42)))
     
     assert_that(query.to_string(Book), equal_to(dedent("""
         FieldQuery(
             key="title",
             field=Book.fields.title,
             type_query=scalar_query,
             args=(
                 Book.fields.title.params.truncate(42),
             ),
         )
     """)))
Ejemplo n.º 26
0
 def test_calling_field_with_unknown_types_raises_error(self):
     field = schema.field("x", type=schema.Int)
     error = pytest.raises(GraphError, lambda: field(42))
     assert_that(str(error.value), equal_to("unexpected argument: 42\nExpected arguments of type Argument or FieldQuery but had type int"))
Ejemplo n.º 27
0
 def test_object_type_has_field_types_in_child_types(self):
     User = schema.ObjectType("User", fields=lambda: (
         schema.field("name", type=schema.String),
     ))
     collected_types = schema.collect_types((User, ))
     assert_that(collected_types, includes(schema.String))
Ejemplo n.º 28
0
def test_fields_are_accessible_with_trailing_underscore():
    fields = schema.Fields("Book", (
        schema.field("class", type=schema.String),
    ))
    assert_that(fields.class_, equal_to(getattr(fields, "class")))