aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit
diff options
context:
space:
mode:
authorMarco Bubke <marco.bubke@qt.io>2020-05-27 23:48:03 +0200
committerTim Jenssen <tim.jenssen@qt.io>2020-06-05 09:52:42 +0000
commita61eff007900ba572a40fc4116e1f217cd83c36f (patch)
treee22d219041138ba9707d98862cd011b4608ef3b4 /tests/unit
parentd6b1281a653bcd3c3fcf9595b471c88220bb210c (diff)
Sqlite: Add session support
Session are captured by hooking in the sqlite changes. They are saved in blobs and containing inserts, update and deletes. Because the are semantically coupled to translactions we add a Change-Id: Ie095558ebc50601fcaae32958ebeeb98b72d73b9 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/unittest/createtablesqlstatementbuilder-test.cpp20
-rw-r--r--tests/unit/unittest/data/sqlite_database.dbbin4096 -> 12288 bytes
-rw-r--r--tests/unit/unittest/gtest-creator-printing.cpp87
-rw-r--r--tests/unit/unittest/gtest-creator-printing.h2
-rw-r--r--tests/unit/unittest/mocksqlitedatabase.h4
-rw-r--r--tests/unit/unittest/mocksqlitetransactionbackend.h3
-rw-r--r--tests/unit/unittest/sqlitedatabase-test.cpp43
-rw-r--r--tests/unit/unittest/sqlitesessions-test.cpp443
-rw-r--r--tests/unit/unittest/sqlitestatement-test.cpp29
-rw-r--r--tests/unit/unittest/sqlitetransaction-test.cpp59
-rw-r--r--tests/unit/unittest/unittest.pro1
-rw-r--r--tests/unit/unittest/unittests-main.cpp3
12 files changed, 671 insertions, 23 deletions
diff --git a/tests/unit/unittest/createtablesqlstatementbuilder-test.cpp b/tests/unit/unittest/createtablesqlstatementbuilder-test.cpp
index 33231d92c3..49099081a0 100644
--- a/tests/unit/unittest/createtablesqlstatementbuilder-test.cpp
+++ b/tests/unit/unittest/createtablesqlstatementbuilder-test.cpp
@@ -510,4 +510,24 @@ TEST_F(CreateTableSqlStatementBuilder, GeneratedAlwaysVirtual)
"VIRTUAL)");
}
+TEST_F(CreateTableSqlStatementBuilder, PrimaryKeyAutoincrement)
+{
+ builder.clear();
+ builder.setTableName("test");
+
+ builder.addColumn("id", ColumnType::Integer, {Sqlite::PrimaryKey{Sqlite::AutoIncrement::Yes}});
+
+ ASSERT_THAT(builder.sqlStatement(), "CREATE TABLE test(id INTEGER PRIMARY KEY AUTOINCREMENT)");
+}
+
+TEST_F(CreateTableSqlStatementBuilder, BlobType)
+{
+ builder.clear();
+ builder.setTableName("test");
+
+ builder.addColumn("data", ColumnType::Blob);
+
+ ASSERT_THAT(builder.sqlStatement(), "CREATE TABLE test(data BLOB)");
+}
+
} // namespace
diff --git a/tests/unit/unittest/data/sqlite_database.db b/tests/unit/unittest/data/sqlite_database.db
index 9c5879579e..5a7284d087 100644
--- a/tests/unit/unittest/data/sqlite_database.db
+++ b/tests/unit/unittest/data/sqlite_database.db
Binary files differ
diff --git a/tests/unit/unittest/gtest-creator-printing.cpp b/tests/unit/unittest/gtest-creator-printing.cpp
index 6d5f7a2cf3..eab506e344 100644
--- a/tests/unit/unittest/gtest-creator-printing.cpp
+++ b/tests/unit/unittest/gtest-creator-printing.cpp
@@ -58,6 +58,7 @@
#include <sourcedependency.h>
#include <sourcelocationentry.h>
#include <sourcelocationscontainer.h>
+#include <sqlitesessionchangeset.h>
#include <sqlitevalue.h>
#include <symbol.h>
#include <symbolentry.h>
@@ -67,6 +68,8 @@
#include <usedmacro.h>
#include <utils/link.h>
+#include <sqlite3ext.h>
+
namespace {
ClangBackEnd::FilePathCaching *filePathCache = nullptr;
}
@@ -321,6 +324,90 @@ std::ostream &operator<<(std::ostream &out, const Value &value)
return out << ")";
}
+
+namespace {
+Utils::SmallStringView operationText(int operation)
+{
+ switch (operation) {
+ case SQLITE_INSERT:
+ return "INSERT";
+ case SQLITE_UPDATE:
+ return "UPDATE";
+ case SQLITE_DELETE:
+ return "DELETE";
+ }
+
+ return {};
+}
+
+std::ostream &operator<<(std::ostream &out, sqlite3_changeset_iter *iter)
+{
+ out << "(";
+
+ const char *tableName = nullptr;
+ int columns = 0;
+ int operation = 0;
+ sqlite3_value *value = nullptr;
+
+ sqlite3changeset_op(iter, &tableName, &columns, &operation, 0);
+
+ out << operationText(operation) << " " << tableName << " {";
+
+ if (operation == SQLITE_UPDATE || operation == SQLITE_DELETE) {
+ out << "Old: [";
+
+ for (int i = 0; i < columns; i++) {
+ sqlite3changeset_old(iter, i, &value);
+
+ if (value)
+ out << " " << sqlite3_value_text(value);
+ else
+ out << " -";
+ }
+ out << "]";
+ }
+
+ if (operation == SQLITE_UPDATE)
+ out << ", ";
+
+ if (operation == SQLITE_UPDATE || operation == SQLITE_INSERT) {
+ out << "New: [";
+ for (int i = 0; i < columns; i++) {
+ sqlite3changeset_new(iter, i, &value);
+
+ if (value)
+ out << " " << sqlite3_value_text(value);
+ else
+ out << " -";
+ }
+ out << "]";
+ }
+
+ out << "})";
+
+ return out;
+}
+} // namespace
+
+std::ostream &operator<<(std::ostream &out, const SessionChangeSet &changeset)
+{
+ sqlite3_changeset_iter *iter = nullptr;
+ sqlite3changeset_start(&iter, changeset.size, const_cast<void *>(changeset.data));
+
+ out << "ChangeSets([";
+
+ if (SQLITE_ROW == sqlite3changeset_next(iter)) {
+ out << iter;
+ while (SQLITE_ROW == sqlite3changeset_next(iter))
+ out << ", " << iter;
+ }
+
+ sqlite3changeset_finalize(iter);
+
+ out << "])";
+
+ return out;
+}
} // namespace Sqlite
namespace ClangBackEnd {
diff --git a/tests/unit/unittest/gtest-creator-printing.h b/tests/unit/unittest/gtest-creator-printing.h
index ea33bc9d49..c791290ae2 100644
--- a/tests/unit/unittest/gtest-creator-printing.h
+++ b/tests/unit/unittest/gtest-creator-printing.h
@@ -66,8 +66,10 @@ void PrintTo(const TextRange &range, ::std::ostream *os);
namespace Sqlite {
class Value;
+class SessionChangeSet;
std::ostream &operator<<(std::ostream &out, const Value &value);
+std::ostream &operator<<(std::ostream &out, const SessionChangeSet &changeset);
} // namespace Sqlite
namespace ProjectExplorer {
diff --git a/tests/unit/unittest/mocksqlitedatabase.h b/tests/unit/unittest/mocksqlitedatabase.h
index 97d5394339..c627f995a4 100644
--- a/tests/unit/unittest/mocksqlitedatabase.h
+++ b/tests/unit/unittest/mocksqlitedatabase.h
@@ -63,5 +63,9 @@ public:
MOCK_METHOD1(setUpdateHook, void(Sqlite::DatabaseInterface::UpdateCallback &));
MOCK_METHOD0(resetUpdateHook, void());
+
+ MOCK_METHOD0(applyAndUpdateSessions, void());
+
+ MOCK_METHOD1(setAttachedTables, void(const Utils::SmallStringVector &tables));
};
diff --git a/tests/unit/unittest/mocksqlitetransactionbackend.h b/tests/unit/unittest/mocksqlitetransactionbackend.h
index 0cfeee2892..363ad573d7 100644
--- a/tests/unit/unittest/mocksqlitetransactionbackend.h
+++ b/tests/unit/unittest/mocksqlitetransactionbackend.h
@@ -40,4 +40,7 @@ public:
MOCK_METHOD0(rollback, void ());
MOCK_METHOD0(lock, void ());
MOCK_METHOD0(unlock, void ());
+ MOCK_METHOD0(immediateSessionBegin, void());
+ MOCK_METHOD0(sessionCommit, void());
+ MOCK_METHOD0(sessionRollback, void());
};
diff --git a/tests/unit/unittest/sqlitedatabase-test.cpp b/tests/unit/unittest/sqlitedatabase-test.cpp
index ff3436c459..5d7fdb1e54 100644
--- a/tests/unit/unittest/sqlitedatabase-test.cpp
+++ b/tests/unit/unittest/sqlitedatabase-test.cpp
@@ -28,6 +28,7 @@
#include "spydummy.h"
#include <sqlitedatabase.h>
+#include <sqlitereadstatement.h>
#include <sqlitetable.h>
#include <sqlitewritestatement.h>
#include <utf8string.h>
@@ -50,26 +51,33 @@ using Sqlite::Table;
class SqliteDatabase : public ::testing::Test
{
protected:
- void SetUp() override
+ SqliteDatabase()
{
database.setJournalMode(JournalMode::Memory);
database.setDatabaseFilePath(databaseFilePath);
auto &table = database.addTable();
table.setName("test");
+ table.addColumn("id", Sqlite::ColumnType::Integer, {Sqlite::PrimaryKey{}});
table.addColumn("name");
database.open();
}
- void TearDown() override
+ ~SqliteDatabase()
{
if (database.isOpen())
database.close();
}
+ std::vector<Utils::SmallString> names() const
+ {
+ return Sqlite::ReadStatement("SELECT name FROM test", database).values<Utils::SmallString>(8);
+ }
+
+protected:
SpyDummy spyDummy;
QString databaseFilePath{":memory:"};
- Sqlite::Database database;
+ mutable Sqlite::Database database;
Sqlite::TransactionInterface &transactionInterface = database;
MockFunction<void(Sqlite::ChangeType tupe, char const *, char const *, long long)> callbackMock;
Sqlite::Database::UpdateCallback callback = callbackMock.AsStdFunction();
@@ -307,4 +315,33 @@ TEST_F(SqliteDatabase, TableUpdateHookCall)
Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42);
}
+TEST_F(SqliteDatabase, SessionsCommit)
+{
+ database.setAttachedTables({"test"});
+ Sqlite::WriteStatement("INSERT INTO test(id, name) VALUES (?,?)", database).write(1, "foo");
+
+ Sqlite::ImmediateSessionTransaction transaction{database};
+ Sqlite::WriteStatement("INSERT INTO test(id, name) VALUES (?,?)", database).write(2, "bar");
+ transaction.commit();
+ Sqlite::WriteStatement("INSERT OR REPLACE INTO test(id, name) VALUES (?,?)", database).write(2, "hoo");
+ database.applyAndUpdateSessions();
+
+ ASSERT_THAT(names(), ElementsAre("foo", "bar"));
+}
+
+TEST_F(SqliteDatabase, SessionsRollback)
+{
+ database.setAttachedTables({"test"});
+ Sqlite::WriteStatement("INSERT INTO test(id, name) VALUES (?,?)", database).write(1, "foo");
+
+ {
+ Sqlite::ImmediateSessionTransaction transaction{database};
+ Sqlite::WriteStatement("INSERT INTO test(id, name) VALUES (?,?)", database).write(2, "bar");
+ }
+ Sqlite::WriteStatement("INSERT OR REPLACE INTO test(id, name) VALUES (?,?)", database).write(2, "hoo");
+ database.applyAndUpdateSessions();
+
+ ASSERT_THAT(names(), ElementsAre("foo", "hoo"));
+}
+
} // namespace
diff --git a/tests/unit/unittest/sqlitesessions-test.cpp b/tests/unit/unittest/sqlitesessions-test.cpp
new file mode 100644
index 0000000000..f10dde1bc8
--- /dev/null
+++ b/tests/unit/unittest/sqlitesessions-test.cpp
@@ -0,0 +1,443 @@
+/****************************************************************************
+**
+** Copyright (C) 2020 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+****************************************************************************/
+
+#include "googletest.h"
+
+#include <sqlitedatabase.h>
+#include <sqlitereadstatement.h>
+#include <sqlitesessionchangeset.h>
+#include <sqlitesessions.h>
+#include <sqlitetransaction.h>
+#include <sqlitewritestatement.h>
+
+#include <ostream>
+
+namespace {
+
+using Sqlite::SessionChangeSet;
+using Sqlite::SessionChangeSets;
+
+class DatabaseExecute
+{
+public:
+ DatabaseExecute(Utils::SmallStringView sqlStatement, Sqlite::Database &database)
+ {
+ database.execute(sqlStatement);
+ }
+};
+
+class Data
+{
+public:
+ Data(Sqlite::ValueView name, Sqlite::ValueView number, Sqlite::ValueView value)
+ : name(name)
+ , number(number)
+ , value(value)
+ {}
+
+ Sqlite::Value name;
+ Sqlite::Value number;
+ Sqlite::Value value;
+};
+
+std::ostream &operator<<(std::ostream &out, const Data &data)
+{
+ return out << "(" << data.name << ", " << data.number << " " << data.value << ")";
+}
+
+MATCHER_P3(HasData,
+ name,
+ number,
+ value,
+ std::string(negation ? "hasn't " : "has ") + PrintToString(name) + ", "
+ + PrintToString(number) + ", " + PrintToString(value))
+{
+ const Data &data = arg;
+
+ return data.name == name && data.number == number && data.value == value;
+}
+
+class Tag
+{
+public:
+ Tag(Sqlite::ValueView name, Sqlite::ValueView tag)
+ : name(name)
+ , tag(tag)
+ {}
+
+ Sqlite::Value name;
+ Sqlite::Value tag;
+};
+
+std::ostream &operator<<(std::ostream &out, const Tag &tag)
+{
+ return out << "(" << tag.name << ", " << tag.tag << ")";
+}
+
+MATCHER_P2(HasTag,
+ name,
+ tag,
+ std::string(negation ? "hasn't " : "has ") + PrintToString(name) + ", " + PrintToString(tag))
+{
+ const Tag &t = arg;
+
+ return t.name == name && t.tag == tag;
+}
+
+class Sessions : public testing::Test
+{
+protected:
+ Sessions() { sessions.setAttachedTables({"data", "tags"}); }
+
+ std::vector<Data> fetchData() { return selectData.values<Data, 3>(8); }
+ std::vector<Tag> fetchTags() { return selectTags.values<Tag, 2>(8); }
+ SessionChangeSets fetchChangeSets() { return selectChangeSets.values<SessionChangeSet>(8); }
+
+protected:
+ Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory};
+ DatabaseExecute createTable{"CREATE TABLE data(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT "
+ "UNIQUE, number NUMERIC, value NUMERIC)",
+ database};
+ DatabaseExecute createTable2{"CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, dataId "
+ "INTEGER NOT NULL REFERENCES data ON DELETE CASCADE DEFERRABLE "
+ "INITIALLY DEFERRED, tag NUMERIC)",
+ database};
+ Sqlite::Sessions sessions{database, "main", "testsessions"};
+ Sqlite::WriteStatement insertData{"INSERT INTO data(name, number, value) VALUES (?1, ?2, ?3) "
+ "ON CONFLICT (name) DO UPDATE SET (number, value) = (?2, ?3)",
+ database};
+ Sqlite::WriteStatement updateNumber{"UPDATE data SET number = ?002 WHERE name=?001", database};
+ Sqlite::WriteStatement updateValue{"UPDATE data SET value = ?002 WHERE name=?001", database};
+ Sqlite::WriteStatement deleteData{"DELETE FROM data WHERE name=?", database};
+ Sqlite::WriteStatement deleteTag{
+ "DELETE FROM tags WHERE dataId=(SELECT id FROM data WHERE name=?)", database};
+ Sqlite::WriteStatement insertTag{
+ "INSERT INTO tags(dataId, tag) VALUES ((SELECT id FROM data WHERE name=?1), ?2) ", database};
+ Sqlite::ReadStatement selectData{"SELECT name, number, value FROM data", database};
+ Sqlite::ReadStatement selectTags{"SELECT name, tag FROM tags JOIN data ON data.id=tags.dataId",
+ database};
+ Sqlite::ReadStatement selectChangeSets{"SELECT changeset FROM testsessions", database};
+};
+
+TEST_F(Sessions, DontThrowForCommittingWithoutSessionStart)
+{
+ ASSERT_NO_THROW(sessions.commit());
+}
+
+TEST_F(Sessions, CreateEmptySession)
+{
+ sessions.create();
+ sessions.commit();
+
+ ASSERT_THAT(fetchChangeSets(), IsEmpty());
+}
+
+TEST_F(Sessions, CreateSessionWithInsert)
+{
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+
+ ASSERT_THAT(fetchChangeSets(), SizeIs(1));
+}
+
+TEST_F(Sessions, CreateSessionWithUpdate)
+{
+ insertData.write("foo", 22, 3.14);
+
+ sessions.create();
+ updateNumber.write("foo", "bar");
+ sessions.commit();
+
+ ASSERT_THAT(fetchChangeSets(), SizeIs(1));
+}
+
+TEST_F(Sessions, CreateSessionWithDelete)
+{
+ insertData.write("foo", 22, 3.14);
+
+ sessions.create();
+ deleteData.write("foo");
+ sessions.commit();
+
+ ASSERT_THAT(fetchChangeSets(), SizeIs(1));
+}
+
+TEST_F(Sessions, CreateSessionWithInsertAndUpdate)
+{
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+
+ sessions.create();
+ updateNumber.write("foo", "bar");
+ sessions.commit();
+
+ ASSERT_THAT(fetchChangeSets(), SizeIs(2));
+}
+
+TEST_F(Sessions, CreateSession)
+{
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+
+ sessions.commit();
+
+ ASSERT_THAT(fetchChangeSets(), SizeIs(1));
+}
+
+TEST_F(Sessions, RevertSession)
+{
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+
+ sessions.revert();
+
+ ASSERT_THAT(fetchData(), IsEmpty());
+}
+
+TEST_F(Sessions, RevertSessionToBase)
+{
+ insertData.write("bar", "foo", 99);
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+
+ sessions.revert();
+
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("bar", "foo", 99)));
+}
+
+TEST_F(Sessions, RevertMultipleSession)
+{
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+ sessions.create();
+ updateNumber.write("foo", "bar");
+ sessions.commit();
+
+ sessions.revert();
+
+ ASSERT_THAT(fetchData(), IsEmpty());
+}
+
+TEST_F(Sessions, ApplySession)
+{
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", 22, 3.14)));
+}
+
+TEST_F(Sessions, ApplySessionAfterAddingNewEntries)
+{
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+ insertData.write("bar", "foo", 99);
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchData(),
+ UnorderedElementsAre(HasData("foo", 22, 3.14), HasData("bar", "foo", 99)));
+}
+
+TEST_F(Sessions, ApplyOverridesEntriesWithUniqueConstraint)
+{
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+ insertData.write("foo", "bar", 3.14);
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", 22, 3.14)));
+}
+
+TEST_F(Sessions, ApplyDoesNotOverrideDeletedEntries)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ insertData.write("foo", 22, 3.14);
+ sessions.commit();
+ deleteData.write("foo");
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchData(), IsEmpty());
+}
+
+TEST_F(Sessions, ApplyDoesOnlyOverwriteUpdatedValues)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ updateValue.write("foo", 1234);
+ sessions.commit();
+ insertData.write("foo", "poo", 891);
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "poo", 1234)));
+}
+
+TEST_F(Sessions, ApplyDoesDoesNotOverrideForeignKeyIfReferenceIsDeleted)
+{
+ insertData.write("foo2", "bar", 3.14);
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ insertTag.write("foo2", 4321);
+ insertTag.write("foo", 1234);
+ sessions.commit();
+ deleteData.write("foo");
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchTags(), ElementsAre(HasTag("foo2", 4321)));
+}
+
+TEST_F(Sessions, ApplyDoesDoesNotOverrideIfConstraintsIsApplied)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ deleteData.write("foo");
+ sessions.commit();
+ sessions.revert();
+ insertTag.write("foo", 1234);
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchTags(), IsEmpty());
+}
+
+TEST_F(Sessions, ApplyDoesDoesNotOverrideForeignKeyIfReferenceIsDeletedDeferred)
+{
+ Sqlite::DeferredTransaction transaction{database};
+ insertData.write("foo2", "bar", 3.14);
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ insertTag.write("foo2", 4321);
+ insertTag.write("foo", 1234);
+ sessions.commit();
+ deleteData.write("foo");
+
+ sessions.apply();
+
+ transaction.commit();
+ ASSERT_THAT(fetchTags(), ElementsAre(HasTag("foo2", 4321)));
+}
+
+TEST_F(Sessions, EndSessionOnRollback)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ updateValue.write("foo", 99);
+ sessions.rollback();
+ sessions.commit();
+ sessions.create();
+ updateNumber.write("foo", 333);
+ sessions.commit();
+ updateValue.write("foo", 666);
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", 333, 666)));
+}
+
+TEST_F(Sessions, EndSessionOnCommit)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ updateValue.write("foo", 99);
+ sessions.commit();
+ updateValue.write("foo", 666);
+ sessions.commit();
+
+ sessions.apply();
+
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "bar", 99)));
+}
+
+TEST_F(Sessions, DeleteSessions)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ updateValue.write("foo", 99);
+ sessions.commit();
+ sessions.revert();
+
+ sessions.deleteAll();
+
+ sessions.apply();
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "bar", 3.14)));
+}
+
+TEST_F(Sessions, DeleteAllSessions)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ updateValue.write("foo", 99);
+ sessions.commit();
+ sessions.revert();
+
+ sessions.deleteAll();
+
+ sessions.apply();
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "bar", 3.14)));
+}
+
+TEST_F(Sessions, ApplyAndUpdateSessions)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ updateValue.write("foo", 99);
+ sessions.commit();
+ updateValue.write("foo", 99);
+
+ sessions.applyAndUpdateSessions();
+
+ updateValue.write("foo", 22);
+ sessions.apply();
+ ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "bar", 22)));
+}
+
+TEST_F(Sessions, ApplyAndUpdateSessionsHasOnlyOneChangeSet)
+{
+ insertData.write("foo", "bar", 3.14);
+ sessions.create();
+ updateValue.write("foo", 99);
+ sessions.commit();
+ updateValue.write("foo", 99);
+
+ sessions.applyAndUpdateSessions();
+
+ ASSERT_THAT(fetchChangeSets(), SizeIs(1));
+}
+
+} // namespace
diff --git a/tests/unit/unittest/sqlitestatement-test.cpp b/tests/unit/unittest/sqlitestatement-test.cpp
index 4b59d52af2..a7dd5d5f9b 100644
--- a/tests/unit/unittest/sqlitestatement-test.cpp
+++ b/tests/unit/unittest/sqlitestatement-test.cpp
@@ -460,9 +460,8 @@ TEST_F(SqliteStatement, GetTupleValuesWithoutArguments)
auto values = statement.values<Tuple, 3>(3);
- ASSERT_THAT(values, ElementsAre(Tuple{"bar", 0, 1},
- Tuple{"foo", 23.3, 2},
- Tuple{"poo", 40.0, 3}));
+ ASSERT_THAT(values,
+ UnorderedElementsAre(Tuple{"bar", 0, 1}, Tuple{"foo", 23.3, 2}, Tuple{"poo", 40.0, 3}));
}
TEST_F(SqliteStatement, GetSingleValuesWithoutArguments)
@@ -471,7 +470,7 @@ TEST_F(SqliteStatement, GetSingleValuesWithoutArguments)
std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3);
- ASSERT_THAT(values, ElementsAre("bar", "foo", "poo"));
+ ASSERT_THAT(values, UnorderedElementsAre("bar", "foo", "poo"));
}
class FooValue
@@ -497,7 +496,7 @@ TEST_F(SqliteStatement, GetSingleSqliteValuesWithoutArguments)
std::vector<FooValue> values = statement.values<FooValue>(3);
- ASSERT_THAT(values, ElementsAre(Eq("blah"), Eq(23.3), Eq(40), IsNull()));
+ ASSERT_THAT(values, UnorderedElementsAre(Eq("blah"), Eq(23.3), Eq(40), IsNull()));
}
TEST_F(SqliteStatement, GetStructValuesWithoutArguments)
@@ -506,9 +505,10 @@ TEST_F(SqliteStatement, GetStructValuesWithoutArguments)
auto values = statement.values<Output, 3>(3);
- ASSERT_THAT(values, ElementsAre(Output{"bar", "blah", 1},
- Output{"foo", "23.3", 2},
- Output{"poo", "40", 3}));
+ ASSERT_THAT(values,
+ UnorderedElementsAre(Output{"bar", "blah", 1},
+ Output{"foo", "23.3", 2},
+ Output{"poo", "40", 3}));
}
TEST_F(SqliteStatement, GetValuesForSingleOutputWithBindingMultipleTimes)
@@ -529,8 +529,7 @@ TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndContainerQueryValues)
auto values = statement.values<Tuple, 3>(3, queryValues);
- ASSERT_THAT(values, ElementsAre(Tuple{"poo", 40, 3.},
- Tuple{"foo", 23.3, 2.}));
+ ASSERT_THAT(values, UnorderedElementsAre(Tuple{"poo", 40, 3.}, Tuple{"foo", 23.3, 2.}));
}
TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryValues)
@@ -540,7 +539,7 @@ TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryValues)
std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3, queryValues);
- ASSERT_THAT(values, ElementsAre("poo", "foo"));
+ ASSERT_THAT(values, UnorderedElementsAre("poo", "foo"));
}
TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndContainerQueryTupleValues)
@@ -552,8 +551,7 @@ TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndContainerQueryTupleVa
auto values = statement.values<ResultTuple, 3>(3, queryValues);
- ASSERT_THAT(values, ElementsAre(ResultTuple{"poo", 40, 3},
- ResultTuple{"bar", 0, 1}));
+ ASSERT_THAT(values, UnorderedElementsAre(ResultTuple{"poo", 40, 3}, ResultTuple{"bar", 0, 1}));
}
TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryTupleValues)
@@ -564,7 +562,7 @@ TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryTupleValu
std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3, queryValues);
- ASSERT_THAT(values, ElementsAre("poo", "bar"));
+ ASSERT_THAT(values, UnorderedElementsAre("poo", "bar"));
}
TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndMultipleQueryValue)
@@ -616,8 +614,7 @@ TEST_F(SqliteStatement, GetStructOutputValuesAndContainerQueryTupleValues)
auto values = statement.values<Output, 3>(3, queryValues);
- ASSERT_THAT(values, ElementsAre(Output{"poo", "40", 3},
- Output{"bar", "blah", 1}));
+ ASSERT_THAT(values, UnorderedElementsAre(Output{"poo", "40", 3}, Output{"bar", "blah", 1}));
}
TEST_F(SqliteStatement, GetBlobValues)
diff --git a/tests/unit/unittest/sqlitetransaction-test.cpp b/tests/unit/unittest/sqlitetransaction-test.cpp
index dc3e5bba70..0aa426db67 100644
--- a/tests/unit/unittest/sqlitetransaction-test.cpp
+++ b/tests/unit/unittest/sqlitetransaction-test.cpp
@@ -33,12 +33,13 @@
namespace {
+using Sqlite::DeferredNonThrowingDestructorTransaction;
using Sqlite::DeferredTransaction;
-using Sqlite::ImmediateTransaction;
+using Sqlite::ExclusiveNonThrowingDestructorTransaction;
using Sqlite::ExclusiveTransaction;
-using Sqlite::DeferredNonThrowingDestructorTransaction;
using Sqlite::ImmediateNonThrowingDestructorTransaction;
-using Sqlite::ExclusiveNonThrowingDestructorTransaction;
+using Sqlite::ImmediateSessionTransaction;
+using Sqlite::ImmediateTransaction;
class SqliteTransaction : public testing::Test
{
@@ -316,4 +317,56 @@ TEST_F(SqliteTransaction, TransactionRollbackInDestructorDontThrows)
ASSERT_NO_THROW(ExclusiveNonThrowingDestructorTransaction{mockTransactionBackend});
}
+TEST_F(SqliteTransaction, ImmediateSessionTransactionCommit)
+{
+ InSequence s;
+
+ EXPECT_CALL(mockTransactionBackend, lock());
+ EXPECT_CALL(mockTransactionBackend, immediateSessionBegin());
+ EXPECT_CALL(mockTransactionBackend, sessionCommit());
+ EXPECT_CALL(mockTransactionBackend, unlock());
+
+ ImmediateSessionTransaction transaction{mockTransactionBackend};
+ transaction.commit();
+}
+
+TEST_F(SqliteTransaction, ImmediateSessionTransactionRollBack)
+{
+ InSequence s;
+
+ EXPECT_CALL(mockTransactionBackend, lock());
+ EXPECT_CALL(mockTransactionBackend, immediateSessionBegin());
+ EXPECT_CALL(mockTransactionBackend, sessionRollback());
+ EXPECT_CALL(mockTransactionBackend, unlock());
+
+ ImmediateSessionTransaction transaction{mockTransactionBackend};
+}
+
+TEST_F(SqliteTransaction, SessionTransactionRollbackInDestructorThrows)
+{
+ ON_CALL(mockTransactionBackend, sessionRollback()).WillByDefault(Throw(Sqlite::Exception("foo")));
+
+ ASSERT_THROW(ImmediateSessionTransaction{mockTransactionBackend}, Sqlite::Exception);
+}
+
+TEST_F(SqliteTransaction, ImmidiateSessionTransactionBeginThrows)
+{
+ ON_CALL(mockTransactionBackend, immediateSessionBegin())
+ .WillByDefault(Throw(Sqlite::Exception("foo")));
+
+ ASSERT_THROW(ImmediateSessionTransaction{mockTransactionBackend}, Sqlite::Exception);
}
+
+TEST_F(SqliteTransaction, ImmediateSessionTransactionBeginThrowsAndNotRollback)
+{
+ InSequence s;
+
+ EXPECT_CALL(mockTransactionBackend, lock());
+ EXPECT_CALL(mockTransactionBackend, immediateSessionBegin()).WillOnce(Throw(Sqlite::Exception("foo")));
+ EXPECT_CALL(mockTransactionBackend, sessionRollback()).Times(0);
+ EXPECT_CALL(mockTransactionBackend, unlock());
+
+ ASSERT_ANY_THROW(ImmediateSessionTransaction{mockTransactionBackend});
+}
+
+} // namespace
diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro
index 7340f00775..03f04f3982 100644
--- a/tests/unit/unittest/unittest.pro
+++ b/tests/unit/unittest/unittest.pro
@@ -81,6 +81,7 @@ SOURCES += \
smallstring-test.cpp \
sourcerangefilter-test.cpp \
spydummy.cpp \
+ sqlitesessions-test.cpp \
sqlitevalue-test.cpp \
symbolindexer-test.cpp \
symbolsfindfilter-test.cpp \
diff --git a/tests/unit/unittest/unittests-main.cpp b/tests/unit/unittest/unittests-main.cpp
index f5eaa8846c..6704a08034 100644
--- a/tests/unit/unittest/unittests-main.cpp
+++ b/tests/unit/unittest/unittests-main.cpp
@@ -25,8 +25,8 @@
#include "googletest.h"
+#include <sqlitedatabase.h>
#include <sqliteglobal.h>
-
#include <utils/temporarydirectory.h>
#include <QCoreApplication>
@@ -38,6 +38,7 @@
int main(int argc, char *argv[])
{
+ Sqlite::Database::activateLogging();
const QString temporayDirectoryPath = QDir::tempPath() +"/QtCreator-UnitTests-XXXXXX";
Utils::TemporaryDirectory::setMasterTemporaryDirectory(temporayDirectoryPath);
qputenv("TMPDIR", Utils::TemporaryDirectory::masterDirectoryPath().toUtf8());