Ejemplo n.º 1
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")
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
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
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()
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")
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
Ejemplo n.º 8
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_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_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, "====== ")
Ejemplo n.º 11
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)
Ejemplo 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
Ejemplo n.º 13
0
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
Ejemplo n.º 14
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
Ejemplo n.º 15
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()
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()
Ejemplo n.º 17
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
Ejemplo n.º 18
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
Ejemplo n.º 19
0
def test_rule_source_not_initialized_too(reinitialize_ruleclass_variables):
    """Rule object correctly initialized sources from multiple rules."""
    x = Rule(1, "NOW", "a.txt", "b.txt", 0)
    x.is_valid()
    y = Rule(1, "LATER", "b.txt", "c.txt", 0)
    y.is_valid()
    sources = ["a.txt", "b.txt", "c.txt"]
    assert Rule.sources_list == sources
Ejemplo n.º 20
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."""
    x = Rule("1", "NOW", "a.txt", "b.txt", "0")
    x.is_valid()
    y = Rule("1", "LATER", "c.txt", "d.txt", "0")
    with pytest.raises(SystemExit):
        y.is_valid()
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"
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_rule_number_fields_are_integers(reinitialize_ruleclass_variables):
    x = Rule("1", "N(OW", "a", "b", 2)
    assert x._number_fields_are_integers() == 1
Ejemplo n.º 24
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)
Ejemplo n.º 25
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
Ejemplo n.º 26
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()
Ejemplo n.º 27
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()
Ejemplo n.º 28
0
def test_rule_source_not_initialized(reinitialize_ruleclass_variables):
    """Rule object was initialized with 'source' of first rule."""
    x = Rule(1, "NOW", "a.txt", "b.txt", 0)
    x.is_valid()
    y = Rule(1, "LATER", "b.txt", "c.txt", 0)
    assert y.is_valid()
Ejemplo n.º 29
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
Ejemplo n.º 30
0
def test_rule_is_valid_number_fields_are_integers(reinitialize_ruleclass_variables):
    """First and last fields of rule object are integers."""
    x = Rule("1", "NOW", "a.txt", "b.txt", "0")
    assert x.is_valid()
Ejemplo n.º 31
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()
Ejemplo n.º 32
0
def test_rule_is_valid_number_fields_are_integers_too(reinitialize_ruleclass_variables):
    """Rule object is valid even if initialized with string integers."""
    x = Rule("1", "NOW", "a.txt", "b.txt", "2")
    assert x.is_valid()
Ejemplo n.º 33
0
def test_rule_source_matchpattern_is_not_valid_too(reinitialize_ruleclass_variables):
    """Rule object fails self-validation because regex is bad."""
    x = Rule(1, "N(OW", "a", "b", 2)
    with pytest.raises(SystemExit):
        x.is_valid()
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()
def test_rule_source_is_not_equal_target_oops():
    """Source and target fields of rule object are same, raises SystemExit."""
    x = Rule("1", "NOW", "a.txt", "a.txt", "0")
    with pytest.raises(SystemExit):
        x._source_is_not_equal_target()
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"
Ejemplo n.º 37
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()
Ejemplo n.º 38
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
Ejemplo n.º 39
0
def test_rule_is_valid(reinitialize_ruleclass_variables):
    """A well-formed rule object is valid."""
    x = Rule(1, "NOW", "a.txt", "b.txt", 2)
    assert x.is_valid()