comparison Tests/Tests.py @ 515:5dca7ef42156

added a non-regression test wrt StableStudy
author Alain Mazy <am@osimis.io>
date Tue, 04 Apr 2023 12:05:27 +0200
parents e2ce0edb9002
children 3a8e4de5aff7
comparison
equal deleted inserted replaced
512:e2ce0edb9002 515:5dca7ef42156
29 import numpy 29 import numpy
30 import pprint 30 import pprint
31 import shutil 31 import shutil
32 import tempfile 32 import tempfile
33 import unittest 33 import unittest
34 import time
34 35
35 from PIL import ImageChops 36 from PIL import ImageChops
36 from Toolbox import * 37 from Toolbox import *
37 from xml.dom import minidom 38 from xml.dom import minidom
38 from datetime import datetime 39 from datetime import datetime
185 DropOrthanc(_REMOTE) 186 DropOrthanc(_REMOTE)
186 UninstallLuaCallbacks(_REMOTE) 187 UninstallLuaCallbacks(_REMOTE)
187 188
188 # Reset stuff possibly set by some integration tests 189 # Reset stuff possibly set by some integration tests
189 DoPut(_REMOTE, '/tools/default-encoding', 'Latin1') 190 DoPut(_REMOTE, '/tools/default-encoding', 'Latin1')
190 DoPut(_REMOTE, '/tools/accepted-transfer-syntaxes', [ '1.2.840.10008.1.*' ]) 191 if IsOrthancVersionAbove(_REMOTE, 1, 9, 0):
191 DoPut(_REMOTE, '/tools/unknown-sop-class-accepted', '0') 192 DoPut(_REMOTE, '/tools/accepted-transfer-syntaxes', [ '1.2.840.10008.1.*' ])
193 DoPut(_REMOTE, '/tools/unknown-sop-class-accepted', '0')
192 194
193 for i in [ 'toto', 'tata' ]: 195 for i in [ 'toto', 'tata' ]:
194 if i in DoGet(_REMOTE, '/modalities'): 196 if i in DoGet(_REMOTE, '/modalities'):
195 DoDelete(_REMOTE, '/modalities/%s' % i) 197 DoDelete(_REMOTE, '/modalities/%s' % i)
196 if i in DoGet(_REMOTE, '/peers'): 198 if i in DoGet(_REMOTE, '/peers'):
9246 9248
9247 def test_rest_api_write_to_file_system(self): 9249 def test_rest_api_write_to_file_system(self):
9248 if IsOrthancVersionAbove(_REMOTE, 1, 12, 0): 9250 if IsOrthancVersionAbove(_REMOTE, 1, 12, 0):
9249 a = UploadInstance(_REMOTE, '2022-11-14-RLEPlanarConfiguration.dcm') ['ID'] 9251 a = UploadInstance(_REMOTE, '2022-11-14-RLEPlanarConfiguration.dcm') ['ID']
9250 self.assertRaises(Exception, lambda: DoPost(_REMOTE, '/instances/%s/export' % a, '/tmp/test.dcm')) 9252 self.assertRaises(Exception, lambda: DoPost(_REMOTE, '/instances/%s/export' % a, '/tmp/test.dcm'))
9253
9254 def test_overwrite_generates_stable_study(self):
9255
9256 # This test makes sure there are no regression wrt StableStudy when uploading instances in Orthanc
9257 # The current behaviour (tested from 1.5.7 to 1.12.0) is
9258 # If you upload 2 instances with a delay > StableAge, you get 2 StableStudy events and they are both listed in /changes
9259 # If you upload twice the same instance with a delay > StableAge, you get 2 StableStudy events but only the last one is listed in /changes, the first one is deleted
9260 # If you upload an instance and a modified version of this instance with a delay > StableAge, you get 2 StableStudy events but only the last one is listed in /changes, the first one is deleted
9261
9262
9263 def GetAllStableStudyChangesIds(studyId, timeout):
9264 # try to be as fast as possible -> stop as soon as we've found a StableStudy event that appeared after we started monitoring
9265 fromSeq = DoGet(_REMOTE, '/changes')["Last"]
9266
9267 endTime = time.time() + timeout
9268 newStableStudyFound = False
9269 while not newStableStudyFound and time.time() < endTime:
9270 time.sleep(0.1)
9271 changes = DoGet(_REMOTE, '/changes')
9272 stableStudyChangesIds = []
9273
9274 for change in changes["Changes"]:
9275 if change["ChangeType"] == "StableStudy" and studyId == change["ID"]:
9276 stableStudyChangesIds.append(change["Seq"])
9277 if change["Seq"] > fromSeq:
9278 newStableStudyFound = True
9279
9280 return stableStudyChangesIds
9281
9282 if True:
9283 DropOrthanc(_REMOTE)
9284 upload1 = UploadInstance(_REMOTE, 'Knix/Loc/IM-0001-0002.dcm')
9285 # StableAge is set to 1, expect a StableStudy within 4 seconds
9286 changes1 = GetAllStableStudyChangesIds(upload1["ParentStudy"], 4)
9287 self.assertEqual(1, len(changes1))
9288
9289 # upload the same instance again and check a new change has been generated with a new id, the first change has been deleted
9290 upload1b = UploadInstance(_REMOTE, 'Knix/Loc/IM-0001-0002.dcm')
9291 changes1b = GetAllStableStudyChangesIds(upload1b["ParentStudy"], 4)
9292 self.assertEqual(1, len(changes1b))
9293 self.assertNotEqual(changes1[0], changes1b[0])
9294
9295 if True:
9296 DropOrthanc(_REMOTE)
9297 upload1 = UploadInstance(_REMOTE, 'Knix/Loc/IM-0001-0002.dcm')
9298 # StableAge is set to 1, expect a StableStudy within 4 seconds
9299 changes1 = GetAllStableStudyChangesIds(upload1["ParentStudy"], 4)
9300 self.assertEqual(1, len(changes1))
9301
9302 # reupload a modified instance in the same study and check a new change has been generated with a new id, the first change has been deleted
9303 modified = DoPost(_REMOTE, '/instances/%s/modify' % upload1["ID"],
9304 json.dumps({
9305 "Replace" : {
9306 "InstitutionName" : "hello",
9307 "SOPInstanceUID": "1.2.840.113619.2.176.2025.1499492.7040.1171286241.705"
9308 },
9309 "Force": True
9310 }),
9311 'application/json')
9312 upload1b = DoPost(_REMOTE, '/instances', modified, 'application/dicom')
9313 changes1b = GetAllStableStudyChangesIds(upload1b["ParentStudy"], 4)
9314 self.assertEqual(upload1["ParentStudy"], upload1b["ParentStudy"])
9315 self.assertEqual(1, len(changes1b))
9316 self.assertNotEqual(changes1[0], changes1b[0])
9317
9318
9319 if True:
9320 DropOrthanc(_REMOTE)
9321 upload1 = UploadInstance(_REMOTE, 'Knix/Loc/IM-0001-0002.dcm')
9322 # StableAge is set to 1, expect a StableStudy within 4 seconds
9323 changes1 = GetAllStableStudyChangesIds(upload1["ParentStudy"], 4)
9324 self.assertEqual(1, len(changes1))
9325
9326 # upload a new instance in the same study and check a second StableStudy change has been generated with a new id
9327 upload2 = UploadInstance(_REMOTE, 'Knix/Loc/IM-0001-0003.dcm')
9328 changes2 = GetAllStableStudyChangesIds(upload2["ParentStudy"], 4)
9329 self.assertEqual(upload1["ParentStudy"], upload2["ParentStudy"])
9330 self.assertEqual(2, len(changes2))
9331 self.assertEqual(changes1[0], changes2[0])