2017-11-13 23:33:12 +03:00
|
|
|
import datetime
|
|
|
|
|
2022-11-14 13:56:18 +03:00
|
|
|
from pytest import raises
|
2019-06-25 11:40:29 +03:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
from graphene import Field, ResolveInfo
|
2017-07-18 22:01:43 +03:00
|
|
|
from graphene.types.inputobjecttype import InputObjectType
|
2017-05-31 01:30:03 +03:00
|
|
|
|
2017-07-18 22:01:43 +03:00
|
|
|
from ...types import DjangoObjectType
|
2020-04-20 15:23:20 +03:00
|
|
|
from ..models import MyFakeModel, MyFakeModelWithDate, MyFakeModelWithPassword
|
2017-05-31 01:30:03 +03:00
|
|
|
from ..mutation import SerializerMutation
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
def mock_info():
|
2018-07-20 02:51:33 +03:00
|
|
|
return ResolveInfo(
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2020-05-09 14:13:47 +03:00
|
|
|
path=None,
|
2018-07-20 02:51:33 +03:00
|
|
|
schema=None,
|
|
|
|
fragments=None,
|
|
|
|
root_value=None,
|
|
|
|
operation=None,
|
|
|
|
variable_values=None,
|
|
|
|
context=None,
|
2020-05-09 14:13:47 +03:00
|
|
|
is_awaitable=None,
|
2018-07-20 02:51:33 +03:00
|
|
|
)
|
2017-11-15 01:02:28 +03:00
|
|
|
|
2017-05-31 01:30:03 +03:00
|
|
|
|
2017-07-18 22:01:43 +03:00
|
|
|
class MyModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = MyFakeModel
|
2018-07-20 02:51:33 +03:00
|
|
|
fields = "__all__"
|
|
|
|
|
2017-07-18 22:01:43 +03:00
|
|
|
|
2020-02-21 20:42:47 +03:00
|
|
|
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(2020, 1, 8)
|
|
|
|
return (now - obj.last_edited).days
|
|
|
|
|
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
class MyModelMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MyModelSerializer
|
2017-07-18 22:01:43 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-05-31 01:30:03 +03:00
|
|
|
class MySerializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
2017-07-18 22:01:43 +03:00
|
|
|
model = MyModelSerializer()
|
2017-05-31 01:30:03 +03:00
|
|
|
|
2017-08-31 20:15:18 +03:00
|
|
|
def create(self, validated_data):
|
|
|
|
return validated_data
|
|
|
|
|
2017-05-31 01:30:03 +03:00
|
|
|
|
|
|
|
def test_needs_serializer_class():
|
|
|
|
with raises(Exception) as exc:
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-05-31 01:30:03 +03:00
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
pass
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert str(exc.value) == "serializer_class is required for the SerializerMutation"
|
2017-05-31 01:30:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_has_fields():
|
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MySerializer
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "text" in MyMutation._meta.fields
|
|
|
|
assert "model" in MyMutation._meta.fields
|
|
|
|
assert "errors" in MyMutation._meta.fields
|
2017-05-31 01:30:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_has_input_fields():
|
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MySerializer
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "text" in MyMutation.Input._meta.fields
|
|
|
|
assert "model" in MyMutation.Input._meta.fields
|
2017-07-18 22:01:43 +03:00
|
|
|
|
|
|
|
|
2017-12-12 05:23:00 +03:00
|
|
|
def test_exclude_fields():
|
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MyModelSerializer
|
2018-07-20 02:51:33 +03:00
|
|
|
exclude_fields = ["created"]
|
2017-12-12 05:23:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "cool_name" in MyMutation._meta.fields
|
|
|
|
assert "created" not in MyMutation._meta.fields
|
|
|
|
assert "errors" in MyMutation._meta.fields
|
|
|
|
assert "cool_name" in MyMutation.Input._meta.fields
|
|
|
|
assert "created" not in MyMutation.Input._meta.fields
|
2017-12-12 05:23:00 +03:00
|
|
|
|
|
|
|
|
2019-06-10 02:25:34 +03:00
|
|
|
def test_write_only_field():
|
|
|
|
class WriteOnlyFieldModelSerializer(serializers.ModelSerializer):
|
|
|
|
password = serializers.CharField(write_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = MyFakeModelWithPassword
|
|
|
|
fields = ["cool_name", "password"]
|
|
|
|
|
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = WriteOnlyFieldModelSerializer
|
|
|
|
|
|
|
|
result = MyMutation.mutate_and_get_payload(
|
|
|
|
None, mock_info(), **{"cool_name": "New Narf", "password": "admin"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert hasattr(result, "cool_name")
|
2019-06-11 06:54:30 +03:00
|
|
|
assert not hasattr(
|
|
|
|
result, "password"
|
|
|
|
), "'password' is write_only field and shouldn't be visible"
|
2019-06-10 02:25:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_write_only_field_using_extra_kwargs():
|
|
|
|
class WriteOnlyFieldModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = MyFakeModelWithPassword
|
|
|
|
fields = ["cool_name", "password"]
|
|
|
|
extra_kwargs = {"password": {"write_only": True}}
|
|
|
|
|
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = WriteOnlyFieldModelSerializer
|
|
|
|
|
|
|
|
result = MyMutation.mutate_and_get_payload(
|
|
|
|
None, mock_info(), **{"cool_name": "New Narf", "password": "admin"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert hasattr(result, "cool_name")
|
2019-06-11 06:54:30 +03:00
|
|
|
assert not hasattr(
|
|
|
|
result, "password"
|
|
|
|
), "'password' is write_only field and shouldn't be visible"
|
2019-06-10 02:25:34 +03:00
|
|
|
|
|
|
|
|
2020-02-23 12:48:33 +03:00
|
|
|
def test_read_only_fields():
|
|
|
|
class ReadOnlyFieldModelSerializer(serializers.ModelSerializer):
|
2020-12-31 02:37:57 +03:00
|
|
|
id = serializers.CharField(read_only=True)
|
2020-02-23 12:48:33 +03:00
|
|
|
cool_name = serializers.CharField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = MyFakeModelWithPassword
|
2020-12-31 02:37:57 +03:00
|
|
|
lookup_field = "id"
|
|
|
|
fields = ["id", "cool_name", "password"]
|
2020-02-23 12:48:33 +03:00
|
|
|
|
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = ReadOnlyFieldModelSerializer
|
|
|
|
|
|
|
|
assert "password" in MyMutation.Input._meta.fields
|
2020-12-31 02:37:57 +03:00
|
|
|
assert "id" in MyMutation.Input._meta.fields
|
2020-02-23 12:48:33 +03:00
|
|
|
assert (
|
|
|
|
"cool_name" not in MyMutation.Input._meta.fields
|
|
|
|
), "'cool_name' is read_only field and shouldn't be on arguments"
|
|
|
|
|
|
|
|
|
2017-07-18 22:01:43 +03:00
|
|
|
def test_nested_model():
|
|
|
|
class MyFakeModelGrapheneType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = MyFakeModel
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-07-18 22:01:43 +03:00
|
|
|
|
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MySerializer
|
2017-05-31 01:30:03 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
model_field = MyMutation._meta.fields["model"]
|
2017-07-18 22:01:43 +03:00
|
|
|
assert isinstance(model_field, Field)
|
|
|
|
assert model_field.type == MyFakeModelGrapheneType
|
2017-05-31 01:30:03 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
model_input = MyMutation.Input._meta.fields["model"]
|
2017-08-31 19:07:05 +03:00
|
|
|
model_input_type = model_input._type.of_type
|
|
|
|
assert issubclass(model_input_type, InputObjectType)
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "cool_name" in model_input_type._meta.fields
|
|
|
|
assert "created" in model_input_type._meta.fields
|
2017-08-31 20:15:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_mutate_and_get_payload_success():
|
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MySerializer
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
result = MyMutation.mutate_and_get_payload(
|
|
|
|
None, mock_info(), **{"text": "value", "model": {"cool_name": "other_value"}}
|
|
|
|
)
|
2017-08-31 20:15:18 +03:00
|
|
|
assert result.errors is None
|
|
|
|
|
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
def test_model_add_mutate_and_get_payload_success():
|
2018-07-20 02:51:33 +03:00
|
|
|
result = MyModelMutation.mutate_and_get_payload(
|
|
|
|
None, mock_info(), **{"cool_name": "Narf"}
|
|
|
|
)
|
2017-11-13 23:33:12 +03:00
|
|
|
assert result.errors is None
|
2018-07-20 02:51:33 +03:00
|
|
|
assert result.cool_name == "Narf"
|
2017-11-13 23:33:12 +03:00
|
|
|
assert isinstance(result.created, datetime.datetime)
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
def test_model_update_mutate_and_get_payload_success():
|
|
|
|
instance = MyFakeModel.objects.create(cool_name="Narf")
|
2018-07-20 02:51:33 +03:00
|
|
|
result = MyModelMutation.mutate_and_get_payload(
|
|
|
|
None, mock_info(), **{"id": instance.id, "cool_name": "New Narf"}
|
|
|
|
)
|
2017-11-15 01:02:28 +03:00
|
|
|
assert result.errors is None
|
2018-07-20 02:51:33 +03:00
|
|
|
assert result.cool_name == "New Narf"
|
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
|
2019-12-27 17:46:48 +03:00
|
|
|
def test_model_partial_update_mutate_and_get_payload_success():
|
|
|
|
instance = MyFakeModel.objects.create(cool_name="Narf")
|
|
|
|
result = MyModelMutation.mutate_and_get_payload(
|
|
|
|
None, mock_info(), **{"id": instance.id}
|
|
|
|
)
|
|
|
|
assert result.errors is None
|
|
|
|
assert result.cool_name == "Narf"
|
|
|
|
|
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
def test_model_invalid_update_mutate_and_get_payload_success():
|
|
|
|
class InvalidModelMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MyModelSerializer
|
2018-07-20 02:51:33 +03:00
|
|
|
model_operations = ["update"]
|
2017-11-15 01:02:28 +03:00
|
|
|
|
|
|
|
with raises(Exception) as exc:
|
2018-07-20 02:51:33 +03:00
|
|
|
result = InvalidModelMutation.mutate_and_get_payload(
|
|
|
|
None, mock_info(), **{"cool_name": "Narf"}
|
|
|
|
)
|
2017-11-15 01:02:28 +03:00
|
|
|
|
|
|
|
assert '"id" required' in str(exc.value)
|
|
|
|
|
2017-08-31 20:15:18 +03:00
|
|
|
|
2020-02-21 20:42:47 +03:00
|
|
|
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(2020, 1, 4)}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result.errors is None
|
|
|
|
assert result.cool_name == "Narf"
|
|
|
|
assert result.days_since_last_edit == 4
|
|
|
|
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
def test_mutate_and_get_payload_error():
|
2017-08-31 20:15:18 +03:00
|
|
|
class MyMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MySerializer
|
|
|
|
|
|
|
|
# missing required fields
|
2017-11-15 01:02:28 +03:00
|
|
|
result = MyMutation.mutate_and_get_payload(None, mock_info(), **{})
|
2017-11-13 23:33:12 +03:00
|
|
|
assert len(result.errors) > 0
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-11-13 23:33:12 +03:00
|
|
|
def test_model_mutate_and_get_payload_error():
|
|
|
|
# missing required fields
|
2017-11-15 01:02:28 +03:00
|
|
|
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
|
2017-11-13 23:33:12 +03:00
|
|
|
assert len(result.errors) > 0
|
2017-11-15 01:02:28 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2020-04-20 15:23:20 +03:00
|
|
|
def test_mutation_error_camelcased(graphene_settings):
|
2019-07-09 00:22:08 +03:00
|
|
|
graphene_settings.CAMELCASE_ERRORS = True
|
2019-06-25 11:40:29 +03:00
|
|
|
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
|
|
|
|
assert result.errors[0].field == "coolName"
|
|
|
|
|
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
def test_invalid_serializer_operations():
|
|
|
|
with raises(Exception) as exc:
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-11-15 01:02:28 +03:00
|
|
|
class MyModelMutation(SerializerMutation):
|
|
|
|
class Meta:
|
|
|
|
serializer_class = MyModelSerializer
|
2018-07-20 02:51:33 +03:00
|
|
|
model_operations = ["Add"]
|
2017-11-15 01:02:28 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "model_operations" in str(exc.value)
|