Mercurial > hg > orthanc-databases
comparison Framework/Plugins/IndexUnitTests.h @ 0:7cea966b6829
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Jul 2018 08:16:29 +0200 |
parents | |
children | 19eec364236b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7cea966b6829 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #pragma once | |
23 | |
24 #include <orthanc/OrthancCDatabasePlugin.h> | |
25 #include <OrthancServer/ServerEnumerations.h> | |
26 | |
27 #include <gtest/gtest.h> | |
28 #include <list> | |
29 | |
30 | |
31 static std::auto_ptr<OrthancPluginAttachment> expectedAttachment; | |
32 static std::list<OrthancPluginDicomTag> expectedDicomTags; | |
33 static std::auto_ptr<OrthancPluginExportedResource> expectedExported; | |
34 | |
35 static void CheckAttachment(const OrthancPluginAttachment& attachment) | |
36 { | |
37 ASSERT_STREQ(expectedAttachment->uuid, attachment.uuid); | |
38 ASSERT_EQ(expectedAttachment->contentType, attachment.contentType); | |
39 ASSERT_EQ(expectedAttachment->uncompressedSize, attachment.uncompressedSize); | |
40 ASSERT_STREQ(expectedAttachment->uncompressedHash, attachment.uncompressedHash); | |
41 ASSERT_EQ(expectedAttachment->compressionType, attachment.compressionType); | |
42 ASSERT_EQ(expectedAttachment->compressedSize, attachment.compressedSize); | |
43 ASSERT_STREQ(expectedAttachment->compressedHash, attachment.compressedHash); | |
44 } | |
45 | |
46 static void CheckExportedResource(const OrthancPluginExportedResource& exported) | |
47 { | |
48 ASSERT_EQ(expectedExported->seq, exported.seq); | |
49 ASSERT_EQ(expectedExported->resourceType, exported.resourceType); | |
50 ASSERT_STREQ(expectedExported->publicId, exported.publicId); | |
51 ASSERT_STREQ(expectedExported->modality, exported.modality); | |
52 ASSERT_STREQ(expectedExported->date, exported.date); | |
53 ASSERT_STREQ(expectedExported->patientId, exported.patientId); | |
54 ASSERT_STREQ(expectedExported->studyInstanceUid, exported.studyInstanceUid); | |
55 ASSERT_STREQ(expectedExported->seriesInstanceUid, exported.seriesInstanceUid); | |
56 ASSERT_STREQ(expectedExported->sopInstanceUid, exported.sopInstanceUid); | |
57 } | |
58 | |
59 static void CheckDicomTag(const OrthancPluginDicomTag& tag) | |
60 { | |
61 for (std::list<OrthancPluginDicomTag>::const_iterator | |
62 it = expectedDicomTags.begin(); it != expectedDicomTags.end(); ++it) | |
63 { | |
64 if (it->group == tag.group && | |
65 it->element == tag.element && | |
66 !strcmp(it->value, tag.value)) | |
67 { | |
68 // OK, match | |
69 return; | |
70 } | |
71 } | |
72 | |
73 ASSERT_TRUE(0); // Error | |
74 } | |
75 | |
76 | |
77 | |
78 static OrthancPluginErrorCode InvokeService(struct _OrthancPluginContext_t* context, | |
79 _OrthancPluginService service, | |
80 const void* params) | |
81 { | |
82 if (service == _OrthancPluginService_DatabaseAnswer) | |
83 { | |
84 const _OrthancPluginDatabaseAnswer& answer = | |
85 *reinterpret_cast<const _OrthancPluginDatabaseAnswer*>(params); | |
86 | |
87 switch (answer.type) | |
88 { | |
89 case _OrthancPluginDatabaseAnswerType_Attachment: | |
90 { | |
91 const OrthancPluginAttachment& attachment = | |
92 *reinterpret_cast<const OrthancPluginAttachment*>(answer.valueGeneric); | |
93 CheckAttachment(attachment); | |
94 break; | |
95 } | |
96 | |
97 case _OrthancPluginDatabaseAnswerType_ExportedResource: | |
98 { | |
99 const OrthancPluginExportedResource& attachment = | |
100 *reinterpret_cast<const OrthancPluginExportedResource*>(answer.valueGeneric); | |
101 CheckExportedResource(attachment); | |
102 break; | |
103 } | |
104 | |
105 case _OrthancPluginDatabaseAnswerType_DicomTag: | |
106 { | |
107 const OrthancPluginDicomTag& tag = | |
108 *reinterpret_cast<const OrthancPluginDicomTag*>(answer.valueGeneric); | |
109 CheckDicomTag(tag); | |
110 break; | |
111 } | |
112 | |
113 default: | |
114 printf("Unhandled message: %d\n", answer.type); | |
115 break; | |
116 } | |
117 } | |
118 | |
119 return OrthancPluginErrorCode_Success; | |
120 } | |
121 | |
122 | |
123 TEST(IndexBackend, Basic) | |
124 { | |
125 using namespace OrthancDatabases; | |
126 | |
127 OrthancPluginContext context; | |
128 context.pluginsManager = NULL; | |
129 context.orthancVersion = "mainline"; | |
130 context.Free = ::free; | |
131 context.InvokeService = InvokeService; | |
132 | |
133 #if ORTHANC_ENABLE_POSTGRESQL == 1 | |
134 PostgreSQLIndex db(globalParameters_); | |
135 db.SetClearAll(true); | |
136 #elif ORTHANC_ENABLE_MYSQL == 1 | |
137 MySQLIndex db(globalParameters_); | |
138 db.SetClearAll(true); | |
139 #elif ORTHANC_ENABLE_SQLITE == 1 | |
140 SQLiteIndex db; // Open in memory | |
141 #else | |
142 # error Unsupported database backend | |
143 #endif | |
144 | |
145 db.RegisterOutput(new OrthancPlugins::DatabaseBackendOutput(&context, NULL)); | |
146 db.Open(); | |
147 | |
148 std::string s; | |
149 ASSERT_TRUE(db.LookupGlobalProperty(s, Orthanc::GlobalProperty_DatabaseSchemaVersion)); | |
150 ASSERT_EQ("6", s); | |
151 | |
152 ASSERT_FALSE(db.LookupGlobalProperty(s, Orthanc::GlobalProperty_AnonymizationSequence)); | |
153 db.SetGlobalProperty(Orthanc::GlobalProperty_AnonymizationSequence, "Hello"); | |
154 ASSERT_TRUE(db.LookupGlobalProperty(s, Orthanc::GlobalProperty_AnonymizationSequence)); | |
155 ASSERT_EQ("Hello", s); | |
156 db.SetGlobalProperty(Orthanc::GlobalProperty_AnonymizationSequence, "HelloWorld"); | |
157 ASSERT_TRUE(db.LookupGlobalProperty(s, Orthanc::GlobalProperty_AnonymizationSequence)); | |
158 ASSERT_EQ("HelloWorld", s); | |
159 | |
160 int64_t a = db.CreateResource("study", OrthancPluginResourceType_Study); | |
161 ASSERT_TRUE(db.IsExistingResource(a)); | |
162 ASSERT_FALSE(db.IsExistingResource(a + 1)); | |
163 | |
164 int64_t b; | |
165 OrthancPluginResourceType t; | |
166 ASSERT_FALSE(db.LookupResource(b, t, "world")); | |
167 ASSERT_TRUE(db.LookupResource(b, t, "study")); | |
168 ASSERT_EQ(a, b); | |
169 ASSERT_EQ(OrthancPluginResourceType_Study, t); | |
170 | |
171 b = db.CreateResource("series", OrthancPluginResourceType_Series); | |
172 ASSERT_NE(a, b); | |
173 | |
174 ASSERT_EQ("study", db.GetPublicId(a)); | |
175 ASSERT_EQ("series", db.GetPublicId(b)); | |
176 ASSERT_EQ(OrthancPluginResourceType_Study, db.GetResourceType(a)); | |
177 ASSERT_EQ(OrthancPluginResourceType_Series, db.GetResourceType(b)); | |
178 | |
179 db.AttachChild(a, b); | |
180 | |
181 int64_t c; | |
182 ASSERT_FALSE(db.LookupParent(c, a)); | |
183 ASSERT_TRUE(db.LookupParent(c, b)); | |
184 ASSERT_EQ(a, c); | |
185 | |
186 c = db.CreateResource("series2", OrthancPluginResourceType_Series); | |
187 db.AttachChild(a, c); | |
188 | |
189 ASSERT_EQ(3u, db.GetResourcesCount()); | |
190 ASSERT_EQ(0u, db.GetResourceCount(OrthancPluginResourceType_Patient)); | |
191 ASSERT_EQ(1u, db.GetResourceCount(OrthancPluginResourceType_Study)); | |
192 ASSERT_EQ(2u, db.GetResourceCount(OrthancPluginResourceType_Series)); | |
193 | |
194 ASSERT_FALSE(db.GetParentPublicId(s, a)); | |
195 ASSERT_TRUE(db.GetParentPublicId(s, b)); ASSERT_EQ("study", s); | |
196 ASSERT_TRUE(db.GetParentPublicId(s, c)); ASSERT_EQ("study", s); | |
197 | |
198 std::list<std::string> children; | |
199 db.GetChildren(children, a); | |
200 ASSERT_EQ(2u, children.size()); | |
201 db.GetChildren(children, b); | |
202 ASSERT_EQ(0u, children.size()); | |
203 db.GetChildren(children, c); | |
204 ASSERT_EQ(0u, children.size()); | |
205 | |
206 std::list<std::string> cp; | |
207 db.GetChildrenPublicId(cp, a); | |
208 ASSERT_EQ(2u, cp.size()); | |
209 ASSERT_TRUE(cp.front() == "series" || cp.front() == "series2"); | |
210 ASSERT_TRUE(cp.back() == "series" || cp.back() == "series2"); | |
211 ASSERT_NE(cp.front(), cp.back()); | |
212 | |
213 std::list<std::string> pub; | |
214 db.GetAllPublicIds(pub, OrthancPluginResourceType_Patient); | |
215 ASSERT_EQ(0u, pub.size()); | |
216 db.GetAllPublicIds(pub, OrthancPluginResourceType_Study); | |
217 ASSERT_EQ(1u, pub.size()); | |
218 ASSERT_EQ("study", pub.front()); | |
219 db.GetAllPublicIds(pub, OrthancPluginResourceType_Series); | |
220 ASSERT_EQ(2u, pub.size()); | |
221 ASSERT_TRUE(pub.front() == "series" || pub.front() == "series2"); | |
222 ASSERT_TRUE(pub.back() == "series" || pub.back() == "series2"); | |
223 ASSERT_NE(pub.front(), pub.back()); | |
224 | |
225 std::list<int64_t> ci; | |
226 db.GetChildrenInternalId(ci, a); | |
227 ASSERT_EQ(2u, ci.size()); | |
228 ASSERT_TRUE(ci.front() == b || ci.front() == c); | |
229 ASSERT_TRUE(ci.back() == b || ci.back() == c); | |
230 ASSERT_NE(ci.front(), ci.back()); | |
231 | |
232 db.SetMetadata(a, Orthanc::MetadataType_ModifiedFrom, "modified"); | |
233 db.SetMetadata(a, Orthanc::MetadataType_LastUpdate, "update2"); | |
234 ASSERT_FALSE(db.LookupMetadata(s, b, Orthanc::MetadataType_LastUpdate)); | |
235 ASSERT_TRUE(db.LookupMetadata(s, a, Orthanc::MetadataType_LastUpdate)); | |
236 ASSERT_EQ("update2", s); | |
237 db.SetMetadata(a, Orthanc::MetadataType_LastUpdate, "update"); | |
238 ASSERT_TRUE(db.LookupMetadata(s, a, Orthanc::MetadataType_LastUpdate)); | |
239 ASSERT_EQ("update", s); | |
240 | |
241 std::list<int32_t> md; | |
242 db.ListAvailableMetadata(md, a); | |
243 ASSERT_EQ(2u, md.size()); | |
244 ASSERT_TRUE(md.front() == Orthanc::MetadataType_ModifiedFrom || md.back() == Orthanc::MetadataType_ModifiedFrom); | |
245 ASSERT_TRUE(md.front() == Orthanc::MetadataType_LastUpdate || md.back() == Orthanc::MetadataType_LastUpdate); | |
246 std::string mdd; | |
247 ASSERT_TRUE(db.LookupMetadata(mdd, a, Orthanc::MetadataType_ModifiedFrom)); | |
248 ASSERT_EQ("modified", mdd); | |
249 ASSERT_TRUE(db.LookupMetadata(mdd, a, Orthanc::MetadataType_LastUpdate)); | |
250 ASSERT_EQ("update", mdd); | |
251 | |
252 db.ListAvailableMetadata(md, b); | |
253 ASSERT_EQ(0u, md.size()); | |
254 | |
255 db.DeleteMetadata(a, Orthanc::MetadataType_LastUpdate); | |
256 db.DeleteMetadata(b, Orthanc::MetadataType_LastUpdate); | |
257 ASSERT_FALSE(db.LookupMetadata(s, a, Orthanc::MetadataType_LastUpdate)); | |
258 | |
259 db.ListAvailableMetadata(md, a); | |
260 ASSERT_EQ(1u, md.size()); | |
261 ASSERT_EQ(Orthanc::MetadataType_ModifiedFrom, md.front()); | |
262 | |
263 ASSERT_EQ(0u, db.GetTotalCompressedSize()); | |
264 ASSERT_EQ(0u, db.GetTotalUncompressedSize()); | |
265 | |
266 | |
267 std::list<int32_t> fc; | |
268 | |
269 OrthancPluginAttachment a1; | |
270 a1.uuid = "uuid1"; | |
271 a1.contentType = Orthanc::FileContentType_Dicom; | |
272 a1.uncompressedSize = 42; | |
273 a1.uncompressedHash = "md5_1"; | |
274 a1.compressionType = Orthanc::CompressionType_None; | |
275 a1.compressedSize = 42; | |
276 a1.compressedHash = "md5_1"; | |
277 | |
278 OrthancPluginAttachment a2; | |
279 a2.uuid = "uuid2"; | |
280 a2.contentType = Orthanc::FileContentType_DicomAsJson; | |
281 a2.uncompressedSize = 4242; | |
282 a2.uncompressedHash = "md5_2"; | |
283 a2.compressionType = Orthanc::CompressionType_None; | |
284 a2.compressedSize = 4242; | |
285 a2.compressedHash = "md5_2"; | |
286 | |
287 db.AddAttachment(a, a1); | |
288 db.ListAvailableAttachments(fc, a); | |
289 ASSERT_EQ(1u, fc.size()); | |
290 ASSERT_EQ(Orthanc::FileContentType_Dicom, fc.front()); | |
291 db.AddAttachment(a, a2); | |
292 db.ListAvailableAttachments(fc, a); | |
293 ASSERT_EQ(2u, fc.size()); | |
294 ASSERT_FALSE(db.LookupAttachment(b, Orthanc::FileContentType_Dicom)); | |
295 | |
296 ASSERT_EQ(4284u, db.GetTotalCompressedSize()); | |
297 ASSERT_EQ(4284u, db.GetTotalUncompressedSize()); | |
298 | |
299 expectedAttachment.reset(new OrthancPluginAttachment); | |
300 expectedAttachment->uuid = "uuid1"; | |
301 expectedAttachment->contentType = Orthanc::FileContentType_Dicom; | |
302 expectedAttachment->uncompressedSize = 42; | |
303 expectedAttachment->uncompressedHash = "md5_1"; | |
304 expectedAttachment->compressionType = Orthanc::CompressionType_None; | |
305 expectedAttachment->compressedSize = 42; | |
306 expectedAttachment->compressedHash = "md5_1"; | |
307 ASSERT_TRUE(db.LookupAttachment(a, Orthanc::FileContentType_Dicom)); | |
308 | |
309 expectedAttachment.reset(new OrthancPluginAttachment); | |
310 expectedAttachment->uuid = "uuid2"; | |
311 expectedAttachment->contentType = Orthanc::FileContentType_DicomAsJson; | |
312 expectedAttachment->uncompressedSize = 4242; | |
313 expectedAttachment->uncompressedHash = "md5_2"; | |
314 expectedAttachment->compressionType = Orthanc::CompressionType_None; | |
315 expectedAttachment->compressedSize = 4242; | |
316 expectedAttachment->compressedHash = "md5_2"; | |
317 ASSERT_TRUE(db.LookupAttachment(a, Orthanc::FileContentType_DicomAsJson)); | |
318 | |
319 db.ListAvailableAttachments(fc, b); | |
320 ASSERT_EQ(0u, fc.size()); | |
321 db.DeleteAttachment(a, Orthanc::FileContentType_Dicom); | |
322 db.ListAvailableAttachments(fc, a); | |
323 ASSERT_EQ(1u, fc.size()); | |
324 ASSERT_EQ(Orthanc::FileContentType_DicomAsJson, fc.front()); | |
325 db.DeleteAttachment(a, Orthanc::FileContentType_DicomAsJson); | |
326 db.ListAvailableAttachments(fc, a); | |
327 ASSERT_EQ(0u, fc.size()); | |
328 | |
329 | |
330 db.SetIdentifierTag(a, 0x0010, 0x0020, "patient"); | |
331 db.SetIdentifierTag(a, 0x0020, 0x000d, "study"); | |
332 | |
333 expectedDicomTags.clear(); | |
334 expectedDicomTags.push_back(OrthancPluginDicomTag()); | |
335 expectedDicomTags.push_back(OrthancPluginDicomTag()); | |
336 expectedDicomTags.front().group = 0x0010; | |
337 expectedDicomTags.front().element = 0x0020; | |
338 expectedDicomTags.front().value = "patient"; | |
339 expectedDicomTags.back().group = 0x0020; | |
340 expectedDicomTags.back().element = 0x000d; | |
341 expectedDicomTags.back().value = "study"; | |
342 db.GetMainDicomTags(a); | |
343 | |
344 | |
345 db.LookupIdentifier(ci, OrthancPluginResourceType_Study, 0x0010, 0x0020, | |
346 OrthancPluginIdentifierConstraint_Equal, "patient"); | |
347 ASSERT_EQ(1u, ci.size()); | |
348 ASSERT_EQ(a, ci.front()); | |
349 db.LookupIdentifier(ci, OrthancPluginResourceType_Study, 0x0010, 0x0020, | |
350 OrthancPluginIdentifierConstraint_Equal, "study"); | |
351 ASSERT_EQ(0u, ci.size()); | |
352 | |
353 | |
354 OrthancPluginExportedResource exp; | |
355 exp.seq = -1; | |
356 exp.resourceType = OrthancPluginResourceType_Study; | |
357 exp.publicId = "id"; | |
358 exp.modality = "remote"; | |
359 exp.date = "date"; | |
360 exp.patientId = "patient"; | |
361 exp.studyInstanceUid = "study"; | |
362 exp.seriesInstanceUid = "series"; | |
363 exp.sopInstanceUid = "instance"; | |
364 db.LogExportedResource(exp); | |
365 | |
366 expectedExported.reset(new OrthancPluginExportedResource()); | |
367 *expectedExported = exp; | |
368 expectedExported->seq = 1; | |
369 | |
370 bool done; | |
371 db.GetExportedResources(done, 0, 10); | |
372 | |
373 | |
374 db.GetAllPublicIds(pub, OrthancPluginResourceType_Patient); ASSERT_EQ(0u, pub.size()); | |
375 db.GetAllPublicIds(pub, OrthancPluginResourceType_Study); ASSERT_EQ(1u, pub.size()); | |
376 db.GetAllPublicIds(pub, OrthancPluginResourceType_Series); ASSERT_EQ(2u, pub.size()); | |
377 db.GetAllPublicIds(pub, OrthancPluginResourceType_Instance); ASSERT_EQ(0u, pub.size()); | |
378 ASSERT_EQ(3u, db.GetResourcesCount()); | |
379 | |
380 ASSERT_EQ(0u, db.GetUnprotectedPatientsCount()); // No patient was inserted | |
381 ASSERT_TRUE(db.IsExistingResource(c)); | |
382 db.DeleteResource(c); | |
383 ASSERT_FALSE(db.IsExistingResource(c)); | |
384 ASSERT_TRUE(db.IsExistingResource(a)); | |
385 ASSERT_TRUE(db.IsExistingResource(b)); | |
386 ASSERT_EQ(2u, db.GetResourcesCount()); | |
387 db.DeleteResource(a); | |
388 ASSERT_EQ(0u, db.GetResourcesCount()); | |
389 ASSERT_FALSE(db.IsExistingResource(a)); | |
390 ASSERT_FALSE(db.IsExistingResource(b)); | |
391 ASSERT_FALSE(db.IsExistingResource(c)); | |
392 | |
393 ASSERT_EQ(0u, db.GetResourcesCount()); | |
394 ASSERT_EQ(0u, db.GetUnprotectedPatientsCount()); | |
395 int64_t p1 = db.CreateResource("patient1", OrthancPluginResourceType_Patient); | |
396 int64_t p2 = db.CreateResource("patient2", OrthancPluginResourceType_Patient); | |
397 int64_t p3 = db.CreateResource("patient3", OrthancPluginResourceType_Patient); | |
398 ASSERT_EQ(3u, db.GetUnprotectedPatientsCount()); | |
399 int64_t r; | |
400 ASSERT_TRUE(db.SelectPatientToRecycle(r)); | |
401 ASSERT_EQ(p1, r); | |
402 ASSERT_TRUE(db.SelectPatientToRecycle(r, p1)); | |
403 ASSERT_EQ(p2, r); | |
404 ASSERT_FALSE(db.IsProtectedPatient(p1)); | |
405 db.SetProtectedPatient(p1, true); | |
406 ASSERT_TRUE(db.IsProtectedPatient(p1)); | |
407 ASSERT_TRUE(db.SelectPatientToRecycle(r)); | |
408 ASSERT_EQ(p2, r); | |
409 db.SetProtectedPatient(p1, false); | |
410 ASSERT_FALSE(db.IsProtectedPatient(p1)); | |
411 ASSERT_TRUE(db.SelectPatientToRecycle(r)); | |
412 ASSERT_EQ(p2, r); | |
413 db.DeleteResource(p2); | |
414 ASSERT_TRUE(db.SelectPatientToRecycle(r, p3)); | |
415 ASSERT_EQ(p1, r); | |
416 } |