diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 9b4478be..a239e9d3 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,55 +1,56 @@ remove_definitions( -DQT_NO_KEYWORDS -DQT_NO_SIGNALS_SLOTS_KEYWORDS -DQT_NO_CAST_FROM_ASCII -DQT_USE_QSTRINGBUILDER ) set(FILES_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(KDB_LOCAL_PLUGINS_DIR ${PROJECT_BINARY_DIR}/plugins) add_definitions( -DFILES_OUTPUT_DIR=\"${FILES_OUTPUT_DIR}\" # make plugins work without installing them: -DKDB_LOCAL_PLUGINS_DIR=\"${KDB_LOCAL_PLUGINS_DIR}\" # nonstandard path for sqlite3 extensions, they would work too if we placed them # in ${KDB_LOCAL_PLUGINS_DIR}/sqlite3 but we want to test the "extraSqliteExtensionPaths" # connection option in ConnectionTest::testCreateDb(): -DSQLITE_LOCAL_ICU_EXTENSION_PATH=\"${KDB_LOCAL_PLUGINS_DIR}/sqlite3\" -DYYTOKENTYPE # this removes access to yytokentype enum as we should access KDb::Token instead ) include(ECMAddTests) find_package(Qt5Test) set_package_properties(Qt5Test PROPERTIES DESCRIPTION "Qt5Test library" URL "https://www.qt.io" TYPE RECOMMENDED PURPOSE "Required by tests") # A helper library for db-related tests add_library(kdbtestutils SHARED KDbTestUtils.cpp ) target_link_libraries(kdbtestutils PUBLIC KDb Qt5::Test ) generate_export_header(kdbtestutils) # Tests ecm_add_tests( ConnectionOptionsTest.cpp ConnectionTest.cpp DriverTest.cpp ExpressionsTest.cpp + QuerySchemaTest.cpp KDbTest.cpp LINK_LIBRARIES kdbtestutils ) if(NOT WIN32) #TODO enable for Windows when headers_test.sh is ported e.g. to python add_subdirectory(headers) endif() add_subdirectory(tools) add_subdirectory(parser) diff --git a/autotests/QuerySchemaTest.cpp b/autotests/QuerySchemaTest.cpp new file mode 100644 index 00000000..67fe66c1 --- /dev/null +++ b/autotests/QuerySchemaTest.cpp @@ -0,0 +1,67 @@ +/* This file is part of the KDE project + Copyright (C) 2017 Jarosław Staniek + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#include "QuerySchemaTest.h" + +#include +#include +#include +#include +#include + +#include +#include + +QTEST_GUILESS_MAIN(QuerySchemaTest) + +void QuerySchemaTest::initTestCase() +{ +} + +void QuerySchemaTest::testCaching() +{ + QVERIFY(utils.testCreateDbWithTables("QuerySchemaTest")); + KDbQuerySchema query; + KDbTableSchema *carsTable = utils.connection->tableSchema("cars"); + QVERIFY(carsTable); + query.addTable(carsTable); + KDbField *idField = carsTable->field("id"); + QVERIFY(idField); + // "SELECT id, cars.* from cars" + query.addField(idField); + query.addAsterisk(new KDbQueryAsterisk(&query, *carsTable)); + QCOMPARE(query.fieldCount(), 2); + const KDbQueryColumnInfo::Vector expandedAll1 = query.fieldsExpanded(utils.connection.data()); + QCOMPARE(expandedAll1.count(), 4); + const KDbQueryColumnInfo::Vector expandedUnique1 + = query.fieldsExpanded(utils.connection.data(), KDbQuerySchema::FieldsExpandedMode::Unique); + QCOMPARE(expandedUnique1.count(), 3); + // remove the asterisk -> "SELECT id from cars" + query.removeField(query.field(1)); + QCOMPARE(query.fieldCount(), 1); + const KDbQueryColumnInfo::Vector expandedAll2 = query.fieldsExpanded(utils.connection.data()); + QCOMPARE(expandedAll2.count(), 1); + const KDbQueryColumnInfo::Vector expandedUnique2 + = query.fieldsExpanded(utils.connection.data(), KDbQuerySchema::FieldsExpandedMode::Unique); + QCOMPARE(expandedUnique2.count(), 1); +} + +void QuerySchemaTest::cleanupTestCase() +{ +} diff --git a/autotests/QuerySchemaTest.h b/autotests/QuerySchemaTest.h new file mode 100644 index 00000000..a9c8e231 --- /dev/null +++ b/autotests/QuerySchemaTest.h @@ -0,0 +1,40 @@ +/* This file is part of the KDE project + Copyright (C) 2017 Jarosław Staniek + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#ifndef KDBQUERYSCHEMATEST_H +#define KDBQUERYSCHEMATEST_H + +#include "KDbTestUtils.h" + +class QuerySchemaTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase(); + + //! Tests if expanded fields cache is updated when query schema object changes + void testCaching(); + + void cleanupTestCase(); + +private: + KDbTestUtils utils; +}; + +#endif