Skip to content

Commit 66c8901

Browse files
authored
Convert Django form / DRF decimals to Graphene decimals (#958)
* Convert Django form decimals to Graphene decimals * Ugly fix for test_should_query_filter_node_limit * Convert DRF serializer decimal to Graphene decimal
1 parent e24675e commit 66c8901

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

graphene_django/filter/tests/test_fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.db.models import TextField, Value
66
from django.db.models.functions import Concat
77

8-
from graphene import Argument, Boolean, Field, Float, ObjectType, Schema, String
8+
from graphene import Argument, Boolean, Decimal, Field, ObjectType, Schema, String
99
from graphene.relay import Node
1010
from graphene_django import DjangoObjectType
1111
from graphene_django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField
@@ -388,7 +388,7 @@ class Meta:
388388
field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleIdFilter)
389389
max_time = field.args["max_time"]
390390
assert isinstance(max_time, Argument)
391-
assert max_time.type == Float
391+
assert max_time.type == Decimal
392392
assert max_time.description == "The maximum time"
393393

394394

@@ -671,7 +671,7 @@ def resolve_all_reporters(self, info, **args):
671671
schema = Schema(query=Query)
672672
query = """
673673
query NodeFilteringQuery {
674-
allReporters(limit: 1) {
674+
allReporters(limit: "1") {
675675
edges {
676676
node {
677677
id

graphene_django/forms/converter.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
from django import forms
22
from django.core.exceptions import ImproperlyConfigured
33

4-
from graphene import ID, Boolean, Float, Int, List, String, UUID, Date, DateTime, Time
4+
from graphene import (
5+
Boolean,
6+
Date,
7+
DateTime,
8+
Decimal,
9+
Float,
10+
ID,
11+
Int,
12+
List,
13+
String,
14+
Time,
15+
UUID,
16+
)
517

618
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
719
from ..utils import import_single_dispatch
820

9-
1021
singledispatch = import_single_dispatch()
1122

1223

@@ -52,6 +63,10 @@ def convert_form_field_to_nullboolean(field):
5263

5364

5465
@convert_form_field.register(forms.DecimalField)
66+
def convert_field_to_decimal(field):
67+
return Decimal(description=field.help_text, required=field.required)
68+
69+
5570
@convert_form_field.register(forms.FloatField)
5671
def convert_form_field_to_float(field):
5772
return Float(description=field.help_text, required=field.required)

graphene_django/forms/tests/test_converter.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
from django import forms
22
from py.test import raises
33

4-
import graphene
54
from graphene import (
6-
String,
7-
Int,
85
Boolean,
6+
Date,
7+
DateTime,
8+
Decimal,
99
Float,
1010
ID,
11-
UUID,
11+
Int,
1212
List,
1313
NonNull,
14-
DateTime,
15-
Date,
14+
String,
1615
Time,
16+
UUID,
1717
)
1818

1919
from ..converter import convert_form_field
@@ -97,8 +97,8 @@ def test_should_float_convert_float():
9797
assert_conversion(forms.FloatField, Float)
9898

9999

100-
def test_should_decimal_convert_float():
101-
assert_conversion(forms.DecimalField, Float)
100+
def test_should_decimal_convert_decimal():
101+
assert_conversion(forms.DecimalField, Decimal)
102102

103103

104104
def test_should_multiple_choice_convert_list():

graphene_django/rest_framework/serializer_converter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,12 @@ def convert_serializer_field_to_bool(field):
115115
return graphene.Boolean
116116

117117

118-
@get_graphene_type_from_serializer_field.register(serializers.FloatField)
119118
@get_graphene_type_from_serializer_field.register(serializers.DecimalField)
119+
def convert_serializer_field_to_decimal(field):
120+
return graphene.Decimal
121+
122+
123+
@get_graphene_type_from_serializer_field.register(serializers.FloatField)
120124
def convert_serializer_field_to_float(field):
121125
return graphene.Float
122126

graphene_django/rest_framework/tests/test_field_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ def test_should_float_convert_float():
133133
assert_conversion(serializers.FloatField, graphene.Float)
134134

135135

136-
def test_should_decimal_convert_float():
136+
def test_should_decimal_convert_decimal():
137137
assert_conversion(
138-
serializers.DecimalField, graphene.Float, max_digits=4, decimal_places=2
138+
serializers.DecimalField, graphene.Decimal, max_digits=4, decimal_places=2
139139
)
140140

141141

graphene_django/tests/test_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def test_should_float_convert_float():
242242
assert_conversion(models.FloatField, graphene.Float)
243243

244244

245-
def test_should_float_convert_decimal():
245+
def test_should_decimal_convert_decimal():
246246
assert_conversion(models.DecimalField, graphene.Decimal)
247247

248248

0 commit comments

Comments
 (0)