Skip to content

Commit

Permalink
Feat: Get all valid values set (#42)
Browse files Browse the repository at this point in the history
# Goal  
As outlined in issue #26, the purpose of this PR is to introduce a new
feature to the `fr-format` project: Retrieving all valid values set
associated to a specific French format.

This feature is only possible for Set formats, which is defined a set of
all valid values.
Fixes #26

---------

Co-authored-by: Pierre Camilleri <[email protected]>
  • Loading branch information
Sarrabah and pierrecamilleri authored Feb 5, 2025
1 parent 3da1419 commit 8548fc2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 66 deletions.
9 changes: 9 additions & 0 deletions src/frformat/set_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ 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]:
"""Returns the canonical set of valid values.
In the case of versioned data, it will only return the valid values for the version the validator has been initialized with.
Validation options have no impact on the output - in particular, extra user-defined valid values will not be part of the result.
"""
return self._valid_values


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

Expand Down
57 changes: 0 additions & 57 deletions src/tests/test_geo_data_validation.py

This file was deleted.

18 changes: 9 additions & 9 deletions src/tests/test_geo_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from frformat.common import NBSP, NNBSP


def test_insee_geo_format():
class TestInseeGeoFormat:
class ValidatorTest:
"""
This class tests all INSEE geographical formats, versioned by the Millesime enum.
Expand Down Expand Up @@ -49,7 +49,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 +234,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 @@ -254,10 +254,10 @@ def test_code_commune_insee_format():
assert code_commune_insee_cog_2024.format(cog_2024_value) == cog_2024_value


def test_geo_format():
class TestGeoFormat:
"""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 +266,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 +275,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 +290,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
91 changes: 91 additions & 0 deletions src/tests/test_set_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import pytest

from frformat import set_format
from frformat.set_format import Millesime, new
from frformat.versioned_set import VersionedSet


def test_format_validation():
versioned_data = VersionedSet[Millesime]()
versioned_data.add_version(
Millesime.M2023,
frozenset({"Ambléon", "Ambronay"}),
)

versioned_data.add_version(Millesime.M2024, frozenset({"Arandas"}))

FormatTest = new(
"Versionned format", "Versionned format", "Versionned format", versioned_data
)

test_cases = [
{
"version": Millesime.M2023,
"value_to_test": "Ambléon",
"expected_valid": True,
},
{
"version": Millesime.M2023,
"value_to_test": "Anglefort",
"expected_valid": False,
},
{
"version": Millesime.M2024,
"value_to_test": "Arandas",
"expected_valid": True,
},
{
"version": Millesime.LATEST,
"value_to_test": "Arandas",
"expected_valid": True,
},
{
"version": Millesime.LATEST,
"value_to_test": "Ambléon",
"expected_valid": False,
},
{"version": "2025", "expected_error": ValueError},
]

for tc in test_cases:
if "expected_error" in tc:
with pytest.raises(tc["expected_error"]):
FormatTest(tc["version"])
else:
test_format = FormatTest(tc["version"])
assert (
test_format.is_valid(tc["value_to_test"]) == tc["expected_valid"]
), f'Error on data format definition with version { tc["version"] } and value { tc["value_to_test"] }'


def test_formats_valid_values():
versioned_data = VersionedSet[Millesime]()
versioned_data.add_version(Millesime.M2024, frozenset({"Paris", "Lyon"}))
test_cases = [
{
"name": "VersionedSetFormat",
"valid_data": versioned_data,
"version": "2024",
"expected_result": frozenset({"Paris", "Lyon"}),
},
{
"name": "SingleSetFormat",
"valid_data": frozenset({"Nomandie", "Nice"}),
"version": None,
"expected_result": frozenset({"Nomandie", "Nice"}),
},
]

name = "Validator name"
description = "Validator description"

for tc in test_cases:
validator = set_format.new("Validator", name, description, tc["valid_data"])
if tc["version"]:
assert (
validator(tc["version"]).get_valid_values_set() == tc["expected_result"]
), f"While we test {tc['name']}, the returned data is not equal to {tc['expected_result']} when the valid_data is {tc['valid_data']} and the version is equal to {tc['version']}"
else:
assert (
validator().get_valid_values_set() == tc["expected_result"]
), f"While we test {tc['name']}, the returned data is not equal to {tc['expected_result']} when the valid_data is {tc['valid_data']} and the version is equal to {tc['version']}"

0 comments on commit 8548fc2

Please sign in to comment.