Skip to content

Commit

Permalink
Convert Django form / DRF decimals to Graphene decimals (#958)
Browse files Browse the repository at this point in the history
* Convert Django form decimals to Graphene decimals

* Ugly fix for test_should_query_filter_node_limit

* Convert DRF serializer decimal to Graphene decimal
  • Loading branch information
ulgens authored Jan 10, 2021
1 parent e24675e commit 66c8901
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
6 changes: 3 additions & 3 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.db.models import TextField, Value
from django.db.models.functions import Concat

from graphene import Argument, Boolean, Field, Float, ObjectType, Schema, String
from graphene import Argument, Boolean, Decimal, Field, ObjectType, Schema, String
from graphene.relay import Node
from graphene_django import DjangoObjectType
from graphene_django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField
Expand Down Expand Up @@ -388,7 +388,7 @@ class Meta:
field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleIdFilter)
max_time = field.args["max_time"]
assert isinstance(max_time, Argument)
assert max_time.type == Float
assert max_time.type == Decimal
assert max_time.description == "The maximum time"


Expand Down Expand Up @@ -671,7 +671,7 @@ def resolve_all_reporters(self, info, **args):
schema = Schema(query=Query)
query = """
query NodeFilteringQuery {
allReporters(limit: 1) {
allReporters(limit: "1") {
edges {
node {
id
Expand Down
19 changes: 17 additions & 2 deletions graphene_django/forms/converter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
from django import forms
from django.core.exceptions import ImproperlyConfigured

from graphene import ID, Boolean, Float, Int, List, String, UUID, Date, DateTime, Time
from graphene import (
Boolean,
Date,
DateTime,
Decimal,
Float,
ID,
Int,
List,
String,
Time,
UUID,
)

from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
from ..utils import import_single_dispatch


singledispatch = import_single_dispatch()


Expand Down Expand Up @@ -52,6 +63,10 @@ def convert_form_field_to_nullboolean(field):


@convert_form_field.register(forms.DecimalField)
def convert_field_to_decimal(field):
return Decimal(description=field.help_text, required=field.required)


@convert_form_field.register(forms.FloatField)
def convert_form_field_to_float(field):
return Float(description=field.help_text, required=field.required)
Expand Down
16 changes: 8 additions & 8 deletions graphene_django/forms/tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from django import forms
from py.test import raises

import graphene
from graphene import (
String,
Int,
Boolean,
Date,
DateTime,
Decimal,
Float,
ID,
UUID,
Int,
List,
NonNull,
DateTime,
Date,
String,
Time,
UUID,
)

from ..converter import convert_form_field
Expand Down Expand Up @@ -97,8 +97,8 @@ def test_should_float_convert_float():
assert_conversion(forms.FloatField, Float)


def test_should_decimal_convert_float():
assert_conversion(forms.DecimalField, Float)
def test_should_decimal_convert_decimal():
assert_conversion(forms.DecimalField, Decimal)


def test_should_multiple_choice_convert_list():
Expand Down
6 changes: 5 additions & 1 deletion graphene_django/rest_framework/serializer_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ def convert_serializer_field_to_bool(field):
return graphene.Boolean


@get_graphene_type_from_serializer_field.register(serializers.FloatField)
@get_graphene_type_from_serializer_field.register(serializers.DecimalField)
def convert_serializer_field_to_decimal(field):
return graphene.Decimal


@get_graphene_type_from_serializer_field.register(serializers.FloatField)
def convert_serializer_field_to_float(field):
return graphene.Float

Expand Down
4 changes: 2 additions & 2 deletions graphene_django/rest_framework/tests/test_field_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def test_should_float_convert_float():
assert_conversion(serializers.FloatField, graphene.Float)


def test_should_decimal_convert_float():
def test_should_decimal_convert_decimal():
assert_conversion(
serializers.DecimalField, graphene.Float, max_digits=4, decimal_places=2
serializers.DecimalField, graphene.Decimal, max_digits=4, decimal_places=2
)


Expand Down
2 changes: 1 addition & 1 deletion graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_should_float_convert_float():
assert_conversion(models.FloatField, graphene.Float)


def test_should_float_convert_decimal():
def test_should_decimal_convert_decimal():
assert_conversion(models.DecimalField, graphene.Decimal)


Expand Down

0 comments on commit 66c8901

Please sign in to comment.