mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-04-23 02:22:08 +03:00
add test for annotations
This commit is contained in:
parent
3247eeacff
commit
077b5aa96f
|
@ -1120,3 +1120,55 @@ def test_should_preserve_prefetch_related(django_assert_num_queries):
|
|||
with django_assert_num_queries(3) as captured:
|
||||
result = schema.execute(query)
|
||||
assert not result.errors
|
||||
|
||||
|
||||
def test_should_preserve_annotations():
|
||||
class ReporterType(DjangoObjectType):
|
||||
class Meta:
|
||||
model = Reporter
|
||||
interfaces = (graphene.relay.Node,)
|
||||
|
||||
class FilmType(DjangoObjectType):
|
||||
reporters = DjangoConnectionField(ReporterType)
|
||||
reporters_count = graphene.Int()
|
||||
|
||||
class Meta:
|
||||
model = Film
|
||||
interfaces = (graphene.relay.Node,)
|
||||
|
||||
class Query(graphene.ObjectType):
|
||||
films = DjangoConnectionField(FilmType)
|
||||
|
||||
def resolve_films(root, info):
|
||||
qs = Film.objects.prefetch_related("reporters")
|
||||
return qs.annotate(reporters_count=models.Count("reporters"))
|
||||
|
||||
r1 = Reporter.objects.create(first_name="Dave", last_name="Smith")
|
||||
r2 = Reporter.objects.create(first_name="Jane", last_name="Doe")
|
||||
|
||||
f1 = Film.objects.create()
|
||||
f1.reporters.set([r1, r2])
|
||||
f2 = Film.objects.create()
|
||||
f2.reporters.set([r2])
|
||||
|
||||
query = """
|
||||
query {
|
||||
films {
|
||||
edges {
|
||||
node {
|
||||
reportersCount
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
schema = graphene.Schema(query=Query)
|
||||
result = schema.execute(query)
|
||||
assert not result.errors, str(result)
|
||||
|
||||
expected = {
|
||||
"films": {
|
||||
"edges": [{"node": {"reportersCount": 2}}, {"node": {"reportersCount": 1}}]
|
||||
}
|
||||
}
|
||||
assert result.data == expected, str(result.data)
|
||||
|
|
Loading…
Reference in New Issue
Block a user