Skip to content

Commit

Permalink
tests(config): Corretto test per valore vuoto
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeSavefrogs committed Sep 8, 2023
1 parent 62c53fb commit 8cc94cf
Showing 1 changed file with 57 additions and 29 deletions.
86 changes: 57 additions & 29 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
class ConfigurationTestCase(unittest.TestCase):
maxDiff = None

def assertHasKey(self, obj, key, access_method: Literal["dict", "getattr"]="dict") -> None:
""" Assert that the given object has the given key. """
def assertHasKey(
self, obj, key, access_method: Literal["dict", "getattr"] = "dict"
) -> None:
"""Assert that the given object has the given key."""
current_obj = obj
for k in key.split("."):
try:
Expand All @@ -25,56 +27,70 @@ def assertHasKey(self, obj, key, access_method: Literal["dict", "getattr"]="dict
except KeyError:
self.fail(f"Object '{current_obj}' does not have key '{k}'")


@with_temporary_file(file_prefix="veryeasyfatt-", file_suffix=".toml", content="""
@with_temporary_file(
file_prefix="veryeasyfatt-",
file_suffix=".toml",
content="""
[features.shipping]
default_interval = "00:00-06:00"
""")
""",
)
def test_changed_value(self, temp_config_file: Path):
settings = _get_settings()
settings.reload_settings(temp_config_file)

self.assertHasKey(settings, "features.shipping.default_interval")

self.assertEqual(
settings["features"]["shipping"]["default_interval"],
'00:00-06:00'
settings.features.shipping.default_interval,
"00:00-06:00",
)

@with_temporary_file(file_prefix="veryeasyfatt-", file_suffix=".toml", content="""
@with_temporary_file(
file_prefix="veryeasyfatt-",
file_suffix=".toml",
content="""
[features.shipping]
default_interval = ""
""")
""",
)
def test_empty_value(self, temp_config_file: Path):
settings = _get_settings()
settings.reload_settings(temp_config_file)

self.assertHasKey(settings, "features.shipping.default_interval")

self.assertEqual(
settings["features"]["shipping"]["default_interval"],
''
settings.features.shipping.default_interval,
"07:00-16:00",
)

@with_temporary_file(file_prefix="veryeasyfatt-", file_suffix=".toml", content="""
@with_temporary_file(
file_prefix="veryeasyfatt-",
file_suffix=".toml",
content="""
[features.shipping]
""")
""",
)
def test_default_value(self, temp_config_file: Path):
settings = _get_settings()
settings.reload_settings(temp_config_file)

self.assertHasKey(settings, "features.shipping.default_interval")

self.assertEqual(
settings["features"]["shipping"]["default_interval"],
'07:00-16:00'
settings.features.shipping.default_interval,
"07:00-16:00",
)


class ConfigurationValuesTestCase(unittest.TestCase):
maxDiff = None

def assertHasKey(self, obj, key, access_method: Literal["dict", "getattr"]="dict") -> None:
""" Assert that the given object has the given key. """
def assertHasKey(
self, obj, key, access_method: Literal["dict", "getattr"] = "dict"
) -> None:
"""Assert that the given object has the given key."""
current_obj = obj
for k in key.split("."):
try:
Expand All @@ -85,50 +101,62 @@ def assertHasKey(self, obj, key, access_method: Literal["dict", "getattr"]="dict
except KeyError:
self.fail(f"Object '{current_obj}' does not have key '{k}'")

@with_temporary_file(file_prefix="veryeasyfatt-", file_suffix=".toml", content="""
@with_temporary_file(
file_prefix="veryeasyfatt-",
file_suffix=".toml",
content="""
[easyfatt.customers]
export_filename = ["file.xlsx"]
""")
""",
)
def test_single_list(self, temp_config_file: Path):
settings = _get_settings()
settings.reload_settings(temp_config_file)

self.assertHasKey(settings, "easyfatt.customers.export_filename")

self.assertEqual(
settings.easyfatt.customers.export_filename,
[Path("file.xlsx")],
)

@with_temporary_file(file_prefix="veryeasyfatt-", file_suffix=".toml", content="""
@with_temporary_file(
file_prefix="veryeasyfatt-",
file_suffix=".toml",
content="""
[easyfatt.customers]
export_filename = "file.xlsx"
""")
""",
)
def test_single_string(self, temp_config_file: Path):
settings = _get_settings()
settings.reload_settings(temp_config_file)

self.assertHasKey(settings, "easyfatt.customers.export_filename")

self.assertEqual(
settings.easyfatt.customers.export_filename,
[Path("file.xlsx")],
)

@with_temporary_file(file_prefix="veryeasyfatt-", file_suffix=".toml", content="""
@with_temporary_file(
file_prefix="veryeasyfatt-",
file_suffix=".toml",
content="""
[easyfatt.customers]
""")
""",
)
def test_empty_value(self, temp_config_file: Path):
settings = _get_settings()
settings.reload_settings(temp_config_file)

self.assertHasKey(settings, "easyfatt.customers.export_filename")

self.assertEqual(
settings.easyfatt.customers.export_filename,
[ Path("Soggetti.xlsx"), Path("Soggetti.ods") ],
[Path("Soggetti.xlsx"), Path("Soggetti.ods")],
)


if __name__ == '__main__':
unittest.main(verbosity=2)
if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 8cc94cf

Please sign in to comment.