def validate(self, data): data = super(BaseResultSerializer, self).validate(data) observation = data['observation'] value_type = observation.value_type validators = [] if value_type == OBSERVATION_VALUE_TYPE.INTEGER or value_type == OBSERVATION_VALUE_TYPE.REAL: min_value = observation.min_value max_value = observation.max_value if min_value is not None: # Check min validators.append(min_(min_value)) if max_value is not None: # Check max validators.append(max_(max_value)) elif value_type == OBSERVATION_VALUE_TYPE.ENUM: codes = observation.option_codes validators.append(in_(codes)) elif value_type == OBSERVATION_VALUE_TYPE.STRING: min_length_value = observation.min_length max_length_value = observation.max_length if min_length_value is not None: # Check min length validators.append(min_length(min_length_value)) if max_length_value is not None: # Check max length validators.append(max_length(max_length_value)) self.run_validators_on_field(data, 'value', validators) return data
def test_valid(): value = max_(10)(5) assert value == 5
def test_units(): with pytest.raises(ValidationError) as e: max_(10, 'kg')(11) assert e.value.errors[0] == 'Must be less than or equal to 10 kg.'
def test_greater_than(): with pytest.raises(ValidationError): max_(10)(11)
def test_equal(): max_(10)(10)
def test_less_than(): max_(10)(9)