Skip to content

Commit

Permalink
Move validators to dedicated file. Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Apr 17, 2024
1 parent 2c84271 commit fa94851
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lemarche/siaes/management/commands/import_sep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions lemarche/siaes/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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",
),
),
Expand All @@ -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",
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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",
),
),
Expand Down Expand Up @@ -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",
),
),
Expand Down
6 changes: 3 additions & 3 deletions lemarche/siaes/migrations/0032_avoid_null_on_charfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.db import migrations, models

import lemarche.siaes.validators
import lemarche.utils.validators


class Migration(migrations.Migration):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
42 changes: 42 additions & 0 deletions lemarche/utils/tests_validators.py
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.

0 comments on commit fa94851

Please sign in to comment.