from nose.tools import *
import sys, os, shutil
from qipipe.helpers import xnat_helper

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from test.helpers.xnat_test_helper import generate_subject_name, delete_subjects

ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..'))
"""The test parent directory."""

FIXTURE = os.path.join(ROOT, 'fixtures', 'helpers', 'xnat', 'dummy.nii.gz')
"""The test fixture parent directory."""

RESULTS = os.path.join(ROOT, 'results', 'helpers', 'xnat')
"""The test results directory."""

SUBJECT = generate_subject_name(__name__)
"""The test subject name."""


class TestXNATHelper(object):
    """The XNAT helper unit tests."""
    
    def setUp(self):
        shutil.rmtree(RESULTS, True)
        delete_subjects(SUBJECT)
        
    def tearDown(self):
        shutil.rmtree(RESULTS, True)
        delete_subjects(SUBJECT)
        
    def test_upload_and_download(self):
        session = SUBJECT + '_MR1'
        with xnat_helper.connection() as xnat:
            # Upload the file.
            xnat.upload('QIN', SUBJECT, session, FIXTURE, scan=1, modality='MR', format='NIFTI')
            _, fname = os.path.split(FIXTURE)
            sbj = xnat.interface.select('/project/QIN').subject(SUBJECT)
            file_obj = sbj.experiment(session).scan('1').resource('NIFTI').file(fname)
            assert_true(file_obj.exists(), "File not uploaded: %s" % fname)
            
            # Download the uploaded file.
            files = list(xnat.download('QIN', sbj.label(), session, dest=RESULTS, scan=1, format='NIFTI'))
        assert_not_equal(0, len(files), "No files were downloaded")
        assert_equal(1, len(files), "Too many files were downloaded: %s" % files)
        f = files[0]
        assert_true(os.path.exists(f), "File not downloaded: %s" % f)


if __name__ == "__main__":
    import nose
    
    nose.main(defaultTest=__name__)

@@ -1518,13 +1518,8 @@
 s = 
-list(
 xnat
@@ -1594,17 +1594,16 @@
 'NIFTI')
-)
 %0A       
@@ -1607,149 +1607,288 @@
     
-assert_not_equal(0, len(files), %22No files were downloaded%22)%0A        assert_equal(1, len(files), %22Too many files were downloaded: %25s
+    # Download all scans.%0A            all_files = xnat.download('QIN', sbj.label(), session, dest=RESULTS, container_type='scan', format='NIFTI')%0A            %0A        # Verify the result.%0A        assert_equal(1, len(files), %22The download file count is incorrect: %25d
 %22 %25 
+len(
 files)
+)
 %0A   
@@ -1975,16 +1975,151 @@
 s%22 %25 f)%0A
+        assert_equal(set(files), set(all_files), %22The scan 1 download differs from all scans download: %25s vs %25s%22 %25 (files, all_files))%0A
 %0A%0Aif __n

Test downloading all scan files.