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

Feat: Get all valid values set #42

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions src/frformat/set_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def is_valid(self, value: str) -> bool:
normalized_value = normalize_value(value, self._options)
return normalized_value in self._normalized_values

def get_valid_values_set(self) -> FrozenSet[str]:
return self._valid_values


V = TypeVar("V", bound="Version")

Expand Down
49 changes: 40 additions & 9 deletions src/tests/test_geo_format.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

from frformat import (
Canton,
CodeCommuneInsee,
Expand All @@ -14,11 +16,13 @@
NumeroDepartement,
Pays,
Region,
set_format,
)
from frformat.common import NBSP, NNBSP
from frformat.versioned_set import VersionedSet


def test_insee_geo_format():
class Insee_geo_format(unittest.TestCase):
class ValidatorTest:
"""
This class tests all INSEE geographical formats, versioned by the Millesime enum.
Expand Down Expand Up @@ -49,7 +53,7 @@ def run_all_tests(self):
self.test_valid_cases()
self.test_invalid_cases()

def test_all_validators_with_cog():
def test_all_fomats_validation(self):
test_cases = [
{
"name": "code_region_millesime2023",
Expand Down Expand Up @@ -234,15 +238,15 @@ def test_all_validators_with_cog():
]

for tc in test_cases:
validatorTest = ValidatorTest(
validatorTest = self.ValidatorTest(
tc["cog"],
tc["validTestCases"],
tc["invalidTestCases"],
tc["formatClass"],
)
validatorTest.run_all_tests()

def test_code_commune_insee_format():
def test_code_commune_insee_format(self):
code_commune_insee_cog_2023 = CodeCommuneInsee(Millesime.M2023)
code_commune_insee_cog_2024 = CodeCommuneInsee(Millesime.M2024)

Expand All @@ -253,11 +257,38 @@ def test_code_commune_insee_format():

assert code_commune_insee_cog_2024.format(cog_2024_value) == cog_2024_value

def test_formats_valid_values_set_with_versioned_set(self):
name = "Validator name"
description = "Validator description"

validator_versioned_data = VersionedSet[Millesime]()
validator_versioned_data.add_version(
Millesime.M2024, frozenset({"Paris", "Lyon"})
)

validator = set_format.new(
"Validator", name, description, validator_versioned_data
)

assert validator("2024").get_valid_values_set() == frozenset({"Paris", "Lyon"})

def test_formats_valid_values_set_with_single_set(self):
name = "Validator name"
description = "Validator description"

validator_valid_values_single_set = frozenset({"Nomandie", "Nice"})

validator = set_format.new(
"Validator", name, description, validator_valid_values_single_set
)

assert validator().get_valid_values_set() == frozenset({"Nomandie", "Nice"})


def test_geo_format():
class Geo_format(unittest.TestCase):
"""This method tests geographical formats, which does not belong to the Official Geographic Code."""

def test_code_fantoir():
def test_code_fantoir(self):
fantoir_valid = "ZB03A"
fantoir_invalid = ["1000", "zB03A"]

Expand All @@ -266,7 +297,7 @@ def test_code_fantoir():
for fi in fantoir_invalid:
assert not code_fantoir.is_valid(fi)

def test_code_postal():
def test_code_postal(self):
value = "05560"
code_postal = CodePostal()
assert code_postal.is_valid(value)
Expand All @@ -275,7 +306,7 @@ def test_code_postal():
for cpi in codes_postales_invalides:
assert not code_postal.is_valid(cpi)

def test_longitude_l93():
def test_longitude_l93(self):
longitudel93 = LongitudeL93()
assert longitudel93.format(224234) == "224" + NNBSP + "234" + NBSP + "m"
assert longitudel93.format(224234.0) == "224" + NNBSP + "234,00" + NBSP + "m"
Expand All @@ -290,7 +321,7 @@ def test_longitude_l93():
for tc in valid_test_cases:
assert longitudel93.is_valid(tc)

def test_latitude_l93():
def test_latitude_l93(self):
latitudel93 = LatitudeL93()
assert (
latitudel93.format(6757121)
Expand Down