File indexing completed on 2024-12-01 04:35:24

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "filesforroommodeltest.h"
0008 #include "model/filesforroommodel.h"
0009 #include <QSignalSpy>
0010 #include <QTest>
0011 
0012 QTEST_GUILESS_MAIN(FilesForRoomModelTest)
0013 
0014 FilesForRoomModelTest::FilesForRoomModelTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 void FilesForRoomModelTest::shouldHaveDefaultValue()
0020 {
0021     FilesForRoomModel w;
0022     QVERIFY(w.fileAttachments());
0023     QSignalSpy rowInsertedSpy(&w, &FilesForRoomModel::rowsInserted);
0024     // (if it had 0 columns, it would have to emit column insertions, too much trouble)
0025     QCOMPARE(w.rowCount(), 0);
0026     QCOMPARE(rowInsertedSpy.count(), 0);
0027     QVERIFY(!w.loadMoreFilesInProgress());
0028 }
0029 
0030 void FilesForRoomModelTest::shouldAddFiles()
0031 {
0032     FilesForRoomModel w;
0033     QVector<File> mFiles;
0034     for (int i = 0; i < 10; ++i) {
0035         File f;
0036         f.setFileName(QStringLiteral("name%1").arg(i));
0037         f.setDescription(QStringLiteral("description%1").arg(i));
0038         f.setUserId(QStringLiteral("userid%1").arg(i));
0039         mFiles.append(std::move(f));
0040     }
0041     QSignalSpy rowInsertedSpy(&w, &FilesForRoomModel::rowsInserted);
0042 
0043     w.setFiles(mFiles);
0044     QCOMPARE(w.rowCount(), 10);
0045     QCOMPARE(rowInsertedSpy.count(), 1);
0046 
0047     mFiles.clear();
0048     for (int i = 0; i < 3; ++i) {
0049         File f;
0050         f.setFileName(QStringLiteral("name%1").arg(i));
0051         f.setDescription(QStringLiteral("description%1").arg(i));
0052         f.setUserId(QStringLiteral("userid%1").arg(i));
0053         mFiles.append(std::move(f));
0054     }
0055     w.setFiles(mFiles);
0056     QCOMPARE(w.rowCount(), 3);
0057     QCOMPARE(rowInsertedSpy.count(), 2);
0058 }
0059 
0060 void FilesForRoomModelTest::shouldVerifyData()
0061 {
0062     FilesForRoomModel w;
0063     QVector<File> mFiles;
0064     for (int i = 0; i < 10; ++i) {
0065         File f;
0066         f.setFileName(QStringLiteral("name%1").arg(i));
0067         f.setDescription(QStringLiteral("description%1").arg(i));
0068         f.setUserId(QStringLiteral("userid%1").arg(i));
0069         mFiles.append(std::move(f));
0070     }
0071 
0072     w.setFiles(mFiles);
0073     QCOMPARE(w.rowCount(), 10);
0074     for (int i = 0; i < 10; ++i) {
0075         QCOMPARE(w.data(w.index(i), FilesForRoomModel::FileName).toString(), QStringLiteral("name%1").arg(i));
0076         QCOMPARE(w.data(w.index(i), FilesForRoomModel::Description).toString(), QStringLiteral("description%1").arg(i));
0077         QCOMPARE(w.data(w.index(i), FilesForRoomModel::UserId).toString(), QStringLiteral("userid%1").arg(i));
0078     }
0079     mFiles.clear();
0080     for (int i = 0; i < 3; ++i) {
0081         File f;
0082         f.setFileName(QStringLiteral("name%1").arg(i));
0083         f.setDescription(QStringLiteral("description%1").arg(i));
0084         f.setUserId(QStringLiteral("userid%1").arg(i));
0085         mFiles.append(std::move(f));
0086     }
0087     w.setFiles(mFiles);
0088     for (int i = 0; i < 3; ++i) {
0089         QCOMPARE(w.data(w.index(i), FilesForRoomModel::FileName).toString(), QStringLiteral("name%1").arg(i));
0090         QCOMPARE(w.data(w.index(i), FilesForRoomModel::Description).toString(), QStringLiteral("description%1").arg(i));
0091         QCOMPARE(w.data(w.index(i), FilesForRoomModel::UserId).toString(), QStringLiteral("userid%1").arg(i));
0092     }
0093 }
0094 
0095 #include "moc_filesforroommodeltest.cpp"