File indexing completed on 2024-11-10 04:40:19

0001 /*
0002     SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "dbupdatertest.h"
0008 
0009 #include "storage/dbupdater.h"
0010 #include <QTest>
0011 
0012 using namespace Akonadi::Server;
0013 
0014 void DbUpdaterTest::initTestCase()
0015 {
0016     Q_INIT_RESOURCE(akonadidb);
0017 }
0018 
0019 void DbUpdaterTest::testMysqlUpdateStatements()
0020 {
0021     const QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QMYSQL"));
0022     DbUpdater updater(db, QStringLiteral(":unittest_dbupdate.xml"));
0023 
0024     {
0025         UpdateSet::Map updateSets;
0026         QVERIFY(updater.parseUpdateSets(1, updateSets));
0027         const auto expectedSets = {2, 3, 4, 8, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 28, 31, 35};
0028         for (const auto expected : expectedSets) {
0029             QVERIFY(updateSets.contains(expected));
0030         }
0031         QCOMPARE(std::size_t(updateSets.count()), expectedSets.size());
0032     }
0033 
0034     {
0035         UpdateSet::Map updateSets;
0036         QVERIFY(updater.parseUpdateSets(13, updateSets));
0037         const auto expectedSets = {14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 28, 31, 35};
0038         for (int i = 0; i < 13; ++i) {
0039             QVERIFY(!updateSets.contains(i));
0040         }
0041         for (const auto expected : expectedSets) {
0042             QVERIFY(updateSets.contains(expected));
0043         }
0044         QCOMPARE(std::size_t(updateSets.count()), expectedSets.size());
0045 
0046         QCOMPARE(updateSets.value(14).statements.count(), 2);
0047         QCOMPARE(updateSets.value(16).statements.count(), 11);
0048         QCOMPARE(updateSets.value(22).statements.count(), 6);
0049     }
0050 }
0051 
0052 void DbUpdaterTest::testPsqlUpdateStatements()
0053 {
0054     const QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QPSQL"));
0055     DbUpdater updater(db, QStringLiteral(":unittest_dbupdate.xml"));
0056 
0057     {
0058         UpdateSet::Map updateSets;
0059         QVERIFY(updater.parseUpdateSets(1, updateSets));
0060         const auto expectedSets = {2, 3, 4, 8, 10, 12, 13, 14, 15, 16, 19, 23, 24, 25, 26, 28, 30, 33, 35};
0061         for (const auto expected : expectedSets) {
0062             QVERIFY(updateSets.contains(expected));
0063         }
0064         QCOMPARE(std::size_t(updateSets.count()), expectedSets.size());
0065     }
0066 
0067     {
0068         UpdateSet::Map updateSets;
0069         QVERIFY(updater.parseUpdateSets(13, updateSets));
0070         const auto expectedSets = {14, 15, 16, 19, 23, 24, 25, 26, 28, 30, 33, 35};
0071         for (int i = 0; i < 13; ++i) {
0072             QVERIFY(!updateSets.contains(i));
0073         }
0074         for (const auto expected : expectedSets) {
0075             QVERIFY(updateSets.contains(expected));
0076         }
0077         QCOMPARE(std::size_t(updateSets.count()), expectedSets.size());
0078 
0079         QCOMPARE(updateSets.value(14).statements.count(), 2);
0080         QCOMPARE(updateSets.value(16).statements.count(), 11);
0081         QCOMPARE(updateSets.value(17).statements.count(), 0);
0082         QCOMPARE(updateSets.value(22).statements.count(), 0);
0083     }
0084 }
0085 
0086 void DbUpdaterTest::cleanupTestCase()
0087 {
0088     Q_CLEANUP_RESOURCE(akonadidb);
0089 }
0090 
0091 QTEST_MAIN(DbUpdaterTest)
0092 
0093 #include "moc_dbupdatertest.cpp"