| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** Copyright (C) 2016 The Qt Company Ltd. | 
					
						
							|  |  |  | ** Contact: https://www.qt.io/licensing/
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ** 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 | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** 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.
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** 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.
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 17:41:41 +02:00
										 |  |  | #include "googletest.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-16 12:38:04 +02:00
										 |  |  | #include "spydummy.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <sqlitedatabase.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-27 23:48:03 +02:00
										 |  |  | #include <sqlitereadstatement.h>
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | #include <sqlitetable.h>
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:33:26 +02:00
										 |  |  | #include <sqlitewritestatement.h>
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | #include <utf8string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-03 17:44:38 +01:00
										 |  |  | #include <utils/temporarydirectory.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-16 12:38:04 +02:00
										 |  |  | #include <QSignalSpy>
 | 
					
						
							| 
									
										
										
										
											2018-09-25 15:42:52 +02:00
										 |  |  | #include <QTemporaryFile>
 | 
					
						
							| 
									
										
										
										
											2015-06-16 12:38:04 +02:00
										 |  |  | #include <QVariant>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 11:21:58 +02:00
										 |  |  | #include <functional>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 16:02:24 +02:00
										 |  |  | using testing::Contains; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | using Sqlite::ColumnType; | 
					
						
							|  |  |  | using Sqlite::JournalMode; | 
					
						
							|  |  |  | using Sqlite::OpenMode; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  | using Sqlite::Table; | 
					
						
							| 
									
										
										
										
											2017-07-26 16:02:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | class SqliteDatabase : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2020-05-27 23:48:03 +02:00
										 |  |  |     SqliteDatabase() | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         database.setJournalMode(JournalMode::Memory); | 
					
						
							|  |  |  |         database.setDatabaseFilePath(databaseFilePath); | 
					
						
							|  |  |  |         auto &table = database.addTable(); | 
					
						
							|  |  |  |         table.setName("test"); | 
					
						
							| 
									
										
										
										
											2020-05-27 23:48:03 +02:00
										 |  |  |         table.addColumn("id", Sqlite::ColumnType::Integer, {Sqlite::PrimaryKey{}}); | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |         table.addColumn("name"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         database.open(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 23:48:03 +02:00
										 |  |  |     ~SqliteDatabase() | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (database.isOpen()) | 
					
						
							|  |  |  |             database.close(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 23:48:03 +02:00
										 |  |  |     std::vector<Utils::SmallString> names() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Sqlite::ReadStatement("SELECT name FROM test", database).values<Utils::SmallString>(8); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     static void updateHookCallback( | 
					
						
							|  |  |  |         void *object, int type, char const *database, char const *table, long long rowId) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         static_cast<SqliteDatabase *>(object)->callback(static_cast<Sqlite::ChangeType>(type), | 
					
						
							|  |  |  |                                                         database, | 
					
						
							|  |  |  |                                                         table, | 
					
						
							|  |  |  |                                                         rowId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 23:48:03 +02:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     SpyDummy spyDummy; | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     QString databaseFilePath{":memory:"}; | 
					
						
							| 
									
										
										
										
											2020-05-27 23:48:03 +02:00
										 |  |  |     mutable Sqlite::Database database; | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     Sqlite::TransactionInterface &transactionInterface = database; | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  |     MockFunction<void(Sqlite::ChangeType tupe, char const *, char const *, long long)> callbackMock; | 
					
						
							| 
									
										
										
										
											2020-06-10 11:21:58 +02:00
										 |  |  |     std::function<void(Sqlite::ChangeType tupe, char const *, char const *, long long)> | 
					
						
							|  |  |  |         callback = callbackMock.AsStdFunction(); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, SetDatabaseFilePath) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT_THAT(database.databaseFilePath(), databaseFilePath); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, SetJournalMode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     database.setJournalMode(JournalMode::Memory); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(database.journalMode(), JournalMode::Memory); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 18:32:29 +02:00
										 |  |  | TEST_F(SqliteDatabase, SetOpenlMode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     database.setOpenMode(OpenMode::ReadOnly); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(database.openMode(), OpenMode::ReadOnly); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | TEST_F(SqliteDatabase, OpenDatabase) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     database.close(); | 
					
						
							| 
									
										
										
										
											2017-07-26 16:02:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     database.open(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_TRUE(database.isOpen()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, CloseDatabase) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     database.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_FALSE(database.isOpen()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 15:42:52 +02:00
										 |  |  | TEST_F(SqliteDatabase, DatabaseIsNotInitializedAfterOpening) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT_FALSE(database.isInitialized()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, DatabaseIsIntializedAfterSettingItBeforeOpening) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     database.setIsInitialized(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_TRUE(database.isInitialized()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, DatabaseIsInitializedIfDatabasePathExistsAtOpening) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Sqlite::Database database{TESTDATA_DIR "/sqlite_database.db"}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_TRUE(database.isInitialized()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, DatabaseIsNotInitializedIfDatabasePathDoesNotExistAtOpening) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-03 17:44:38 +01:00
										 |  |  |     Sqlite::Database database{Utils::PathString{Utils::TemporaryDirectory::masterDirectoryPath() | 
					
						
							|  |  |  |                                                 + "/database_does_not_exist.db"}}; | 
					
						
							| 
									
										
										
										
											2018-09-25 15:42:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_FALSE(database.isInitialized()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | TEST_F(SqliteDatabase, AddTable) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-01 14:02:57 +02:00
										 |  |  |     auto sqliteTable = database.addTable(); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 16:02:24 +02:00
										 |  |  |     ASSERT_THAT(database.tables(), Contains(sqliteTable)); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:33:26 +02:00
										 |  |  | TEST_F(SqliteDatabase, GetChangesCount) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Sqlite::WriteStatement statement("INSERT INTO test(name) VALUES (?)", database); | 
					
						
							|  |  |  |     statement.write(42); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(database.changesCount(), 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, GetTotalChangesCount) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Sqlite::WriteStatement statement("INSERT INTO test(name) VALUES (?)", database); | 
					
						
							|  |  |  |     statement.write(42); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(database.lastInsertedRowId(), 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, GetLastInsertedRowId) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Sqlite::WriteStatement statement("INSERT INTO test(name) VALUES (?)", database); | 
					
						
							|  |  |  |     statement.write(42); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(database.lastInsertedRowId(), 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  | TEST_F(SqliteDatabase, TableIsReadyAfterOpenDatabase) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     database.close(); | 
					
						
							|  |  |  |     auto &table = database.addTable(); | 
					
						
							|  |  |  |     table.setName("foo"); | 
					
						
							|  |  |  |     table.addColumn("name"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     database.open(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_TRUE(table.isReady()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-27 19:22:13 +01:00
										 |  |  | TEST_F(SqliteDatabase, LastRowId) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     database.setLastInsertedRowId(42); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(database.lastInsertedRowId(), 42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | TEST_F(SqliteDatabase, DeferredBegin) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     ASSERT_NO_THROW(transactionInterface.deferredBegin()); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     transactionInterface.commit(); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, ImmediateBegin) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     ASSERT_NO_THROW(transactionInterface.immediateBegin()); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     transactionInterface.commit(); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, ExclusiveBegin) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     ASSERT_NO_THROW(transactionInterface.exclusiveBegin()); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     transactionInterface.commit(); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, Commit) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     transactionInterface.deferredBegin(); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     ASSERT_NO_THROW(transactionInterface.commit()); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, Rollback) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     transactionInterface.deferredBegin(); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 10:38:04 +02:00
										 |  |  |     ASSERT_NO_THROW(transactionInterface.rollback()); | 
					
						
							| 
									
										
										
										
											2018-03-29 18:33:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | TEST_F(SqliteDatabase, SetUpdateHookSet) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(_, _, _, _)); | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, SetNullUpdateHook) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(nullptr, nullptr); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(_, _, _, _)).Times(0); | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, ResetUpdateHook) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     database.resetUpdateHook(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(_, _, _, _)).Times(0); | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, DeleteUpdateHookCall) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42); | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(Eq(Sqlite::ChangeType::Delete), _, _, _)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Sqlite::WriteStatement("DELETE FROM test WHERE name = 42", database).execute(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, InsertUpdateHookCall) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(Eq(Sqlite::ChangeType::Insert), _, _, _)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, UpdateUpdateHookCall) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(Eq(Sqlite::ChangeType::Insert), _, _, _)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, RowIdUpdateHookCall) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(_, _, _, Eq(42))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(rowid, name) VALUES (?,?)", database).write(42, "foo"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, DatabaseUpdateHookCall) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(_, StrEq("main"), _, _)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteDatabase, TableUpdateHookCall) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-05 19:55:10 +02:00
										 |  |  |     database.setUpdateHook(this, updateHookCallback); | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, Call(_, _, StrEq("test"), _)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 23:48:03 +02:00
										 |  |  | 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")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 12:54:18 +02:00
										 |  |  | } // namespace
 |