comparison UnitTestsSources/ServerIndexTests.cpp @ 2826:c277e0421200

unit testing of overwriting
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 19 Sep 2018 16:06:41 +0200
parents 925d8dc03a23
children 859a4950d48f
comparison
equal deleted inserted replaced
2825:8aa6aef11b70 2826:c277e0421200
33 33
34 #include "PrecompiledHeadersUnitTests.h" 34 #include "PrecompiledHeadersUnitTests.h"
35 #include "gtest/gtest.h" 35 #include "gtest/gtest.h"
36 36
37 #include "../Core/FileStorage/FilesystemStorage.h" 37 #include "../Core/FileStorage/FilesystemStorage.h"
38 #include "../Core/FileStorage/MemoryStorageArea.h"
38 #include "../Core/Logging.h" 39 #include "../Core/Logging.h"
39 #include "../OrthancServer/DatabaseWrapper.h" 40 #include "../OrthancServer/DatabaseWrapper.h"
40 #include "../OrthancServer/ServerContext.h" 41 #include "../OrthancServer/ServerContext.h"
41 #include "../OrthancServer/ServerIndex.h" 42 #include "../OrthancServer/ServerIndex.h"
42 #include "../OrthancServer/Search/LookupIdentifierQuery.h" 43 #include "../OrthancServer/Search/LookupIdentifierQuery.h"
845 TEST(LookupIdentifierQuery, NormalizeIdentifier) 846 TEST(LookupIdentifierQuery, NormalizeIdentifier)
846 { 847 {
847 ASSERT_EQ("H^L.LO", ServerToolbox::NormalizeIdentifier(" Hé^l.LO %_ ")); 848 ASSERT_EQ("H^L.LO", ServerToolbox::NormalizeIdentifier(" Hé^l.LO %_ "));
848 ASSERT_EQ("1.2.840.113619.2.176.2025", ServerToolbox::NormalizeIdentifier(" 1.2.840.113619.2.176.2025 ")); 849 ASSERT_EQ("1.2.840.113619.2.176.2025", ServerToolbox::NormalizeIdentifier(" 1.2.840.113619.2.176.2025 "));
849 } 850 }
851
852
853 TEST(ServerIndex, Overwrite)
854 {
855 for (unsigned int i = 0; i < 2; i++)
856 {
857 bool overwrite = (i == 0);
858
859 MemoryStorageArea storage;
860 DatabaseWrapper db; // The SQLite DB is in memory
861 db.Open();
862 ServerContext context(db, storage, true /* running unit tests */);
863 context.SetupJobsEngine(true, false);
864 context.SetCompressionEnabled(true);
865
866 DicomMap instance;
867 instance.SetValue(DICOM_TAG_PATIENT_ID, "patient", false);
868 instance.SetValue(DICOM_TAG_PATIENT_NAME, "name", false);
869 instance.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, "study", false);
870 instance.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, "series", false);
871 instance.SetValue(DICOM_TAG_SOP_INSTANCE_UID, "sop", false);
872 instance.SetValue(DICOM_TAG_SOP_CLASS_UID, "1.2.840.10008.5.1.4.1.1.1", false); // CR image
873
874 DicomInstanceHasher hasher(instance);
875 std::string id = hasher.HashInstance();
876 context.GetIndex().SetOverwriteInstances(overwrite);
877
878 Json::Value tmp;
879 context.GetIndex().ComputeStatistics(tmp);
880 ASSERT_EQ(0, tmp["CountInstances"].asInt());
881 ASSERT_EQ(0, boost::lexical_cast<int>(tmp["TotalDiskSize"].asString()));
882
883 {
884 DicomInstanceToStore toStore;
885 toStore.SetSummary(instance);
886 toStore.SetOrigin(DicomInstanceOrigin::FromPlugins());
887
888 std::string id2;
889 ASSERT_EQ(StoreStatus_Success, context.Store(id2, toStore));
890 ASSERT_EQ(id, id2);
891 }
892
893 FileInfo dicom1, json1;
894 ASSERT_TRUE(context.GetIndex().LookupAttachment(dicom1, id, FileContentType_Dicom));
895 ASSERT_TRUE(context.GetIndex().LookupAttachment(json1, id, FileContentType_DicomAsJson));
896
897 context.GetIndex().ComputeStatistics(tmp);
898 ASSERT_EQ(1, tmp["CountInstances"].asInt());
899 ASSERT_EQ(dicom1.GetCompressedSize() + json1.GetCompressedSize(),
900 boost::lexical_cast<int>(tmp["TotalDiskSize"].asString()));
901 ASSERT_EQ(dicom1.GetUncompressedSize() + json1.GetUncompressedSize(),
902 boost::lexical_cast<int>(tmp["TotalUncompressedSize"].asString()));
903
904 context.ReadDicomAsJson(tmp, id);
905 ASSERT_EQ("name", tmp["0010,0010"]["Value"].asString());
906
907 {
908 ServerContext::DicomCacheLocker locker(context, id);
909 std::string tmp;
910 locker.GetDicom().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME);
911 ASSERT_EQ("name", tmp);
912 }
913
914 {
915 DicomMap instance2;
916 instance2.Assign(instance);
917 instance2.SetValue(DICOM_TAG_PATIENT_NAME, "overwritten", false);
918
919 DicomInstanceToStore toStore;
920 toStore.SetSummary(instance2);
921 toStore.SetOrigin(DicomInstanceOrigin::FromPlugins());
922
923 std::string id2;
924 ASSERT_EQ(overwrite ? StoreStatus_Success : StoreStatus_AlreadyStored, context.Store(id2, toStore));
925 ASSERT_EQ(id, id2);
926 }
927
928 FileInfo dicom2, json2;
929 ASSERT_TRUE(context.GetIndex().LookupAttachment(dicom2, id, FileContentType_Dicom));
930 ASSERT_TRUE(context.GetIndex().LookupAttachment(json2, id, FileContentType_DicomAsJson));
931
932 context.GetIndex().ComputeStatistics(tmp);
933 ASSERT_EQ(1, tmp["CountInstances"].asInt());
934 ASSERT_EQ(dicom2.GetCompressedSize() + json2.GetCompressedSize(),
935 boost::lexical_cast<int>(tmp["TotalDiskSize"].asString()));
936 ASSERT_EQ(dicom2.GetUncompressedSize() + json2.GetUncompressedSize(),
937 boost::lexical_cast<int>(tmp["TotalUncompressedSize"].asString()));
938
939 if (overwrite)
940 {
941 ASSERT_NE(dicom1.GetUuid(), dicom2.GetUuid());
942 ASSERT_NE(json1.GetUuid(), json2.GetUuid());
943 ASSERT_NE(dicom1.GetUncompressedSize(), dicom2.GetUncompressedSize());
944 ASSERT_NE(json1.GetUncompressedSize(), json2.GetUncompressedSize());
945
946 context.ReadDicomAsJson(tmp, id);
947 ASSERT_EQ("overwritten", tmp["0010,0010"]["Value"].asString());
948
949 {
950 ServerContext::DicomCacheLocker locker(context, id);
951 std::string tmp;
952 locker.GetDicom().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME);
953 ASSERT_EQ("overwritten", tmp);
954 }
955 }
956 else
957 {
958 ASSERT_EQ(dicom1.GetUuid(), dicom2.GetUuid());
959 ASSERT_EQ(json1.GetUuid(), json2.GetUuid());
960 ASSERT_EQ(dicom1.GetUncompressedSize(), dicom2.GetUncompressedSize());
961 ASSERT_EQ(json1.GetUncompressedSize(), json2.GetUncompressedSize());
962
963 context.ReadDicomAsJson(tmp, id);
964 ASSERT_EQ("name", tmp["0010,0010"]["Value"].asString());
965
966 {
967 ServerContext::DicomCacheLocker locker(context, id);
968 std::string tmp;
969 locker.GetDicom().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME);
970 ASSERT_EQ("name", tmp);
971 }
972 }
973
974 context.Stop();
975 db.Close();
976 }
977 }
978
979