def test_compile_regex_with_phone_number_regex():
    """Returns compiled regex from regex for a US telephone number."""
    rule_obj = Rule(1, "^(\d{3})-(\d{3})-(\d{4})$", "a", "b", 2)
    actual = rule_obj._coerce_source_matchpattern_as_compiled_regex()
    expected = Rule(1, re.compile("^(\\d{3})-(\\d{3})-(\\d{4})$"), "a", "b", 2)
    assert actual == expected
    assert re.search(expected.source_matchpattern, "216-321-1234")
Esempio n. 2
0
def test_compiles_regexstr_with_uppercase_letters_only():
    """Returns compiled regex from regex with uppercase characters."""
    rule_obj = Rule(1, "^[A-Z]*$", "a", "b", 2)
    actual = rule_obj.coerce_types()
    expected = Rule(1, re.compile("^[A-Z]*$"), "a", "b", 2)
    assert actual == expected
    assert re.search(expected.source_matchpattern, "ASDF")
Esempio n. 3
0
def test_compiles_regexstr_with_space():
    """Second field of Rule object, a regex, has an allowable space."""
    rule_obj = Rule(1, "^X 19", "a", "b", 2)
    actual = rule_obj.coerce_types()
    expected = Rule(1, re.compile("^X 19"), "a", "b", 2)
    assert rule_obj.source_matchpattern == re.compile("^X 19")
    assert actual == expected
def test_compiles_regexstr_correctly():
    """Returns regex string (field 2) in rule object as compiled regex."""
    rule_obj = Rule("1", "NOW", "a.txt", "a.txt", "0")
    actual = rule_obj._coerce_source_matchpattern_as_compiled_regex()
    expected = Rule("1", re.compile("NOW"), "a.txt", "a.txt", "0")
    assert isinstance(rule_obj.source_matchpattern, re.Pattern)
    assert rule_obj.source_matchpattern == re.compile("NOW")
    assert actual == expected
def test_rule_source_filename_field_was_properly_initialized_subsequent_source(
    reinitialize_ruleclass_variables,
):
    """Once initialized, the list of sources grows with each additional target."""
    rule_obj = Rule(1, "NOW", "lines", "now.txt", 0)
    rule_obj._source_filename_field_was_properly_initialized()
    rule_obj2 = Rule(2, "WORK", "now.txt", "now_work.txt", 0)
    assert rule_obj2._source_filename_field_was_properly_initialized()
def test_rule_source_not_initialized_too(reinitialize_ruleclass_variables):
    """Rule object correctly initialized sources from multiple rules."""
    rule_obj = Rule(1, "NOW", "a.txt", "b.txt", 0)
    rule_obj.is_valid()
    rule_obj2 = Rule(1, "LATER", "b.txt", "c.txt", 0)
    rule_obj2.is_valid()
    sources = ["a.txt", "b.txt", "c.txt"]
    assert Rule.sources_list == sources
Esempio n. 7
0
def test_returns_compiled_regex_given_already_compiled_regex():
    """Returns compiled regex given already compiled regex."""
    rule_obj = Rule("1", re.compile("NOW"), "a.txt", "a.txt", "0")
    actual = rule_obj.coerce_types()
    expected = Rule(1, re.compile("NOW"), "a.txt", "a.txt", 0)
    assert isinstance(rule_obj.source_matchpattern, re.Pattern)
    assert rule_obj.source_matchpattern == re.compile("NOW")
    assert actual == expected
def test_compiles_regexstr_that_was_already_compile():
    """Returns compiled regex given already compiled regex."""
    rule_obj = Rule("1", re.compile("NOW"), "a.txt", "a.txt", "0")
    actual = rule_obj._coerce_source_matchpattern_as_compiled_regex()
    expected = Rule("1", re.compile("NOW"), "a.txt", "a.txt", "0")
    assert isinstance(rule_obj.source_matchpattern, re.Pattern)
    assert rule_obj.source_matchpattern == re.compile("NOW")
    assert actual == expected
Esempio n. 9
0
def test_exits_when_passed_empty_datalines_list():
    """Exits with error if datalines list passed as argument is empty."""
    rules = [
        Rule(1, "NOW", "a.txt", "now.txt", 0),
        Rule(1, "LATER", "a.txt", "later.txt", 0),
    ]
    lines = []
    with pytest.raises(SystemExit):
        apply_rules_to_datalines(rules=rules, datalines=lines)
def test_compiles_regexstr_with_wildcards_and_one_space():
    """Returns compiled regex from regex with uppercase characters."""
    rule_obj = Rule(1, "^=* ", "a", "b", 2)
    actual = rule_obj._coerce_source_matchpattern_as_compiled_regex()
    expected = Rule(1, re.compile("^=* "), "a", "b", 2)
    assert actual == expected
    assert re.search(expected.source_matchpattern, "= ")
    assert re.search(expected.source_matchpattern, "== ")
    assert re.search(expected.source_matchpattern, "====== ")
Esempio n. 11
0
def test_rule_source_not_initialized_unprecedented(
        reinitialize_ruleclass_variables):
    """Rule class keeps track of instances registered, so
    second rule instance 'y' should raise exception because
    'c.txt' will not have been registered as a source."""
    rule_obj = Rule(1, "NOW", "a.txt", "b.txt", 0)
    rule_obj.is_valid()
    rule_obj2 = Rule(1, "LATER", "c.txt", "d.txt", 0)
    with pytest.raises(SystemExit):
        rule_obj2.is_valid()
Esempio n. 12
0
def test_two_rules_and_original_source_now_empty():
    """After processing two rules, lines now in values of new source keys."""
    rules = [
        Rule(1, "NOW", "a.txt", "now.txt", 0),
        Rule(1, "LATER", "a.txt", "later.txt", 0),
    ]
    lines = ["NOW Summer\n", "LATER Winter\n"]
    result_dict = {
        "now.txt": ["NOW Summer\n"],
        "later.txt": ["LATER Winter\n"],
        "a.txt": [],
    }
    actual_dict = apply_rules_to_datalines(rules, lines)
    assert actual_dict == result_dict
Esempio n. 13
0
def test_coerce_target_sortorder_as_integer_raise_exception_given_non_integer(
):
    """Perversely, int(1.2) evaluates to 1; improbable edge case?"""
    rule_obj = Rule(1.2, "NOW", "a", "b", 1.2)
    rule_obj.coerce_types()
    assert isinstance(rule_obj.target_sortorder, int)
    assert rule_obj.target_sortorder == 1
Esempio n. 14
0
def test_coerce_source_matchfield_as_integer_raise_exception_given_non_integer(
):
    """Perversely, int(1.2) evaluates to 1, so why not accept it?"""
    rule_obj = Rule(1.2, "NOW", "a", "b", 1.2)
    rule_obj._coerce_source_matchfield_as_integer()
    assert isinstance(rule_obj.source_matchfield, int)
    assert rule_obj.source_matchfield == 1
Esempio n. 15
0
def test_all_lines_moved_to_target():
    """Returns correct dictionary where all lines moved to target."""
    rules = [Rule(1, ".", "a.txt", "b.txt", None)]
    lines = ["LATER Winter\n", "NOW Summer\n"]
    expected_dict = {"a.txt": [], "b.txt": ["LATER Winter\n", "NOW Summer\n"]}
    actual_dict = apply_rules_to_datalines(rules, lines)
    assert actual_dict == expected_dict
def test_rule_source_filename_field_was_properly_initialized_initial_source(
    reinitialize_ruleclass_variables,
):
    """The 'source' field in the first rule (here: "a.txt")
    is used to initialize the list of sources."""
    rule_obj = Rule(1, "NOW", "a.txt", "b.txt", 0)
    assert rule_obj._source_filename_field_was_properly_initialized()
Esempio n. 17
0
def test_sorts_on_entire_line_given_sortorder_zero():
    """Correctly sorts on entire line."""
    rules = [Rule(0, "i", "a.txt", "b.txt", 0)]
    lines = ["two ticks\n", "an ant\n", "the mite\n"]
    expected_dict = {
        "a.txt": ["an ant\n"],
        "b.txt": ["the mite\n", "two ticks\n"]
    }
    actual_dict = apply_rules_to_datalines(rules, lines)
    assert actual_dict == expected_dict
Esempio n. 18
0
def test_sorts_on_second_field_given_sortorder_two():
    """Correctly sorts on second field."""
    rules = [Rule(2, "i", "a.txt", "b.txt", 2)]
    lines = ["the tick\n", "an ant\n", "two mites\n"]
    expected_dict = {
        "a.txt": ["an ant\n"],
        "b.txt": ["two mites\n", "the tick\n"]
    }
    actual_dict = apply_rules_to_datalines(rules, lines)
    assert actual_dict == expected_dict
def test_source_is_valid_filename():
    """Field 3 (source) must be a valid filename."""
    rule_obj = Rule(1, "NOW", "a", "b", 2)
    rule_obj._coerce_source_as_valid_filename()
    assert rule_obj.source == "a"
def test_raise_exception_given_bad_filename_string():
    """Field 3 (source) must not contain invalid characters."""
    rule_obj = Rule(1, "NOW", "a/2:", "b", 2)
    with pytest.raises(SystemExit):
        rule_obj._coerce_source_as_valid_filename()
def test_raise_exception_given_source_filename_none():
    """Field 3 (source) must not be None."""
    rule_obj = Rule(1, "NOW", None, "b", 2)
    with pytest.raises(SystemExit):
        rule_obj._coerce_source_as_valid_filename()
Esempio n. 22
0
def test_coerce_source_matchfield_as_integer():
    """Field 1 (source_matchfield) must be an integer."""
    rule_obj = Rule(1, "NOW", "a", "b", 2)
    rule_obj._coerce_source_matchfield_as_integer()
    assert isinstance(rule_obj.source_matchfield, int)
def test_pathlike_object_is_valid_filename():
    """Source could be a Path object."""
    rule_obj = Rule(1, "NOW", Path("a"), "b", 2)
    rule_obj._coerce_source_as_valid_filename()
    assert rule_obj.source == "a"
Esempio n. 24
0
def test_rule_source_filename_field_is_not_equal_target_oops():
    """Source and target fields of rule object are same, raises SystemExit."""
    rule_obj = Rule("1", "NOW", "a.txt", "a.txt", "0")
    with pytest.raises(SystemExit):
        rule_obj._source_filename_field_is_not_equal_target()
Esempio n. 25
0
def test_coerce_source_matchfield_as_integer_raise_exception_given_bad_string(
):
    """Field 1 (source_matchfield) must be an integer."""
    rule_obj = Rule("1 2", "NOW", "a", "b", 2)
    with pytest.raises(SystemExit):
        rule_obj._coerce_source_matchfield_as_integer()
Esempio n. 26
0
def test_coerce_source_matchfield_as_integer_given_good_string():
    """Field 1 (source_matchfield) must be an integer."""
    rule_obj = Rule("1", "NOW", "a", "b", 2)
    rule_obj._coerce_source_matchfield_as_integer()
    assert isinstance(rule_obj.source_matchfield, int)
    assert rule_obj.source_matchfield == 1
Esempio n. 27
0
def test_compiles_regexstr_with_double_escaped_backslash():
    """Compiles regex string with double-escaped backslash."""
    rule_obj = Rule(1, "N\\\\OW", "a", "b", 2)
    actual = rule_obj.coerce_types()
    expected = Rule(1, re.compile("N\\\\OW"), "a", "b", 2)
    assert actual == expected
Esempio n. 28
0
def test_coerce_target_sortorder_as_integer_raise_exception_given_bad_string():
    """Target sortorder must be an integer."""
    rule_obj = Rule("1 2", "NOW", "a", "b", "1 2")
    with pytest.raises(SystemExit):
        rule_obj.coerce_types()
Esempio n. 29
0
def test_rule_source_filename_field_is_not_equal_target():
    """Source and target fields of rule object are not equivalent."""
    rule_obj = Rule("1", "NOW", "a.txt", "b.txt", "0")
    assert rule_obj._source_filename_field_is_not_equal_target
Esempio n. 30
0
def test_exits_if_regexstr_has_unescaped_parenthesis():
    """Exits if regex string does not compile(here: unescaped parenthesis)."""
    rule_obj = Rule("1", "N(OW", "a.txt", "a.txt", "0")
    with pytest.raises(SystemExit):
        rule_obj.coerce_types()