changeset 220:7b1c976caa9b Orthanc-1.5.5

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 25 Feb 2019 10:57:28 +0100
parents 00e5aeea33dd
children 7daf6a227745
files Tests/Tests.py Tests/Toolbox.py
diffstat 2 files changed, 54 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/Tests/Tests.py	Sun Feb 24 08:50:02 2019 +0100
+++ b/Tests/Tests.py	Mon Feb 25 10:57:28 2019 +0100
@@ -4438,7 +4438,7 @@
                       headers = { 'Accept' : 'application/dicom+json' })
             with open(GetDatabasePath(reference), 'rb') as c:
                 d = json.load(c)
-                self.assertEqual(d, b)
+                AssertAlmostEqualRecursive(self, d, b)
                     
         Compare('DummyCT.dcm', 'DummyCT.json')
         Compare('MarekLatin2.dcm', 'MarekLatin2.json')
@@ -4546,9 +4546,9 @@
         self.assertEqual(1, len(DoGet(_REMOTE, '/queries/%s/answers' % a)))
 
         self.assertEqual(0, len(DoGet(_LOCAL, '/instances')))
-        self.assertTrue(MonitorJob(_REMOTE, lambda: DoPost
-                                   (_REMOTE, '/queries/%s/retrieve' % a,
-                                    '{"TargetAet":"ORTHANCTEST","Synchronous":false}')))
+        WaitAllNewJobsDone(_REMOTE, lambda: DoPost
+                           (_REMOTE, '/queries/%s/retrieve' % a,
+                            '{"TargetAet":"ORTHANCTEST","Synchronous":false}'))
 
         # The two studies are matched, as we made the request at the
         # Study level, thus the shared StudyInstanceUID is used as the key
@@ -4562,9 +4562,9 @@
         self.assertEqual(2, len(DoGet(_REMOTE, '/queries/%s/answers' % a)))
         DropOrthanc(_LOCAL)
         self.assertEqual(0, len(DoGet(_LOCAL, '/instances')))
-        self.assertTrue(MonitorJob(_REMOTE, lambda: DoPost
-                                   (_REMOTE, '/queries/%s/retrieve' % a,
-                                    '{"TargetAet":"ORTHANCTEST","Synchronous":false}')))
+        WaitAllNewJobsDone(_REMOTE, lambda: DoPost
+                           (_REMOTE, '/queries/%s/retrieve' % a,
+                            '{"TargetAet":"ORTHANCTEST","Synchronous":false}'))
         self.assertEqual(2, len(DoGet(_LOCAL, '/instances')))
 
         
@@ -4575,9 +4575,9 @@
         self.assertEqual(1, len(DoGet(_REMOTE, '/queries/%s/answers' % a)))
         DropOrthanc(_LOCAL)
         self.assertEqual(0, len(DoGet(_LOCAL, '/instances')))
-        self.assertTrue(MonitorJob(_REMOTE, lambda: DoPost
-                                   (_REMOTE, '/queries/%s/retrieve' % a,
-                                    '{"TargetAet":"ORTHANCTEST","Synchronous":false}')))
+        WaitAllNewJobsDone(_REMOTE, lambda: DoPost
+                           (_REMOTE, '/queries/%s/retrieve' % a,
+                            '{"TargetAet":"ORTHANCTEST","Synchronous":false}'))
         self.assertEqual(1, len(DoGet(_LOCAL, '/instances')))
         
 
@@ -4588,8 +4588,8 @@
         self.assertEqual(1, len(DoGet(_REMOTE, '/queries/%s/answers' % a)))
         DropOrthanc(_LOCAL)
         self.assertEqual(0, len(DoGet(_LOCAL, '/instances')))
-        self.assertTrue(MonitorJob(_REMOTE, lambda: DoPost
-                                   (_REMOTE, '/queries/%s/retrieve' % a,
-                                    '{"TargetAet":"ORTHANCTEST","Synchronous":false}')))
+        WaitAllNewJobsDone(_REMOTE, lambda: DoPost
+                           (_REMOTE, '/queries/%s/retrieve' % a,
+                            '{"TargetAet":"ORTHANCTEST","Synchronous":false}'))
         self.assertEqual(1, len(DoGet(_LOCAL, '/instances')))
         
--- a/Tests/Toolbox.py	Sun Feb 24 08:50:02 2019 +0100
+++ b/Tests/Toolbox.py	Mon Feb 25 10:57:28 2019 +0100
@@ -275,6 +275,30 @@
         print('Error while executing the job')
         return None
 
+def WaitAllNewJobsDone(orthanc, func):  # "func" is a lambda
+    a = set(DoGet(orthanc, '/jobs'))
+    func()
+
+    first = True
+
+    while True:
+        b = set(DoGet(orthanc, '/jobs'))
+        
+        diff = list(b - a)
+        if len(diff) == 0:
+            if first:
+                raise Exception('No job was created')
+            else:
+                return  # We're done
+        else:
+            first = False
+
+            if WaitJobDone(orthanc, diff[0]):
+                a.add(diff[0])
+            else:
+                raise Exception('Error while executing the job')
+
+
 def GetDockerHostAddress():
     route = subprocess.check_output([ '/sbin/ip', 'route' ])
     m = re.search(r'default via ([0-9.]+)', route)
@@ -335,3 +359,20 @@
     def stop(self):
         self.thread_stop.set()
         self.thread.join()
+
+
+def AssertAlmostEqualRecursive(self, a, b, places = 7):
+    if type(a) is dict:
+        self.assertTrue(type(b) is dict)
+        self.assertEqual(a.keys(), b.keys())
+        for key, value in a.items():
+            AssertAlmostEqualRecursive(self, a[key], b[key], places)
+
+    elif type(a) is list:
+        self.assertTrue(type(b) is list)
+        self.assertEqual(len(a), len(b))
+        for i in range(len(a)):
+            AssertAlmostEqualRecursive(self, a[i], b[i], places)
+
+    else:
+        self.assertAlmostEqual(a, b, places = places)