-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_reader.py
More file actions
75 lines (60 loc) · 2.6 KB
/
test_reader.py
File metadata and controls
75 lines (60 loc) · 2.6 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Test cases for reader.py functions."""
import pathlib
import re
import pandas as pd
import pytest
from graphomotor.core import models
from graphomotor.io import reader
def test_parse_filename_valid(sample_data: pathlib.Path) -> None:
"""Test that valid filenames are parsed correctly."""
expected_metadata = {
"id": "5123456",
"hand": "Dom",
"task": "spiral_trace1",
}
metadata = reader._parse_filename(sample_data.stem)
assert metadata == expected_metadata, (
f"Expected {expected_metadata}, but got {metadata}"
)
@pytest.mark.parametrize(
"invalid_filename",
[
"asdf123-spiral_trace1_Dom.csv", # missing ID
"[5123456]-spiral_trace1_Dom.csv", # missing mindloggerID
"[5123456]asdf123-Dom.csv", # missing task
"[5123456]asdf123-spiral_trace1.csv", # missing hand
],
)
def test_parse_filename_invalid(invalid_filename: str) -> None:
"""Test that invalid filenames raise a ValueError."""
filename = re.escape(invalid_filename)
with pytest.raises(
ValueError,
match=f"Filename does not match expected pattern: {filename}",
):
reader._parse_filename(invalid_filename)
@pytest.mark.parametrize("missing_column", list(reader.DTYPE_MAP.keys()))
def test_check_missing_columns(
valid_spiral_data: pd.DataFrame, missing_column: str
) -> None:
"""Test that missing columns raise a KeyError."""
valid_spiral_data = valid_spiral_data.drop(columns=[missing_column])
with pytest.raises(KeyError, match=f"Missing required columns: {missing_column}"):
reader._check_missing_columns(valid_spiral_data)
def test_convert_start_time() -> None:
"""Test that start time is converted correctly."""
dummy_data = pd.DataFrame({"epoch_time_in_seconds_start": [10**15]})
with pytest.raises(ValueError, match="Error converting 'start_time' to datetime"):
reader._convert_start_time(dummy_data)
def test_load_spiral(sample_data: pathlib.Path) -> None:
"""Test that spiral loads with string input and start time is moved to metadata."""
spiral = reader.load_spiral(str(sample_data))
assert isinstance(spiral, models.Spiral)
assert "epoch_time_in_seconds_start" not in spiral.data.columns
assert "start_time" in spiral.metadata
def test_load_spiral_invalid_extension(sample_data: pathlib.Path) -> None:
"""Test that loading a non-CSV file raises an error."""
invalid_file = sample_data.with_suffix(".txt")
filename = re.escape(str(invalid_file))
with pytest.raises(IOError, match=f"Error reading file {filename}"):
reader.load_spiral(invalid_file)