diff --git a/lemarche/siaes/management/commands/import_sep.py b/lemarche/siaes/management/commands/import_sep.py index 44a03d29a..b059f21ad 100644 --- a/lemarche/siaes/management/commands/import_sep.py +++ b/lemarche/siaes/management/commands/import_sep.py @@ -7,10 +7,10 @@ from lemarche.sectors.models import Sector from lemarche.siaes import constants as siae_constants from lemarche.siaes.models import Siae -from lemarche.siaes.validators import validate_siret from lemarche.utils.apis.geocoding import get_geocoding_data from lemarche.utils.constants import DEPARTMENT_TO_REGION, department_from_postcode from lemarche.utils.data import rename_dict_key, reset_app_sql_sequences +from lemarche.utils.validators import validate_siret SEP_FILE_NAME = "sep.csv" diff --git a/lemarche/siaes/migrations/0001_initial.py b/lemarche/siaes/migrations/0001_initial.py index 05ff92301..5c68791d8 100644 --- a/lemarche/siaes/migrations/0001_initial.py +++ b/lemarche/siaes/migrations/0001_initial.py @@ -3,7 +3,7 @@ import django.utils.timezone from django.db import migrations, models -import lemarche.siaes.validators +import lemarche.utils.validators class Migration(migrations.Migration): @@ -50,7 +50,7 @@ class Migration(migrations.Migration): models.CharField( db_index=True, max_length=14, - validators=[lemarche.siaes.validators.validate_siret], + validators=[lemarche.utils.validators.validate_siret], verbose_name="Siret", ), ), @@ -59,7 +59,7 @@ class Migration(migrations.Migration): models.CharField( blank=True, max_length=5, - validators=[lemarche.siaes.validators.validate_naf], + validators=[lemarche.utils.validators.validate_naf], verbose_name="Naf", ), ), diff --git a/lemarche/siaes/migrations/0003_siae_add_missing_fields_update_others.py b/lemarche/siaes/migrations/0003_siae_add_missing_fields_update_others.py index 96695523a..d004645ca 100644 --- a/lemarche/siaes/migrations/0003_siae_add_missing_fields_update_others.py +++ b/lemarche/siaes/migrations/0003_siae_add_missing_fields_update_others.py @@ -3,7 +3,7 @@ import django.contrib.postgres.fields from django.db import migrations, models -import lemarche.siaes.validators +import lemarche.utils.validators class Migration(migrations.Migration): @@ -180,7 +180,7 @@ class Migration(migrations.Migration): blank=True, max_length=5, null=True, - validators=[lemarche.siaes.validators.validate_naf], + validators=[lemarche.utils.validators.validate_naf], verbose_name="Naf", ), ), @@ -340,7 +340,7 @@ class Migration(migrations.Migration): blank=True, max_length=5, null=True, - validators=[lemarche.siaes.validators.validate_post_code], + validators=[lemarche.utils.validators.validate_post_code], verbose_name="Code Postal", ), ), diff --git a/lemarche/siaes/migrations/0032_avoid_null_on_charfields.py b/lemarche/siaes/migrations/0032_avoid_null_on_charfields.py index bfa831bf0..cb0767d69 100644 --- a/lemarche/siaes/migrations/0032_avoid_null_on_charfields.py +++ b/lemarche/siaes/migrations/0032_avoid_null_on_charfields.py @@ -2,7 +2,7 @@ from django.db import migrations, models -import lemarche.siaes.validators +import lemarche.utils.validators class Migration(migrations.Migration): @@ -241,7 +241,7 @@ class Migration(migrations.Migration): blank=True, default="", max_length=5, - validators=[lemarche.siaes.validators.validate_naf], + validators=[lemarche.utils.validators.validate_naf], verbose_name="Naf", ), preserve_default=False, @@ -274,7 +274,7 @@ class Migration(migrations.Migration): blank=True, default="", max_length=5, - validators=[lemarche.siaes.validators.validate_post_code], + validators=[lemarche.utils.validators.validate_post_code], verbose_name="Code Postal", ), preserve_default=False, diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index 91d2f35b6..9102f7cf9 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -20,13 +20,13 @@ from lemarche.perimeters.models import Perimeter from lemarche.siaes import constants as siae_constants from lemarche.siaes.tasks import set_siae_coords -from lemarche.siaes.validators import validate_naf, validate_post_code, validate_siret from lemarche.stats.models import Tracker from lemarche.users.models import User from lemarche.utils.constants import DEPARTMENTS_PRETTY, RECALCULATED_FIELD_HELP_TEXT, REGIONS_PRETTY from lemarche.utils.data import round_by_base from lemarche.utils.fields import ChoiceArrayField from lemarche.utils.urls import get_object_admin_url +from lemarche.utils.validators import validate_naf, validate_post_code, validate_siret def get_region_filter(perimeter): diff --git a/lemarche/utils/tests_validators.py b/lemarche/utils/tests_validators.py new file mode 100644 index 000000000..574f42dab --- /dev/null +++ b/lemarche/utils/tests_validators.py @@ -0,0 +1,42 @@ +from django.core.exceptions import ValidationError +from django.test import TestCase + +from lemarche.utils.validators import validate_naf, validate_post_code, validate_siren, validate_siret + + +class ValidatorsTest(TestCase): + def test_post_code_validator(self): + validator = validate_post_code + POST_CODE_OK = ["00000", "12345", "38000"] + for item in POST_CODE_OK: + validator(item) + POST_CODE_NOT_OK = ["0", "1234"] + for item in POST_CODE_NOT_OK: + self.assertRaises(ValidationError, validator, item) + + def test_siren_validator(self): + validator = validate_siren + SIREN_OK = ["123123123"] + for item in SIREN_OK: + validator(item) + SIREN_NOT_OK = ["123"] + for item in SIREN_NOT_OK: + self.assertRaises(ValidationError, validator, item) + + def test_siret_validator(self): + validator = validate_siret + SIRET_OK = ["12312312312345"] + for item in SIRET_OK: + validator(item) + SIRET_NOT_OK = ["123123123"] + for item in SIRET_NOT_OK: + self.assertRaises(ValidationError, validator, item) + + def test_naf_validator(self): + validator = validate_naf + NAF_OK = ["1234A"] + for item in NAF_OK: + validator(item) + NAF_NOT_OK = ["1234", "12345", "ABCDE"] + for item in NAF_NOT_OK: + self.assertRaises(ValidationError, validator, item) diff --git a/lemarche/siaes/validators.py b/lemarche/utils/validators.py similarity index 100% rename from lemarche/siaes/validators.py rename to lemarche/utils/validators.py