Ejemplo n.º 1
0
def test_split_file():
    input_file = "tests/data/mock_file.txt"
    output_file = "tests/data/mock_output_file.txt"

    splits = split_file(input_file, output_file)
    assert splits == [input_file]

    expected = ["tests/data/mock_output_file.txt.0", "tests/data/mock_output_file.txt.1"]
    splits = split_file(input_file, output_file, 2)
    assert splits == expected
    assert compare_file_contents(input_file, expected)
    cleanup(splits)

    expected = [
        "tests/data/mock_output_file.txt.0",
        "tests/data/mock_output_file.txt.1",
        "tests/data/mock_output_file.txt.2",
    ]
    splits = split_file(input_file, output_file, 3)
    assert splits == expected
    assert compare_file_contents(input_file, expected)
    cleanup(splits)

    expected = [
        "tests/data/mock_output_file.txt.0",
        "tests/data/mock_output_file.txt.1",
        "tests/data/mock_output_file.txt.2",
        "tests/data/mock_output_file.txt.3",
        "tests/data/mock_output_file.txt.4",
    ]
    splits = split_file(input_file, output_file, 5)
    assert splits == expected
    assert compare_file_contents(input_file, expected)
    cleanup(splits)
def test_split_file():
    input_file = 'tests/data/mock_file.txt'
    output_file = 'tests/data/mock_output_file.txt'

    expected = ['tests/data/mock_output_file.txt.0',
                'tests/data/mock_output_file.txt.1']
    splits = split_file(input_file, output_file)
    assert splits == expected
    assert compare_file_contents(input_file, expected)
    cleanup(splits)


    expected = ['tests/data/mock_output_file.txt.0',
                'tests/data/mock_output_file.txt.1',
                'tests/data/mock_output_file.txt.2']
    splits = split_file(input_file, output_file, 3)
    assert splits == expected
    assert compare_file_contents(input_file, expected)
    cleanup(splits)


    expected = ['tests/data/mock_output_file.txt.0',
                'tests/data/mock_output_file.txt.1',
                'tests/data/mock_output_file.txt.2',
                'tests/data/mock_output_file.txt.3',
                'tests/data/mock_output_file.txt.4']
    splits = split_file(input_file, output_file, 5)
    assert splits == expected
    assert compare_file_contents(input_file, expected)
    cleanup(splits)
def test_split_file_header():
    input_file = "tests/data/mock_file_header.txt"
    input_file_no_header = "tests/data/mock_file.txt"
    output_file = "tests/data/mock_output_file_header.txt"

    splits = split_file(input_file, output_file, ignore_header=1)
    assert splits == [input_file]

    expected = [
        "tests/data/mock_output_file_header.txt.0",
        "tests/data/mock_output_file_header.txt.1",
    ]
    splits = split_file(input_file, output_file, 2, ignore_header=1)
    assert splits == expected
    assert compare_file_contents(input_file_no_header, expected)
    cleanup(splits)

    expected = [
        "tests/data/mock_output_file_header.txt.0",
        "tests/data/mock_output_file_header.txt.1",
        "tests/data/mock_output_file_header.txt.2",
    ]
    splits = split_file(input_file, output_file, 3, ignore_header=1)
    assert splits == expected
    assert compare_file_contents(input_file_no_header, expected)
    cleanup(splits)

    expected = [
        "tests/data/mock_output_file_header.txt.0",
        "tests/data/mock_output_file_header.txt.1",
        "tests/data/mock_output_file_header.txt.2",
        "tests/data/mock_output_file_header.txt.3",
        "tests/data/mock_output_file_header.txt.4",
    ]
    splits = split_file(input_file, output_file, 5, ignore_header=1)
    assert splits == expected
    assert compare_file_contents(input_file_no_header, expected)
    cleanup(splits)
def test_split_file_exception():
    input_file = "tests/data/mock_file.txt"
    output_file = "tests/data/mock_output_file.txt"

    if sys.version_info.major == 3:
        builtin_module_name = "builtins"
    else:
        builtin_module_name = "__builtin__"

    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, -1)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, 0)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, 5.65)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, "123")
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, "Test")

    with mock.patch("{0}.next".format(builtin_module_name)) as mock_next:
        with mock.patch("os.remove") as mock_remove:
            mock_next.side_effect = Exception("SomeException")

            with pytest.raises(LocopySplitError):
                split_file(input_file, output_file, 2)
            assert mock_remove.call_count == 2
            mock_remove.reset_mock()

            with pytest.raises(LocopySplitError):
                split_file(input_file, output_file, 3)
            assert mock_remove.call_count == 3

    cleanup([
        "tests/data/mock_output_file.txt.0",
        "tests/data/mock_output_file.txt.1",
        "tests/data/mock_output_file.txt.2",
    ])
def test_split_file_exception():
    input_file = 'tests/data/mock_file.txt'
    output_file = 'tests/data/mock_output_file.txt'

    if sys.version_info.major == 3:
        builtin_module_name = 'builtins'
    else:
        builtin_module_name = '__builtin__'

    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, -1)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, 0)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, 1)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, 5.65)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, "123")
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, "Test")


    with mock.patch('{0}.next'.format(builtin_module_name)) as mock_next:
        with mock.patch('os.remove') as mock_remove:
            mock_next.side_effect = Exception('SomeException')

            with pytest.raises(LocopySplitError):
                split_file(input_file, output_file)
            assert mock_remove.call_count == 2
            mock_remove.reset_mock()

            with pytest.raises(LocopySplitError):
                split_file(input_file, output_file, 3)
            assert mock_remove.call_count == 3

    cleanup(['tests/data/mock_output_file.txt.0',
             'tests/data/mock_output_file.txt.1',
             'tests/data/mock_output_file.txt.2'])
Ejemplo n.º 6
0
def test_split_file_exception():
    input_file = "tests/data/mock_file.txt"
    output_file = "tests/data/mock_output_file.txt"

    if sys.version_info.major == 3:
        builtin_module_name = "builtins"
    else:
        builtin_module_name = "__builtin__"

    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, -1)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, 0)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, 5.65)
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, "123")
    with pytest.raises(LocopySplitError):
        split_file(input_file, output_file, "Test")

    with mock.patch("{0}.next".format(builtin_module_name)) as mock_next:
            mock_next.side_effect = Exception("SomeException")

            with pytest.raises(LocopySplitError):
                split_file(input_file, output_file, 2)
            assert not Path("tests/data/mock_output_file.txt.0").exists()
            assert not Path("tests/data/mock_output_file.txt.1").exists()

            with pytest.raises(LocopySplitError):
                split_file(input_file, output_file, 3)
            assert not Path("tests/data/mock_output_file.txt.0").exists()
            assert not Path("tests/data/mock_output_file.txt.1").exists()
            assert not Path("tests/data/mock_output_file.txt.2").exists()