| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-16 12:38:04 +02:00
										 |  |  | #include <sqlitecolumn.h>
 | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  | #include <mocksqlitedatabase.h>
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | #include <sqlitetable.h>
 | 
					
						
							| 
									
										
										
										
											2015-06-16 12:38:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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::Column; | 
					
						
							|  |  |  | using Sqlite::Database; | 
					
						
							| 
									
										
										
										
											2017-07-26 16:02:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | class SqliteTable : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  |     NiceMock<MockSqliteDatabase> mockDatabase; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     Sqlite::Table table; | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  |     Utils::SmallString tableName = "testTable"; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteTable, ColumnIsAddedToTable) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-01 14:02:57 +02:00
										 |  |  |     table.setUseWithoutRowId(true); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 14:02:57 +02:00
										 |  |  |     ASSERT_TRUE(table.useWithoutRowId()); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteTable, SetTableName) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-01 14:02:57 +02:00
										 |  |  |     table.setName(tableName.clone()); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 14:02:57 +02:00
										 |  |  |     ASSERT_THAT(table.name(), tableName); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteTable, SetUseWithoutRowid) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-01 14:02:57 +02:00
										 |  |  |     table.setUseWithoutRowId(true); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 14:02:57 +02:00
										 |  |  |     ASSERT_TRUE(table.useWithoutRowId()); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  | TEST_F(SqliteTable, AddIndex) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-01 14:02:57 +02:00
										 |  |  |     table.setName(tableName.clone()); | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  |     auto &column = table.addColumn("name"); | 
					
						
							|  |  |  |     auto &column2 = table.addColumn("value"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  |     auto index = table.addIndex({column, column2}); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  |     ASSERT_THAT(Utils::SmallStringView(index.sqlStatement()), | 
					
						
							|  |  |  |                 Eq("CREATE INDEX IF NOT EXISTS index_testTable_name_value ON testTable(name, value)")); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  | TEST_F(SqliteTable, InitializeTable) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  |     table.setName(tableName.clone()); | 
					
						
							|  |  |  |     table.setUseIfNotExists(true); | 
					
						
							|  |  |  |     table.setUseTemporaryTable(true); | 
					
						
							|  |  |  |     table.setUseWithoutRowId(true); | 
					
						
							|  |  |  |     table.addColumn("name"); | 
					
						
							|  |  |  |     table.addColumn("value"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockDatabase, execute(Eq("CREATE TEMPORARY TABLE IF NOT EXISTS testTable(name NUMERIC, value NUMERIC) WITHOUT ROWID"))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.initialize(mockDatabase); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  | TEST_F(SqliteTable, InitializeTableWithIndex) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-17 12:44:52 +02:00
										 |  |  |     InSequence sequence; | 
					
						
							|  |  |  |     table.setName(tableName.clone()); | 
					
						
							|  |  |  |     auto &column = table.addColumn("name"); | 
					
						
							|  |  |  |     auto &column2 = table.addColumn("value"); | 
					
						
							|  |  |  |     table.addIndex({column}); | 
					
						
							|  |  |  |     table.addIndex({column2}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockDatabase, execute(Eq("CREATE TABLE testTable(name NUMERIC, value NUMERIC)"))); | 
					
						
							|  |  |  |     EXPECT_CALL(mockDatabase, execute(Eq("CREATE INDEX IF NOT EXISTS index_testTable_name ON testTable(name)"))); | 
					
						
							|  |  |  |     EXPECT_CALL(mockDatabase, execute(Eq("CREATE INDEX IF NOT EXISTS index_testTable_value ON testTable(value)"))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.initialize(mockDatabase); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 15:13:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteTable, InitializeTableWithUniqueIndex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     InSequence sequence; | 
					
						
							|  |  |  |     table.setName(tableName.clone()); | 
					
						
							|  |  |  |     auto &column = table.addColumn("name"); | 
					
						
							|  |  |  |     auto &column2 = table.addColumn("value"); | 
					
						
							|  |  |  |     table.addUniqueIndex({column}); | 
					
						
							|  |  |  |     table.addIndex({column2}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockDatabase, execute(Eq("CREATE TABLE testTable(name NUMERIC, value NUMERIC)"))); | 
					
						
							|  |  |  |     EXPECT_CALL(mockDatabase, execute(Eq("CREATE UNIQUE INDEX IF NOT EXISTS index_testTable_name ON testTable(name)"))); | 
					
						
							|  |  |  |     EXPECT_CALL(mockDatabase, execute(Eq("CREATE INDEX IF NOT EXISTS index_testTable_value ON testTable(value)"))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table.initialize(mockDatabase); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } |