Exemplo n.º 1
0
def test_rowtype_dict(filepath, fieldnames=FIELDNAMES, rowdicts=ROWDICTS):
    with open_writer(str(filepath), rowtype='dict',
                     fieldnames=fieldnames) as writer:
        assert isinstance(writer, csv.DictWriter)
        writer.writeheader()
        for r in rowdicts:
            writer.writerow(r)

    with open_reader(str(filepath), rowtype='dict') as reader:
        assert isinstance(reader, csv.DictReader)
        assert list(reader) == rowdicts
Exemplo n.º 2
0
def test_open_writer(py2, filepath, encoding, row, fmtparams, expected, n=12):
    try:
        expected.encode(encoding)
    except UnicodeEncodeError:
        pytest.skip('impossible combination of row and encoding')

    write_n = len(
        expected.encode(encoding
                        ) if py2 and is_8bit_clean(encoding) else expected)

    filename = str(filepath)

    with open_writer(filename, encoding=encoding, **fmtparams) as w:
        written = w.writerow(row)
        w.writerows([row] * (n - 1))

    with io.open(filename, encoding=encoding, newline='') as f:
        line = f.read()

    assert line == expected * n
    assert written == write_n
Exemplo n.º 3
0
def test_open_writer_missing_fieldnames(mocker):
    with pytest.raises(TypeError, match='fieldnames'):
        open_writer(mocker.sentinel.stream, rowtype='dict')
Exemplo n.º 4
0
def test_open_writer_fail(filepath, encoding, row, fmtparams, expected, match):
    filename = str(filepath)
    with open_writer(filename, encoding=encoding, **fmtparams) as w:
        with pytest.raises(expected, match=match):
            w.writerow(row)