Skip to content

Commit

Permalink
Refactor serializers, remove set_context
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Aug 8, 2023
1 parent e4a8da8 commit f738798
Show file tree
Hide file tree
Showing 28 changed files with 222 additions and 208 deletions.
1 change: 1 addition & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ python --reuse-db # keep the datab
pytest rdmo/domain # test only the domain app
pytest rdmo/domain/tests/test_viewsets.py # run only a specific test file
pytest rdmo/domain/tests/test_viewsets.py::test_attribute_list # run only a specific test
pytest -k 'test_validator' # run only set of test files, using substring matching
```

Coverage
Expand Down
24 changes: 12 additions & 12 deletions rdmo/conditions/tests/test_validator_locked.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,29 @@ def test_update_unlock(db):

def test_serializer_create(db):
validator = ConditionLockedValidator()
validator.set_context(ConditionSerializer())
serializer = ConditionSerializer()

validator({
'locked': False
})
}, serializer)


def test_serializer_create_locked(db):
validator = ConditionLockedValidator()
validator.set_context(ConditionSerializer())
serializer = ConditionSerializer()

validator({
'locked': True
})
}, serializer)


def test_serializer_update(db):
condition = Condition.objects.first()

validator = ConditionLockedValidator()
validator.set_context(ConditionSerializer(instance=condition))
serializer = ConditionSerializer(instance=condition)

validator({})
validator({}, serializer)


def test_serializer_update_error(db):
Expand All @@ -90,23 +90,23 @@ def test_serializer_update_error(db):
condition.save()

validator = ConditionLockedValidator()
validator.set_context(ConditionSerializer(instance=condition))
serializer = ConditionSerializer(instance=condition)

with pytest.raises(RestFameworkValidationError):
validator({
'locked': True
})
}, serializer)


def test_serializer_update_lock(db):
condition = Condition.objects.first()

validator = ConditionLockedValidator()
validator.set_context(ConditionSerializer(instance=condition))
serializer = ConditionSerializer(instance=condition)

validator({
'locked': True
})
}, serializer)


def test_serializer_update_unlock(db):
Expand All @@ -115,8 +115,8 @@ def test_serializer_update_unlock(db):
condition.save()

validator = ConditionLockedValidator()
validator.set_context(ConditionSerializer(instance=condition))
serializer = ConditionSerializer(instance=condition)

validator({
'locked': False
})
}, serializer)
16 changes: 8 additions & 8 deletions rdmo/conditions/tests/test_validator_unique_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,45 @@ def test_unique_uri_validator_update_error(db):

def test_unique_uri_validator_serializer_create(db):
validator = ConditionUniqueURIValidator()
validator.set_context(ConditionSerializer())
serializer = ConditionSerializer()

validator({
'uri_prefix': settings.DEFAULT_URI_PREFIX,
'uri_path': 'test'
})
}, serializer)


def test_unique_uri_validator_serializer_create_error(db):
validator = ConditionUniqueURIValidator()
validator.set_context(ConditionSerializer())
serializer = ConditionSerializer()

with pytest.raises(RestFameworkValidationError):
validator({
'uri_prefix': settings.DEFAULT_URI_PREFIX,
'uri_path': Condition.objects.filter(uri_prefix=settings.DEFAULT_URI_PREFIX).last().uri_path
})
}, serializer)


def test_unique_uri_validator_serializer_update(db):
condition = Condition.objects.first()

validator = ConditionUniqueURIValidator()
validator.set_context(ConditionSerializer(instance=condition))
serializer = ConditionSerializer(instance=condition)

validator({
'uri_prefix': condition.uri_prefix,
'uri_path': condition.uri_path
})
}, serializer)


def test_unique_uri_validator_serializer_update_error(db):
condition = Condition.objects.filter(uri_prefix=settings.DEFAULT_URI_PREFIX).first()

validator = ConditionUniqueURIValidator()
validator.set_context(ConditionSerializer(instance=condition))
serializer = ConditionSerializer(instance=condition)

with pytest.raises(RestFameworkValidationError):
validator({
'uri_prefix': condition.uri_prefix,
'uri_path': Condition.objects.filter(uri_prefix=settings.DEFAULT_URI_PREFIX).last().uri_path
})
}, serializer)
11 changes: 8 additions & 3 deletions rdmo/core/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def test_instance_validator_instance():
def test_instance_validator_serializer():
validator = InstanceValidator()
serializer = serializers.Serializer()
validator.set_context(serializer)

validator({}, serializer)

assert validator.serializer == serializer
assert validator.serializer.instance is None
Expand All @@ -31,7 +32,9 @@ def test_instance_validator_serializer_instance():
validator = InstanceValidator()
instance = object()
serializer = serializers.Serializer(instance=instance)
validator.set_context(serializer)

validator({}, serializer)

assert validator.serializer == serializer
assert validator.serializer.instance == serializer.instance

Expand All @@ -47,7 +50,9 @@ def test_instance_validator_validation_error():

def test_instance_validator_validation_serializer_error():
validator = InstanceValidator()
validator.set_context(serializers.Serializer())
serializer = serializers.Serializer()

validator({}, serializer)

with pytest.raises(serializers.ValidationError):
validator.raise_validation_error({
Expand Down
20 changes: 12 additions & 8 deletions rdmo/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ class InstanceValidator(object):
It is used as InstanceValidator() for (1) and InstanceValidator(instance)(self.cleaned_data) for (2)
'''

requires_context = True

def __init__(self, instance=None):
self.instance = instance
self.serializer = None

def set_context(self, serializer):
self.instance = serializer.instance
self.serializer = serializer
def __call__(self, data, serializer=None):
if serializer is not None:
self.instance = serializer.instance
self.serializer = serializer

def raise_validation_error(self, errors):
if self.serializer:
raise serializers.ValidationError(errors)
else:
raise ValidationError(errors)

def __call__(self, value):
raise NotImplementedError


class UniqueURIValidator(InstanceValidator):

Expand All @@ -43,7 +43,9 @@ class UniqueURIValidator(InstanceValidator):

path_pattern = re.compile(r'^[\w\-\/]+\Z')

def __call__(self, data):
def __call__(self, data, serializer=None):
super().__call__(data, serializer)

self.validate(self.model, self.instance, self.get_uri(data))

def validate(self, model, instance, uri):
Expand Down Expand Up @@ -91,7 +93,9 @@ class LockedValidator(InstanceValidator):

parent_fields = ()

def __call__(self, data):
def __call__(self, data, serializer=None):
super().__call__(data, serializer)

is_locked = False

# lock if parent_fields are set and a parent is locked
Expand Down
16 changes: 8 additions & 8 deletions rdmo/domain/tests/test_validator_locked.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,31 @@ def test_update_parent_error(db):

def test_serializer_create(db):
validator = AttributeLockedValidator()
validator.set_context(AttributeSerializer())
serializer = AttributeSerializer()

validator({
'locked': False
})
}, serializer)


def test_serializer_create_locked(db):
validator = AttributeLockedValidator()
validator.set_context(AttributeSerializer())
serializer = AttributeSerializer()

validator({
'locked': True
})
}, serializer)


def test_serializer_update(db):
attribute = Attribute.objects.get(uri='http://example.com/terms/domain/individual/single/text')

validator = AttributeLockedValidator()
validator.set_context(AttributeSerializer(instance=attribute))
serializer = AttributeSerializer(instance=attribute)

validator({
'locked': False
})
}, serializer)


def test_serializer_update_error(db):
Expand All @@ -147,9 +147,9 @@ def test_serializer_update_error(db):
attribute.save()

validator = AttributeLockedValidator()
validator.set_context(AttributeSerializer(instance=attribute))
serializer = AttributeSerializer(instance=attribute)

with pytest.raises(RestFameworkValidationError):
validator({
'locked': True
})
}, serializer)
14 changes: 7 additions & 7 deletions rdmo/domain/tests/test_validator_parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,39 @@ class MockedView(object):
action = 'create'

validator = AttributeParentValidator()
validator.set_context(AttributeSerializer())
validator.serializer.context['view'] = MockedView()
serializer = AttributeSerializer()
serializer.context['view'] = MockedView()

validator({
'uri_prefix': settings.DEFAULT_URI_PREFIX,
'key': 'test',
'parent': Attribute.objects.get(uri='http://example.com/terms/domain/individual/single')
})
}, serializer)


def test_serializer_update(db):
attribute = Attribute.objects.get(uri='http://example.com/terms/domain/individual/single/text')
validator = AttributeParentValidator()
validator.set_context(AttributeSerializer(instance=attribute))
serializer = AttributeSerializer(instance=attribute)

validator({
'uri_prefix': attribute.uri_prefix,
'key': attribute.key,
'parent': attribute.parent
})
}, serializer)


def test_serializer_update_error(db):
attribute = Attribute.objects.get(uri='http://example.com/terms/domain/individual/single/text')
validator = AttributeParentValidator()
validator.set_context(AttributeSerializer(instance=attribute))
serializer = AttributeSerializer(instance=attribute)

with pytest.raises(RestFameworkValidationError):
validator({
'uri_prefix': attribute.uri_prefix,
'key': attribute.key,
'parent': attribute # set self as parent
})
}, serializer)


def test_import_create(db):
Expand Down
16 changes: 8 additions & 8 deletions rdmo/domain/tests/test_validator_unique_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,47 @@ def test_validator_update_error(db):

def test_validator_serializer_create(db):
validator = AttributeUniqueURIValidator()
validator.set_context(AttributeSerializer())
serializer = AttributeSerializer()

validator({
'uri_prefix': settings.DEFAULT_URI_PREFIX,
'key': 'test',
'parent': Attribute.objects.get(uri='http://example.com/terms/domain/individual/single')
})
}, serializer)


def test_validator_serializer_create_error(db):
validator = AttributeUniqueURIValidator()
validator.set_context(AttributeSerializer())
serializer = AttributeSerializer()

with pytest.raises(RestFameworkValidationError):
validator({
'uri_prefix': settings.DEFAULT_URI_PREFIX,
'key': 'text',
'parent': Attribute.objects.get(uri='http://example.com/terms/domain/individual/single')
})
}, serializer)


def test_validator_serializer_update(db):
attribute = Attribute.objects.get(uri='http://example.com/terms/domain/individual/single/text')
validator = AttributeUniqueURIValidator()
validator.set_context(AttributeSerializer(instance=attribute))
serializer = AttributeSerializer(instance=attribute)

validator({
'uri_prefix': attribute.uri_prefix,
'key': 'test',
'parent': attribute.parent
})
}, serializer)


def test_validator_serializer_update_error(db):
attribute = Attribute.objects.get(uri='http://example.com/terms/domain/individual/single/text')
validator = AttributeUniqueURIValidator()
validator.set_context(AttributeSerializer(instance=attribute))
serializer = AttributeSerializer(instance=attribute)

with pytest.raises(RestFameworkValidationError):
validator({
'uri_prefix': attribute.uri_prefix,
'key': 'textarea',
'parent': attribute.parent
})
}, serializer)
4 changes: 3 additions & 1 deletion rdmo/domain/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def get_uri(self, data):

class AttributeParentValidator(InstanceValidator):

def __call__(self, data):
def __call__(self, data, serializer=None):
super().__call__(data, serializer)

parent = data.get('parent')
if parent is None:
# workaround for import
Expand Down
Loading

0 comments on commit f738798

Please sign in to comment.