overrides formatter tests
This commit is contained in:
parent
5969cd0268
commit
21e8e6fa51
1 changed files with 45 additions and 22 deletions
|
|
@ -1,6 +1,12 @@
|
|||
import pytest
|
||||
|
||||
from ytdl_subscribe.validators.base.string_formatter_validators import DictFormatterValidator
|
||||
from ytdl_subscribe.validators.base.string_formatter_validators import (
|
||||
OverridesDictFormatterValidator,
|
||||
)
|
||||
from ytdl_subscribe.validators.base.string_formatter_validators import (
|
||||
OverridesStringFormatterValidator,
|
||||
)
|
||||
from ytdl_subscribe.validators.base.string_formatter_validators import StringFormatterValidator
|
||||
from ytdl_subscribe.validators.exceptions import StringFormattingException
|
||||
from ytdl_subscribe.validators.exceptions import ValidationException
|
||||
|
|
@ -22,18 +28,21 @@ def error_message_unequal_regex_matches_str():
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"string_formatter_class", [StringFormatterValidator, OverridesStringFormatterValidator]
|
||||
)
|
||||
class TestStringFormatterValidator(object):
|
||||
def test_parse(self):
|
||||
def test_parse(self, string_formatter_class):
|
||||
format_string = "Here is my {var_one} and {var_two} 💩"
|
||||
validator = StringFormatterValidator(name="test_format_variables", value=format_string)
|
||||
validator = string_formatter_class(name="test_format_variables", value=format_string)
|
||||
|
||||
assert validator.format_string == format_string
|
||||
assert validator.format_variables == ["var_one", "var_two"]
|
||||
|
||||
def test_format_variables(self):
|
||||
def test_format_variables(self, string_formatter_class):
|
||||
format_string = "No vars 💩"
|
||||
assert (
|
||||
StringFormatterValidator(
|
||||
string_formatter_class(
|
||||
name="test_format_variables_empty", value=format_string
|
||||
).format_variables
|
||||
== []
|
||||
|
|
@ -48,11 +57,13 @@ class TestStringFormatterValidator(object):
|
|||
"Try }var_one} and {var_one}",
|
||||
],
|
||||
)
|
||||
def test_validate_fail_uneven_brackets(self, format_string, error_message_unequal_brackets_str):
|
||||
def test_validate_fail_uneven_brackets(
|
||||
self, string_formatter_class, format_string, error_message_unequal_brackets_str
|
||||
):
|
||||
expected_error_msg = f"Validation error in fail: {error_message_unequal_brackets_str}"
|
||||
|
||||
with pytest.raises(ValidationException, match=expected_error_msg):
|
||||
_ = StringFormatterValidator(name="fail", value=format_string)
|
||||
_ = string_formatter_class(name="fail", value=format_string)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"format_string",
|
||||
|
|
@ -66,12 +77,12 @@ class TestStringFormatterValidator(object):
|
|||
],
|
||||
)
|
||||
def test_validate_fail_bad_variable(
|
||||
self, format_string, error_message_unequal_regex_matches_str
|
||||
self, string_formatter_class, format_string, error_message_unequal_regex_matches_str
|
||||
):
|
||||
expected_error_msg = f"Validation error in fail: {error_message_unequal_regex_matches_str}"
|
||||
|
||||
with pytest.raises(ValidationException, match=expected_error_msg):
|
||||
_ = StringFormatterValidator(name="fail", value=format_string)
|
||||
_ = string_formatter_class(name="fail", value=format_string)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"format_string, bad_variable",
|
||||
|
|
@ -80,17 +91,19 @@ class TestStringFormatterValidator(object):
|
|||
("{try} {valid_var}", "try"),
|
||||
],
|
||||
)
|
||||
def test_validate_fail_variable_keyword_or_not_identifier(self, format_string, bad_variable):
|
||||
def test_validate_fail_variable_keyword_or_not_identifier(
|
||||
self, string_formatter_class, format_string, bad_variable
|
||||
):
|
||||
expected_error_msg = (
|
||||
f"Validation error in fail: "
|
||||
f"'{bad_variable}' is a Python keyword and cannot be used as a variable."
|
||||
)
|
||||
|
||||
with pytest.raises(ValidationException, match=expected_error_msg):
|
||||
_ = StringFormatterValidator(name="fail", value=format_string)
|
||||
_ = string_formatter_class(name="fail", value=format_string)
|
||||
|
||||
def test_entry_formatter_fails_missing_field(self):
|
||||
format_string = StringFormatterValidator(name="test", value=f"prefix {{bah_humbug}} suffix")
|
||||
def test_entry_formatter_fails_missing_field(self, string_formatter_class):
|
||||
format_string = string_formatter_class(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. "
|
||||
|
|
@ -100,16 +113,16 @@ class TestStringFormatterValidator(object):
|
|||
with pytest.raises(StringFormattingException, match=expected_error_msg):
|
||||
assert format_string.apply_formatter(variable_dict=variable_dict)
|
||||
|
||||
def test_string_formatter_single_field(self):
|
||||
def test_string_formatter_single_field(self, string_formatter_class):
|
||||
uid = "this uid"
|
||||
format_string = StringFormatterValidator(name="test", value=f"prefix {{uid}} suffix")
|
||||
format_string = string_formatter_class(name="test", value=f"prefix {{uid}} suffix")
|
||||
expected_string = f"prefix {uid} suffix"
|
||||
|
||||
assert format_string.apply_formatter(variable_dict={"uid": uid}) == expected_string
|
||||
|
||||
def test_entry_formatter_duplicate_fields(self):
|
||||
def test_entry_formatter_duplicate_fields(self, string_formatter_class):
|
||||
upload_year = "2022"
|
||||
format_string = StringFormatterValidator(
|
||||
format_string = string_formatter_class(
|
||||
name="test", value=f"prefix {{upload_year}} {{upload_year}} suffix"
|
||||
)
|
||||
expected_string = f"prefix {upload_year} {upload_year} suffix"
|
||||
|
|
@ -119,19 +132,19 @@ class TestStringFormatterValidator(object):
|
|||
== expected_string
|
||||
)
|
||||
|
||||
def test_entry_formatter_override_recursive(self):
|
||||
def test_entry_formatter_override_recursive(self, string_formatter_class):
|
||||
variable_dict = {
|
||||
"level_a": "level a",
|
||||
"level_b": "level b and {level_a}",
|
||||
"level_c": "level c and {level_b}",
|
||||
}
|
||||
|
||||
format_string = StringFormatterValidator(name="test", value="level d and {level_c}")
|
||||
format_string = string_formatter_class(name="test", value="level d and {level_c}")
|
||||
expected_string = "level d and level c and level b and level a"
|
||||
|
||||
assert format_string.apply_formatter(variable_dict=variable_dict) == expected_string
|
||||
|
||||
def test_entry_formatter_override_recursive_fail_cycle(self):
|
||||
def test_entry_formatter_override_recursive_fail_cycle(self, string_formatter_class):
|
||||
variable_dict = {
|
||||
"level_a": "{level_b}",
|
||||
"level_b": "{level_a}",
|
||||
|
|
@ -144,21 +157,31 @@ class TestStringFormatterValidator(object):
|
|||
"Unresolved variables: level_b"
|
||||
)
|
||||
|
||||
format_string = StringFormatterValidator(name="test", value="{level_a}")
|
||||
format_string = string_formatter_class(name="test", value="{level_a}")
|
||||
|
||||
with pytest.raises(StringFormattingException, match=expected_error_msg):
|
||||
_ = format_string.apply_formatter(variable_dict=variable_dict)
|
||||
|
||||
|
||||
class TestDictFormatterValidator(object):
|
||||
def test_validates_values(self):
|
||||
@pytest.mark.parametrize(
|
||||
"dict_validator_class, expected_formatter_class",
|
||||
[
|
||||
(DictFormatterValidator, StringFormatterValidator),
|
||||
(OverridesDictFormatterValidator, OverridesStringFormatterValidator),
|
||||
],
|
||||
)
|
||||
def test_validates_values(self, dict_validator_class, expected_formatter_class):
|
||||
key1_format_string = "string with {variable}"
|
||||
key2_format_string = "no variables"
|
||||
validator = DictFormatterValidator(
|
||||
validator = dict_validator_class(
|
||||
name="validator",
|
||||
value={"key1": key1_format_string, "key2": key2_format_string},
|
||||
)
|
||||
|
||||
assert isinstance(validator.dict["key1"], expected_formatter_class)
|
||||
assert isinstance(validator.dict["key2"], expected_formatter_class)
|
||||
|
||||
assert validator.dict["key1"].format_string == key1_format_string
|
||||
assert validator.dict["key2"].format_string == key2_format_string
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue