Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Test calling the scripts #78

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ doc = [
test = [
"coverage",
"pytest",
"pytest-console-scripts",
]

[project.urls]
Expand Down
54 changes: 27 additions & 27 deletions tract_querier/tests/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@
import os
from os import path
import tempfile
from six.moves import urllib

import unittest
import urllib.request


FILES = {
'tract_file': (
'http://midas.kitware.com/bitstream/view/17631',
'https://osf.io/download/aq3cu/',
'IIT3mean_left_hemisphere_small.trk',
'\xe7\xec\xfd+\xd2n\xff\x96\xae\xb4\xdf+\x194\xdf\x81'
b'\xe7\xec\xfd+\xd2n\xff\x96\xae\xb4\xdf+\x194\xdf\x81'
),
'atlas_file': (
'http://midas.kitware.com/bitstream/view/17622',
'https://osf.io/download/rk7a5/',
'IIT3mean_desikan_2009.nii.gz',
'vx\x13\xbaE\x1dR\t\xcd\xc9EF\x17\xa66\xb7'
b'vx\x13\xbaE\x1dR\t\xcd\xc9EF\x17\xa66\xb7'
),
'query_uf_file': (
'http://midas.kitware.com/bitstream/view/17627',
'https://osf.io/download/3zj5h/',
'wmql_2_uf.qry',
'\\+R\x8c<B#\xea\xfc\x9aE\xbd\xb0(\xbdn'
b'\\+R\x8c<B#\xea\xfc\x9aE\xbd\xb0(\xbdn'
)
}


class TestDataSet(unittest.TestCase):

@unittest.skip("temporarily disabled")
class TestDataSet:
def __init__(self):
self.dirname = path.join(
tempfile.gettempdir(),
Expand All @@ -40,25 +36,29 @@ def __init__(self):
if not path.exists(self.dirname):
os.mkdir(self.dirname)

for k, v in FILES.items():
dst_filename = path.join(self.dirname, v[1])

for k, (url, filename, md5) in FILES.items():
dst_filename = path.join(self.dirname, filename)
if (
not path.exists(dst_filename) or
hashlib.md5(open(dst_filename).read()).digest() != v[2]
hashlib.md5(open(dst_filename, "rb").read()).digest() != md5
):
dl_file = urllib.request.urlopen(v[0])
dst_file = open(dst_filename, 'wb')
dst_file.write(dl_file.read())
dst_file.close()

request = urllib.request.Request(url)
try:
response = urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
if e.status not in (307, 308):
raise
redirected_url = urllib.parse.urljoin(url, e.headers['Location'])
response = urllib.request.urlopen(redirected_url)
with open(dst_filename, 'wb') as out_file:
while True:
chunk = response.read(8192) # Read 8 KB at a time
if not chunk:
break
out_file.write(chunk)
if (
hashlib.md5(open(dst_filename).read()).digest() != v[2]
hashlib.md5(open(dst_filename, "rb").read()).digest() != md5
):
raise IOError('File %s url %s was not properly downloaded' % (v[1], v[0]))
raise IOError('File %s url %s was not properly downloaded' % (filename, url))

self.files[k] = dst_filename




80 changes: 0 additions & 80 deletions tract_querier/tests/test_scripts.py

This file was deleted.

19 changes: 19 additions & 0 deletions tract_querier/tests/test_tract_math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re

from tract_querier.tests import datasets


def test_help_option(script_runner):
ret = script_runner.run(["tract_math", "--help"])
assert ret.success


def test_tract_math_count(script_runner):
test_data = datasets.TestDataSet()
tract_file = test_data.files["tract_file"]
ret = script_runner.run(["tract_math", tract_file, "count"])
assert re.search('[^0-9]6783[^0-9]', ret.stdout) is not None
assert ret.success
29 changes: 29 additions & 0 deletions tract_querier/tests/test_tract_querier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from os import path

from tract_querier.tests import datasets


def test_help_option(script_runner):
ret = script_runner.run(["tract_querier", "--help"])
assert ret.success


def test_tract_querier_query(script_runner):
test_data = datasets.TestDataSet()
atlas_file = test_data.files["atlas_file"]
query_uf_file = test_data.files["query_uf_file"]
tract_file = test_data.files["tract_file"]
output_prefix = test_data.dirname + "/test"

ret = script_runner.run(["tract_querier", "-a", atlas_file,"-t", tract_file, "-q", query_uf_file, "-o", output_prefix])
tract_fname_end = "_uncinate.left.trk"
assert "uncinate.left: 000102" in ret.stdout
assert "uncinate.right: 000000" in ret.stdout
assert path.exists(output_prefix + tract_fname_end)
assert ret.success
if path.exists(output_prefix + tract_fname_end):
os.remove(output_prefix + tract_fname_end)
4 changes: 2 additions & 2 deletions tract_querier/tract_math/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def wrapper(*input_args):
args, options_dict = find_optional_args(input_args)

total_args = len(args)
argspec = inspect.getargspec(func)
argspec = inspect.getfullargspec(func)
# Subtract 1 for implicit options_dict
func_total_args = len(argspec.args) - 1

Expand Down Expand Up @@ -196,7 +196,7 @@ def process_output(output, file_output=None):
header = dict(zip(writer.fieldnames, writer.fieldnames))
writer.writerow(header)

first_value = output.values()[0]
first_value = next(iter(output.values()))
if (
not isinstance(first_value, str) and
isinstance(first_value, Iterable)
Expand Down
Loading