update unnecesary property clean-up and add tests

master
Emmanuelle Vargas-Gonzalez 2019-04-23 09:27:21 -04:00
parent f8857569d5
commit c3aecd76ba
3 changed files with 17 additions and 10 deletions

View File

@ -183,11 +183,12 @@ class CallableValues(list):
class StringProperty(Property): class StringProperty(Property):
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.string_type = text_type
super(StringProperty, self).__init__(**kwargs) super(StringProperty, self).__init__(**kwargs)
def clean(self, value): def clean(self, value):
return self.string_type(value) if not isinstance(value, string_types):
return text_type(value)
return value
class TypeProperty(Property): class TypeProperty(Property):
@ -439,21 +440,22 @@ class EnumProperty(StringProperty):
super(EnumProperty, self).__init__(**kwargs) super(EnumProperty, self).__init__(**kwargs)
def clean(self, value): def clean(self, value):
value = super(EnumProperty, self).clean(value) cleaned_value = super(EnumProperty, self).clean(value)
if value not in self.allowed: if cleaned_value not in self.allowed:
raise ValueError("value '{}' is not valid for this enumeration.".format(value)) raise ValueError("value '{}' is not valid for this enumeration.".format(cleaned_value))
return self.string_type(value)
return cleaned_value
class PatternProperty(StringProperty): class PatternProperty(StringProperty):
def clean(self, value): def clean(self, value):
str_value = super(PatternProperty, self).clean(value) cleaned_value = super(PatternProperty, self).clean(value)
errors = run_validator(str_value) errors = run_validator(cleaned_value)
if errors: if errors:
raise ValueError(str(errors[0])) raise ValueError(str(errors[0]))
return self.string_type(value) return cleaned_value
class ObservableProperty(Property): class ObservableProperty(Property):

View File

@ -76,7 +76,7 @@ EXPECTED_CAMPAIGN_WITH_GRANULAR_REF_MARKINGS = """{
}""" }"""
EXPECTED_CAMPAIGN_WITH_GRANULAR_LANG_MARKINGS = """{ EXPECTED_CAMPAIGN_WITH_GRANULAR_LANG_MARKINGS = u"""{
"type": "campaign", "type": "campaign",
"spec_version": "2.1", "spec_version": "2.1",
"id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f", "id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",

View File

@ -450,6 +450,11 @@ def test_enum_property_valid(value):
assert enum_prop.clean('b') assert enum_prop.clean('b')
def test_enum_property_clean():
enum_prop = EnumProperty(['1'])
assert enum_prop.clean(1) == '1'
def test_enum_property_invalid(): def test_enum_property_invalid():
enum_prop = EnumProperty(['a', 'b', 'c']) enum_prop = EnumProperty(['a', 'b', 'c'])
with pytest.raises(ValueError): with pytest.raises(ValueError):