Ejemplo n.º 1
0
def test_catch_check_length():
    my_check = catch_check(check_length, [2, 2], 2)
    assert my_check is None

    my_check = catch_check(
        check_length,
        [2, 2],
        expected_length=2,
        handle_with=Warning,
        message="Length problem",
    )
    assert my_check is None

    my_check_not = catch_check(check_length, [2, 2], 3)
    assert isinstance(my_check_not, LengthError)
    with pytest.raises(LengthError):
        raise my_check_not

    my_check = catch_check(
        check_length,
        [2, 2],
        expected_length=3,
        handle_with=Warning,
        message="Length problem",
    )
    assert isinstance(my_check, Warning)
    assert "Length problem" in str(my_check)
Ejemplo n.º 2
0
def test_catch_check_paths_with_return():
    existing_path = os.listdir('.')[0]
    with pytest.raises(ValueError, match='execution_mode="return"'):
        catch_check(check_if_paths_exist,
                    paths=existing_path,
                    execution_mode='return')
    with pytest.raises(ValueError, match='execution_mode="return"'):
        catch_check(check_if_paths_exist, existing_path, FileNotFoundError,
                    'Path not found', 'return')
Ejemplo n.º 3
0
def test_catch_check_if_not():
    my_check = catch_check(check_if_not, 2 > 2)
    assert my_check is None

    my_check = catch_check(check_if_not, 2 > 2, Warning, "Problem!")
    assert my_check is None

    my_check_not = catch_check(check_if_not, 2 == 2)
    assert isinstance(my_check_not, AssertionError)
    with pytest.raises(AssertionError):
        raise my_check_not

    my_check_not = catch_check(check_if_not,
                               2 == 2,
                               handle_with=Warning,
                               message="Problem!")
    assert isinstance(my_check_not, Warning)
Ejemplo n.º 4
0
def test_catch_check_paths_one_path():
    existing_path = os.listdir('.')[0]
    my_check = catch_check(check_if_paths_exist, paths=existing_path)
    my_check_path = catch_check(check_if_paths_exist,
                                paths=Path(existing_path))
    assert my_check is None
    assert my_check_path is None

    my_check = catch_check(check_if_paths_exist,
                           paths=existing_path,
                           handle_with=Warning,
                           message='Path problem')
    assert my_check is None
    my_check_path = catch_check(check_if_paths_exist,
                                paths=Path(existing_path),
                                handle_with=Warning,
                                message='Path problem')
    assert my_check_path is None

    non_existing_path = 'W:/Op/No_no'
    my_check_not = catch_check(check_if_paths_exist, paths=non_existing_path)
    my_check_not_path = catch_check(check_if_paths_exist,
                                    paths=Path(non_existing_path))
    assert isinstance(my_check_not, FileNotFoundError)
    with pytest.raises(FileNotFoundError):
        raise my_check_not
    assert isinstance(my_check_not_path, FileNotFoundError)
    with pytest.raises(FileNotFoundError):
        raise my_check_not_path

    my_check_not = catch_check(check_if_paths_exist,
                               paths=non_existing_path,
                               handle_with=Warning,
                               message='Path problem')
    assert isinstance(my_check_not, Warning)
Ejemplo n.º 5
0
def test_catch_check_type():
    my_check = catch_check(check_type, 25, int)
    assert my_check is None

    my_check = catch_check(check_type, 25, int, Warning, "Instance issue")
    assert my_check is None

    my_check_not = catch_check(check_type, 25, float, ValueError,
                               "This is no float!")
    assert isinstance(my_check_not, ValueError)
    with pytest.raises(ValueError, match="This is no float!"):
        raise my_check_not

    my_check = catch_check(check_type, 25, float, Warning, "Instance issue")
    assert isinstance(my_check, Warning)

    my_check = catch_check(check_type, "a", int)
    assert isinstance(my_check, TypeError)
    with pytest.raises(TypeError):
        raise my_check
Ejemplo n.º 6
0
def test_catch_check_instance():
    my_check = catch_check(check_instance, 25, int)
    assert my_check is None

    my_check = catch_check(check_instance, 25, int, Warning, 'Instance issue')
    assert my_check is None

    my_check_not = catch_check(check_instance, 25, float, ValueError,
                               'This is no float!')
    assert isinstance(my_check_not, ValueError)
    with pytest.raises(ValueError, match='This is no float!'):
        raise my_check_not

    my_check = catch_check(check_instance, 25, float, Warning,
                           'Instance issue')
    assert isinstance(my_check, Warning)

    my_check = catch_check(check_instance, 'a', int)
    assert isinstance(my_check, TypeError)
    with pytest.raises(TypeError):
        raise my_check
Ejemplo n.º 7
0
def test_catch_check_paths_many_paths():
    existing_paths = os.listdir(".")
    my_check = catch_check(check_if_paths_exist, paths=existing_paths)
    my_check_path = catch_check(check_if_paths_exist,
                                paths=[Path(path) for path in existing_paths])
    assert my_check is None
    assert my_check_path is None

    my_check = catch_check(
        check_if_paths_exist,
        paths=existing_paths,
        handle_with=Warning,
        message="Path issue",
    )
    assert my_check is None

    non_existing_paths = ["W:/Op/No_no"] + os.listdir(".")
    my_check_not = catch_check(check_if_paths_exist, paths=non_existing_paths)
    my_check_not_path = catch_check(
        check_if_paths_exist,
        paths=[Path(path) for path in non_existing_paths])
    assert isinstance(my_check_not, FileNotFoundError)
    with pytest.raises(FileNotFoundError):
        raise my_check_not
    assert isinstance(my_check_not_path, FileNotFoundError)
    with pytest.raises(FileNotFoundError):
        raise my_check_not_path

    my_check_not = catch_check(
        check_if_paths_exist,
        paths=non_existing_paths,
        handle_with=Warning,
        message="Path issue",
    )
    assert isinstance(my_check_not, Warning)
Ejemplo n.º 8
0
def test_catch_check_edge_cases():
    with pytest.raises(TypeError, match="required positional argument"):
        catch_check(check=check_type)
    with pytest.raises(TypeError, match="easycheck function"):
        catch_check(1)
    with pytest.raises(ArgumentValueError,
                       match="acceptable valid easycheck functions"):
        catch_check(sum)
Ejemplo n.º 9
0
def test_catch_check_edge_cases():
    with pytest.raises(TypeError, match='required positional argument'):
        catch_check(check=check_instance)
    with pytest.raises(TypeError, match='easycheck function'):
        catch_check(1)
    with pytest.raises(ArgumentValueError,
                       match='acceptable valid easycheck functions'):
        catch_check(sum)
Ejemplo n.º 10
0
def test_catch_check_if():
    my_check = catch_check(check_if, 2 == 2)
    assert my_check is None

    my_check = catch_check(check_if, 2 == 2, Warning)
    assert my_check is None

    my_check_not = catch_check(check_if, 2 > 2)
    with pytest.raises(AssertionError):
        raise my_check_not

    my_check_not = catch_check(check_if, 2 > 2, UserWarning, "Problem!")
    assert isinstance(my_check_not, Warning)

    my_check_not = catch_check(check_if, 2 > 2, ValueError)
    assert isinstance(my_check_not, ValueError)
    with pytest.raises(ValueError):
        raise my_check_not

    my_check_not = catch_check(check_if, 2 > 2, Warning)
    assert isinstance(my_check_not, Warning)