Debian Codegraph @

src/plugins/schedulers/tj/tests/TJSchedulerTester.cpp

raw
1 /* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2021 Dag Andersen <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 // clazy:excludeall=qstring-arg
8 #include "TJSchedulerTester.h"
9
10 #include "PlanTJScheduler.h"
11
12 #include "plan/kptmaindocument.h"
13 #include "kptpart.h"
14 #include "kptcommand.h"
15 #include "kptcalendar.h"
16 #include "kptdatetime.h"
17 #include "kptresource.h"
18 #include "kptnode.h"
19 #include "kpttask.h"
20 #include "kptproject.h"
21 #include "kptschedule.h"
22 #include "kptschedulerplugin.h"
23 #include "SchedulingContext.h"
24
25 #include <ExtraProperties.h>
26
27 #include <QMultiMap>
28 #include <QTest>
29
30 #include "tests/DateTimeTester.h"
31
32 #include "tests/debug.cpp"
33
34 using namespace KPlato;
35
36 void TJSchedulerTester::init()
37 {
38 ulong granularity = 60*60*1000;
39 m_scheduler = new PlanTJScheduler(granularity);
40 }
41
42 void TJSchedulerTester::cleanup()
43 {
44 delete m_scheduler;
45 }
46
47 void TJSchedulerTester::populateSchedulingContext(SchedulingContext &context, const QString &name, const QList<Part*> &projects, const QList<Part*> &bookings) const
48 {
49 context.project = new Project();
50 context.project->setName(name);
51 context.project->setConstraintStartTime(DateTime());
52 context.project->setConstraintEndTime(DateTime());
53 int prio = projects.count();
54 for (const auto part : std::as_const(projects)) {
55 auto doc = part->document();
56 auto project = doc->project();
57 ScheduleManager *sm = nullptr;
58 ScheduleManager *parentManager = nullptr;
59 if (project->isStarted()) {
60 parentManager = project->scheduleManagers().value(0);
61 sm = project->createScheduleManager(parentManager);
62 sm->setParentManager(parentManager);
63 } else {
64 sm = new KPlato::ScheduleManager(*project, project->uniqueScheduleName());
65 }
66 AddScheduleManagerCmd cmd(*project, sm, 0);
67 cmd.redo();
68 sm->createSchedules();
69 doc->setProperty(SCHEDULEMANAGERNAME, sm->name());
70 project->setProperty(SCHEDULEMANAGERNAME, sm->name());
71 project->initiateCalculation(*(sm->expected()));
72 project->setCurrentScheduleManager(sm);
73 project->setCurrentSchedule(sm->expected()->id());
74 if (parentManager) {
75 project->setParentSchedule(parentManager->expected());
76 }
77 context.projects.insert(prio--, doc);
78
79 if (!context.project->constraintStartTime().isValid()) {
80 context.project->setConstraintStartTime(project->constraintStartTime());
81 context.project->setConstraintEndTime(project->constraintEndTime());
82 } else {
83 context.project->setConstraintStartTime(std::min(project->constraintStartTime(), context.project->constraintStartTime()));
84 context.project->setConstraintEndTime(std::max(project->constraintEndTime(), context.project->constraintEndTime()));
85 }
86 }
87 for (const auto part : bookings) {
88 auto doc = part->document();
89 auto project = doc->project();
90 auto sm = project->scheduleManagers().value(0);
91 project->setCurrentScheduleManager(sm);
92 project->setCurrentSchedule(sm->expected()->id());
93 doc->setProperty(SCHEDULEMANAGERNAME, sm->name());
94 context.resourceBookings.append(doc);
95 }
96 }
97
98 Part *TJSchedulerTester::loadDocument(const QString &dir, const QString &fname)
99 {
100 const QString localFilePath = dir + fname;
101 if (!QFile::exists(localFilePath)) {
102 qDebug()<<"File does not exist:"<<localFilePath;
103 return nullptr;
104 }
105 Part *part = new Part(nullptr);
106 MainDocument *doc = new MainDocument(part, false);
107 part->setDocument(doc);
108 doc->setProgressEnabled(false);
109 doc->setProperty(NOUI, true); // avoid possible error message
110 doc->setUrl(QUrl::fromUserInput(localFilePath));
111 if (!doc->openLocalFile(localFilePath)) {
112 delete part;
113 return nullptr;
114 }
115 return part;
116 }
117
118 QList<Part*> TJSchedulerTester::loadDocuments(QString &dir, QList<QString> files)
119 {
120 QList<Part*> parts;
121 for (const QString &fname : files) {
122 Part *part = loadDocument(dir, fname);
123 if (part) {
124 parts << part;
125 } else {
126 qDebug()<<"Failed to load:"<<dir<<fname;
127 }
128 }
129 return parts;
130 }
131
132 void TJSchedulerTester::deleteAll(const QList<Part*> parts)
133 {
134 for (auto part : parts) {
135 delete part->document();
136 delete part;
137 }
138 }
139
140 void TJSchedulerTester::testSingleProject()
141 {
142 const auto projectFiles = QStringList() << "Test 1.plan";
143 QString dir = QFINDTESTDATA("data/multi/schedule/");
144 QList<Part*> projects = loadDocuments(dir, projectFiles);
145 QCOMPARE(projects.count(), projectFiles.count());
146
147 SchedulingContext context;
148 context.scheduleInParallel = true;
149 populateSchedulingContext(context, "Test Single Project", projects);
150
151 m_scheduler->schedule(context);
152 auto project = context.projects.first()->project();
153 Debug::print(project, "--", true);
154 QCOMPARE(project->childNode(0)->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 8));
155 QCOMPARE(project->childNode(1)->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 9));
156
157 deleteAll(projects);
158 }
159
160 void TJSchedulerTester::testSingleProjectWithBookings()
161 {
162 const auto projectFiles = QStringList() << "Test 1.plan";
163 QString dir = QFINDTESTDATA("data/multi/schedule/");
164 QList<Part*> projects = loadDocuments(dir, projectFiles);
165 QCOMPARE(projects.count(), projectFiles.count());
166
167 const auto bookingFiles = QStringList() << "Booking 1.plan";
168 dir = QFINDTESTDATA("data/multi/bookings/");
169 const QList<Part*> bookings = loadDocuments(dir, bookingFiles);
170 QCOMPARE(bookings.count(), bookingFiles.count());
171
172 SchedulingContext context;
173 context.scheduleInParallel = true;
174 populateSchedulingContext(context, "Test Single Project With Bookings", projects, bookings);
175 m_scheduler->schedule(context);
176 // Booking 1: R1 booked 2021-04-08, 2021-04-09
177 auto project = context.projects.first()->project();
178 // Debug::print(project, "--", true);
179 for (auto &log : std::as_const(context.log)) {
180 qInfo()<<log.formatMsg();
181 }
182 Debug::print(project, "", true);
183 QCOMPARE(project->childNode(0)->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 10));
184 QCOMPARE(project->childNode(1)->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 11));
185
186 deleteAll(projects);
187 deleteAll(bookings);
188 }
189
190 void TJSchedulerTester::testMultiple()
191 {
192 const auto projectFiles = QStringList() << "Test 1.plan" << "Test 2.plan";
193 QString dir = QFINDTESTDATA("data/multi/schedule/");
194 QList<Part*> projects = loadDocuments(dir, projectFiles);
195 QCOMPARE(projects.count(), 2);
196
197 SchedulingContext context;
198 context.scheduleInParallel = true;
199 populateSchedulingContext(context, "Test Multiple Project", projects);
200 m_scheduler->schedule(context);
201 // for (const Schedule::Log &l : std::as_const(context.log)) qDebug()<<l;
202 auto project = projects.value(0)->document()->project();
203 QCOMPARE(project->childNode(0)->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 8));
204 QCOMPARE(project->childNode(1)->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 9));
205 project = projects.value(1)->document()->project();
206 QCOMPARE(project->childNode(0)->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 10));
207 QCOMPARE(project->childNode(1)->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 11));
208
209 deleteAll(projects);
210 }
211
212 void TJSchedulerTester::testMultipleWithBookingsParalell()
213 {
214 const auto projectFiles = QStringList() << "Test 1.plan" << "Test 2.plan";
215 QString dir = QFINDTESTDATA("data/multi/schedule/");
216 QList<Part*> projects = loadDocuments(dir, projectFiles);
217 QCOMPARE(projects.count(), projectFiles.count());
218
219 const auto bookingFiles = QStringList() << "Booking 1.plan" << "Booking 2.plan";
220 dir = QFINDTESTDATA("data/multi/bookings/");
221 QList<Part*> bookings = loadDocuments(dir, bookingFiles);
222 QCOMPARE(bookings.count(), bookingFiles.count());
223
224 SchedulingContext context;
225 context.scheduleInParallel = true;
226 populateSchedulingContext(context, "Test Multiple Project With Bookings", projects, bookings);
227 m_scheduler->schedule(context);
228 // for (const Schedule::Log &l : std::as_const(context.log)) qDebug()<<l;
229 // Booking 1: R1 booked 2021-04-08, 2021-04-09
230 // Booking 2: R1 booked 2021-04-12, 2021-04-13
231 auto project = projects.value(0)->document()->project();
232 QCOMPARE(project->childNode(0)->startTime().date(), QDate(2021, 4, 10));
233 QCOMPARE(project->childNode(1)->startTime().date(), QDate(2021, 4, 11));
234 project = projects.value(1)->document()->project();
235 QCOMPARE(project->childNode(0)->startTime().date(), QDate(2021, 4, 14));
236 QCOMPARE(project->childNode(1)->startTime().date(), QDate(2021, 4, 15));
237
238 deleteAll(projects);
239 deleteAll(bookings);
240 }
241
242 void TJSchedulerTester::testMultipleWithBookingsSequential()
243 {
244 const auto projectFiles = QStringList() << "Test 1.plan" << "Test 2.plan";
245 QString dir = QFINDTESTDATA("data/multi/schedule/");
246 QList<Part*> projects = loadDocuments(dir, projectFiles);
247 QCOMPARE(projects.count(), projectFiles.count());
248
249 const auto bookingFiles = QStringList() << "Booking 1.plan" << "Booking 2.plan";
250 dir = QFINDTESTDATA("data/multi/bookings/");
251 QList<Part*> bookings = loadDocuments(dir, bookingFiles);
252 QCOMPARE(bookings.count(), bookingFiles.count());
253
254 SchedulingContext context;
255 context.scheduleInParallel = false;
256 populateSchedulingContext(context, "Test Multiple Project With Bookings", projects, bookings);
257 m_scheduler->schedule(context);
258 // for (const Schedule::Log &l : std::as_const(context.log)) qDebug()<<l;
259 // Booking 1: R1 booked 2021-04-08, 2021-04-09
260 // Booking 2: R1 booked 2021-04-12, 2021-04-13
261 auto project = projects.value(0)->document()->project();
262 QCOMPARE(project->childNode(0)->startTime().date(), QDate(2021, 4, 10));
263 QCOMPARE(project->childNode(1)->startTime().date(), QDate(2021, 4, 11));
264 project = projects.value(1)->document()->project();
265 QCOMPARE(project->childNode(0)->startTime().date(), QDate(2021, 4, 14));
266 QCOMPARE(project->childNode(1)->startTime().date(), QDate(2021, 4, 15));
267
268 deleteAll(projects);
269 deleteAll(bookings);
270 }
271
272 void TJSchedulerTester::testRecalculate()
273 {
274 const auto projectFiles = QStringList() << "Test Recalculate 1.plan";
275 QString dir = QFINDTESTDATA("data/multi/schedule/");
276 QList<Part*> projects = loadDocuments(dir, projectFiles);
277 QCOMPARE(projects.count(), 1);
278
279 SchedulingContext context;
280 context.scheduleInParallel = true;
281 populateSchedulingContext(context, "Test Recalculate Project", projects);
282 QVERIFY(projects.value(0)->document()->project()->childNode(0)->schedule()->parent());
283 context.calculateFrom = QDate(2021, 4, 26).startOfDay();
284 m_scheduler->schedule(context);
285 //for (const Schedule::Log &l : std::as_const(context.log)) qDebug()<<l;
286 // T1 Recalculate 1: Two first days has been completed, 3 last days moved
287 auto project = projects.value(0)->document()->project();
288 auto T1 = static_cast<Task*>(project->childNode(0));
289 auto T2 = static_cast<Task*>(project->childNode(1));
290 auto Length = static_cast<Task*>(project->childNode(2));
291 QCOMPARE(T1->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 19)); // as before
292 QCOMPARE(T1->endTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 28));
293 QCOMPARE(T2->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 29));
294
295 QCOMPARE(Length->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 19));
296 QCOMPARE(Length->endTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 23));
297
298 deleteAll(projects);
299 }
300
301 void TJSchedulerTester::testRecalculateMultiple()
302 {
303 const auto projectFiles = QStringList() << "Test 1.plan" << "Test Recalculate 1.plan";
304 QString dir = QFINDTESTDATA("data/multi/schedule/");
305 QList<Part*> projects = loadDocuments(dir, projectFiles);
306 QCOMPARE(projects.count(), projectFiles.count());
307
308 SchedulingContext context;
309 context.scheduleInParallel = true;
310 populateSchedulingContext(context, "Test Recalculate Multiple Projects", projects);
311 QVERIFY(projects.value(0)->document()->project()->childNode(0)->schedule()->parent());
312
313 auto project = projects.value(0)->document()->project();
314 auto T1 = static_cast<Task*>(project->childNode(0));
315 auto T2 = static_cast<Task*>(project->childNode(1));
316
317 context.calculateFrom = QDateTime(QDate(2021, 4, 26), QTime());
318 m_scheduler->schedule(context);
319 //for (const Schedule::Log &l : std::as_const(context.log)) qDebug()<<l;
320 //qDebug()<<"Check project"<<project;
321 QCOMPARE(T1->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 26));
322 QCOMPARE(T1->endTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 26));
323 QCOMPARE(T2->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 30));
324
325 project = projects.value(1)->document()->project();
326 T1 = static_cast<Task*>(project->childNode(0));
327 T2 = static_cast<Task*>(project->childNode(1));
328 auto Length = static_cast<Task*>(project->childNode(2));
329
330 //qDebug()<<"Check project"<<project;
331 // T1 Recalculate 1: Two first days has been completed, 3 last days moved
332 QCOMPARE(T1->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 19)); // as before
333 QCOMPARE(T1->endTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 29));
334 QCOMPARE(T2->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 5, 1));
335
336 qInfo()<<"Length:"<<Length->startTime().toTimeZone(project->timeZone());
337 QCOMPARE(Length->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 19));
338 QCOMPARE(Length->endTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 23));
339
340 deleteAll(projects);
341 }
342
343 void TJSchedulerTester::testRecalculateMultipleSeq()
344 {
345 const auto projectFiles = QStringList() << "Test 1.plan" << "Test Recalculate 1.plan";
346 QString dir = QFINDTESTDATA("data/multi/schedule/");
347 QList<Part*> projects = loadDocuments(dir, projectFiles);
348 QCOMPARE(projects.count(), projectFiles.count());
349
350 SchedulingContext context;
351 context.scheduleInParallel = false;
352 populateSchedulingContext(context, "Test Recalculate Multiple Projects", projects);
353 QVERIFY(projects.value(0)->document()->project()->childNode(0)->schedule()->parent());
354
355 auto project = projects.value(0)->document()->project();
356 auto T1 = static_cast<Task*>(project->childNode(0));
357 auto T2 = static_cast<Task*>(project->childNode(1));
358
359 context.calculateFrom = QDateTime(QDate(2021, 4, 26), QTime());
360 m_scheduler->schedule(context);
361 //for (const Schedule::Log &l : std::as_const(context.log)) qDebug()<<l;
362 //qDebug()<<"Check project"<<project;
363 QCOMPARE(T1->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 26));
364 QCOMPARE(T1->endTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 26));
365 QCOMPARE(T2->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 27));
366
367 project = projects.value(1)->document()->project();
368 T1 = static_cast<Task*>(project->childNode(0));
369 T2 = static_cast<Task*>(project->childNode(1));
370 auto Length = static_cast<Task*>(project->childNode(2));
371
372 //qDebug()<<"Check project"<<project;
373 // T1 Recalculate 1: Two first days has been completed, 3 last days moved
374 QCOMPARE(T1->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 19)); // as before
375 QCOMPARE(T1->endTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 30)); // continued 28/29/30
376 QCOMPARE(T2->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 5, 1));
377
378 qInfo()<<"Length:"<<Length->startTime().toTimeZone(project->timeZone());
379 QCOMPARE(Length->startTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 19));
380 QCOMPARE(Length->endTime().toTimeZone(project->timeZone()).date(), QDate(2021, 4, 23));
381
382 deleteAll(projects);
383 }
384
385 QTEST_MAIN(KPlato::TJSchedulerTester)