Example #1
0
def test_write_csv_hash(rows, header, encoding, hash_name, expected):
    kwargs = {'header': header, 'encoding': encoding}
    hashsum = hashlib.new(hash_name)

    if isinstance(expected, tuple):
        assert encoding is None
        with pytest.raises(expected[0], match=expected[1]):
            write_csv(hashsum, rows, **kwargs)
        return

    result = write_csv(hashsum, rows, **kwargs)

    assert result is hashsum
    assert result.hexdigest() == expected
Example #2
0
def test_roundtrip_csv_autocompress(tmp_path, filename, open_module, raw,
                                    encoding, rows):
    if sys.version_info < (3, 6):
        tmp_path = pathlib.Path(str(tmp_path))

    target = tmp_path / filename

    filename = str(target) if sys.version_info < (3, 6) else target

    import importlib

    open_module = importlib.import_module(open_module)

    with open_module.open(filename, 'wb') as f:
        f.write(raw)

    kwargs = {'encoding': encoding, 'autocompress': True}

    assert read_csv(filename, as_list=True, **kwargs) == rows

    target.unlink()

    result = write_csv(filename, rows, **kwargs)

    assert result.exists()
    assert result.samefile(target)

    assert read_csv(filename, as_list=True, **kwargs) == rows
Example #3
0
def test_write_csv_equivalence(tmp_path, rows, header, encoding, hash_name):
    if sys.version_info < (3, 6):
        tmp_path = pathlib.Path(str(tmp_path))

    make_hash = functools.partial(hashlib.new, hash_name)

    kwargs = {'header': header, 'encoding': encoding}

    r_hash = write_csv(make_hash(), rows, **kwargs)

    r_none = write_csv(None, rows, **kwargs)
    r_write = write_csv(io.BytesIO(), rows, **kwargs).getvalue()
    r_filename = write_csv(tmp_path / 'spam.csv', rows, **kwargs).read_bytes()

    assert (r_hash.hexdigest() == make_hash(r_none).hexdigest() ==
            make_hash(r_write).hexdigest() ==
            make_hash(r_filename).hexdigest())
Example #4
0
def test_write_csv_zipfile(tmp_path, rows, header, encoding, expected):
    if sys.version_info < (3, 6):
        tmp_path = pathlib.Path(str(tmp_path))

    kwargs = {'header': header, 'encoding': encoding}

    archive = tmp_path / 'spam.zip'
    filename = 'spam.csv'
    with zipfile.ZipFile(archive, 'w') as z,\
         z.open(filename, 'w') as f:
        if encoding is None:
            with pytest.raises(expected[0], match=expected[1]):
                write_csv(f, rows, **kwargs)
            return

        result = write_csv(f, rows, **kwargs)

    assert result is f
    assert archive.exists()
    assert archive.stat().st_size
    with zipfile.ZipFile(archive) as z:
        assert z.namelist() == [filename]
        assert z.read(filename) == expected
Example #5
0
def test_write_csv_filename(tmp_path, filename, rows, header, encoding, expected):
    if sys.version_info < (3, 6):
        tmp_path = pathlib.Path(str(tmp_path))

    kwargs = {'header': header, 'encoding': encoding}

    if isinstance(expected, tuple):
        assert encoding is None
        with pytest.raises(expected[0], match=expected[1]):
            write_csv(filename, rows, **kwargs)
        return

    target = tmp_path / filename

    with chdir(tmp_path):
        result = write_csv(filename, rows, **kwargs)
        assert result.exists()
        assert target.exists()
        assert result.samefile(target)
        assert result.samefile(pathlib.Path(filename))

    assert target.stat().st_size
    assert target.read_bytes() == expected
Example #6
0
def test_write_csv_py2():
    with pytest.raises(NotImplementedError):
        write_csv('spam.csv', ROWS, header=None, encoding='utf-8')
Example #7
0
def test_write_csv_write(rows, header, encoding, expected):
    buf = io.StringIO() if encoding is None else io.BytesIO()
    result = write_csv(buf, rows, header=header, encoding=encoding)
    assert result is buf
    assert result.getvalue() == expected
Example #8
0
def test_write_csv_none(rows, header, encoding, expected):
    result = write_csv(None, rows, header=header, encoding=encoding)
    assert result == expected