from nose.tools import *
import os, glob, shutil

import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..'))
from qipipe.staging import fix_dicom_headers
from qipipe.helpers.dicom_helper import iter_dicom

# The test parent directory.
ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..'))
# The test fixture.
FIXTURE = os.path.join(ROOT, 'fixtures', 'staging', 'fix_dicom', 'sarcoma', 'Sarcoma03')
# The test results.
RESULTS = os.path.join(ROOT, 'results', 'staging', 'fix_dicom', 'sarcoma', 'Sarcoma03')
# The collection name.
COLLECTION = 'Sarcoma'

class TestFixDicom:
    """Fix DICOM header unit tests."""
    
    def test_fix_dicom_headers(self):
        shutil.rmtree(RESULTS, True)
        dest = os.path.dirname(RESULTS)
        for d in glob.glob(FIXTURE + '/session*/series*'):
            fixed = fix_dicom_headers(d, dest, COLLECTION)
            # Verify the result.
            for ds in iter_dicom(*fixed):
                assert_equal('CHEST', ds.BodyPartExamined, "Incorrect Body Part: %s" % ds.BodyPartExamined)
                assert_equal('Sarcoma03', ds.PatientID, "Incorrect Patient ID: %s" % ds.PatientID)
        # Cleanup.
        shutil.rmtree(RESULTS, True)


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

@@ -335,16 +335,17 @@
  '..'))%0A
+%0A
 # The te
@@ -441,24 +441,25 @@
 Sarcoma03')%0A
+%0A
 # The test r
@@ -554,16 +554,17 @@
 oma03')%0A
+%0A
 # The co
@@ -602,16 +602,58 @@
 rcoma'%0A%0A
+# The new subject.%0ASUBJECT = 'Sarcoma03'%0A%0A
 class Te
@@ -827,71 +827,8 @@
 TS)%0A
-        for d in glob.glob(FIXTURE + '/session*/series*'):%0A    
     
@@ -861,17 +861,8 @@
 ers(
-d, dest, 
 COLL
@@ -867,22 +867,47 @@
 LLECTION
-)%0A    
+, SUBJECT, FIXTURE, dest=dest)%0A
         
@@ -927,20 +927,16 @@
 result.%0A
-    
         
@@ -965,20 +965,16 @@
 fixed):%0A
-    
         
@@ -1073,36 +1073,32 @@
 ed)%0A            
-    
 assert_equal('Sa
@@ -1094,27 +1094,23 @@
 t_equal(
-'Sarcoma03'
+SUBJECT
 , ds.Pat

Fix all of the files in one call.