Skip to content

Commit

Permalink
[TEST] making tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
bclenet committed Oct 5, 2023
1 parent f9a3d3a commit e14b986
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
6 changes: 4 additions & 2 deletions narps_open/data/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
class TaskInformation(dict, metaclass=SingletonMeta):
""" This class allows to access information about the task performed in NARPS """

task_information_file = join(Configuration()['directories']['dataset'], 'task-MGT_bold.json')

def __init__(self):
super().__init__()

# Load information from the task-MGT_bold.json file
file_name = join(Configuration()['directories']['dataset'], 'task-MGT_bold.json')
self.update(load(file))
with open(self.task_information_file, 'rb') as file:
self.update(load(file))

# Compute derived information
self['NumberOfSlices'] = len(self['SliceTiming'])
Expand Down
57 changes: 57 additions & 0 deletions tests/data/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/python
# coding: utf-8

""" Tests of the 'narps_open.data.task' module.
Launch this test with PyTest
Usage:
======
pytest -q test_task.py
pytest -q test_task.py -k <selected_test>
"""
from os.path import join

from pytest import mark, fixture

from narps_open.utils.configuration import Configuration
import narps_open.data.task as task

@fixture(scope='function', autouse=True)
def mock_task_data(mocker):
""" Patch the json.load method to mock task data """
mocker.patch.object(
task.TaskInformation, 'task_information_file',
join(Configuration()['directories']['test_data'], 'data', 'task', 'task-info.json')
)

class TestTaskInformation:
""" A class that contains all the unit tests for the TaskInformation class."""

@staticmethod
@mark.unit_test
def test_accessing():
""" Check that task information is reachable """

assert task.TaskInformation()['RepetitionTime'] == 1
assert len(task.TaskInformation()['SliceTiming']) == 6

@staticmethod
@mark.unit_test
def test_singleton():
""" Check that TaskInformation is a singleton. """

obj1 = task.TaskInformation()
obj2 = task.TaskInformation()

assert id(obj1) == id(obj2)

@staticmethod
@mark.unit_test
def test_derived():
""" Test the derived values of a TaskInformation object """

task_info = task.TaskInformation()
assert task_info['NumberOfSlices'] == 6
assert task_info['AcquisitionTime'] == 1 / 6
assert task_info['TotalReadoutTime'] == 12
12 changes: 12 additions & 0 deletions tests/test_data/data/task/task-info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"RepetitionTime": 1,
"EffectiveEchoSpacing": 2,
"SliceTiming": [
0,
0.4375,
0.875,
0.3125,
0.75,
0.1875
]
}

0 comments on commit e14b986

Please sign in to comment.