File indexing completed on 2022-10-04 15:18:59

0001 /* This file is part of the KDE project
0002    Copyright (C) 2017 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "QuerySchemaTest.h"
0021 
0022 #include <KDb>
0023 #include <KDbConnectionData>
0024 #include <KDbQueryAsterisk>
0025 #include <KDbQuerySchema>
0026 #include <KDbVersionInfo>
0027 
0028 #include <QRegularExpression>
0029 #include <QTest>
0030 
0031 QTEST_GUILESS_MAIN(QuerySchemaTest)
0032 
0033 void QuerySchemaTest::initTestCase()
0034 {
0035 }
0036 
0037 void QuerySchemaTest::testCaching()
0038 {
0039     QVERIFY(utils.testCreateDbWithTables("QuerySchemaTest"));
0040     KDbQuerySchema query;
0041     KDbTableSchema *carsTable = utils.connection()->tableSchema("cars");
0042     QVERIFY(carsTable);
0043     query.addTable(carsTable);
0044     KDbField *idField = carsTable->field("id");
0045     QVERIFY(idField);
0046     // "SELECT id, cars.* from cars"
0047     query.addField(idField);
0048     query.addAsterisk(new KDbQueryAsterisk(&query, *carsTable));
0049     QCOMPARE(query.fieldCount(), 2);
0050     const KDbQueryColumnInfo::Vector expandedAll1 = query.fieldsExpanded(utils.connection());
0051     QCOMPARE(expandedAll1.count(), 4);
0052     const KDbQueryColumnInfo::Vector expandedUnique1
0053         = query.fieldsExpanded(utils.connection(), KDbQuerySchema::FieldsExpandedMode::Unique);
0054     QCOMPARE(expandedUnique1.count(), 3);
0055     // remove the asterisk -> "SELECT id from cars"
0056     query.removeField(query.field(1));
0057     QCOMPARE(query.fieldCount(), 1);
0058     const KDbQueryColumnInfo::Vector expandedAll2 = query.fieldsExpanded(utils.connection());
0059     QCOMPARE(expandedAll2.count(), 1);
0060     const KDbQueryColumnInfo::Vector expandedUnique2
0061         = query.fieldsExpanded(utils.connection(), KDbQuerySchema::FieldsExpandedMode::Unique);
0062     QCOMPARE(expandedUnique2.count(), 1);
0063 }
0064 
0065 void QuerySchemaTest::cleanupTestCase()
0066 {
0067 }