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
This commit is contained in:
Ülgen Sarıkavak 2021-01-10 09:00:11 +03:00 committed by GitHub
parent e24675e5b7
commit 66c8901041
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 17 deletions

View File

@ -5,7 +5,7 @@ import pytest
from django.db.models import TextField, Value from django.db.models import TextField, Value
from django.db.models.functions import Concat 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.relay import Node
from graphene_django import DjangoObjectType from graphene_django import DjangoObjectType
from graphene_django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField from graphene_django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField
@ -388,7 +388,7 @@ def test_filterset_descriptions():
field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleIdFilter) field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleIdFilter)
max_time = field.args["max_time"] max_time = field.args["max_time"]
assert isinstance(max_time, Argument) assert isinstance(max_time, Argument)
assert max_time.type == Float assert max_time.type == Decimal
assert max_time.description == "The maximum time" assert max_time.description == "The maximum time"
@ -671,7 +671,7 @@ def test_should_query_filter_node_limit():
schema = Schema(query=Query) schema = Schema(query=Query)
query = """ query = """
query NodeFilteringQuery { query NodeFilteringQuery {
allReporters(limit: 1) { allReporters(limit: "1") {
edges { edges {
node { node {
id id

View File

@ -1,12 +1,23 @@
from django import forms from django import forms
from django.core.exceptions import ImproperlyConfigured 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 .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
from ..utils import import_single_dispatch from ..utils import import_single_dispatch
singledispatch = import_single_dispatch() singledispatch = import_single_dispatch()
@ -52,6 +63,10 @@ def convert_form_field_to_nullboolean(field):
@convert_form_field.register(forms.DecimalField) @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) @convert_form_field.register(forms.FloatField)
def convert_form_field_to_float(field): def convert_form_field_to_float(field):
return Float(description=field.help_text, required=field.required) return Float(description=field.help_text, required=field.required)

View File

@ -1,19 +1,19 @@
from django import forms from django import forms
from py.test import raises from py.test import raises
import graphene
from graphene import ( from graphene import (
String,
Int,
Boolean, Boolean,
Date,
DateTime,
Decimal,
Float, Float,
ID, ID,
UUID, Int,
List, List,
NonNull, NonNull,
DateTime, String,
Date,
Time, Time,
UUID,
) )
from ..converter import convert_form_field from ..converter import convert_form_field
@ -97,8 +97,8 @@ def test_should_float_convert_float():
assert_conversion(forms.FloatField, Float) assert_conversion(forms.FloatField, Float)
def test_should_decimal_convert_float(): def test_should_decimal_convert_decimal():
assert_conversion(forms.DecimalField, Float) assert_conversion(forms.DecimalField, Decimal)
def test_should_multiple_choice_convert_list(): def test_should_multiple_choice_convert_list():

View File

@ -115,8 +115,12 @@ def convert_serializer_field_to_bool(field):
return graphene.Boolean return graphene.Boolean
@get_graphene_type_from_serializer_field.register(serializers.FloatField)
@get_graphene_type_from_serializer_field.register(serializers.DecimalField) @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): def convert_serializer_field_to_float(field):
return graphene.Float return graphene.Float

View File

@ -133,9 +133,9 @@ def test_should_float_convert_float():
assert_conversion(serializers.FloatField, graphene.Float) assert_conversion(serializers.FloatField, graphene.Float)
def test_should_decimal_convert_float(): def test_should_decimal_convert_decimal():
assert_conversion( assert_conversion(
serializers.DecimalField, graphene.Float, max_digits=4, decimal_places=2 serializers.DecimalField, graphene.Decimal, max_digits=4, decimal_places=2
) )

View File

@ -242,7 +242,7 @@ def test_should_float_convert_float():
assert_conversion(models.FloatField, graphene.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) assert_conversion(models.DecimalField, graphene.Decimal)