def test_depth_1_nested_serializer_invalid_data_on_update(self, db): user = UserFactory.create() address_pk = user.address.pk address_data = dict(_pk=address_pk, state="CA", zip_code="12345") user_data = dict(email="*****@*****.**", password="******") user_data.update(address=address_data) serializer = UserSerializer(user, data=user_data) assert serializer.is_valid(raise_exception=True) serializer.validated_data["address"].pop("zip_code") with pytest.raises(ValidationError) as exc_info: serializer.save() assert exc_dict_has_keys(exc_info.value, "address") address_keys = { address_key for arg in exc_info.value.args if isinstance(arg, dict) for value in arg.values() if isinstance(value, dict) for address_key in value } assert "zip_code" in address_keys
def test_create_nested_object_when_exists(self, db): user = UserFactory.create() address_data = dict(state="CA", zip_code="12345") user_data = dict(address=address_data) serializer = UserSerializer(user, data=user_data) assert serializer.is_valid(raise_exception=True) with pytest.raises(ValidationError) as exc_info: serializer.save() assert exc_dict_has_keys(exc_info.value, "address")
def test_non_required_fields_empty(self): # Need to use a required field i.e. either an explicitly # defined field or a `blank=False` field of model class AddressSerializer(serializers.ModelSerializer, metaclass=FieldOptionsMetaclass): class Meta: model = Address fields = ("zip_code", ) non_required_fields = () serializer = AddressSerializer(data={}) with pytest.raises(ValidationError) as exc_info: assert serializer.is_valid(raise_exception=True) assert exc_dict_has_keys(exc_info.value, "zip_code")
def test_exc_dict_has_keys(): exc_dict = { "foo": ["spamegg", "baz"], "bar": ["baz"], } exc = django_ValidationError(exc_dict) assert exc_dict_has_keys(exc, "foo") assert exc_dict_has_keys(exc, ("foo", "bar")) assert not exc_dict_has_keys(exc, "spam") assert not exc_dict_has_keys(exc, ("foo", "spam")) exc = ValidationError(exc_dict) assert exc_dict_has_keys(exc, "foo") assert exc_dict_has_keys(exc, ("foo", "bar")) assert not exc_dict_has_keys(exc, "spam") assert not exc_dict_has_keys(exc, ("foo", "spam"))
def test_required_fields_on_update_any(self, db): class AddressSerializer(serializers.ModelSerializer, metaclass=FieldOptionsMetaclass): class Meta: model = Address fields = ("pk", "state", "zip_code") read_only_fields = ("pk", ) required_fields_on_update_any = ("state", "zip_code") instance = AddressFactory.create() serializer = AddressSerializer(instance, data={}) with pytest.raises(ValidationError) as exc_info: serializer.is_valid(raise_exception=True) assert exc_dict_has_keys(exc_info.value, NON_FIELD_ERRORS_KEY) serializer = AddressSerializer(instance, data={"zip_code": "12345"}) assert serializer.is_valid(raise_exception=False)
def test_required_fields_on_create(self): class AddressSerializer(serializers.ModelSerializer, metaclass=FieldOptionsMetaclass): class Meta: model = Address fields = ("pk", "state", "zip_code") read_only_fields = ("pk", ) required_fields_on_create = ("zip_code", ) data = dict(state="CA", zip_code="12345") serializer = AddressSerializer(data=data) assert serializer.is_valid(raise_exception=False) data.pop("zip_code") serializer = AddressSerializer(data=data) with pytest.raises(ValidationError) as exc_info: serializer.is_valid(raise_exception=True) assert exc_dict_has_keys(exc_info.value, "zip_code")
def test_depth_2_nested_serializer_invalid_data_on_update(self, db): client = ClientFactory.create() user_pk = client.user.pk address_pk = client.user.address.pk address_data = dict(_pk=address_pk, state="CA", zip_code="12345") user_data = dict(_pk=user_pk, email="*****@*****.**", password="******") user_data.update(address=address_data) client_data = dict(user=user_data) serializer = ClientSerializer(client, data=client_data) assert serializer.is_valid(raise_exception=True) serializer.validated_data["user"]["address"].pop("zip_code") with pytest.raises(ValidationError) as exc_info: serializer.save() assert exc_dict_has_keys(exc_info.value, "user") user_keys = { user_key for arg in exc_info.value.args if isinstance(arg, dict) for value in arg.values() if isinstance(value, dict) for user_key in value } assert "address" in user_keys address_keys = { inner_key for arg in exc_info.value.args if isinstance(arg, dict) for user_value in arg.values() if isinstance(user_value, dict) for address_value in user_value.values() if isinstance(address_value, dict) for inner_key in address_value } assert "zip_code" in address_keys
def test_depth_1_nested_serializer_invalid_data_on_create(self, db): address_data = dict(state="CA", zip_code="12345") user_data = dict(username="******", password="******") user_data.update(address=address_data) serializer = UserSerializer(data=user_data) assert serializer.is_valid(raise_exception=True) serializer.validated_data["address"].pop("zip_code") # remove `zip_code` with pytest.raises(ValidationError) as exc_info: serializer.save() assert exc_dict_has_keys(exc_info.value, "address") # Retreive nested error keys address_keys = { address_key for arg in exc_info.value.args if isinstance(arg, dict) for value in arg.values() if isinstance(value, dict) for address_key in value } assert "zip_code" in address_keys