def test_max_length(self): field = serializers.StringField(max_length=3) with pytest.raises(serializers.ValidationError) as excinfo: field.run_validation('abcd') assert isinstance(excinfo.value.message, list) e1 = excinfo.value.message[0] assert e1.message == 'Ensure this field has maximum 3 characters'
def test_allow_null(self): field = serializers.StringField(allow_null=True, trim_whitespace=True) value = field.run_validation(None) assert value is None
def test_not_allow_null(self): field = serializers.StringField(allow_null=False, trim_whitespace=True) with pytest.raises(serializers.ValidationError) as excinfo: field.run_validation(None) assert excinfo.value.message == 'null is not allowed'
def test_allow_blank(self): field = serializers.StringField(allow_blank=True, trim_whitespace=True) assert field.run_validation(' ') == ''
def test_not_allow_blank(self): field = serializers.StringField() with pytest.raises(serializers.ValidationError) as excinfo: field.run_validation('') assert excinfo.value.message == 'blank is not allowed'
def test_if_trim_whitespace_false(self): field = serializers.StringField(trim_whitespace=False) assert field.to_internal_value(' abc ') == ' abc '
def test_if_stripped_by_default(self): field = serializers.StringField() assert field.to_internal_value(' abc ') == 'abc'