comparison Plugins/Recycling/Run.py @ 203:89ed88b2f1a0

integration tests for patient recycling
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 15 Jan 2019 20:12:37 +0100
parents
children 943166deebcb
comparison
equal deleted inserted replaced
202:f26f9ae9c599 203:89ed88b2f1a0
1 #!/usr/bin/python
2
3 # Orthanc - A Lightweight, RESTful DICOM Store
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
5 # Department, University Hospital of Liege, Belgium
6 # Copyright (C) 2017-2019 Osimis S.A., Belgium
7 #
8 # This program is free software: you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22
23 # You must add the following to the configuration file:
24 #
25 # {
26 # "MaximumPatientCount" : 4
27 # }
28
29
30
31 import os
32 import pprint
33 import sys
34 import argparse
35 import unittest
36 import re
37
38 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests'))
39 from Toolbox import *
40
41
42 ##
43 ## Parse the command-line arguments
44 ##
45
46 parser = argparse.ArgumentParser(description = 'Run the integration tests for the patient recycling behavior.')
47
48 parser.add_argument('--server',
49 default = 'localhost',
50 help = 'Address of the Orthanc server to test')
51 parser.add_argument('--rest',
52 type = int,
53 default = 8042,
54 help = 'Port to the REST API')
55 parser.add_argument('--username',
56 default = 'alice',
57 help = 'Username to the REST API')
58 parser.add_argument('--password',
59 default = 'orthanctest',
60 help = 'Password to the REST API')
61 parser.add_argument('--force', help = 'Do not warn the user',
62 action = 'store_true')
63 parser.add_argument('options', metavar = 'N', nargs = '*',
64 help='Arguments to Python unittest')
65
66 args = parser.parse_args()
67
68
69 ##
70 ## Configure the testing context
71 ##
72
73 if not args.force:
74 print("""
75 WARNING: This test will remove all the content of your
76 Orthanc instance running on %s!
77
78 Are you sure ["yes" to go on]?""" % args.server)
79
80 if sys.stdin.readline().strip() != 'yes':
81 print('Aborting...')
82 exit(0)
83
84
85 ORTHANC = DefineOrthanc(server = args.server,
86 username = args.username,
87 password = args.password,
88 restPort = args.rest)
89
90
91 ##
92 ## The tests
93 ##
94
95
96 DICOM = {
97 'brainix' : [
98 'Brainix/Flair/IM-0001-0001.dcm',
99 'Brainix/Epi/IM-0001-0001.dcm',
100 ],
101 'knee' : [
102 'Knee/T1/IM-0001-0001.dcm',
103 'Knee/T2/IM-0001-0001.dcm',
104 ],
105 'beaufix' : [
106 'Beaufix/IM-0001-0001.dcm',
107 ],
108 'phenix' : [
109 'Phenix/IM-0001-0001.dcm',
110 ],
111 'dummy' : [
112 'DummyCT.dcm',
113 ],
114 'comunix' : [
115 'Comunix/Pet/IM-0001-0001.dcm',
116 'Comunix/Pet/IM-0001-0002.dcm',
117 ],
118 }
119
120 PATIENTS = list(DICOM.keys())
121
122
123
124 def UploadAndGetPatientId(patient, instance):
125 a = UploadInstance(ORTHANC, DICOM[patient][instance])['ID']
126 return DoGet(ORTHANC, '/instances/%s/patient' % a)['ID']
127
128 def TestContent(expectedPatients):
129 patients = DoGet(ORTHANC, '/patients')
130 if len(patients) != len(expectedPatients):
131 return False
132
133 for i in expectedPatients:
134 if not i in patients:
135 return False
136
137 for i in patients:
138 if not i in expectedPatients:
139 return False
140
141 return True
142
143
144 class Orthanc(unittest.TestCase):
145 def setUp(self):
146 if (sys.version_info >= (3, 0)):
147 # Remove annoying warnings about unclosed socket in Python 3
148 import warnings
149 warnings.simplefilter("ignore", ResourceWarning)
150
151 DropOrthanc(ORTHANC)
152
153
154 def test_config(self):
155 # Make sure that "MaximumPatientCount" equals 4
156 a = UploadAndGetPatientId('brainix', 0)
157 b = UploadAndGetPatientId('knee', 0)
158 c = UploadAndGetPatientId('beaufix', 0)
159 d = UploadAndGetPatientId('phenix', 0)
160 self.assertEqual(4, len(DoGet(ORTHANC, '/instances')))
161
162 e = UploadAndGetPatientId('dummy', 0)
163 self.assertTrue(TestContent([b, c, d, e ]))
164
165
166 def test_loop(self):
167 ids = []
168
169 for i in range(5):
170 ids.append(UploadAndGetPatientId(PATIENTS[i], 0))
171
172 self.assertEqual(4, len(DoGet(ORTHANC, '/instances')))
173
174 for i in range(20):
175 expected = set(ids)
176 expected.remove(ids[i % 5])
177 TestContent(expected)
178
179 self.assertEqual(ids[i % 5], UploadAndGetPatientId(PATIENTS[i % 5], 0))
180
181
182 def test_protection(self):
183 a = UploadAndGetPatientId('brainix', 0)
184 b = UploadAndGetPatientId('knee', 0)
185 c = UploadAndGetPatientId('beaufix', 0)
186 d = UploadAndGetPatientId('phenix', 0)
187
188 DoPut(ORTHANC, '/patients/%s/protected' % b, '1')
189 UploadAndGetPatientId('knee', 1)
190
191 e = UploadAndGetPatientId('dummy', 0)
192 f = UploadAndGetPatientId('comunix', 0)
193
194 self.assertTrue(TestContent([ d, e, f, b ]))
195
196 # This puts "b" at the end of the recycling order
197 DoPut(ORTHANC, '/patients/%s/protected' % b, '0')
198
199 a = UploadAndGetPatientId('brainix', 0)
200 self.assertTrue(TestContent([ e, f, b, a ]))
201
202 c = UploadAndGetPatientId('beaufix', 0)
203 self.assertTrue(TestContent([ f, b, a, c ]))
204
205 d = UploadAndGetPatientId('phenix', 0)
206 self.assertTrue(TestContent([ b, a, c, d ]))
207
208 e = UploadAndGetPatientId('dummy', 0)
209 self.assertTrue(TestContent([ a, c, d, e ]))
210
211
212 def test_bitbucket_issue_58(self):
213 a = UploadAndGetPatientId('brainix', 0)
214 b = UploadAndGetPatientId('knee', 0)
215 c = UploadAndGetPatientId('beaufix', 0)
216 d = UploadAndGetPatientId('phenix', 0)
217 self.assertEqual(4, len(DoGet(ORTHANC, '/instances')))
218
219 e = UploadAndGetPatientId('dummy', 0)
220 self.assertTrue(TestContent([b, c, d, e ]))
221
222 UploadAndGetPatientId('knee', 1)
223 self.assertTrue(TestContent([c, d, e, b ]))
224
225 f = UploadAndGetPatientId('comunix', 0)
226 self.assertTrue(TestContent([d, e, b, f ]))
227
228
229 try:
230 print('\nStarting the tests...')
231 unittest.main(argv = [ sys.argv[0] ] + args.options)
232
233 finally:
234 print('\nDone')