From e14b98699bf4fa27bbc0bb2525bb4d227a996d41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Cl=C3=A9net?= Date: Thu, 5 Oct 2023 09:30:39 +0200 Subject: [PATCH] [TEST] making tests pass --- narps_open/data/task.py | 6 ++- tests/data/test_task.py | 57 ++++++++++++++++++++++++ tests/test_data/data/task/task-info.json | 12 +++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/data/test_task.py create mode 100644 tests/test_data/data/task/task-info.json diff --git a/narps_open/data/task.py b/narps_open/data/task.py index 58b192bd..f3e86803 100644 --- a/narps_open/data/task.py +++ b/narps_open/data/task.py @@ -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']) diff --git a/tests/data/test_task.py b/tests/data/test_task.py new file mode 100644 index 00000000..8b6860dd --- /dev/null +++ b/tests/data/test_task.py @@ -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 +""" +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 diff --git a/tests/test_data/data/task/task-info.json b/tests/test_data/data/task/task-info.json new file mode 100644 index 00000000..7927183d --- /dev/null +++ b/tests/test_data/data/task/task-info.json @@ -0,0 +1,12 @@ +{ + "RepetitionTime": 1, + "EffectiveEchoSpacing": 2, + "SliceTiming": [ + 0, + 0.4375, + 0.875, + 0.3125, + 0.75, + 0.1875 + ] +} \ No newline at end of file