2017-08-21 12:00:27 +02:00
/****************************************************************************
* *
* * Copyright ( C ) 2017 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 "mocksqlitedatabase.h"
# include "mocksqlitereadstatement.h"
# include "mocksqlitewritestatement.h"
# include <storagesqlitestatementfactory.h>
namespace {
2018-02-20 12:43:05 +01:00
using StatementFactory = ClangBackEnd : : StorageSqliteStatementFactory < NiceMock < MockSqliteDatabase > > ;
2017-08-21 12:00:27 +02:00
2017-09-18 10:21:45 +02:00
using Sqlite : : Table ;
2017-08-21 12:00:27 +02:00
class StorageSqliteStatementFactory : public testing : : Test
{
protected :
2018-01-22 14:21:01 +01:00
NiceMock < MockSqliteDatabase > mockDatabase ;
2017-08-21 12:00:27 +02:00
StatementFactory factory { mockDatabase } ;
} ;
TEST_F ( StorageSqliteStatementFactory , AddNewSymbolsTable )
{
InSequence s ;
2018-04-05 10:58:33 +02:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE TEMPORARY TABLE newSymbols(temporarySymbolId INTEGER PRIMARY KEY, symbolId INTEGER, usr TEXT, symbolName TEXT, symbolKind INTEGER) " ) ) ) ;
2017-08-17 12:44:52 +02:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE INDEX IF NOT EXISTS index_newSymbols_usr_symbolName ON newSymbols(usr, symbolName) " ) ) ) ;
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE INDEX IF NOT EXISTS index_newSymbols_symbolId ON newSymbols(symbolId) " ) ) ) ;
2017-08-21 12:00:27 +02:00
factory . createNewSymbolsTable ( ) ;
}
TEST_F ( StorageSqliteStatementFactory , AddNewLocationsTable )
{
InSequence s ;
2018-04-09 13:30:30 +02:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE TEMPORARY TABLE newLocations(temporarySymbolId INTEGER, symbolId INTEGER, sourceId INTEGER, line INTEGER, column INTEGER, locationKind INTEGER) " ) ) ) ;
2018-03-29 18:40:35 +02:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE UNIQUE INDEX IF NOT EXISTS index_newLocations_sourceId_line_column ON newLocations(sourceId, line, column) " ) ) ) ;
2017-08-21 12:00:27 +02:00
factory . createNewLocationsTable ( ) ;
}
2018-01-25 15:20:47 +01:00
TEST_F ( StorageSqliteStatementFactory , AddNewUsedMacroTable )
2018-01-24 18:06:15 +01:00
{
InSequence s ;
2018-01-25 15:20:47 +01:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE TEMPORARY TABLE newUsedMacros(sourceId INTEGER, macroName TEXT) " ) ) ) ;
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE INDEX IF NOT EXISTS index_newUsedMacros_sourceId_macroName ON newUsedMacros(sourceId, macroName) " ) ) ) ;
2018-01-24 18:06:15 +01:00
2018-01-25 15:20:47 +01:00
factory . createNewUsedMacrosTable ( ) ;
2018-01-24 18:06:15 +01:00
}
2018-01-30 18:41:45 +01:00
TEST_F ( StorageSqliteStatementFactory , AddNewSourceDependenciesTable )
{
InSequence s ;
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE TEMPORARY TABLE newSourceDependencies(sourceId INTEGER, dependencySourceId TEXT) " ) ) ) ;
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE INDEX IF NOT EXISTS index_newSourceDependencies_sourceId_dependencySourceId ON newSourceDependencies(sourceId, dependencySourceId) " ) ) ) ;
factory . createNewSourceDependenciesTable ( ) ;
}
2017-08-21 12:00:27 +02:00
TEST_F ( StorageSqliteStatementFactory , AddTablesInConstructor )
{
2018-01-24 18:06:15 +01:00
InSequence s ;
2017-09-19 17:56:54 +02:00
2018-01-24 18:06:15 +01:00
EXPECT_CALL ( mockDatabase , immediateBegin ( ) ) ;
2018-04-05 10:58:33 +02:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE TEMPORARY TABLE newSymbols(temporarySymbolId INTEGER PRIMARY KEY, symbolId INTEGER, usr TEXT, symbolName TEXT, symbolKind INTEGER) " ) ) ) ;
2017-08-17 12:44:52 +02:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE INDEX IF NOT EXISTS index_newSymbols_usr_symbolName ON newSymbols(usr, symbolName) " ) ) ) ;
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE INDEX IF NOT EXISTS index_newSymbols_symbolId ON newSymbols(symbolId) " ) ) ) ;
2018-04-09 13:30:30 +02:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE TEMPORARY TABLE newLocations(temporarySymbolId INTEGER, symbolId INTEGER, sourceId INTEGER, line INTEGER, column INTEGER, locationKind INTEGER) " ) ) ) ;
2018-03-29 18:40:35 +02:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE UNIQUE INDEX IF NOT EXISTS index_newLocations_sourceId_line_column ON newLocations(sourceId, line, column) " ) ) ) ;
2018-01-25 15:20:47 +01:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE TEMPORARY TABLE newUsedMacros(sourceId INTEGER, macroName TEXT) " ) ) ) ;
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE INDEX IF NOT EXISTS index_newUsedMacros_sourceId_macroName ON newUsedMacros(sourceId, macroName) " ) ) ) ;
2018-01-30 18:41:45 +01:00
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE TEMPORARY TABLE newSourceDependencies(sourceId INTEGER, dependencySourceId TEXT) " ) ) ) ;
EXPECT_CALL ( mockDatabase , execute ( Eq ( " CREATE INDEX IF NOT EXISTS index_newSourceDependencies_sourceId_dependencySourceId ON newSourceDependencies(sourceId, dependencySourceId) " ) ) ) ;
2018-01-24 18:06:15 +01:00
EXPECT_CALL ( mockDatabase , commit ( ) ) ;
2017-08-21 12:00:27 +02:00
StatementFactory factory { mockDatabase } ;
}
TEST_F ( StorageSqliteStatementFactory , InsertNewSymbolsStatement )
{
ASSERT_THAT ( factory . insertSymbolsToNewSymbolsStatement . sqlStatement ,
2018-04-05 10:58:33 +02:00
Eq ( " INSERT INTO newSymbols(temporarySymbolId, usr, symbolName, symbolKind) VALUES(?,?,?,?) " ) ) ;
2017-08-21 12:00:27 +02:00
}
TEST_F ( StorageSqliteStatementFactory , InsertNewLocationsToLocations )
{
ASSERT_THAT ( factory . insertLocationsToNewLocationsStatement . sqlStatement ,
2018-04-09 13:30:30 +02:00
Eq ( " INSERT OR IGNORE INTO newLocations(temporarySymbolId, line, column, sourceId, locationKind) VALUES(?,?,?,?,?) " ) ) ;
2017-08-21 12:00:27 +02:00
}
TEST_F ( StorageSqliteStatementFactory , SelectNewSourceIdsStatement )
{
ASSERT_THAT ( factory . selectNewSourceIdsStatement . sqlStatement ,
Eq ( " SELECT DISTINCT sourceId FROM newLocations WHERE NOT EXISTS (SELECT sourceId FROM sources WHERE newLocations.sourceId == sources.sourceId) " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , AddNewSymbolsToSymbolsStatement )
{
ASSERT_THAT ( factory . addNewSymbolsToSymbolsStatement . sqlStatement ,
2018-04-05 10:58:33 +02:00
Eq ( " INSERT INTO symbols(usr, symbolName, symbolKind) SELECT usr, symbolName, symbolKind FROM newSymbols WHERE NOT EXISTS (SELECT usr FROM symbols WHERE symbols.usr == newSymbols.usr) " ) ) ;
2017-08-21 12:00:27 +02:00
}
TEST_F ( StorageSqliteStatementFactory , SyncNewSymbolsFromSymbolsStatement )
{
ASSERT_THAT ( factory . syncNewSymbolsFromSymbolsStatement . sqlStatement ,
Eq ( " UPDATE newSymbols SET symbolId = (SELECT symbolId FROM symbols WHERE newSymbols.usr = symbols.usr) " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , SyncSymbolsIntoNewLocations )
{
ASSERT_THAT ( factory . syncSymbolsIntoNewLocationsStatement . sqlStatement ,
Eq ( " UPDATE newLocations SET symbolId = (SELECT symbolId FROM newSymbols WHERE newSymbols.temporarySymbolId = newLocations.temporarySymbolId) " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , DeleteAllLocationsFromUpdatedFiles )
{
ASSERT_THAT ( factory . deleteAllLocationsFromUpdatedFilesStatement . sqlStatement ,
Eq ( " DELETE FROM locations WHERE sourceId IN (SELECT DISTINCT sourceId FROM newLocations) " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , InsertNewLocationsInLocations )
{
ASSERT_THAT ( factory . insertNewLocationsInLocationsStatement . sqlStatement ,
2018-04-09 13:30:30 +02:00
Eq ( " INSERT INTO locations(symbolId, line, column, sourceId, locationKind) SELECT symbolId, line, column, sourceId, locationKind FROM newLocations " ) ) ;
2017-08-21 12:00:27 +02:00
}
TEST_F ( StorageSqliteStatementFactory , DeleteNewSymbolsTableStatement )
{
ASSERT_THAT ( factory . deleteNewSymbolsTableStatement . sqlStatement ,
Eq ( " DELETE FROM newSymbols " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , DeleteNewLocationsTableStatement )
{
ASSERT_THAT ( factory . deleteNewLocationsTableStatement . sqlStatement ,
Eq ( " DELETE FROM newLocations " ) ) ;
}
2018-01-22 14:21:01 +01:00
TEST_F ( StorageSqliteStatementFactory , InsertProjectPart )
{
2018-01-24 19:06:58 +01:00
ASSERT_THAT ( factory . insertProjectPartStatement . sqlStatement ,
2018-02-08 12:48:46 +01:00
Eq ( " INSERT OR IGNORE INTO projectParts(projectPartName, compilerArguments, compilerMacros, includeSearchPaths) VALUES (?,?,?,?) " ) ) ;
2017-08-21 12:00:27 +02:00
}
2018-01-22 14:21:01 +01:00
TEST_F ( StorageSqliteStatementFactory , UpdateProjectPart )
{
2018-01-24 19:06:58 +01:00
ASSERT_THAT ( factory . updateProjectPartStatement . sqlStatement ,
2018-02-08 12:48:46 +01:00
Eq ( " UPDATE projectParts SET compilerArguments = ?, compilerMacros = ?, includeSearchPaths = ? WHERE projectPartName = ? " ) ) ;
2018-01-22 14:21:01 +01:00
}
TEST_F ( StorageSqliteStatementFactory , GetProjectPartIdForProjectPartName )
{
2018-01-24 19:06:58 +01:00
ASSERT_THAT ( factory . getProjectPartIdStatement . sqlStatement ,
2018-01-22 14:21:01 +01:00
Eq ( " SELECT projectPartId FROM projectParts WHERE projectPartName = ? " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , DeleteAllProjectPartsSourcesWithProjectPartId )
{
2018-01-24 19:06:58 +01:00
ASSERT_THAT ( factory . deleteAllProjectPartsSourcesWithProjectPartIdStatement . sqlStatement ,
2018-01-22 14:21:01 +01:00
Eq ( " DELETE FROM projectPartsSources WHERE projectPartId = ? " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , InsertProjectPartsSources )
{
2018-01-24 19:06:58 +01:00
ASSERT_THAT ( factory . insertProjectPartSourcesStatement . sqlStatement ,
2018-01-22 14:21:01 +01:00
Eq ( " INSERT INTO projectPartsSources(projectPartId, sourceId) VALUES (?,?) " ) ) ;
}
2018-01-24 18:06:15 +01:00
TEST_F ( StorageSqliteStatementFactory , GetCompileArgumentsForFileId )
2018-01-22 14:21:01 +01:00
{
2018-01-24 19:06:58 +01:00
ASSERT_THAT ( factory . getCompileArgumentsForFileIdStatement . sqlStatement ,
2018-01-22 14:21:01 +01:00
Eq ( " SELECT compilerArguments FROM projectParts WHERE projectPartId = (SELECT projectPartId FROM projectPartsSources WHERE sourceId = ?) " ) ) ;
}
2018-01-25 15:20:47 +01:00
TEST_F ( StorageSqliteStatementFactory , InsertIntoNewUsedMacros )
2018-01-24 18:06:15 +01:00
{
2018-01-25 15:20:47 +01:00
ASSERT_THAT ( factory . insertIntoNewUsedMacrosStatement . sqlStatement ,
Eq ( " INSERT INTO newUsedMacros(sourceId, macroName) VALUES (?,?) " ) ) ;
2018-01-24 18:06:15 +01:00
}
2018-01-25 15:20:47 +01:00
TEST_F ( StorageSqliteStatementFactory , SyncNewUsedMacros )
2018-01-24 18:06:15 +01:00
{
2018-01-25 15:20:47 +01:00
ASSERT_THAT ( factory . syncNewUsedMacrosStatement . sqlStatement ,
Eq ( " INSERT INTO usedMacros(sourceId, macroName) SELECT sourceId, macroName FROM newUsedMacros WHERE NOT EXISTS (SELECT sourceId FROM usedMacros WHERE usedMacros.sourceId == newUsedMacros.sourceId AND usedMacros.macroName == newUsedMacros.macroName) " ) ) ;
2018-01-24 18:06:15 +01:00
}
2018-01-30 18:41:45 +01:00
TEST_F ( StorageSqliteStatementFactory , DeleteOutdatedUnusedMacros )
2018-01-24 18:06:15 +01:00
{
2018-01-25 15:20:47 +01:00
ASSERT_THAT ( factory . deleteOutdatedUsedMacrosStatement . sqlStatement ,
Eq ( " DELETE FROM usedMacros WHERE sourceId IN (SELECT sourceId FROM newUsedMacros) AND NOT EXISTS (SELECT sourceId FROM newUsedMacros WHERE newUsedMacros.sourceId == usedMacros.sourceId AND newUsedMacros.macroName == usedMacros.macroName) " ) ) ;
2018-01-24 18:06:15 +01:00
}
2018-01-25 15:20:47 +01:00
TEST_F ( StorageSqliteStatementFactory , DeleteAllInNewUnusedMacros )
2018-01-24 18:06:15 +01:00
{
2018-01-25 15:20:47 +01:00
ASSERT_THAT ( factory . deleteNewUsedMacrosTableStatement . sqlStatement ,
Eq ( " DELETE FROM newUsedMacros " ) ) ;
2018-01-24 18:06:15 +01:00
}
2018-01-30 13:31:14 +01:00
2018-02-01 18:29:45 +01:00
TEST_F ( StorageSqliteStatementFactory , InsertFileStatuses )
2018-01-30 13:31:14 +01:00
{
2018-02-01 18:29:45 +01:00
ASSERT_THAT ( factory . insertFileStatuses . sqlStatement ,
2018-02-14 18:49:25 +01:00
Eq ( " INSERT OR REPLACE INTO fileStatuses(sourceId, size, lastModified, isInPrecompiledHeader) VALUES (?,?,?,?) " ) ) ;
2018-01-30 13:31:14 +01:00
}
2018-01-30 18:41:45 +01:00
TEST_F ( StorageSqliteStatementFactory , InsertIntoNewSourceDependencies )
{
ASSERT_THAT ( factory . insertIntoNewSourceDependenciesStatement . sqlStatement ,
Eq ( " INSERT INTO newSourceDependencies(sourceId, dependencySourceId) VALUES (?,?) " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , SyncNewSourceDependencies )
{
ASSERT_THAT ( factory . syncNewSourceDependenciesStatement . sqlStatement ,
Eq ( " INSERT INTO sourceDependencies(sourceId, dependencySourceId) SELECT sourceId, dependencySourceId FROM newSourceDependencies WHERE NOT EXISTS (SELECT sourceId FROM sourceDependencies WHERE sourceDependencies.sourceId == newSourceDependencies.sourceId AND sourceDependencies.dependencySourceId == newSourceDependencies.dependencySourceId) " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , DeleteOutdatedSourceDependencies )
{
ASSERT_THAT ( factory . deleteOutdatedSourceDependenciesStatement . sqlStatement ,
Eq ( " DELETE FROM sourceDependencies WHERE sourceId IN (SELECT sourceId FROM newSourceDependencies) AND NOT EXISTS (SELECT sourceId FROM newSourceDependencies WHERE newSourceDependencies.sourceId == sourceDependencies.sourceId AND newSourceDependencies.dependencySourceId == sourceDependencies.dependencySourceId) " ) ) ;
}
TEST_F ( StorageSqliteStatementFactory , DeleteAllInNewSourceDependencies )
{
ASSERT_THAT ( factory . deleteNewSourceDependenciesStatement . sqlStatement ,
Eq ( " DELETE FROM newSourceDependencies " ) ) ;
}
2018-02-06 19:03:14 +01:00
TEST_F ( StorageSqliteStatementFactory , GetProjectPartCompilerArgumentsAndCompilerMacrosBySourceId )
2018-02-06 11:16:29 +01:00
{
2018-02-08 12:48:46 +01:00
ASSERT_THAT ( factory . getProjectPartArtefactsBySourceId . sqlStatement ,
Eq ( " SELECT compilerArguments, compilerMacros, includeSearchPaths, projectPartId FROM projectParts WHERE projectPartId = (SELECT projectPartId FROM projectPartsSources WHERE sourceId = ?) " ) ) ;
2018-02-06 11:16:29 +01:00
}
2018-02-08 12:48:46 +01:00
TEST_F ( StorageSqliteStatementFactory , GetProjectPartArtefactsByProjectPartName )
2018-02-07 16:18:16 +01:00
{
2018-02-08 12:48:46 +01:00
ASSERT_THAT ( factory . getProjectPartArtefactsByProjectPartName . sqlStatement ,
Eq ( " SELECT compilerArguments, compilerMacros, includeSearchPaths, projectPartId FROM projectParts WHERE projectPartName = ? " ) ) ;
2018-02-07 16:18:16 +01:00
}
2018-02-08 17:49:02 +01:00
TEST_F ( StorageSqliteStatementFactory , GetLowestLastModifiedTimeOfDependencies )
{
ASSERT_THAT ( factory . getLowestLastModifiedTimeOfDependencies . sqlStatement ,
Eq ( " WITH RECURSIVE sourceIds(sourceId) AS (VALUES(?) UNION SELECT dependencySourceId FROM sourceDependencies, sourceIds WHERE sourceDependencies.sourceId = sourceIds.sourceId) SELECT min(lastModified) FROM fileStatuses, sourceIds WHERE fileStatuses.sourceId = sourceIds.sourceId " ) ) ;
}
2018-02-20 12:43:05 +01:00
TEST_F ( StorageSqliteStatementFactory , GetPrecompiledHeaderForProjectPartName )
{
ASSERT_THAT ( factory . getPrecompiledHeader . sqlStatement ,
Eq ( " SELECT pchPath, pchBuildTime FROM precompiledHeaders WHERE projectPartId = ? " ) ) ;
}
2018-01-22 14:21:01 +01:00
}