annotate SQLite/UnitTests/UnitTestsMain.cpp @ 201:42990b2dd51b

create IDatabaseBackendOutput only if needed
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 18 Mar 2021 16:51:51 +0100
parents 3236894320d6
children b40b30075c51
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
193
3236894320d6 upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 157
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
22 #include "../../Framework/SQLite/SQLiteDatabase.h"
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "../Plugins/SQLiteIndex.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
25 #include <Compatibility.h> // For std::unique_ptr<>
152
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
26 #include <Logging.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
27 #include <SystemToolbox.h>
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <gtest/gtest.h>
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
32 #include "../../Framework/Plugins/IndexUnitTests.h"
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
33
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 TEST(SQLiteIndex, Lock)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 // No locking if using memory backend
201
42990b2dd51b create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
39 OrthancDatabases::SQLiteIndex db1(NULL);
42990b2dd51b create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
40 OrthancDatabases::SQLiteIndex db2(NULL);
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 db1.Open();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 db2.Open();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 Orthanc::SystemToolbox::RemoveFile("index.db");
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
201
42990b2dd51b create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
49 OrthancDatabases::SQLiteIndex db1(NULL, "index.db");
42990b2dd51b create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
50 OrthancDatabases::SQLiteIndex db2(NULL, "index.db");
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 db1.Open();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 ASSERT_THROW(db2.Open(), Orthanc::OrthancException);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
201
42990b2dd51b create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
57 OrthancDatabases::SQLiteIndex db3(NULL, "index.db");
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 db3.Open();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
63 TEST(SQLite, ImplicitTransaction)
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
64 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
65 OrthancDatabases::SQLiteDatabase db;
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
66 db.OpenInMemory();
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
67
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
68 ASSERT_FALSE(db.DoesTableExist("test"));
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
69 ASSERT_FALSE(db.DoesTableExist("test2"));
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
70
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
71 {
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
72 std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(false));
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
73 ASSERT_FALSE(t->IsImplicit());
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
74 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
75
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
76 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
77 OrthancDatabases::Query query("CREATE TABLE test(id INT)", false);
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
78 std::unique_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
79
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
80 std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
81 ASSERT_TRUE(t->IsImplicit());
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
82 ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
83 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
84
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
85 OrthancDatabases::Dictionary args;
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
86 t->ExecuteWithoutResult(*s, args);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
87 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
88 t->Commit();
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
89
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
90 ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
91 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
92
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
93 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
94 // An implicit transaction does not need to be explicitely committed
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
95 OrthancDatabases::Query query("CREATE TABLE test2(id INT)", false);
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
96 std::unique_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
97
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
98 std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
99
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
100 OrthancDatabases::Dictionary args;
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
101 t->ExecuteWithoutResult(*s, args);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
102 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
103
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
104 ASSERT_TRUE(db.DoesTableExist("test"));
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
105 ASSERT_TRUE(db.DoesTableExist("test2"));
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
106 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
107
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 int main(int argc, char **argv)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 ::testing::InitGoogleTest(&argc, argv);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 Orthanc::Logging::Initialize();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 Orthanc::Logging::EnableInfoLevel(true);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 Orthanc::Logging::EnableTraceLevel(true);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 int result = RUN_ALL_TESTS();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 Orthanc::Logging::Finalize();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 return result;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 }