ctkDICOMDatabaseTest2.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QCoreApplication>
  16. #include <QDir>
  17. #include <QTimer>
  18. // ctkDICOMCore includes
  19. #include "ctkDICOMDatabase.h"
  20. // STD includes
  21. #include <iostream>
  22. #include <cstdlib>
  23. int ctkDICOMDatabaseTest2( int argc, char * argv [] )
  24. {
  25. QCoreApplication app(argc, argv);
  26. if (argc < 2)
  27. {
  28. std::cerr << "ctkDICOMDatabaseTest2: missing dicom filePath argument";
  29. std::cerr << std::endl;
  30. return EXIT_FAILURE;
  31. }
  32. QString dicomFilePath(argv[1]);
  33. ctkDICOMDatabase database;
  34. QDir databaseDirectory = QDir::temp();
  35. databaseDirectory.remove("ctkDICOMDatabase.sql");
  36. databaseDirectory.remove("ctkDICOMTagCache.sql");
  37. QFileInfo databaseFile(databaseDirectory, QString("database.test"));
  38. database.openDatabase(databaseFile.absoluteFilePath());
  39. if (!database.lastError().isEmpty())
  40. {
  41. std::cerr << "ctkDICOMDatabase::openDatabase() failed: "
  42. << qPrintable(database.lastError()) << std::endl;
  43. return EXIT_FAILURE;
  44. }
  45. if (!database.database().isValid())
  46. {
  47. std::cerr << "ctkDICOMDatabase::openDatabase() failed: "
  48. << "invalid sql database" << std::endl;
  49. return EXIT_FAILURE;
  50. }
  51. if (database.isInMemory())
  52. {
  53. std::cerr << "ctkDICOMDatabase::openDatabase() failed: "
  54. << "database should not be in memory" << std::endl;
  55. return EXIT_FAILURE;
  56. }
  57. bool res = database.initializeDatabase();
  58. if (!res)
  59. {
  60. std::cerr << "ctkDICOMDatabase::initializeDatabase() failed." << std::endl;
  61. return EXIT_FAILURE;
  62. }
  63. //
  64. // Test that the tag interface works to parse ascii
  65. //
  66. QString tag("0008,103e");
  67. unsigned short group, element;
  68. if ( !database.tagToGroupElement(tag, group, element) )
  69. {
  70. std::cerr << "ctkDICOMDatabase: could not parse tag" << std::endl;
  71. return EXIT_FAILURE;
  72. }
  73. if ( group != 0x8 || element != 0x103e )
  74. {
  75. std::cerr << "ctkDICOMDatabase: expected: " << "0008,103e" << std::endl;
  76. std::cerr << "ctkDICOMDatabase: got: " << group << " " << element << std::endl;
  77. std::cerr << "ctkDICOMDatabase: parsed tag does not match group/element" << std::endl;
  78. return EXIT_FAILURE;
  79. }
  80. if ( database.groupElementToTag(group, element) != tag )
  81. {
  82. std::cerr << "ctkDICOMDatabase: could not convert a uints to tag string" << std::endl;
  83. return EXIT_FAILURE;
  84. }
  85. //
  86. // Basic test:
  87. // - insert the file specified on the command line
  88. // - ask for tag values and compare to known results
  89. //
  90. database.insert(dicomFilePath, false, false);
  91. QString instanceUID("1.2.840.113619.2.135.3596.6358736.4843.1115808177.83");
  92. QString foundFile = database.fileForInstance(instanceUID);
  93. if (foundFile != dicomFilePath)
  94. {
  95. std::cerr << "ctkDICOMDatabase: didn't get back the original file path" << std::endl;
  96. return EXIT_FAILURE;
  97. }
  98. QString foundInstance = database.instanceForFile(dicomFilePath);
  99. if (foundInstance != instanceUID)
  100. {
  101. std::cerr << "ctkDICOMDatabase: didn't get back the original instance uid" << std::endl;
  102. return EXIT_FAILURE;
  103. }
  104. //
  105. // Test the tag cache
  106. //
  107. if (!database.tagCacheExists())
  108. {
  109. std::cerr << "ctkDICOMDatabase: tag cache should be configured when database opens" << std::endl;
  110. return EXIT_FAILURE;
  111. }
  112. if (!database.initializeTagCache())
  113. {
  114. std::cerr << "ctkDICOMDatabase: could not initialize tag cache" << std::endl;
  115. return EXIT_FAILURE;
  116. }
  117. if (!database.tagCacheExists())
  118. {
  119. std::cerr << "ctkDICOMDatabase: tag cache should exist but is not detected" << std::endl;
  120. return EXIT_FAILURE;
  121. }
  122. if (database.cachedTag(instanceUID, tag) != QString(""))
  123. {
  124. std::cerr << "ctkDICOMDatabase: tag cache should return empty string for unknown instance tag" << std::endl;
  125. return EXIT_FAILURE;
  126. }
  127. QString knownSeriesDescription("3D Cor T1 FAST IR-prepped GRE");
  128. if (!database.cacheTag(instanceUID, tag, knownSeriesDescription))
  129. {
  130. std::cerr << "ctkDICOMDatabase: could not insert instance tag" << std::endl;
  131. return EXIT_FAILURE;
  132. }
  133. if (database.cachedTag(instanceUID, tag) != knownSeriesDescription)
  134. {
  135. std::cerr << "ctkDICOMDatabase: could not retrieve cached tag" << std::endl;
  136. return EXIT_FAILURE;
  137. }
  138. QString foundSeriesDescription = database.instanceValue(instanceUID, tag);
  139. if (foundSeriesDescription != knownSeriesDescription)
  140. {
  141. std::cerr << "ctkDICOMDatabase: invalid element value returned" << std::endl;
  142. return EXIT_FAILURE;
  143. }
  144. // now update the database
  145. database.updateSchema();
  146. // and repeat the above checks
  147. foundFile = database.fileForInstance(instanceUID);
  148. if (foundFile != dicomFilePath)
  149. {
  150. std::cerr << "ctkDICOMDatabase: didn't get back the original file path" << std::endl;
  151. return EXIT_FAILURE;
  152. }
  153. foundSeriesDescription = database.instanceValue(instanceUID, tag);
  154. if (foundSeriesDescription != knownSeriesDescription)
  155. {
  156. std::cerr << "ctkDICOMDatabase: invalid element value returned" << std::endl;
  157. return EXIT_FAILURE;
  158. }
  159. database.closeDatabase();
  160. database.initializeDatabase();
  161. return EXIT_SUCCESS;
  162. }