Exemplo n.º 1
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()),
     )))
Exemplo n.º 2
0
 def test_list_query_string_includes_element_query(self):
     query = schema.ListType(schema.Int)()
     assert_that(str(query), equal_to(dedent("""
         ListQuery(
             type=List(Int),
             element_query=scalar_query,
         )
     """)))
Exemplo n.º 3
0
 def test_list_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.ListType(Item)(
         schema.key("length", Song.fields.length()),
         schema.key("length", Book.fields.length()),
     ).for_type(schema.ListType(Song))
     
     assert_that(query, is_query(schema.ListType(Song)(
         schema.key("length", Song.fields.length()),
     )))
Exemplo n.º 4
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",
         },
     ]))
Exemplo n.º 5
0
 def test_list_type_has_element_type_as_child_type(self):
     collected_types = schema.collect_types((schema.ListType(schema.String), ))
     assert_that(collected_types, contains_exactly(schema.ListType(schema.String), schema.String))
Exemplo n.º 6
0
    def test_cannot_change_type_of_list_query_to_non_list_query(self):
        query = schema.ListType(schema.Boolean)()

        error = pytest.raises(TypeError, lambda: query.for_type(schema.NullableType(schema.Boolean)))
        assert_that(str(error.value), equal_to("cannot coerce query for List(Boolean) to query for Nullable(Boolean)"))
Exemplo n.º 7
0
 def test_adding_list_query_to_list_query_of_different_element_type_raises_type_error(self):
     error = pytest.raises(TypeError, lambda: schema.ListType(schema.Boolean)() + schema.ListType(schema.Int)())
     assert_that(str(error.value), equal_to("cannot add queries for lists with different element types: Boolean and Int"))
Exemplo n.º 8
0
 def test_adding__query_to_non_list_query_raises_type_error(self):
     pytest.raises(TypeError, lambda: schema.ListType(schema.Boolean)() + schema.Boolean())
Exemplo n.º 9
0
 def test_lists_type_coerces_elements_to_element_type(self):
     self._assert_coercion(schema.ListType(schema.Float), [1, 2], [1.0, 2.0])
     assert_that(type(schema.ListType(schema.Float).coerce([1])[0]), equal_to(float))
Exemplo n.º 10
0
        collected_types = schema.collect_types((User, ))
        assert_that(collected_types, includes(schema.Boolean))

    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))


@pytest.mark.parametrize("graph_type, element_type", (
    (schema.ListType(schema.Boolean), schema.Boolean),
    (schema.NullableType(schema.Boolean), schema.Boolean),
    (schema.Boolean, schema.Boolean),
    (schema.ListType(schema.ListType(schema.Boolean)), schema.Boolean),
    (schema.NullableType(schema.NullableType(schema.Boolean)), schema.Boolean),
    (schema.ListType(schema.NullableType(schema.Boolean)), schema.Boolean),
))
def test_to_element_type(graph_type, element_type):
    assert_that(schema.to_element_type(graph_type), equal_to(element_type))


@pytest.mark.parametrize("graph_type, element_type, expected", (
    (schema.ListType(schema.Boolean), schema.String, schema.ListType(schema.String)),
    (schema.NullableType(schema.Boolean), schema.String, schema.NullableType(schema.String)),
    (schema.Boolean, schema.String, schema.String),
    (