File indexing completed on 2024-11-10 04:40:19
0001 /* 0002 SPDX-FileCopyrightText: 2012 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <QDebug> 0008 #include <QObject> 0009 #include <QTest> 0010 0011 #include "aktest.h" 0012 #include "storage/dbexception.h" 0013 #include "storage/dbintrospector.h" 0014 0015 #define QL1S(x) QLatin1StringView(x) 0016 0017 using namespace Akonadi::Server; 0018 0019 class DbIntrospectorTest : public QObject 0020 { 0021 Q_OBJECT 0022 private Q_SLOTS: 0023 void testHasIndexQuery_data() 0024 { 0025 QTest::addColumn<QString>("driverName"); 0026 QTest::addColumn<QString>("indexQuery"); 0027 0028 QTest::newRow("mysql") << "QMYSQL" 0029 << "SHOW INDEXES FROM myTable WHERE `Key_name` = 'myIndex'"; 0030 QTest::newRow("sqlite") << "QSQLITE" 0031 << "SELECT * FROM sqlite_master WHERE type='index' AND tbl_name='myTable' AND name='myIndex';"; 0032 QTest::newRow("psql") << "QPSQL" 0033 << "SELECT indexname FROM pg_catalog.pg_indexes WHERE tablename ilike 'myTable' AND indexname ilike 'myIndex' UNION SELECT " 0034 "conname FROM pg_catalog.pg_constraint WHERE conname ilike 'myIndex'"; 0035 } 0036 0037 void testHasIndexQuery() 0038 { 0039 QFETCH(QString, driverName); 0040 QFETCH(QString, indexQuery); 0041 0042 if (QSqlDatabase::drivers().contains(driverName)) { 0043 QSqlDatabase db = QSqlDatabase::addDatabase(driverName, driverName); 0044 DbIntrospector::Ptr introspector = DbIntrospector::createInstance(db); 0045 QVERIFY(introspector); 0046 QCOMPARE(introspector->hasIndexQuery(QL1S("myTable"), QL1S("myIndex")), indexQuery); 0047 } 0048 } 0049 0050 void testHasIndex_data() 0051 { 0052 QTest::addColumn<QString>("driverName"); 0053 QTest::addColumn<bool>("shouldThrow"); 0054 0055 QTest::newRow("mysql") << "QMYSQL" << true; 0056 QTest::newRow("sqlite") << "QSQLITE" << true; 0057 QTest::newRow("psql") << "QPSQL" << true; 0058 } 0059 0060 void testHasIndex() 0061 { 0062 QFETCH(QString, driverName); 0063 QFETCH(bool, shouldThrow); 0064 0065 if (QSqlDatabase::drivers().contains(driverName)) { 0066 QSqlDatabase db = QSqlDatabase::addDatabase(driverName, driverName + QL1S("hasIndex")); 0067 DbIntrospector::Ptr introspector = DbIntrospector::createInstance(db); 0068 QVERIFY(introspector); 0069 0070 bool didThrow = false; 0071 try { 0072 QVERIFY(introspector->hasIndex(QL1S("myTable"), QL1S("myIndex"))); 0073 } catch (const DbException &e) { 0074 didThrow = true; 0075 qDebug() << Q_FUNC_INFO << e.what(); 0076 } 0077 QCOMPARE(didThrow, shouldThrow); 0078 } 0079 } 0080 }; 0081 0082 AKTEST_MAIN(DbIntrospectorTest) 0083 0084 #include "dbintrospectortest.moc"