Skip to content

Commit 8324d47

Browse files
committed
Merge branch 'v2' into main
2 parents c049ab7 + 40e5252 commit 8324d47

File tree

6 files changed

+97
-19
lines changed

6 files changed

+97
-19
lines changed

graphene_django/filter/tests/test_in_filter.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import pytest
22

3+
from django_filters import FilterSet
4+
from django_filters import rest_framework as filters
35
from graphene import ObjectType, Schema
46
from graphene.relay import Node
57
from graphene_django import DjangoObjectType
6-
from graphene_django.tests.models import Pet
8+
from graphene_django.tests.models import Pet, Person
79
from graphene_django.utils import DJANGO_FILTER_INSTALLED
810

911
pytestmark = []
@@ -28,8 +30,27 @@ class Meta:
2830
}
2931

3032

33+
class PersonFilterSet(FilterSet):
34+
class Meta:
35+
model = Person
36+
fields = {}
37+
38+
names = filters.BaseInFilter(method="filter_names")
39+
40+
def filter_names(self, qs, name, value):
41+
return qs.filter(name__in=value)
42+
43+
44+
class PersonNode(DjangoObjectType):
45+
class Meta:
46+
model = Person
47+
interfaces = (Node,)
48+
filterset_class = PersonFilterSet
49+
50+
3151
class Query(ObjectType):
3252
pets = DjangoFilterConnectionField(PetNode)
53+
people = DjangoFilterConnectionField(PersonNode)
3354

3455

3556
def test_string_in_filter():
@@ -61,6 +82,33 @@ def test_string_in_filter():
6182
]
6283

6384

85+
def test_string_in_filter_with_filterset_class():
86+
"""Test in filter on a string field with a custom filterset class."""
87+
Person.objects.create(name="John")
88+
Person.objects.create(name="Michael")
89+
Person.objects.create(name="Angela")
90+
91+
schema = Schema(query=Query)
92+
93+
query = """
94+
query {
95+
people (names: ["John", "Michael"]) {
96+
edges {
97+
node {
98+
name
99+
}
100+
}
101+
}
102+
}
103+
"""
104+
result = schema.execute(query)
105+
assert not result.errors
106+
assert result.data["people"]["edges"] == [
107+
{"node": {"name": "John"}},
108+
{"node": {"name": "Michael"}},
109+
]
110+
111+
64112
def test_int_in_filter():
65113
"""
66114
Test in filter on an integer field.

graphene_django/filter/utils.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def get_filtering_args_from_filterset(filterset_class, type):
1717
model = filterset_class._meta.model
1818
for name, filter_field in filterset_class.base_filters.items():
1919
form_field = None
20+
filter_type = filter_field.lookup_expr
2021

2122
if name in filterset_class.declared_filters:
2223
# Get the filter field from the explicitly declared filter
@@ -25,7 +26,6 @@ def get_filtering_args_from_filterset(filterset_class, type):
2526
else:
2627
# Get the filter field with no explicit type declaration
2728
model_field = get_model_field(model, filter_field.field_name)
28-
filter_type = filter_field.lookup_expr
2929
if filter_type != "isnull" and hasattr(model_field, "formfield"):
3030
form_field = model_field.formfield(
3131
required=filter_field.extra.get("required", False)
@@ -38,14 +38,14 @@ def get_filtering_args_from_filterset(filterset_class, type):
3838

3939
field = convert_form_field(form_field)
4040

41-
if filter_type in ["in", "range"]:
42-
# Replace CSV filters (`in`, `range`) argument type to be a list of the same type as the field.
43-
# See comments in `replace_csv_filters` method for more details.
44-
field = List(field.get_type())
41+
if filter_type in ["in", "range"]:
42+
# Replace CSV filters (`in`, `range`) argument type to be a list of
43+
# the same type as the field. See comments in
44+
# `replace_csv_filters` method for more details.
45+
field = List(field.get_type())
4546

4647
field_type = field.Argument()
4748
field_type.description = str(filter_field.label) if filter_field.label else None
48-
4949
args[name] = field_type
5050

5151
return args
@@ -78,10 +78,7 @@ def replace_csv_filters(filterset_class):
7878
"""
7979
for name, filter_field in list(filterset_class.base_filters.items()):
8080
filter_type = filter_field.lookup_expr
81-
if (
82-
filter_type in ["in", "range"]
83-
and name not in filterset_class.declared_filters
84-
):
81+
if filter_type in ["in", "range"]:
8582
assert isinstance(filter_field, BaseCSVFilter)
8683
filterset_class.base_filters[name] = Filter(
8784
field_name=filter_field.field_name,

graphene_django/tests/models.py

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
CHOICES = ((1, "this"), (2, _("that")))
77

88

9+
class Person(models.Model):
10+
name = models.CharField(max_length=30)
11+
12+
913
class Pet(models.Model):
1014
name = models.CharField(max_length=30)
1115
age = models.PositiveIntegerField()

graphene_django/tests/test_utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def runTest(self):
5151
pass
5252

5353
tc = TestClass()
54+
tc._pre_setup()
5455
tc.setUpClass()
5556
tc.query("query { }", operation_name="QueryName")
5657
body = json.loads(post_mock.call_args.args[1])

graphene_django/utils/testing.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
2+
import warnings
23

3-
from django.test import TestCase, Client
4+
from django.test import Client, TestCase
45

56
DEFAULT_GRAPHQL_URL = "/graphql/"
67

@@ -68,12 +69,6 @@ class GraphQLTestCase(TestCase):
6869
# URL to graphql endpoint
6970
GRAPHQL_URL = DEFAULT_GRAPHQL_URL
7071

71-
@classmethod
72-
def setUpClass(cls):
73-
super(GraphQLTestCase, cls).setUpClass()
74-
75-
cls._client = Client()
76-
7772
def query(
7873
self, query, operation_name=None, input_data=None, variables=None, headers=None
7974
):
@@ -101,10 +96,19 @@ def query(
10196
input_data=input_data,
10297
variables=variables,
10398
headers=headers,
104-
client=self._client,
99+
client=self.client,
105100
graphql_url=self.GRAPHQL_URL,
106101
)
107102

103+
@property
104+
def _client(self):
105+
warnings.warn(
106+
"Using `_client` is deprecated in favour of `client`.",
107+
PendingDeprecationWarning,
108+
stacklevel=2,
109+
)
110+
return self.client
111+
108112
def assertResponseNoErrors(self, resp, msg=None):
109113
"""
110114
Assert that the call went through correctly. 200 means the syntax is ok, if there are no `errors`,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
3+
from .. import GraphQLTestCase
4+
from ...tests.test_types import with_local_registry
5+
6+
7+
@with_local_registry
8+
def test_graphql_test_case_deprecated_client():
9+
"""
10+
Test that `GraphQLTestCase._client`'s should raise pending deprecation warning.
11+
"""
12+
13+
class TestClass(GraphQLTestCase):
14+
GRAPHQL_SCHEMA = True
15+
16+
def runTest(self):
17+
pass
18+
19+
tc = TestClass()
20+
tc._pre_setup()
21+
tc.setUpClass()
22+
23+
with pytest.warns(PendingDeprecationWarning):
24+
tc._client

0 commit comments

Comments
 (0)