From cb77b6632bd88d43e91858d25fa2b3cc0ab9c620 Mon Sep 17 00:00:00 2001 From: saadiqbal09 Date: Fri, 26 Dec 2025 15:55:33 +0530 Subject: [PATCH 1/2] Add CSV and JSON export utilities with basic tests --- tests/test_data_export.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/test_data_export.py diff --git a/tests/test_data_export.py b/tests/test_data_export.py new file mode 100644 index 0000000..19209d4 --- /dev/null +++ b/tests/test_data_export.py @@ -0,0 +1,18 @@ +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() + +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() + +def test_export_empty_data_raises_error(): + with pytest.raises(ValueError): + export_to_csv([], "dummy.csv") From 65ee5b8ee3bcdb1784575c37f48a36fbba78face Mon Sep 17 00:00:00 2001 From: saadiqbal09 Date: Fri, 26 Dec 2025 17:08:53 +0530 Subject: [PATCH 2/2] Improve tests to validate CSV and JSON export contents --- tests/test_data_export.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_data_export.py b/tests/test_data_export.py index 19209d4..d6ef06a 100644 --- a/tests/test_data_export.py +++ b/tests/test_data_export.py @@ -1,18 +1,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")