diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 7756546e2..8bb6598fe 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -3,9 +3,12 @@ from pkg_resources import resource_filename import pytest +import yaml +from csv import reader -from phys2bids.bids import bidsify_units, use_heuristic, readme_file, dataset_description_file +from phys2bids import bids from phys2bids.bids import UNIT_ALIASES +from phys2bids.utils import append_list_as_row def test_bidsify_units(): @@ -20,10 +23,10 @@ def test_bidsify_units(): } # Actually test for unit_key in unit_tests.keys(): - assert bidsify_units(unit_key) == unit_tests[unit_key] + assert bids.bidsify_units(unit_key) == unit_tests[unit_key] # test there is not problem with every unit in the dict for unit_key in UNIT_ALIASES.keys(): - assert bidsify_units(unit_key) == UNIT_ALIASES[unit_key] + assert bids.bidsify_units(unit_key) == bids.UNIT_ALIASES[unit_key] @pytest.mark.parametrize('test_sub', ['SBJ01', 'sub-006', '006']) @@ -38,8 +41,8 @@ def test_use_heuristic(tmpdir, test_sub, test_ses): test_outdir = tmpdir test_record_label = 'test' - heur_path = use_heuristic(test_full_heur_path, test_sub, test_ses, - test_full_input_path, test_outdir, test_record_label) + heur_path = bids.use_heuristic(test_full_heur_path, test_sub, test_ses, + test_full_input_path, test_outdir, test_record_label) if test_sub[:4] == 'sub-': test_sub = test_sub[4:] @@ -62,13 +65,81 @@ def test_use_heuristic(tmpdir, test_sub, test_ses): @pytest.mark.parametrize('outdir', '.') def test_README_file(outdir): - readme_file(outdir) + bids.readme_file(outdir) assert os.path.join(outdir, "README.md") os.remove(os.path.join(outdir, "README.md")) @pytest.mark.parametrize('outdir', '.') def test_dataset_description_file(outdir): - dataset_description_file(outdir) + bids.dataset_description_file(outdir) assert os.path.join(outdir, "dataset_description.json") os.remove(os.path.join(outdir, "dataset_description.json")) + + +@pytest.mark.parametrize('outdir', '.') +def test_participants_file(outdir): + + # Checks first condition in line 198 + test_sub = '001' + test_sub_no_yml = '002' + test_missing_sub = '003' + test_yaml_path = os.path.join(outdir, 'test.yml') + + # Populate yaml file + data = dict(participant=dict( + participant_id=f'sub-{test_sub}', + age='25', + sex='m', + handedness='r')) + + test_header = ['participant_id', 'age', 'sex', 'handedness'] + test_data = [f'sub-{test_sub}', '25', 'm', 'r'] + test_na = [f'sub-{test_sub_no_yml}', 'n/a', 'n/a', 'n/a'] + test_missing = [f'sub-{test_missing_sub}', 'n/a', 'n/a', 'n/a'] + test_list = [test_header, test_data] + test_no_yml = [test_header, test_data, test_na] + test_missing_list = [test_header, test_data, test_na, test_missing] + + # Checks validity of tsv lines when yml file is given + with open(test_yaml_path, 'w') as outfile: + yaml.dump(data, outfile, default_flow_style=False) + + bids.participants_file(outdir, test_yaml_path, test_sub) + + counter = 0 + with open(os.path.join(outdir, 'participants.tsv')) as pf: + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_list[counter] + counter += 1 + + # Checks when no yml file is given + bids.participants_file(outdir=outdir, yml='', sub=test_sub_no_yml) + counter = 0 + with open(os.path.join(outdir, 'participants.tsv')) as pf: + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_no_yml[counter] + counter += 1 + + # Checks validity of tsv lines when file exists but no line for Subject + bids.participants_file(outdir=outdir, yml='', sub=test_missing_sub) + counter = 0 + with open(os.path.join(outdir, 'participants.tsv')) as pf: + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_missing_list[counter] + counter += 1 + + # Test that subject from previous check is already there + bids.participants_file(outdir=outdir, yml='', sub=test_missing_sub) + counter = 0 + with open(os.path.join(outdir, 'participants.tsv')) as pf: + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_missing_list[counter] + counter += 1 + + os.remove(os.path.join(outdir, "participants.tsv")) + os.remove(os.path.join(outdir, "test.yml")) diff --git a/phys2bids/tests/test_utils.py b/phys2bids/tests/test_utils.py index 0163009bf..f5caa3486 100644 --- a/phys2bids/tests/test_utils.py +++ b/phys2bids/tests/test_utils.py @@ -4,7 +4,7 @@ import json import os - +from csv import reader from phys2bids import utils @@ -95,3 +95,14 @@ def test_load_heuristics(): test_heuristic = 'heur_test_acq' heuristic_output_filename = utils.load_heuristic(test_heuristic).filename assert test_heuristic in heuristic_output_filename + + +# Test writing rows util +def test_append_list_as_row(): + file_name = 'test_row.tsv' + list_of_elem = ["01", "32", 'some_info', "132.98", 'M'] + utils.append_list_as_row(file_name, list_of_elem) + with open(file_name, mode='r') as tsv: + tsv_read = reader(tsv, delimiter="\t") + for row in tsv_read: + assert row == list_of_elem