File indexing completed on 2024-12-22 04:44:36

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "localroomsdatabasetest.h"
0007 #include "localdatabase/localroomsdatabase.h"
0008 #include "room.h"
0009 #include "ruqola_autotest_helper.h"
0010 #include <QFile>
0011 #include <QStandardPaths>
0012 
0013 QTEST_GUILESS_MAIN(LocalRoomsDatabaseTest)
0014 static QString accountName()
0015 {
0016     return QStringLiteral("myAccount");
0017 }
0018 LocalRoomsDatabaseTest::LocalRoomsDatabaseTest(QObject *parent)
0019     : QObject{parent}
0020 {
0021 }
0022 
0023 void LocalRoomsDatabaseTest::initTestCase()
0024 {
0025     QStandardPaths::setTestModeEnabled(true);
0026 
0027     // Clean up after previous runs
0028     LocalRoomsDatabase roomDatabase;
0029     QFile::remove(roomDatabase.dbFileName(accountName()));
0030 }
0031 
0032 void LocalRoomsDatabaseTest::shouldDefaultValues()
0033 {
0034     LocalRoomsDatabase roomDatabase;
0035     QCOMPARE(roomDatabase.schemaDatabaseStr(), QStringLiteral("CREATE TABLE ROOMS (roomId TEXT PRIMARY KEY NOT NULL, timestamp INTEGER, json TEXT)"));
0036 }
0037 
0038 void LocalRoomsDatabaseTest::shouldVerifyDbFileName()
0039 {
0040     LocalRoomsDatabase accountDataBase;
0041     QCOMPARE(accountDataBase.dbFileName(accountName()),
0042              QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QStringLiteral("/database/rooms/myAccount/myAccount.sqlite"));
0043 }
0044 
0045 void LocalRoomsDatabaseTest::shouldStoreRoomsSettings()
0046 {
0047     {
0048         const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/rooms/direct-room.json");
0049         const QJsonObject fields = AutoTestHelper::loadJsonObject(originalJsonFile);
0050         LocalRoomsDatabase roomDataBase;
0051         Room r;
0052         r.parseSubscriptionRoom(fields);
0053         const QByteArray ba = Room::serialize(&r, false);
0054         roomDataBase.addRoom(accountName(), &r);
0055 
0056         // WHEN
0057         const QByteArray getInfo = roomDataBase.jsonRoom(accountName(), r.roomId());
0058 
0059         // THEN
0060         QCOMPARE(getInfo, ba);
0061         Room r1;
0062         const auto doc = QJsonDocument::fromJson(getInfo);
0063         Room::deserialize(&r1, doc.object());
0064         QCOMPARE(r1, r);
0065     }
0066 }
0067 
0068 #include "moc_localroomsdatabasetest.cpp"