1
/* This file is part of the KDE project
2
Copyright (C) 2017 Dag Andersen <danders@get2net.dk>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
19
#include "AccountsCommandTester.h"
21
#include "kptdatetime.h"
22
#include "kptcommand.h"
23
#include "kptproject.h"
24
#include "kptcalendar.h"
25
#include "kptresource.h"
28
#include "kptschedule.h"
31
#include <kundo2stack.h>
36
char *toString(const KPlato::DateTime &dt)
38
return toString( dt.toString() );
46
void AccountsCommandTester::printDebug( long id ) const {
47
Project *p = m_project;
48
Resource *r = m_resource;
49
qDebug()<<"Debug info -------------------------------------";
50
qDebug()<<"project start time:"<<p->startTime().toString();
51
qDebug()<<"project end time :"<<p->endTime().toString();
53
qDebug()<<"Resource start:"<<r->startTime( id ).toString();
54
qDebug()<<"Resource end :"<<r->endTime( id ).toString();
55
qDebug()<<"Appointments:"<<r->numAppointments( id )<<"(internal)";
56
foreach ( Appointment *a, r->appointments( id ) ) {
57
foreach ( const AppointmentInterval &i, a->intervals().map() ) {
58
qDebug()<<" "<<i.startTime().toString()<<"-"<<i.endTime().toString()<<";"<<i.load();
61
qDebug()<<"Appointments:"<<r->numExternalAppointments()<<"(external)";
62
foreach ( Appointment *a, r->externalAppointmentList() ) {
63
foreach ( const AppointmentInterval &i, a->intervals().map() ) {
64
qDebug()<<" "<<i.startTime().toString()<<"-"<<i.endTime().toString()<<";"<<i.load();
69
void AccountsCommandTester::printSchedulingLog( const ScheduleManager &sm ) const
71
qDebug()<<"Scheduling log ---------------------------------";
72
foreach ( const QString &s, sm.expected()->logMessages() ) {
77
void AccountsCommandTester::init()
79
m_project = new Project();
80
m_project->setName( "P1" );
81
m_project->setId( m_project->uniqueNodeId() );
82
m_project->registerNodeId( m_project );
83
DateTime targetstart = DateTime( QDate::currentDate(), QTime(0,0,0) );
84
DateTime targetend = DateTime( targetstart.addDays( 3 ) );
85
m_project->setConstraintStartTime( targetstart );
86
m_project->setConstraintEndTime( targetend);
88
// standard worktime defines 8 hour day as default
89
QVERIFY( m_project->standardWorktime() );
90
QCOMPARE( m_project->standardWorktime()->day(), 8.0 );
91
m_calendar = new Calendar( "Test" );
92
m_calendar->setDefault( true );
94
QTime t2 ( 17, 0, 0 );
95
int length = t1.msecsTo( t2 );
96
for ( int i=1; i <= 7; ++i ) {
97
CalendarDay *d = m_calendar->weekday( i );
98
d->setState( CalendarDay::Working );
99
d->addInterval( t1, length );
101
m_project->addCalendar( m_calendar );
104
ResourceGroup *g = new ResourceGroup();
106
m_project->addResourceGroup( g );
107
m_resource = new Resource();
108
m_resource->setName( "R1" );
109
m_resource->setCalendar( m_calendar );
110
m_project->addResource( g, m_resource );
112
m_task = m_project->createTask();
113
m_task->setName( "T1" );
114
m_project->addTask( m_task, m_project );
115
m_task->estimate()->setUnit( Duration::Unit_h );
116
m_task->estimate()->setExpectedEstimate( 8.0 );
117
m_task->estimate()->setType( Estimate::Type_Effort );
120
void AccountsCommandTester::cleanup()
125
void AccountsCommandTester::addAccount()
127
Account *a1 = new Account();
129
AddAccountCmd *cmd1 = new AddAccountCmd(*m_project, a1);
132
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
135
QCOMPARE(m_project->accounts().allAccounts().count(), 0);
141
cmd1 = new AddAccountCmd(*m_project, a1);
144
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
146
Account *a2 = new Account();
148
AddAccountCmd *cmd2 = new AddAccountCmd(*m_project, a2, a1);
151
QCOMPARE(m_project->accounts().allAccounts().count(), 2);
154
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
156
QCOMPARE(m_project->accounts().allAccounts().count(), 0);
162
void AccountsCommandTester::removeAccount()
164
Account *a1 = new Account();
166
m_project->accounts().insert(a1);
167
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
171
RemoveAccountCmd *cmd1 = new RemoveAccountCmd(*m_project, a1);
173
QCOMPARE(m_project->accounts().allAccounts().count(), 0);
176
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
180
QCOMPARE(m_project->accounts().allAccounts().count(), 0);
184
m_project->accounts().insert(a1);
185
Account *a2 = new Account();
187
m_project->accounts().insert(a2, a1);
188
QCOMPARE(m_project->accounts().allAccounts().count(), 2);
190
cmd1 = new RemoveAccountCmd(*m_project, a1);
192
QCOMPARE(m_project->accounts().allAccounts().count(), 0);
195
QCOMPARE(m_project->accounts().allAccounts().count(), 2);
197
RemoveAccountCmd *cmd2 = new RemoveAccountCmd(*m_project, a2);
199
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
202
QCOMPARE(m_project->accounts().allAccounts().count(), 2);
205
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
207
delete cmd2; // should delete a2
208
delete cmd1; // should not delete a1
211
void AccountsCommandTester::costPlace()
214
Account *a1 = new Account();
216
cmds.push(new AddAccountCmd(*m_project, a1));
217
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
219
Account *a2 = new Account();
221
cmds.push(new AddAccountCmd(*m_project, a2));
222
QCOMPARE(m_project->accounts().allAccounts().count(), 2);
224
Account *a3 = new Account();
226
cmds.push(new AddAccountCmd(*m_project, a3));
227
QCOMPARE(m_project->accounts().allAccounts().count(), 3);
229
cmds.push(new NodeModifyRunningAccountCmd(*m_task, 0, a1));
230
QCOMPARE(m_task->runningAccount(), a1);
231
cmds.push(new NodeModifyStartupAccountCmd(*m_task, 0, a2));
232
QCOMPARE(m_task->startupAccount(), a2);
233
cmds.push(new NodeModifyShutdownAccountCmd(*m_task, 0, a3));
234
QCOMPARE(m_task->shutdownAccount(), a3);
236
cmds.push(new RemoveAccountCmd(*m_project, a1));
237
QVERIFY(m_task->runningAccount() == 0);
238
QCOMPARE(m_task->startupAccount(), a2);
239
QCOMPARE(m_task->shutdownAccount(), a3);
241
QCOMPARE(m_task->runningAccount(), a1);
242
QCOMPARE(m_task->startupAccount(), a2);
243
QCOMPARE(m_task->shutdownAccount(), a3);
245
cmds.push(new RemoveAccountCmd(*m_project, a2));
246
QVERIFY(m_task->startupAccount() == 0);
247
QCOMPARE(m_task->runningAccount(), a1);
248
QCOMPARE(m_task->shutdownAccount(), a3);
250
QCOMPARE(m_task->runningAccount(), a1);
251
QCOMPARE(m_task->startupAccount(), a2);
252
QCOMPARE(m_task->shutdownAccount(), a3);
254
cmds.push(new RemoveAccountCmd(*m_project, a3));
255
QVERIFY(m_task->shutdownAccount() == 0);
256
QCOMPARE(m_task->runningAccount(), a1);
257
QCOMPARE(m_task->startupAccount(), a2);
259
QCOMPARE(m_task->runningAccount(), a1);
260
QCOMPARE(m_task->startupAccount(), a2);
261
QCOMPARE(m_task->shutdownAccount(), a3);
263
cmds.push(new ResourceModifyAccountCmd(*m_resource, 0, a1));
264
QCOMPARE(m_resource->account(), a1);
266
cmds.push(new RemoveAccountCmd(*m_project, a1));
267
QVERIFY(m_task->runningAccount() == 0);
268
QVERIFY(m_resource->account() == 0);
270
QCOMPARE(m_task->runningAccount(), a1);
271
QCOMPARE(m_resource->account(), a1);
273
// test when same account is used for running/startup/shutdown
274
while (cmds.canUndo()) {
279
cmds.push(new AddAccountCmd(*m_project, a1));
280
QCOMPARE(m_project->accounts().allAccounts().count(), 1);
282
cmds.push(new NodeModifyRunningAccountCmd(*m_task, 0, a1));
283
QCOMPARE(m_task->runningAccount(), a1);
284
cmds.push(new NodeModifyStartupAccountCmd(*m_task, 0, a1));
285
QCOMPARE(m_task->startupAccount(), a1);
286
cmds.push(new NodeModifyShutdownAccountCmd(*m_task, 0, a1));
287
QCOMPARE(m_task->shutdownAccount(), a1);
290
QCOMPARE(m_task->runningAccount(), a1);
291
QCOMPARE(m_task->startupAccount(), a1);
292
QVERIFY(m_task->shutdownAccount() == 0);
295
QCOMPARE(m_task->runningAccount(), a1);
296
QVERIFY(m_task->startupAccount() == 0);
297
QVERIFY(m_task->shutdownAccount() == 0);
300
QVERIFY(m_task->runningAccount() == 0);
301
QVERIFY(m_task->startupAccount() == 0);
302
QVERIFY(m_task->shutdownAccount() == 0);
305
QCOMPARE(m_task->runningAccount(), a1);
306
QVERIFY(m_task->startupAccount() == 0);
307
QVERIFY(m_task->shutdownAccount() == 0);
310
QCOMPARE(m_task->runningAccount(), a1);
311
QCOMPARE(m_task->startupAccount(), a1);
312
QVERIFY(m_task->shutdownAccount() == 0);
315
QCOMPARE(m_task->runningAccount(), a1);
316
QCOMPARE(m_task->startupAccount(), a1);
317
QCOMPARE(m_task->shutdownAccount(), a1);
322
QTEST_GUILESS_MAIN( KPlato::AccountsCommandTester )