From 293ad6a820ea3236f60d0d06f9f1f6e4b7eea6d1 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Tue, 26 Jan 2021 02:07:16 -0600 Subject: [PATCH] Added validation for the time_format option and wrote tests for it --- tests/test_create_filenames.py | 16 +++++++++++++++- tibi_hardlinks/exceptions.py | 7 +++++++ tibi_hardlinks/filename_creation.py | 11 ++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tibi_hardlinks/exceptions.py diff --git a/tests/test_create_filenames.py b/tests/test_create_filenames.py index b0c1e85..f00b47d 100644 --- a/tests/test_create_filenames.py +++ b/tests/test_create_filenames.py @@ -1,10 +1,11 @@ import pytest from tibi_hardlinks.filename_creation import create_backup_directory_name +from tibi_hardlinks.exceptions import ConfigurationException @pytest.mark.usefixtures("canned_data_output") @pytest.mark.parametrize("filename", ["example-old", "example-new"]) -def test_filename_good(filename, canned_data_output): +def test_filename_good_canned(filename, canned_data_output): data = canned_data_output(filename) filenames = { "example-old": "XT1095/TA44909SO3/QPython3/1.0.3/2017-11-01_07-38-42", @@ -13,3 +14,16 @@ def test_filename_good(filename, canned_data_output): generated_filename = create_backup_directory_name(data) assert filenames[filename] == generated_filename + + +def test_filename_raise_on_bad_time_format(canned_data_output): + data = canned_data_output("example-new") + + with pytest.raises(ConfigurationException): + create_backup_directory_name(data, "obviously bogus time format") + + +def test_filename_raise_on_incomplete_exception(canned_data_output): + data = canned_data_output("example-new") + with pytest.raises(ConfigurationException): + create_backup_directory_name(data, "%Y-%M-%D") diff --git a/tibi_hardlinks/exceptions.py b/tibi_hardlinks/exceptions.py new file mode 100644 index 0000000..09d0994 --- /dev/null +++ b/tibi_hardlinks/exceptions.py @@ -0,0 +1,7 @@ +class ConfigurationException(Exception): + def __init__(self, section, key, value): + self.section = section + self.key = key + self.value = value + self.message = f"Invalid value for {self.section}:{self.key}='{self.value}'" + super().__init__(self.message) diff --git a/tibi_hardlinks/filename_creation.py b/tibi_hardlinks/filename_creation.py index 06b6476..c170662 100644 --- a/tibi_hardlinks/filename_creation.py +++ b/tibi_hardlinks/filename_creation.py @@ -1,6 +1,9 @@ import datetime import os + +from tibi_hardlinks.exceptions import ConfigurationException + HEIRACHY = ( "sys_ro.product.model", "sys_ro.serialno", @@ -13,7 +16,13 @@ HEIRACHY = ( def create_backup_directory_name(backup_info, time_format="%Y-%m-%d_%H-%M-%S"): utc_timestamp = backup_info["backup_time"] backup_datetime = datetime.datetime.utcfromtimestamp(utc_timestamp) - backup_info["backup_time"] = backup_datetime.strftime(time_format) + + try: + new_time = backup_datetime.strftime(time_format) + assert backup_datetime == datetime.datetime.strptime(new_time, time_format) + backup_info["backup_time"] = new_time + except (ValueError, AssertionError): + raise ConfigurationException("customization", "time_format", time_format) ret_list = [] for part in HEIRACHY: data = backup_info[part]