Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLField refactors and tests #28113

Merged
merged 6 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion corehq/apps/custom_data_fields/edit_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def dispatch(self, request, *args, **kwargs):
@classmethod
def get_validator(cls, domain):
data_model = SQLCustomDataFieldsDefinition.get_or_create(domain, cls.field_type)
return data_model.get_validator(cls)
return data_model.get_validator()

@classmethod
def page_name(cls):
Expand Down
58 changes: 28 additions & 30 deletions corehq/apps/custom_data_fields/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ class Meta:
db_table = "custom_data_fields_field"
order_with_respect_to = "definition"

def validate_choices(self, value):
if self.choices and value and str(value) not in self.choices:
return _(
"'{value}' is not a valid choice for {label}. The available "
"options are: {options}."
).format(
value=value,
label=self.label,
options=', '.join(self.choices),
)

def validate_regex(self, value):
if self.regex and value and not re.search(self.regex, value):
return _("'{value}' is not a valid match for {label}").format(
value=value, label=self.label)

def validate_required(self, value):
if self.is_required and not value:
return _(
"{label} is required."
).format(
label=self.label
)


class CustomDataField(JsonObject):
slug = StringProperty()
Expand Down Expand Up @@ -131,43 +155,17 @@ def set_fields(self, fields):
self.sqlfield_set.set(fields, bulk=False)
self.set_sqlfield_order([f.id for f in fields])

def get_validator(self, data_field_class):
def get_validator(self):
"""
Returns a validator to be used in bulk import
"""
def validate_choices(field, value):
if field.choices and value and str(value) not in field.choices:
return _(
"'{value}' is not a valid choice for {slug}, the available "
"options are: {options}."
).format(
value=value,
slug=field.slug,
options=', '.join(field.choices),
)

def validate_regex(field, value):
if field.regex and value and not re.search(field.regex, value):
return _("'{value}' is not a valid match for {slug}").format(
value=value, slug=field.slug)

def validate_required(field, value):
if field.is_required and not value:
return _(
"Cannot create or update a {entity} without "
"the required field: {field}."
).format(
entity=data_field_class.entity_string,
field=field.slug
)

def validate_custom_fields(custom_fields):
errors = []
for field in self.get_fields():
value = custom_fields.get(field.slug, None)
errors.append(validate_required(field, value))
errors.append(validate_choices(field, value))
errors.append(validate_regex(field, value))
errors.append(field.validate_required(value))
errors.append(field.validate_choices(value))
errors.append(field.validate_regex(value))
return ' '.join(filter(None, errors))

return validate_custom_fields
Expand Down
Empty file.
51 changes: 51 additions & 0 deletions corehq/apps/custom_data_fields/tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django.test import SimpleTestCase

from corehq.apps.custom_data_fields.models import SQLField


class TestCustomDataFieldsFields(SimpleTestCase):
def test_validate_required(self):
required_field = SQLField(
slug='favorite_chordata',
is_required=True,
label='Favorite Chordata',
)
self.assertIsNone(required_field.validate_required('sea lamprey'))
self.assertEqual(required_field.validate_required(None), 'Favorite Chordata is required.')

optional_field = SQLField(
slug='fav_echinoderm',
is_required=False,
label='Favorite Echinoderm',
)
self.assertIsNone(optional_field.validate_required('sea cucumber'))
self.assertIsNone(optional_field.validate_required(None))

def test_validate_choices(self):
field = SQLField(
slug='warm_color',
label='Warm Color',
choices=[
'red',
'orange',
'yellow',
],
)
self.assertIsNone(field.validate_choices('orange'))
self.assertEqual(
field.validate_choices('squishy'),
"'squishy' is not a valid choice for Warm Color. The available options are: red, orange, yellow."
)

def test_validate_regex(self):
field = SQLField(
slug='s_word',
label='Word starting with the letter S',
regex='^[Ss]',
regex_msg='That does not start with S',
)
self.assertIsNone(field.validate_regex('sibilant'))
self.assertEqual(
field.validate_regex('whisper'),
"'whisper' is not a valid match for Word starting with the letter S"
)