|
2 | 2 |
|
3 | 3 | import graphene
|
4 | 4 | from graphene.contrib.django import DjangoNode, DjangoConnectionField
|
5 |
| -from graphene.contrib.django.filter import DjangoFilterConnectionField |
| 5 | +from graphene.contrib.django.utils import DJANGO_FILTER_INSTALLED |
6 | 6 |
|
7 | 7 | from ...tests.models import Reporter
|
8 | 8 | from ..plugin import DjangoDebugPlugin
|
9 | 9 |
|
10 | 10 | # from examples.starwars_django.models import Character
|
11 | 11 |
|
12 |
| -from django.db.models import Count |
13 |
| - |
14 | 12 | pytestmark = pytest.mark.django_db
|
15 | 13 |
|
16 | 14 |
|
17 |
| -def count(qs): |
18 |
| - query = qs.query |
19 |
| - query.add_annotation(Count('*'), alias='__count', is_summary=True) |
20 |
| - query.select = [] |
21 |
| - query.default_cols = False |
22 |
| - return query |
23 |
| - |
24 |
| - |
25 | 15 | def test_should_query_field():
|
26 | 16 | r1 = Reporter(last_name='ABA')
|
27 | 17 | r1.save()
|
@@ -154,21 +144,21 @@ def resolve_all_reporters_connection(self, *args, **kwargs):
|
154 | 144 | }
|
155 | 145 | }]
|
156 | 146 | },
|
157 |
| - '__debug': { |
158 |
| - 'sql': [{ |
159 |
| - 'rawSql': str(count(Reporter.objects.all())) |
160 |
| - }, { |
161 |
| - 'rawSql': str(Reporter.objects.all()[:1].query) |
162 |
| - }] |
163 |
| - } |
164 | 147 | }
|
165 | 148 | schema = graphene.Schema(query=Query, plugins=[DjangoDebugPlugin()])
|
166 | 149 | result = schema.execute(query)
|
167 | 150 | assert not result.errors
|
168 |
| - assert result.data == expected |
| 151 | + assert result.data['allReporters'] == expected['allReporters'] |
| 152 | + assert 'COUNT' in result.data['__debug']['sql'][0]['rawSql'] |
| 153 | + query = str(Reporter.objects.all()[:1].query) |
| 154 | + assert result.data['__debug']['sql'][1]['rawSql'] == query |
169 | 155 |
|
170 | 156 |
|
| 157 | +@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED, |
| 158 | + reason="requires django-filter") |
171 | 159 | def test_should_query_connectionfilter():
|
| 160 | + from graphene.contrib.django.filter import DjangoFilterConnectionField |
| 161 | + |
172 | 162 | r1 = Reporter(last_name='ABA')
|
173 | 163 | r1.save()
|
174 | 164 | r2 = Reporter(last_name='Griffin')
|
@@ -209,15 +199,11 @@ def resolve_all_reporters_connection_filter(self, *args, **kwargs):
|
209 | 199 | }
|
210 | 200 | }]
|
211 | 201 | },
|
212 |
| - '__debug': { |
213 |
| - 'sql': [{ |
214 |
| - 'rawSql': str(count(Reporter.objects.all())) |
215 |
| - }, { |
216 |
| - 'rawSql': str(Reporter.objects.all()[:1].query) |
217 |
| - }] |
218 |
| - } |
219 | 202 | }
|
220 | 203 | schema = graphene.Schema(query=Query, plugins=[DjangoDebugPlugin()])
|
221 | 204 | result = schema.execute(query)
|
222 | 205 | assert not result.errors
|
223 |
| - assert result.data == expected |
| 206 | + assert result.data['allReporters'] == expected['allReporters'] |
| 207 | + assert 'COUNT' in result.data['__debug']['sql'][0]['rawSql'] |
| 208 | + query = str(Reporter.objects.all()[:1].query) |
| 209 | + assert result.data['__debug']['sql'][1]['rawSql'] == query |
0 commit comments