File indexing completed on 2024-05-12 16:25:25

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 "filetest.h"
0008 #include "attachments/file.h"
0009 #include "ruqola_autotest_helper.h"
0010 
0011 QTEST_GUILESS_MAIN(FileTest)
0012 
0013 FileTest::FileTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 void FileTest::shouldHaveDefaultValue()
0019 {
0020     File f;
0021     QVERIFY(f.fileName().isEmpty());
0022     QVERIFY(f.description().isEmpty());
0023     QVERIFY(f.userId().isEmpty());
0024     QVERIFY(f.url().isEmpty());
0025     QVERIFY(f.mimeType().isEmpty());
0026     QVERIFY(f.fileId().isEmpty());
0027     QVERIFY(f.rid().isEmpty());
0028     QVERIFY(f.uploadedDateTimeStr().isEmpty());
0029     QCOMPARE(f.uploadedAt(), -1);
0030     QCOMPARE(f.complete(), false);
0031     QVERIFY(f.typeGroup().isEmpty());
0032 }
0033 
0034 void FileTest::shouldAssignValue()
0035 {
0036     File f;
0037     const QString url = QStringLiteral("foo");
0038     const QString name = QStringLiteral("bla");
0039     const QString description = QStringLiteral("des");
0040     const QString userId = QStringLiteral("ble");
0041     const QString mimetype = QStringLiteral("ble1");
0042     const qint64 timeUploaded = 55;
0043     const QString fileId = QStringLiteral("blabla");
0044     const QString rId = QStringLiteral("blabla22");
0045     bool complete = false;
0046     f.setUrl(url);
0047     f.setFileName(name);
0048     f.setDescription(description);
0049     f.setUserId(userId);
0050     f.setMimeType(mimetype);
0051     f.setUploadedAt(timeUploaded);
0052     f.setFileId(fileId);
0053     f.setRid(rId);
0054     f.setComplete(complete);
0055 
0056     QCOMPARE(f.url(), url);
0057     QCOMPARE(f.fileName(), name);
0058     QCOMPARE(f.description(), description);
0059     QCOMPARE(f.userId(), userId);
0060     QCOMPARE(f.mimeType(), mimetype);
0061     QCOMPARE(f.uploadedAt(), timeUploaded);
0062     QCOMPARE(f.fileId(), fileId);
0063     QCOMPARE(f.rid(), rId);
0064     QCOMPARE(f.complete(), complete);
0065 
0066     complete = true;
0067     f.setComplete(complete);
0068     QCOMPARE(f.complete(), complete);
0069 }
0070 
0071 void FileTest::shouldCopyValue()
0072 {
0073     File f;
0074     const QString url = QStringLiteral("foo");
0075     const QString name = QStringLiteral("bla");
0076     const QString description = QStringLiteral("des");
0077     const QString userId = QStringLiteral("ble");
0078     const QString mimetype = QStringLiteral("ble1");
0079     const QString fileId = QStringLiteral("blabla");
0080     const QString rId = QStringLiteral("blabla22");
0081     const qint64 timeUploaded = 55;
0082     bool complete = true;
0083     f.setComplete(complete);
0084     f.setUrl(url);
0085     f.setFileName(name);
0086     f.setDescription(description);
0087     f.setUserId(userId);
0088     f.setMimeType(mimetype);
0089     f.setUploadedAt(timeUploaded);
0090     f.setFileId(fileId);
0091     f.setRid(rId);
0092 
0093     File f2 = f;
0094     QCOMPARE(f2, f);
0095 }
0096 
0097 void FileTest::shouldParseFile_data()
0098 {
0099     QTest::addColumn<QString>("fileName");
0100     QTest::addColumn<File>("expectedFile");
0101     QTest::addColumn<bool>("usingRestApi");
0102     {
0103         File expected;
0104         expected.setFileName(QStringLiteral("191135.jpg"));
0105         expected.setUrl(QStringLiteral("/ufs/FileSystem:Uploads/ybWLKB4FpCkzQXsa/191135.jpg"));
0106         expected.setUserId(QStringLiteral("aX7va58FuNuq4bcti"));
0107         expected.setDescription(QString());
0108         expected.setMimeType(QStringLiteral("image/jpeg"));
0109         expected.setUploadedAt(1507828418338);
0110         expected.setFileId(QStringLiteral("ybWLKB4FepCkzQXsa"));
0111         expected.setRid(QStringLiteral("GENERAL"));
0112         QTest::newRow("roomfile1") << QStringLiteral("roomfile1") << expected << false;
0113     }
0114     {
0115         // RestAPI
0116         File expected;
0117         expected.setFileName(QStringLiteral("Clipboard - February 7, 2018 8:59 AM"));
0118         expected.setUrl(QStringLiteral("/ufs/FileSystem:Uploads/AoqRSa6GMt3wXCeSo/Clipboard%20-%20February%207,%202018%208:59%20AM"));
0119         expected.setUserId(QStringLiteral("vEETYfDxakqpM88Zt"));
0120         expected.setDescription(QString());
0121         expected.setMimeType(QStringLiteral("image/png"));
0122         expected.setUploadedAt(1517990371911);
0123         expected.setFileId(QStringLiteral("AoqRSa6GMt3wXCeSo"));
0124         expected.setRid(QStringLiteral("GENERAL"));
0125         expected.setUserName(QStringLiteral("bli"));
0126         expected.setComplete(true);
0127         QTest::newRow("roomfile1-restapi") << QStringLiteral("roomfile1") << expected << true;
0128     }
0129 }
0130 
0131 void FileTest::shouldParseFile()
0132 {
0133     QFETCH(QString, fileName);
0134     QFETCH(File, expectedFile);
0135     QFETCH(bool, usingRestApi);
0136     QString originalJsonFile;
0137     if (usingRestApi) {
0138         originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/restapi/") + fileName + QLatin1String(".json");
0139     } else {
0140         originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/") + fileName + QLatin1String(".json");
0141     }
0142     const QJsonObject fields = AutoTestHelper::loadJsonObject(originalJsonFile);
0143 
0144     File newFile;
0145     newFile.parseFile(fields, usingRestApi);
0146     const bool equal = (newFile == expectedFile);
0147     if (!equal) {
0148         qDebug() << " current value " << newFile;
0149         qDebug() << " expected value " << expectedFile;
0150     }
0151     QVERIFY(equal);
0152 }
0153 
0154 #include "moc_filetest.cpp"