示例#1
0
def test_assert_functions():
    assert assert_if(10 > 5) == check_if(10 > 5)
    with pytest.raises(AssertionError):
        assert_if(10 < 5) and check_if(10 < 5) is None

    assert assert_if_not(10 < 5) == check_if_not(10 < 5)
    with pytest.raises(AssertionError):
        assert_if_not(10 > 5) and check_if_not(10 > 5) is None

    assert assert_type((10, 10), tuple) == check_type((10, 10), tuple)
    with pytest.raises(TypeError):
        assert_type(10, tuple) and check_type(10, tuple) is None

    assert assert_length("str", 3) == check_length("str", 3)
    assert assert_length(5, 1, assign_length_to_others=True) == check_length(
        5, 1, assign_length_to_others=True)
    with pytest.raises(TypeError):
        assert_length(5, 3) and check_length(5, 3) is None
    with pytest.raises(LengthError):
        (assert_length(5, 3, assign_length_to_others=True)
         and check_length(5, 3, assign_length_to_others=True) is None)

    existing_file = os.listdir(".")[0]
    assert check_if_paths_exist(existing_file,
                                execution_mode="return") == assert_paths(
                                    existing_file, execution_mode="return")
    assert (check_if_paths_exist("Q:/E/",
                                 execution_mode="return")[1] == assert_paths(
                                     "Q:/E/", execution_mode="return")[1])
    with pytest.raises(FileNotFoundError):
        assert_paths("Q:/E/") and check_if_paths_exist("Q:/E/") is None
示例#2
0
def test_assert_functions():
    assert assert_if(10 > 5) == check_if(10 > 5)
    with pytest.raises(AssertionError):
        assert_if(10 < 5) and check_if(10 < 5) is None

    assert assert_if_not(10 < 5) == check_if_not(10 < 5)
    with pytest.raises(AssertionError):
        assert_if_not(10 > 5) and check_if_not(10 > 5) is None

    assert assert_instance((10, 10), tuple) == check_instance((10, 10), tuple)
    with pytest.raises(TypeError):
        assert_instance(10, tuple) and check_instance(10, tuple) is None

    assert assert_length('str', 3) == check_length('str', 3)
    assert (assert_length(5, 1, assign_length_to_others=True) == check_length(
        5, 1, assign_length_to_others=True))
    with pytest.raises(TypeError):
        assert_length(5, 3) and check_length(5, 3) is None
    with pytest.raises(LengthError):
        (assert_length(5, 3, assign_length_to_others=True)
         and check_length(5, 3, assign_length_to_others=True) is None)

    existing_file = os.listdir('.')[0]
    assert (check_if_paths_exist(existing_file,
                                 execution_mode='return') == assert_paths(
                                     existing_file, execution_mode='return'))
    assert (check_if_paths_exist('Q:/E/',
                                 execution_mode='return')[1] == assert_paths(
                                     'Q:/E/', execution_mode='return')[1])
    with pytest.raises(FileNotFoundError):
        assert_paths('Q:/E/') and check_if_paths_exist('Q:/E/') is None
示例#3
0
def test_check_if_negative_warnings():
    with warnings.catch_warnings(record=True) as w:
        check_if(2 < 1, Warning, "This is a testing warning")
        assert "This is a testing warning" in str(w[-1].message)

    with warnings.catch_warnings(record=True) as w:
        check_if(2 < 1, UserWarning, "This is a testing warning")
        assert issubclass(w[-1].category, Warning)
        assert "This is a testing warning" in str(w[-1].message)
示例#4
0
def test_message_is_None_exception_with_docstring():
    with pytest.raises(AssertionError, match=""):
        check_if(1 == 2, message=None)
    with pytest.raises(AssertionError, match="Error"):
        check_if(1 == 2, message="Error")
    with pytest.raises(AssertionError):
        check_if(1 == 2)
    assert check_if(1 == 1, ForTestingErrorWithDoc) is None
    with pytest.raises(ForTestingErrorWithDoc, match="for testing purposes"):
        check_if(1 == 2, ForTestingErrorWithDoc)
示例#5
0
def test_check_all_ifs_edge_cases():
    with pytest.raises(ValueError, match="at least one condition"):
        check_all_ifs()
    with pytest.raises(TypeError, match="Provide all function calls as"):
        check_all_ifs(1)
    with pytest.raises(TypeError, match="Provide all function calls as"):
        check_all_ifs(True)
    with pytest.raises(TypeError, match="Provide all function calls as"):
        check_all_ifs(1, 1)
    with pytest.raises(TypeError, match="Provide all function calls as"):
        check_all_ifs(1 > 1, 2 > 1)
    with pytest.raises(TypeError, match="Provide all function calls as"):
        check_all_ifs((20 > 10))
    with pytest.raises(TypeError, match="Provide all function calls as"):
        check_all_ifs(check_if(20 > 10)),
    with pytest.raises(TypeError, match="Provide all function calls as"):
        check_all_ifs((check_if, 20 > 10), (check_if(20 > 10)))
示例#6
0
def test_check_if_negative():
    with pytest.raises(AssertionError):
        check_if(2 < 1)
    with pytest.raises(ValueError):
        check_if(2 < 1, handle_with=ValueError)
    with pytest.raises(ValueError, match="incorrect value"):
        check_if(2 < 1, handle_with=ValueError, message="incorrect value")
示例#7
0
def test_check_if_positive():
    assert check_if(2 > 1) is None
    assert check_if(2 > 1, Warning) is None
    assert check_if(0 == 0) is None
    assert check_if(0 == 0, Warning) is None
    assert check_if(None is None) is None
    assert check_if(None is None, Warning) is None
示例#8
0
def test_check_if_edge_cases():
    with pytest.raises(TypeError, match="required positional argument"):
        check_if()
    with pytest.raises(ValueError, match="The condition does not return"):
        check_if("tomato soup is good")
    with pytest.raises(ValueError, match="The condition does not return"):
        check_if("")
    with pytest.raises(ValueError, match="The condition does not return"):
        check_if(1)
    with pytest.raises(ValueError, match="The condition does not return"):
        check_if(0)
    assert check_if(True) is None
    with pytest.raises(AssertionError):
        check_if(False)
    with pytest.raises(TypeError, match="handle_with must be an exception"):
        check_if(1, 1)
    with pytest.raises(TypeError,
                       match="message must be either None or string"):
        check_if(1, ValueError, 1)
    with pytest.raises(TypeError, match="takes from 1 to 3 positional"):
        check_if(1, 1, 1, 1)
示例#9
0
def test_message_is_None_exception_without_docstring():
    assert check_if(1 == 1, ForTestingErrorWithoutDoc) is None
    with pytest.raises(ForTestingErrorWithoutDoc, match="Error! Shout!"):
        check_if(1 == 2, ForTestingErrorWithoutDoc, message="Error! Shout!")
    with pytest.raises(ForTestingErrorWithoutDoc, match=""):
        check_if(1 == 2, ForTestingErrorWithoutDoc)