Skip to content

Commit 97fbd33

Browse files
committed
Add test for add_rows_to_scans_keys_file
1 parent 652d53c commit 97fbd33

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

bin/heudiconv

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -985,26 +985,24 @@ def add_rows_to_scans_keys_file(fn, newrows):
985985
if lexists(fn):
986986
with open(fn, 'r') as csvfile:
987987
reader = csv.reader(csvfile, delimiter='\t')
988-
existing_rows = [row[0] for row in reader]
988+
existing_rows = [row for row in reader]
989989
# skip header
990990
fnames2info = {row[0]: row[1:] for row in existing_rows[1:]}
991991

992-
newrows_key = newrows.key()
992+
newrows_key = newrows.keys()
993993
newrows_toadd = list(set(newrows_key) - set(fnames2info.keys()))
994994
for key_toadd in newrows_toadd:
995995
fnames2info[key_toadd] = newrows[key_toadd]
996-
header = []
997996
# remove
998997
os.unlink(fn)
999998
else:
1000-
header = ['filename', 'acq_time', 'operator', 'randstr']
1001999
fnames2info = newrows
10021000

1001+
header = ['filename', 'acq_time', 'operator', 'randstr']
10031002
# save
10041003
with open(fn, 'a') as csvfile:
10051004
writer = csv.writer(csvfile, delimiter='\t')
1006-
if header:
1007-
writer.writerow(header)
1005+
writer.writerow(header)
10081006
for key in sorted(fnames2info.keys()):
10091007
writer.writerow([key] + fnames2info[key])
10101008

tests/test_main.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import csv
12
import os
23
import pytest
34
import sys
@@ -134,5 +135,25 @@ def test_add_rows_to_scans_keys_file(tmpdir):
134135
}
135136
heudiconv.add_rows_to_scans_keys_file(fn, rows)
136137

137-
#with open(fn, 'r') as csvfile:
138+
def _check_rows(fn, rows):
139+
with open(fn, 'r') as csvfile:
140+
reader = csv.reader(csvfile, delimiter='\t')
141+
rows_loaded = []
142+
for row in reader:
143+
rows_loaded.append(row)
144+
for i, row_ in enumerate(rows_loaded):
145+
if i == 0:
146+
assert(row_ == ['filename', 'acq_time', 'operator', 'randstr'])
147+
else:
148+
assert(rows[row_[0]] == row_[1:])
149+
150+
_check_rows(fn, rows)
151+
# add a new one
152+
extra_rows = {
153+
'a_new_file.nii.gz': ['2016adsfasd23', '', 'fasadfasdf'],
154+
'my_file.nii.gz': ['2016adsfasd', '', 'fasadfasdf'],
155+
'another_file.nii.gz': ['2018xxxxx', '', 'fasadfasdf']
156+
}
157+
heudiconv.add_rows_to_scans_keys_file(fn, extra_rows)
158+
_check_rows(fn, extra_rows)
138159

0 commit comments

Comments
 (0)