annotate UnitTests/SQLiteChromium.cpp @ 523:68451838fb2c dicom-rt

dicom-rt branch
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Aug 2013 15:10:05 +0200
parents f333c0398f6e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #include "gtest/gtest.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 #include "../Core/Toolbox.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 #include "../Core/SQLite/Connection.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 #include "../Core/SQLite/Statement.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 #include "../Core/SQLite/Transaction.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 #include <sqlite3.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
10
63
f1d0470ff334 renaming unittests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
11 using namespace Orthanc;
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
12 using namespace Orthanc::SQLite;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 /********************************************************************
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 ** Tests from
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/connection_unittest.cc
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 ********************************************************************/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 class SQLConnectionTest : public testing::Test
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 public:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 SQLConnectionTest()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 virtual ~SQLConnectionTest()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 virtual void SetUp()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 db_.OpenInMemory();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 virtual void TearDown()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 db_.Close();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
41 Connection& db()
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 return db_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 private:
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
47 Connection db_;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 TEST_F(SQLConnectionTest, Execute)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 // Valid statement should return true.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 EXPECT_EQ(SQLITE_OK, db().GetErrorCode());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 // Invalid statement should fail.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 ASSERT_EQ(SQLITE_ERROR,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 TEST_F(SQLConnectionTest, ExecuteWithErrorCode) {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 ASSERT_EQ(SQLITE_OK,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 ASSERT_EQ(SQLITE_ERROR,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 ASSERT_EQ(SQLITE_ERROR,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 db().ExecuteAndReturnErrorCode(
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
71 "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)"));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 TEST_F(SQLConnectionTest, CachedStatement) {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
75 StatementId id1("foo", 12);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 // Create a new cached statement.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
81 Statement s(db(), id1, "SELECT a FROM foo");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 ASSERT_TRUE(s.Step());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 EXPECT_EQ(12, s.ColumnInt(0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 // The statement should be cached still.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 EXPECT_TRUE(db().HasCachedStatement(id1));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 // Get the same statement using different SQL. This should ignore our
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 // SQL and use the cached one (so it will be valid).
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
92 Statement s(db(), id1, "something invalid(");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 ASSERT_TRUE(s.Step());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 EXPECT_EQ(12, s.ColumnInt(0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 // Make sure other statements aren't marked as cached.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 EXPECT_FALSE(db().HasCachedStatement(SQLITE_FROM_HERE));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 TEST_F(SQLConnectionTest, IsSQLValidTest) {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 TEST_F(SQLConnectionTest, DoesStuffExist) {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 // Test DoesTableExist.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 EXPECT_FALSE(db().DoesTableExist("foo"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 EXPECT_TRUE(db().DoesTableExist("foo"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 // Should be case sensitive.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 EXPECT_FALSE(db().DoesTableExist("FOO"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 // Test DoesColumnExist.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 EXPECT_FALSE(db().DoesColumnExist("foo", "bar"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 EXPECT_TRUE(db().DoesColumnExist("foo", "a"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 // Testing for a column on a nonexistent table.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 EXPECT_FALSE(db().DoesColumnExist("bar", "b"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 TEST_F(SQLConnectionTest, GetLastInsertRowId) {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 // Last insert row ID should be valid.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 int64_t row = db().GetLastInsertRowId();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 EXPECT_LT(0, row);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 // It should be the primary key of the row we just inserted.
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
136 Statement s(db(), "SELECT value FROM foo WHERE id=?");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 s.BindInt64(0, row);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 ASSERT_TRUE(s.Step());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 EXPECT_EQ(12, s.ColumnInt(0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 TEST_F(SQLConnectionTest, Rollback) {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 ASSERT_TRUE(db().BeginTransaction());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 ASSERT_TRUE(db().BeginTransaction());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 EXPECT_EQ(2, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 db().RollbackTransaction();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 EXPECT_FALSE(db().CommitTransaction());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 EXPECT_TRUE(db().BeginTransaction());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 /********************************************************************
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 ** Tests from
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/statement_unittest.cc
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 ********************************************************************/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
159 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
161 namespace SQLite
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
162 {
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
163 class SQLStatementTest : public SQLConnectionTest
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
164 {
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
165 };
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
167 TEST_F(SQLStatementTest, Run) {
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
168 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
169 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
171 Statement s(db(), "SELECT b FROM foo WHERE a=?");
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
172 // Stepping it won't work since we haven't bound the value.
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
173 EXPECT_FALSE(s.Step());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
175 // Run should fail since this produces output, and we should use Step(). This
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
176 // gets a bit wonky since sqlite says this is OK so succeeded is set.
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
177 s.Reset(true);
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
178 s.BindInt(0, 3);
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
179 EXPECT_FALSE(s.Run());
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
180 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
182 // Resetting it should put it back to the previous state (not runnable).
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
183 s.Reset(true);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
185 // Binding and stepping should produce one row.
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
186 s.BindInt(0, 3);
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
187 EXPECT_TRUE(s.Step());
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
188 EXPECT_EQ(12, s.ColumnInt(0));
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
189 EXPECT_FALSE(s.Step());
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
190 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
192 TEST_F(SQLStatementTest, BasicErrorCallback) {
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
193 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
194 // Insert in the foo table the primary key. It is an error to insert
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
195 // something other than an number. This error causes the error callback
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
196 // handler to be called with SQLITE_MISMATCH as error code.
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
197 Statement s(db(), "INSERT INTO foo (a) VALUES (?)");
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
198 s.BindCString(0, "bad bad");
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
199 EXPECT_THROW(s.Run(), OrthancException);
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
200 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
202 TEST_F(SQLStatementTest, Reset) {
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
203 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
204 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
205 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
207 Statement s(db(), "SELECT b FROM foo WHERE a = ? ");
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
208 s.BindInt(0, 3);
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
209 ASSERT_TRUE(s.Step());
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
210 EXPECT_EQ(12, s.ColumnInt(0));
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
211 ASSERT_FALSE(s.Step());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
213 s.Reset(false);
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
214 // Verify that we can get all rows again.
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
215 ASSERT_TRUE(s.Step());
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
216 EXPECT_EQ(12, s.ColumnInt(0));
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
217 EXPECT_FALSE(s.Step());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
219 s.Reset(true);
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
220 ASSERT_FALSE(s.Step());
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
221 }
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
222 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 /********************************************************************
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 ** Tests from
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/transaction_unittest.cc
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 ********************************************************************/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 class SQLTransactionTest : public SQLConnectionTest
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 public:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 virtual void SetUp()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 SQLConnectionTest::SetUp();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 // Returns the number of rows in table "foo".
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 int CountFoo()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
247 Statement count(db(), "SELECT count(*) FROM foo");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 count.Step();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 return count.ColumnInt(0);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 TEST_F(SQLTransactionTest, Commit) {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
256 Transaction t(db());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 EXPECT_FALSE(t.IsOpen());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 t.Begin();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 EXPECT_TRUE(t.IsOpen());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 t.Commit();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 EXPECT_FALSE(t.IsOpen());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 EXPECT_EQ(1, CountFoo());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 TEST_F(SQLTransactionTest, Rollback) {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 // Test some basic initialization, and that rollback runs when you exit the
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 // scope.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
274 Transaction t(db());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 EXPECT_FALSE(t.IsOpen());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 t.Begin();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 EXPECT_TRUE(t.IsOpen());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 // Nothing should have been committed since it was implicitly rolled back.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 EXPECT_EQ(0, CountFoo());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 // Test explicit rollback.
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
286 Transaction t2(db());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 EXPECT_FALSE(t2.IsOpen());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 t2.Begin();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 t2.Rollback();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 EXPECT_FALSE(t2.IsOpen());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 // Nothing should have been committed since it was explicitly rolled back.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 EXPECT_EQ(0, CountFoo());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 // Rolling back any part of a transaction should roll back all of them.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 TEST_F(SQLTransactionTest, NestedRollback) {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 EXPECT_EQ(0, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 // Outermost transaction.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
304 Transaction outer(db());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 outer.Begin();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 EXPECT_EQ(1, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 // The first inner one gets committed.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
310 Transaction inner1(db());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 inner1.Begin();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 EXPECT_EQ(2, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 inner1.Commit();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 EXPECT_EQ(1, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 // One row should have gotten inserted.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 EXPECT_EQ(1, CountFoo());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 // The second inner one gets rolled back.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
324 Transaction inner2(db());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 inner2.Begin();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 EXPECT_EQ(2, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 inner2.Rollback();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 EXPECT_EQ(1, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 // A third inner one will fail in Begin since one has already been rolled
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 // back.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 EXPECT_EQ(1, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 {
138
f333c0398f6e some hiding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 63
diff changeset
337 Transaction inner3(db());
63
f1d0470ff334 renaming unittests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
338 EXPECT_THROW(inner3.Begin(), OrthancException);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 EXPECT_EQ(1, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 EXPECT_EQ(0, db().GetTransactionNesting());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 EXPECT_EQ(0, CountFoo());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 }