comparison UnitTestsSources/ServerIndexTests.cpp @ 1287:63a6428771f4

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Feb 2015 10:07:56 +0100
parents b4acdb37e43b
children 6e7e5ed91c2d
comparison
equal deleted inserted replaced
1286:b4acdb37e43b 1287:63a6428771f4
108 } 108 }
109 109
110 virtual void SetUp() 110 virtual void SetUp()
111 { 111 {
112 listener_.reset(new ServerIndexListener); 112 listener_.reset(new ServerIndexListener);
113 index_.reset(new DatabaseWrapper()); 113
114 switch (GetParam())
115 {
116 case DatabaseWrapperClass_SQLite:
117 index_.reset(new DatabaseWrapper());
118 break;
119
120 default:
121 throw OrthancException(ErrorCode_InternalError);
122 }
123
114 index_->SetListener(*listener_); 124 index_->SetListener(*listener_);
115 } 125 }
116 126
117 virtual void TearDown() 127 virtual void TearDown()
118 { 128 {
120 listener_.reset(NULL); 130 listener_.reset(NULL);
121 } 131 }
122 132
123 void CheckTableRecordCount(uint32_t expected, const char* table) 133 void CheckTableRecordCount(uint32_t expected, const char* table)
124 { 134 {
125 if (GetParam() == DatabaseWrapperClass_SQLite) 135 switch (GetParam())
126 { 136 {
127 DatabaseWrapper* sqlite = dynamic_cast<DatabaseWrapper*>(index_.get()); 137 case DatabaseWrapperClass_SQLite:
128 ASSERT_EQ(expected, sqlite->GetTableRecordCount(table)); 138 {
139 DatabaseWrapper* sqlite = dynamic_cast<DatabaseWrapper*>(index_.get());
140 ASSERT_EQ(expected, sqlite->GetTableRecordCount(table));
141 break;
142 }
143
144 default:
145 throw OrthancException(ErrorCode_InternalError);
129 } 146 }
130 } 147 }
131 148
149 void CheckNoParent(int64_t id)
150 {
151 std::string s;
152
153 switch (GetParam())
154 {
155 case DatabaseWrapperClass_SQLite:
156 {
157 DatabaseWrapper* sqlite = dynamic_cast<DatabaseWrapper*>(index_.get());
158 ASSERT_FALSE(sqlite->GetParentPublicId(s, id));
159 break;
160 }
161
162 default:
163 throw OrthancException(ErrorCode_InternalError);
164 }
165 }
166
167 void CheckParentPublicId(const char* expected, int64_t id)
168 {
169 std::string s;
170
171 switch (GetParam())
172 {
173 case DatabaseWrapperClass_SQLite:
174 {
175 DatabaseWrapper* sqlite = dynamic_cast<DatabaseWrapper*>(index_.get());
176 ASSERT_TRUE(sqlite->GetParentPublicId(s, id));
177 ASSERT_EQ(expected, s);
178 break;
179 }
180
181 default:
182 throw OrthancException(ErrorCode_InternalError);
183 }
184 }
185
186 void CheckNoChild(int64_t id)
187 {
188 std::list<std::string> j;
189
190 switch (GetParam())
191 {
192 case DatabaseWrapperClass_SQLite:
193 {
194 DatabaseWrapper* sqlite = dynamic_cast<DatabaseWrapper*>(index_.get());
195 sqlite->GetChildren(j, id);
196 ASSERT_EQ(0, j.size());
197 break;
198 }
199
200 default:
201 throw OrthancException(ErrorCode_InternalError);
202 }
203 }
204
205 void CheckOneChild(const char* expected, int64_t id)
206 {
207 std::list<std::string> j;
208
209 switch (GetParam())
210 {
211 case DatabaseWrapperClass_SQLite:
212 {
213 DatabaseWrapper* sqlite = dynamic_cast<DatabaseWrapper*>(index_.get());
214 sqlite->GetChildren(j, id);
215 ASSERT_EQ(1, j.size());
216 ASSERT_EQ(expected, j.front());
217 break;
218 }
219
220 default:
221 throw OrthancException(ErrorCode_InternalError);
222 }
223 }
224
225 void CheckTwoChildren(const char* expected1,
226 const char* expected2,
227 int64_t id)
228 {
229 std::list<std::string> j;
230
231 switch (GetParam())
232 {
233 case DatabaseWrapperClass_SQLite:
234 {
235 DatabaseWrapper* sqlite = dynamic_cast<DatabaseWrapper*>(index_.get());
236 sqlite->GetChildren(j, id);
237 ASSERT_EQ(2, j.size());
238 ASSERT_TRUE((expected1 == j.front() && expected2 == j.back()) ||
239 (expected1 == j.back() && expected2 == j.front()));
240 break;
241 }
242
243 default:
244 throw OrthancException(ErrorCode_InternalError);
245 }
246 }
132 }; 247 };
133 } 248 }
134 249
135 250
136 INSTANTIATE_TEST_CASE_P(DatabaseWrapperName, 251 INSTANTIATE_TEST_CASE_P(DatabaseWrapperName,
138 ::testing::Values(DatabaseWrapperClass_SQLite)); 253 ::testing::Values(DatabaseWrapperClass_SQLite));
139 254
140 255
141 TEST_P(DatabaseWrapperTest, Simple) 256 TEST_P(DatabaseWrapperTest, Simple)
142 { 257 {
143 DatabaseWrapper* sqlite_ = NULL;
144 if (GetParam() == DatabaseWrapperClass_SQLite)
145 {
146 sqlite_ = dynamic_cast<DatabaseWrapper*>(index_.get());
147 }
148
149 int64_t a[] = { 258 int64_t a[] = {
150 index_->CreateResource("a", ResourceType_Patient), // 0 259 index_->CreateResource("a", ResourceType_Patient), // 0
151 index_->CreateResource("b", ResourceType_Study), // 1 260 index_->CreateResource("b", ResourceType_Study), // 1
152 index_->CreateResource("c", ResourceType_Series), // 2 261 index_->CreateResource("c", ResourceType_Series), // 2
153 index_->CreateResource("d", ResourceType_Instance), // 3 262 index_->CreateResource("d", ResourceType_Instance), // 3
207 ASSERT_TRUE(index_->LookupParent(parent, a[5])); ASSERT_EQ(a[6], parent); 316 ASSERT_TRUE(index_->LookupParent(parent, a[5])); ASSERT_EQ(a[6], parent);
208 ASSERT_FALSE(index_->LookupParent(parent, a[6])); 317 ASSERT_FALSE(index_->LookupParent(parent, a[6]));
209 318
210 std::string s; 319 std::string s;
211 320
212 if (sqlite_ != NULL) 321 CheckNoParent(a[0]);
213 { 322 CheckNoParent(a[6]);
214 ASSERT_FALSE(sqlite_->GetParentPublicId(s, a[0])); 323 CheckParentPublicId("a", a[1]);
215 ASSERT_FALSE(sqlite_->GetParentPublicId(s, a[6])); 324 CheckParentPublicId("b", a[2]);
216 ASSERT_TRUE(sqlite_->GetParentPublicId(s, a[1])); ASSERT_EQ("a", s); 325 CheckParentPublicId("c", a[3]);
217 ASSERT_TRUE(sqlite_->GetParentPublicId(s, a[2])); ASSERT_EQ("b", s); 326 CheckParentPublicId("c", a[4]);
218 ASSERT_TRUE(sqlite_->GetParentPublicId(s, a[3])); ASSERT_EQ("c", s); 327 CheckParentPublicId("g", a[5]);
219 ASSERT_TRUE(sqlite_->GetParentPublicId(s, a[4])); ASSERT_EQ("c", s);
220 ASSERT_TRUE(sqlite_->GetParentPublicId(s, a[5])); ASSERT_EQ("g", s);
221 }
222 328
223 std::list<std::string> l; 329 std::list<std::string> l;
224 index_->GetChildrenPublicId(l, a[0]); ASSERT_EQ(1u, l.size()); ASSERT_EQ("b", l.front()); 330 index_->GetChildrenPublicId(l, a[0]); ASSERT_EQ(1u, l.size()); ASSERT_EQ("b", l.front());
225 index_->GetChildrenPublicId(l, a[1]); ASSERT_EQ(1u, l.size()); ASSERT_EQ("c", l.front()); 331 index_->GetChildrenPublicId(l, a[1]); ASSERT_EQ(1u, l.size()); ASSERT_EQ("c", l.front());
226 index_->GetChildrenPublicId(l, a[3]); ASSERT_EQ(0u, l.size()); 332 index_->GetChildrenPublicId(l, a[3]); ASSERT_EQ(0u, l.size());
242 std::list<MetadataType> md; 348 std::list<MetadataType> md;
243 index_->ListAvailableMetadata(md, a[4]); 349 index_->ListAvailableMetadata(md, a[4]);
244 ASSERT_EQ(0u, md.size()); 350 ASSERT_EQ(0u, md.size());
245 351
246 index_->AddAttachment(a[4], FileInfo("my json file", FileContentType_DicomAsJson, 42, "md5", 352 index_->AddAttachment(a[4], FileInfo("my json file", FileContentType_DicomAsJson, 42, "md5",
247 CompressionType_Zlib, 21, "compressedMD5")); 353 CompressionType_Zlib, 21, "compressedMD5"));
248 index_->AddAttachment(a[4], FileInfo("my dicom file", FileContentType_Dicom, 42, "md5")); 354 index_->AddAttachment(a[4], FileInfo("my dicom file", FileContentType_Dicom, 42, "md5"));
249 index_->AddAttachment(a[6], FileInfo("world", FileContentType_Dicom, 44, "md5")); 355 index_->AddAttachment(a[6], FileInfo("world", FileContentType_Dicom, 44, "md5"));
250 index_->SetMetadata(a[4], MetadataType_Instance_RemoteAet, "PINNACLE"); 356 index_->SetMetadata(a[4], MetadataType_Instance_RemoteAet, "PINNACLE");
251 357
252 index_->ListAvailableMetadata(md, a[4]); 358 index_->ListAvailableMetadata(md, a[4]);
352 458
353 459
354 460
355 TEST_P(DatabaseWrapperTest, Upward) 461 TEST_P(DatabaseWrapperTest, Upward)
356 { 462 {
357 DatabaseWrapper* sqlite_ = NULL;
358 if (GetParam() == DatabaseWrapperClass_SQLite)
359 {
360 sqlite_ = dynamic_cast<DatabaseWrapper*>(index_.get());
361 }
362
363 int64_t a[] = { 463 int64_t a[] = {
364 index_->CreateResource("a", ResourceType_Patient), // 0 464 index_->CreateResource("a", ResourceType_Patient), // 0
365 index_->CreateResource("b", ResourceType_Study), // 1 465 index_->CreateResource("b", ResourceType_Study), // 1
366 index_->CreateResource("c", ResourceType_Series), // 2 466 index_->CreateResource("c", ResourceType_Series), // 2
367 index_->CreateResource("d", ResourceType_Instance), // 3 467 index_->CreateResource("d", ResourceType_Instance), // 3
377 index_->AttachChild(a[2], a[4]); 477 index_->AttachChild(a[2], a[4]);
378 index_->AttachChild(a[1], a[6]); 478 index_->AttachChild(a[1], a[6]);
379 index_->AttachChild(a[0], a[5]); 479 index_->AttachChild(a[0], a[5]);
380 index_->AttachChild(a[5], a[7]); 480 index_->AttachChild(a[5], a[7]);
381 481
382 if (sqlite_ != NULL) 482 CheckTwoChildren("b", "f", a[0]);
383 { 483 CheckTwoChildren("c", "g", a[1]);
384 std::list<std::string> j; 484 CheckTwoChildren("d", "e", a[2]);
385 sqlite_->GetChildren(j, a[0]); 485 CheckNoChild(a[3]);
386 ASSERT_EQ(2u, j.size()); 486 CheckNoChild(a[4]);
387 ASSERT_TRUE((j.front() == "b" && j.back() == "f") || 487 CheckOneChild("h", a[5]);
388 (j.back() == "b" && j.front() == "f")); 488 CheckNoChild(a[6]);
389 489 CheckNoChild(a[7]);
390 sqlite_->GetChildren(j, a[1]);
391 ASSERT_EQ(2u, j.size());
392 ASSERT_TRUE((j.front() == "c" && j.back() == "g") ||
393 (j.back() == "c" && j.front() == "g"));
394
395 sqlite_->GetChildren(j, a[2]);
396 ASSERT_EQ(2u, j.size());
397 ASSERT_TRUE((j.front() == "d" && j.back() == "e") ||
398 (j.back() == "d" && j.front() == "e"));
399
400 sqlite_->GetChildren(j, a[3]); ASSERT_EQ(0u, j.size());
401 sqlite_->GetChildren(j, a[4]); ASSERT_EQ(0u, j.size());
402 sqlite_->GetChildren(j, a[5]); ASSERT_EQ(1u, j.size()); ASSERT_EQ("h", j.front());
403 sqlite_->GetChildren(j, a[6]); ASSERT_EQ(0u, j.size());
404 sqlite_->GetChildren(j, a[7]); ASSERT_EQ(0u, j.size());
405 }
406 490
407 listener_->Reset(); 491 listener_->Reset();
408 index_->DeleteResource(a[3]); 492 index_->DeleteResource(a[3]);
409 ASSERT_EQ("c", listener_->ancestorId_); 493 ASSERT_EQ("c", listener_->ancestorId_);
410 ASSERT_EQ(ResourceType_Series, listener_->ancestorType_); 494 ASSERT_EQ(ResourceType_Series, listener_->ancestorType_);
431 for (int i = 0; i < 10; i++) 515 for (int i = 0; i < 10; i++)
432 { 516 {
433 std::string p = "Patient " + boost::lexical_cast<std::string>(i); 517 std::string p = "Patient " + boost::lexical_cast<std::string>(i);
434 patients.push_back(index_->CreateResource(p, ResourceType_Patient)); 518 patients.push_back(index_->CreateResource(p, ResourceType_Patient));
435 index_->AddAttachment(patients[i], FileInfo(p, FileContentType_Dicom, i + 10, 519 index_->AddAttachment(patients[i], FileInfo(p, FileContentType_Dicom, i + 10,
436 "md5-" + boost::lexical_cast<std::string>(i))); 520 "md5-" + boost::lexical_cast<std::string>(i)));
437 ASSERT_FALSE(index_->IsProtectedPatient(patients[i])); 521 ASSERT_FALSE(index_->IsProtectedPatient(patients[i]));
438 } 522 }
439 523
440 CheckTableRecordCount(10u, "Resources"); 524 CheckTableRecordCount(10u, "Resources");
441 CheckTableRecordCount(10u, "PatientRecyclingOrder"); 525 CheckTableRecordCount(10u, "PatientRecyclingOrder");
492 for (int i = 0; i < 5; i++) 576 for (int i = 0; i < 5; i++)
493 { 577 {
494 std::string p = "Patient " + boost::lexical_cast<std::string>(i); 578 std::string p = "Patient " + boost::lexical_cast<std::string>(i);
495 patients.push_back(index_->CreateResource(p, ResourceType_Patient)); 579 patients.push_back(index_->CreateResource(p, ResourceType_Patient));
496 index_->AddAttachment(patients[i], FileInfo(p, FileContentType_Dicom, i + 10, 580 index_->AddAttachment(patients[i], FileInfo(p, FileContentType_Dicom, i + 10,
497 "md5-" + boost::lexical_cast<std::string>(i))); 581 "md5-" + boost::lexical_cast<std::string>(i)));
498 ASSERT_FALSE(index_->IsProtectedPatient(patients[i])); 582 ASSERT_FALSE(index_->IsProtectedPatient(patients[i]));
499 } 583 }
500 584
501 CheckTableRecordCount(5, "Resources"); 585 CheckTableRecordCount(5, "Resources");
502 CheckTableRecordCount(5, "PatientRecyclingOrder"); 586 CheckTableRecordCount(5, "PatientRecyclingOrder");
623 ASSERT_EQ(1u, s.size()); 707 ASSERT_EQ(1u, s.size());
624 ASSERT_TRUE(std::find(s.begin(), s.end(), a[1]) != s.end()); 708 ASSERT_TRUE(std::find(s.begin(), s.end(), a[1]) != s.end());
625 709
626 710
627 /*{ 711 /*{
628 std::list<std::string> s; 712 std::list<std::string> s;
629 context.GetIndex().LookupIdentifier(s, DICOM_TAG_STUDY_INSTANCE_UID, "1.2.250.1.74.20130819132500.29000036381059"); 713 context.GetIndex().LookupIdentifier(s, DICOM_TAG_STUDY_INSTANCE_UID, "1.2.250.1.74.20130819132500.29000036381059");
630 for (std::list<std::string>::iterator i = s.begin(); i != s.end(); i++) 714 for (std::list<std::string>::iterator i = s.begin(); i != s.end(); i++)
631 { 715 {
632 std::cout << "*** " << *i << std::endl;; 716 std::cout << "*** " << *i << std::endl;;
633 } 717 }
634 }*/ 718 }*/
635 } 719 }
636 720
637 721
638 722
639 TEST(ServerIndex, AttachmentRecycling) 723 TEST(ServerIndex, AttachmentRecycling)