File indexing completed on 2024-05-12 05:22:08

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * move it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <https://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include <QObject>
0022 #include <QTest>
0023 
0024 #include "fakenetworkaccessmanagerfactory.h"
0025 #include "taskstestutils.h"
0026 #include "testutils.h"
0027 
0028 #include "account.h"
0029 #include "task.h"
0030 #include "taskmovejob.h"
0031 #include "types.h"
0032 
0033 using namespace KGAPI2;
0034 
0035 Q_DECLARE_METATYPE(QList<FakeNetworkAccessManager::Scenario>)
0036 Q_DECLARE_METATYPE(KGAPI2::TasksList)
0037 
0038 class TaskMoveJobTest : public QObject
0039 {
0040     Q_OBJECT
0041 private Q_SLOTS:
0042     void initTestCase()
0043     {
0044         NetworkAccessManagerFactory::setFactory(new FakeNetworkAccessManagerFactory);
0045     }
0046 
0047     void testMove_data()
0048     {
0049         QTest::addColumn<QList<FakeNetworkAccessManager::Scenario>>("scenarios");
0050         QTest::addColumn<TasksList>("tasks");
0051         QTest::addColumn<QString>("newParent");
0052 
0053         QTest::newRow("single move to top-level")
0054             << QList<FakeNetworkAccessManager::Scenario>{
0055                     scenarioFromFile(QFINDTESTDATA("data/task1_move_request.txt"),
0056                                      QFINDTESTDATA("data/task1_move_response.txt")),
0057                 }
0058             << TasksList{ taskFromFile(QFINDTESTDATA("data/task1.json")) }
0059             << QString();
0060 
0061         QTest::newRow("single move to new parent")
0062             << QList<FakeNetworkAccessManager::Scenario>{
0063                     scenarioFromFile(QFINDTESTDATA("data/task2_move_with_parent_request.txt"),
0064                                      QFINDTESTDATA("data/task2_move_response.txt")),
0065                 }
0066             << TasksList{ taskFromFile(QFINDTESTDATA("data/task2.json")) }
0067             << QStringLiteral("newMockParent");
0068 
0069         QTest::newRow("batch move") << QList<FakeNetworkAccessManager::Scenario>{scenarioFromFile(QFINDTESTDATA("data/task1_move_request.txt"),
0070                                                                                                   QFINDTESTDATA("data/task1_move_response.txt")),
0071                                                                                  scenarioFromFile(QFINDTESTDATA("data/task2_move_request.txt"),
0072                                                                                                   QFINDTESTDATA("data/task2_move_response.txt"))}
0073                                     << TasksList{taskFromFile(QFINDTESTDATA("data/task1.json")), taskFromFile(QFINDTESTDATA("data/task2.json"))} << QString();
0074     }
0075 
0076     void testMove()
0077     {
0078         QFETCH(QList<FakeNetworkAccessManager::Scenario>, scenarios);
0079         QFETCH(TasksList, tasks);
0080         QFETCH(QString, newParent);
0081 
0082         FakeNetworkAccessManagerFactory::get()->setScenarios(scenarios);
0083 
0084         auto account = AccountPtr::create(QStringLiteral("MockAccount"), QStringLiteral("MockToken"));
0085         TaskMoveJob *job = nullptr;
0086         if (tasks.count() == 1) {
0087             job = new TaskMoveJob(tasks.at(0), QStringLiteral("MockAccount"), newParent, account);
0088         } else {
0089             job = new TaskMoveJob(tasks, QStringLiteral("MockAccount"), newParent, account);
0090         }
0091         QVERIFY(execJob(job));
0092     }
0093 };
0094 
0095 QTEST_GUILESS_MAIN(TaskMoveJobTest)
0096 
0097 #include "taskmovejobtest.moc"