mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 01:32:24 +03:00
add test for serializer method field
This commit is contained in:
parent
adc22b559c
commit
380310c60b
|
@ -9,3 +9,8 @@ class MyFakeModel(models.Model):
|
|||
class MyFakeModelWithPassword(models.Model):
|
||||
cool_name = models.CharField(max_length=50)
|
||||
password = models.CharField(max_length=50)
|
||||
|
||||
|
||||
class MyFakeModelWithDate(models.Model):
|
||||
cool_name = models.CharField(max_length=50)
|
||||
last_edited = models.DateField()
|
|
@ -8,7 +8,7 @@ from graphene.types.inputobjecttype import InputObjectType
|
|||
|
||||
from ...settings import graphene_settings
|
||||
from ...types import DjangoObjectType
|
||||
from ..models import MyFakeModel, MyFakeModelWithPassword
|
||||
from ..models import MyFakeModel, MyFakeModelWithPassword, MyFakeModelWithDate
|
||||
from ..mutation import SerializerMutation
|
||||
|
||||
|
||||
|
@ -33,6 +33,18 @@ class MyModelSerializer(serializers.ModelSerializer):
|
|||
fields = "__all__"
|
||||
|
||||
|
||||
class MyModelSerializerWithMethod(serializers.ModelSerializer):
|
||||
days_since_last_edit = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = MyFakeModelWithDate
|
||||
fields = "__all__"
|
||||
|
||||
def get_days_since_last_edit(self, obj):
|
||||
now = datetime.date.fromisoformat("2020-01-08")
|
||||
return (now - obj.last_edited).days
|
||||
|
||||
|
||||
class MyModelMutation(SerializerMutation):
|
||||
class Meta:
|
||||
serializer_class = MyModelSerializer
|
||||
|
@ -208,6 +220,26 @@ def test_model_invalid_update_mutate_and_get_payload_success():
|
|||
assert '"id" required' in str(exc.value)
|
||||
|
||||
|
||||
@mark.django_db
|
||||
def test_perform_mutate_success():
|
||||
class MyMethodMutation(SerializerMutation):
|
||||
class Meta:
|
||||
serializer_class = MyModelSerializerWithMethod
|
||||
|
||||
result = MyMethodMutation.mutate_and_get_payload(
|
||||
None,
|
||||
mock_info(),
|
||||
**{
|
||||
"cool_name": "Narf",
|
||||
"last_edited": datetime.date.fromisoformat("2020-01-04"),
|
||||
}
|
||||
)
|
||||
|
||||
assert result.errors is None
|
||||
assert result.cool_name == "Narf"
|
||||
assert result.days_since_last_edit == 4
|
||||
|
||||
|
||||
def test_mutate_and_get_payload_error():
|
||||
class MyMutation(SerializerMutation):
|
||||
class Meta:
|
||||
|
|
Loading…
Reference in New Issue
Block a user