Sqlite: Relax LastChangedRowId

Change-Id: Ibc4637ebafd4c0cdedfcea5c52da5025435bc4ab
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2020-05-16 14:53:58 +02:00
committed by Tim Jenssen
parent 613dec7c92
commit 55d1f6b46e
2 changed files with 178 additions and 0 deletions

View File

@@ -36,6 +36,29 @@ namespace Sqlite {
class LastChangedRowId
{
public:
LastChangedRowId(DatabaseInterface &database)
: database(database)
{
callback = [=](ChangeType, char const *database, char const *table, long long rowId) {
this->lastRowId = rowId;
};
database.setUpdateHook(callback);
}
LastChangedRowId(DatabaseInterface &database, Utils::SmallStringView databaseName)
: database(database)
{
callback = [=](ChangeType, char const *database, char const *table, long long rowId) {
if (databaseName == database)
this->lastRowId = rowId;
};
database.setUpdateHook(callback);
}
LastChangedRowId(DatabaseInterface &database,
Utils::SmallStringView databaseName,
Utils::SmallStringView tableName)

View File

@@ -287,4 +287,159 @@ TEST_F(LastChangedRowIdWithThreeTables, TakeLastRowIdResetsRowIdToMinusOne)
ASSERT_THAT(id, -1);
}
class LastChangedRowIdWithNoDatabaseAndTable : public testing::Test
{
protected:
NiceMock<MockSqliteDatabase> mockSqliteDatabase;
Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase};
};
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, SetUpdateHookInContructor)
{
EXPECT_CALL(mockSqliteDatabase, setUpdateHook(_));
Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase, "main", "foo"};
}
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, ResetUpdateHookInDestructor)
{
EXPECT_CALL(mockSqliteDatabase, resetUpdateHook());
}
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, GetMinusOneAsRowIdIfNoCallbackWasCalled)
{
ASSERT_THAT(lastRowId.lastRowId, -1);
}
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, CallbackSetsLastRowId)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
ASSERT_THAT(lastRowId.lastRowId, 42);
}
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, CallbackDoNotChecksDatabaseName)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
lastRowId.callback(Sqlite::ChangeType::Update, "temp", "foo", 42);
ASSERT_THAT(lastRowId.lastRowId, 42);
}
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, CallbackDoNotChecksTableName)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
lastRowId.callback(Sqlite::ChangeType::Update, "main", "bar", 42);
ASSERT_THAT(lastRowId.lastRowId, 42);
}
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, LastCallSetsRowId)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
lastRowId.callback(Sqlite::ChangeType::Insert, "main", "foo", 33);
lastRowId.callback(Sqlite::ChangeType::Delete, "main", "foo", 66);
ASSERT_THAT(lastRowId.lastRowId, 66);
}
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, TakeLastRowId)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
auto id = lastRowId.takeLastRowId();
ASSERT_THAT(id, 42);
}
TEST_F(LastChangedRowIdWithNoDatabaseAndTable, TakeLastRowIdResetsRowIdToMinusOne)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
lastRowId.takeLastRowId();
auto id = lastRowId.takeLastRowId();
ASSERT_THAT(id, -1);
}
class LastChangedRowIdWithNoTable : public testing::Test
{
protected:
NiceMock<MockSqliteDatabase> mockSqliteDatabase;
Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase};
};
TEST_F(LastChangedRowIdWithNoTable, SetUpdateHookInContructor)
{
EXPECT_CALL(mockSqliteDatabase, setUpdateHook(_));
Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase, "main", "foo"};
}
TEST_F(LastChangedRowIdWithNoTable, ResetUpdateHookInDestructor)
{
EXPECT_CALL(mockSqliteDatabase, resetUpdateHook());
}
TEST_F(LastChangedRowIdWithNoTable, GetMinusOneAsRowIdIfNoCallbackWasCalled)
{
ASSERT_THAT(lastRowId.lastRowId, -1);
}
TEST_F(LastChangedRowIdWithNoTable, CallbackSetsLastRowId)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
ASSERT_THAT(lastRowId.lastRowId, 42);
}
TEST_F(LastChangedRowIdWithNoTable, CallbackChecksDatabaseName)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
lastRowId.callback(Sqlite::ChangeType::Update, "temp", "foo", 42);
ASSERT_THAT(lastRowId.lastRowId, 33);
}
TEST_F(LastChangedRowIdWithNoTable, CallbackDoNotChecksTableName)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
lastRowId.callback(Sqlite::ChangeType::Update, "main", "bar", 42);
ASSERT_THAT(lastRowId.lastRowId, 42);
}
TEST_F(LastChangedRowIdWithNoTable, LastCallSetsRowId)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
lastRowId.callback(Sqlite::ChangeType::Insert, "main", "foo", 33);
lastRowId.callback(Sqlite::ChangeType::Delete, "main", "foo", 66);
ASSERT_THAT(lastRowId.lastRowId, 66);
}
TEST_F(LastChangedRowIdWithNoTable, TakeLastRowId)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
auto id = lastRowId.takeLastRowId();
ASSERT_THAT(id, 42);
}
TEST_F(LastChangedRowIdWithNoTable, TakeLastRowIdResetsRowIdToMinusOne)
{
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
lastRowId.takeLastRowId();
auto id = lastRowId.takeLastRowId();
ASSERT_THAT(id, -1);
}
} // namespace