Add dataset_exporter/tests/test_core.py
This commit is contained in:
parent
a7a8aa5294
commit
85fec43cf4
1 changed files with 79 additions and 0 deletions
79
dataset_exporter/tests/test_core.py
Normal file
79
dataset_exporter/tests/test_core.py
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import json
|
||||
import csv
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
import src.dataset_exporter.core as core
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_dataset():
|
||||
return [
|
||||
{
|
||||
'timestamp': '2024-01-01T00:00:00',
|
||||
'decision': 'PASS',
|
||||
'warn_rate': 0.05,
|
||||
'fail_count': 2,
|
||||
'unknown_count': 0,
|
||||
'pinned': False,
|
||||
},
|
||||
{
|
||||
'timestamp': '2024-01-02T00:00:00',
|
||||
'decision': 'FAIL',
|
||||
'warn_rate': 0.2,
|
||||
'fail_count': 10,
|
||||
'unknown_count': 1,
|
||||
'pinned': True,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def test_export_jsonl(tmp_path: Path, sample_dataset):
|
||||
output = tmp_path / 'dataset.jsonl'
|
||||
options = core.ExportOptions(output_format='jsonl', output_path=str(output))
|
||||
core.export_dataset(sample_dataset, options.output_format, options.output_path)
|
||||
|
||||
assert output.exists(), 'JSONL file was not created.'
|
||||
|
||||
with open(output, 'r', encoding='utf-8') as f:
|
||||
lines = [json.loads(line) for line in f]
|
||||
|
||||
assert len(lines) == len(sample_dataset)
|
||||
assert lines[0]['decision'] == 'PASS'
|
||||
assert all(isinstance(item, dict) for item in lines)
|
||||
|
||||
|
||||
def test_export_csv(tmp_path: Path, sample_dataset):
|
||||
output = tmp_path / 'dataset.csv'
|
||||
options = core.ExportOptions(output_format='csv', output_path=str(output))
|
||||
core.export_dataset(sample_dataset, options.output_format, options.output_path)
|
||||
|
||||
assert output.exists(), 'CSV file was not created.'
|
||||
|
||||
with open(output, 'r', encoding='utf-8') as f:
|
||||
reader = csv.DictReader(f)
|
||||
rows = list(reader)
|
||||
|
||||
assert len(rows) == len(sample_dataset)
|
||||
assert set(rows[0].keys()) == set(sample_dataset[0].keys())
|
||||
assert rows[1]['decision'] == 'FAIL'
|
||||
|
||||
|
||||
def test_export_invalid_format(tmp_path: Path, sample_dataset):
|
||||
output = tmp_path / 'dataset.invalid'
|
||||
with pytest.raises(ValueError):
|
||||
core.export_dataset(sample_dataset, 'xml', str(output))
|
||||
|
||||
|
||||
def test_empty_dataset_jsonl(tmp_path: Path):
|
||||
output = tmp_path / 'empty.jsonl'
|
||||
core.export_dataset([], 'jsonl', str(output))
|
||||
with open(output, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
assert content == '', 'JSONL export of empty dataset should produce an empty file.'
|
||||
|
||||
|
||||
def test_invalid_dataset_type(tmp_path: Path):
|
||||
output = tmp_path / 'invalid.jsonl'
|
||||
with pytest.raises(TypeError):
|
||||
core.export_dataset({'non': 'list'}, 'jsonl', str(output))
|
||||
Loading…
Reference in a new issue