Browse Source

Added validation for the time_format option and wrote tests for it

master
Raphael Roberts 5 years ago
parent
commit
293ad6a820
  1. 16
      tests/test_create_filenames.py
  2. 7
      tibi_hardlinks/exceptions.py
  3. 11
      tibi_hardlinks/filename_creation.py

16
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")

7
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)

11
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]

Loading…
Cancel
Save