Skip to content

Commit 2ce30dd

Browse files
committed
Test that validator's context is not used by another serializer
This is a failing test for encode#5760
1 parent 78367ba commit 2ce30dd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tests/test_validators.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,28 @@ def filter(self, **kwargs):
341341
validator.filter_queryset(attrs=data, queryset=queryset)
342342
assert queryset.called_with == {'race_name': 'bar', 'position': 1}
343343

344+
def test_validator_instances_with_context_are_not_used_twice(self):
345+
"""
346+
Every instance of a serializer should use unique instances of validators
347+
when calling `set_context`.
348+
"""
349+
def data():
350+
return {'race_name': 'example', 'position': 3}
351+
352+
def check_validators(serializer):
353+
for validator in serializer.validators:
354+
assert not hasattr(validator, 'instance')
355+
356+
serializer = UniquenessTogetherSerializer(self.instance, data=data())
357+
check_validators(serializer)
358+
serializer.run_validators({})
359+
check_validators(serializer)
360+
361+
another_serializer = UniquenessTogetherSerializer(data=data())
362+
check_validators(another_serializer)
363+
another_serializer.run_validators(data())
364+
check_validators(another_serializer)
365+
344366

345367
# Tests for `UniqueForDateValidator`
346368
# ----------------------------------

0 commit comments

Comments
 (0)