Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/test_data_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +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()
Comment on lines 7 to 12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): CSV export test only checks that the file exists but not that its contents are correct.

This test would still pass if the CSV had wrong headers, delimiters, or malformed rows. Please extend it to read and assert the contents—for example, that the header matches the expected columns, the row count is correct, and at least one data row matches the input—so it verifies the export logic, not just file creation.

Suggested implementation:

import csv
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))

    # File is created
    assert file_path.exists()

    # Contents are correct
    with file_path.open(newline="") as f:
        reader = csv.DictReader(f)
        # Header matches expected columns
        assert reader.fieldnames == ["a", "b"]

        rows = list(reader)
        # Row count is correct
        assert len(rows) == 1

        row = rows[0]
        # Row data matches input (compare as strings to be robust)
        assert row["a"] == str(data[0]["a"])
        assert row["b"] == str(data[0]["b"])
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(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")