Skip to content

Commit 0ee30d2

Browse files
committed
test: Test dataset context and file parts
1 parent d7a7cfc commit 0ee30d2

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/bids_validator/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def from_file(cls, file: FileTree, schema: Namespace) -> t.Self:
253253

254254
datatype = None
255255
if file.parent:
256-
if any(file.parent.name == dtype.value for dtype in schema.objects.datatypes):
256+
if any(file.parent.name == dtype.value for dtype in schema.objects.datatypes.values()):
257257
datatype = file.parent.name
258258

259259
*entity_strings, suffix = stem.split('_')
@@ -263,7 +263,7 @@ def from_file(cls, file: FileTree, schema: Namespace) -> t.Self:
263263
}
264264

265265
return cls(
266-
path=file.relative_path,
266+
path=f'/{file.relative_path}',
267267
stem=stem,
268268
entities=entities,
269269
datatype=datatype,

src/bids_validator/types/files.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ def __fspath__(self):
115115
def __hash__(self):
116116
return hash(self.direntry.path)
117117

118+
def __truediv__(self, relpath: str | os.PathLike) -> t.Self:
119+
parts = Path(relpath).parts
120+
child = self
121+
for part in parts:
122+
child = child.children[part]
123+
return child
124+
118125
@cached_property
119126
def relative_path(self) -> str:
120127
"""The path of the current FileTree, relative to the root.

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from pathlib import Path
66

77
import pytest
8+
from bidsschematools.schema import load_schema
9+
from bidsschematools.types import Namespace
810

911

1012
@pytest.fixture(scope='session')
@@ -31,3 +33,9 @@ def gitignore_test() -> Path:
3133
else: # pragma: no cover
3234
pass
3335
return Path(ret)
36+
37+
38+
@pytest.fixture(scope='session')
39+
def schema() -> Namespace:
40+
"""Load BIDS schema for tests."""
41+
return load_schema()

tests/test_context.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from bids_validator import context
2+
from bids_validator.types.files import FileTree
3+
4+
5+
def test_load(examples):
6+
tree = FileTree.read_from_filesystem(examples / 'synthetic')
7+
ds = context.Dataset(tree)
8+
9+
assert ds.dataset_description.Name.startswith('Synthetic dataset')
10+
assert ds.subjects.participant_id == [f'sub-{i:02d}' for i in range(1, 6)]
11+
assert ds.subjects.sub_dirs == [f'sub-{i:02d}' for i in range(1, 6)]
12+
13+
14+
def test_fileparts(examples, schema):
15+
tree = FileTree.read_from_filesystem(examples / 'synthetic')
16+
17+
T1w = tree / 'sub-01' / 'ses-01' / 'anat' / 'sub-01_ses-01_T1w.nii'
18+
parts = context.FileParts.from_file(T1w, schema)
19+
assert parts == context.FileParts(
20+
path='/sub-01/ses-01/anat/sub-01_ses-01_T1w.nii',
21+
stem='sub-01_ses-01_T1w',
22+
entities={'sub': '01', 'ses': '01'},
23+
datatype='anat',
24+
suffix='T1w',
25+
extension='.nii',
26+
)

0 commit comments

Comments
 (0)