update unit tests

This commit is contained in:
jbannon 2022-04-12 04:23:52 +00:00
parent b17d773248
commit 4c01198ce9
4 changed files with 15 additions and 31 deletions

View file

@ -154,13 +154,12 @@ def validate_entry_dict_contains_valid_formatters():
def _validate_entry_dict_contains_valid_formatters(entry: Entry):
for key, value in entry.to_dict().items():
expected_string = f"test {value} formatting works"
format_string = f"test {{{key}}} formatting works"
assert (
entry.apply_formatter(StringFormatterValidator(name="test", value=format_string))
== expected_string
formatter = StringFormatterValidator(
name="test", value=f"test {{{key}}} formatting works"
)
assert formatter.apply_formatter(entry.to_dict()) == expected_string
return True
return _validate_entry_dict_contains_valid_formatters

View file

@ -39,14 +39,3 @@ class TestEntry(object):
assert mock_entry.kwargs_contains(key) is False
with pytest.raises(KeyError, match=expected_error_msg):
mock_entry.kwargs(key)
def test_entry_formatter_fails_missing_field(self, mock_entry):
format_string = StringFormatterValidator(name="test", value=f"prefix {{bah_humbug}} suffix")
available_fields = ", ".join(sorted(mock_entry.to_dict().keys()))
expected_error_msg = (
f"Validation error in test: Format variable 'bah_humbug' does not exist. "
f"Available variables: {available_fields}"
)
with pytest.raises(StringFormattingException, match=expected_error_msg):
assert mock_entry.apply_formatter(format_string)

View file

@ -89,6 +89,17 @@ class TestStringFormatterValidator(object):
with pytest.raises(ValidationException, match=expected_error_msg):
_ = StringFormatterValidator(name="fail", value=format_string)
def test_entry_formatter_fails_missing_field(self):
format_string = StringFormatterValidator(name="test", value=f"prefix {{bah_humbug}} suffix")
variable_dict = {"varb": "a", "vara": "b"}
expected_error_msg = (
f"Validation error in test: Format variable 'bah_humbug' does not exist. "
f"Available variables: {', '.join(sorted(variable_dict.keys()))}"
)
with pytest.raises(StringFormattingException, match=expected_error_msg):
assert format_string.apply_formatter(variable_dict=variable_dict)
def test_string_formatter_single_field(self):
uid = "this uid"
format_string = StringFormatterValidator(name="test", value=f"prefix {{uid}} suffix")

View file

@ -63,21 +63,6 @@ class BaseEntry(ABC):
"extractor": self.extractor,
}
def apply_formatter(
self,
formatter: StringFormatterValidator,
overrides: Optional[OverridesValidator] = None,
) -> str:
"""
Perform a string format on the given format string, using the entry's dict for format
values. The override dict will overwrite any values within the entry's dict.
"""
entry_dict = self.to_dict()
if overrides:
entry_dict = dict(entry_dict, **overrides.dict_with_format_strings)
return formatter.apply_formatter(variable_dict=entry_dict)
class Entry(BaseEntry):
"""