-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathtest_data_export.py
More file actions
39 lines (27 loc) · 954 Bytes
/
test_data_export.py
File metadata and controls
39 lines (27 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import csv
import json
import pytest
from pslab.utils.data_export import export_to_csv, export_to_json
def test_export_csv_creates_file(tmp_path):
data = [{"a": 1, "b": 2}]
file_path = tmp_path / "data.csv"
export_to_csv(data, str(file_path))
assert file_path.exists()
with file_path.open(newline="") as f:
reader = csv.DictReader(f)
assert reader.fieldnames == ["a", "b"]
rows = list(reader)
assert len(rows) == 1
assert rows[0]["a"] == "1"
assert rows[0]["b"] == "2"
def test_export_json_creates_file(tmp_path):
data = [{"x": 10}]
file_path = tmp_path / "data.json"
export_to_json(data, str(file_path))
assert file_path.exists()
with file_path.open("r", encoding="utf-8") as f:
loaded = json.load(f)
assert loaded == data
def test_export_empty_data_raises_error():
with pytest.raises(ValueError):
export_to_csv([], "dummy.csv")