示例#1
0
def test_csv_export_create_dicts():
    exporter = ExportCSVMixin()
    headers = ["foo", "bar"]
    data = []

    for x in range(10):
        data.append([f"foo{x}", f"bar{x}"])

    csv_dicts = exporter._create_dicts(headers, data)

    for index, dct in enumerate(csv_dicts):
        assert dct == {"foo": f"foo{index}", "bar": f"bar{index}"}
示例#2
0
def test_csv_export_preprocessing(tmp_path, data, elements, lines):
    exporter = ExportCSVMixin()
    processed = exporter._preprocess_data(data)
    assert len(processed) == lines

    # Unfortunately, we have to create an actual file here, as both tempfile
    # and StringIO seem to cause issues with line endings
    with open(tmp_path / "csv.csv", "w+", newline="") as f:
        writer = csv.writer(f)
        writer.writerows(processed)

    with open(tmp_path / "csv.csv", "r", newline="") as f:
        reader = csv.reader(f)
        for line in reader:
            assert len(line) == elements
        assert reader.line_num == lines