~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to src/libs/kernel/tests/AccountsCommandTester.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2017 Dag Andersen <danders@get2net.dk>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
#include "AccountsCommandTester.h"
 
20
 
 
21
#include "kptdatetime.h"
 
22
#include "kptcommand.h"
 
23
#include "kptproject.h"
 
24
#include "kptcalendar.h"
 
25
#include "kptresource.h"
 
26
#include "kptnode.h"
 
27
#include "kpttask.h"
 
28
#include "kptschedule.h"
 
29
 
 
30
#include <QTest>
 
31
#include <kundo2stack.h>
 
32
 
 
33
namespace QTest
 
34
{
 
35
    template<>
 
36
            char *toString(const KPlato::DateTime &dt)
 
37
    {
 
38
        return toString( dt.toString() );
 
39
    }
 
40
}
 
41
 
 
42
 
 
43
namespace KPlato
 
44
{
 
45
 
 
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();
 
52
 
 
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();
 
59
        }
 
60
    }
 
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();
 
65
        }
 
66
    }
 
67
}
 
68
 
 
69
void AccountsCommandTester::printSchedulingLog( const ScheduleManager &sm ) const
 
70
{
 
71
    qDebug()<<"Scheduling log ---------------------------------";
 
72
    foreach ( const QString &s, sm.expected()->logMessages() ) {
 
73
        qDebug()<<s;
 
74
    }
 
75
}
 
76
 
 
77
void AccountsCommandTester::init()
 
78
{
 
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);
 
87
 
 
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 );
 
93
    QTime t1( 9, 0, 0 );
 
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 );
 
100
    }
 
101
    m_project->addCalendar( m_calendar );
 
102
 
 
103
 
 
104
    ResourceGroup *g = new ResourceGroup();
 
105
    g->setName( "G1" );
 
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 );
 
111
 
 
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 );
 
118
}
 
119
 
 
120
void AccountsCommandTester::cleanup()
 
121
{
 
122
    delete m_project;
 
123
}
 
124
 
 
125
void AccountsCommandTester::addAccount()
 
126
{
 
127
    Account *a1 = new Account();
 
128
    a1->setName("a1");
 
129
    AddAccountCmd *cmd1 = new AddAccountCmd(*m_project, a1);
 
130
    cmd1->redo();
 
131
 
 
132
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
133
 
 
134
    cmd1->undo();
 
135
    QCOMPARE(m_project->accounts().allAccounts().count(), 0);
 
136
 
 
137
    delete cmd1;
 
138
 
 
139
    a1 = new Account();
 
140
    a1->setName("a1");
 
141
    cmd1 = new AddAccountCmd(*m_project, a1);
 
142
    cmd1->redo();
 
143
 
 
144
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
145
 
 
146
    Account *a2 = new Account();
 
147
    a2->setName("a2");
 
148
    AddAccountCmd *cmd2 = new AddAccountCmd(*m_project, a2, a1);
 
149
    cmd2->redo();
 
150
 
 
151
    QCOMPARE(m_project->accounts().allAccounts().count(), 2);
 
152
 
 
153
    cmd2->undo();
 
154
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
155
    cmd1->undo();
 
156
    QCOMPARE(m_project->accounts().allAccounts().count(), 0);
 
157
 
 
158
    delete cmd2;
 
159
    delete cmd1;
 
160
}
 
161
 
 
162
void AccountsCommandTester::removeAccount()
 
163
{
 
164
    Account *a1 = new Account();
 
165
    a1->setName("a1");
 
166
    m_project->accounts().insert(a1);
 
167
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
168
 
 
169
 
 
170
 
 
171
    RemoveAccountCmd *cmd1 = new RemoveAccountCmd(*m_project, a1);
 
172
    cmd1->redo();
 
173
    QCOMPARE(m_project->accounts().allAccounts().count(), 0);
 
174
 
 
175
    cmd1->undo();
 
176
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
177
 
 
178
    cmd1->redo();
 
179
    delete cmd1;
 
180
    QCOMPARE(m_project->accounts().allAccounts().count(), 0);
 
181
 
 
182
    a1 = new Account();
 
183
    a1->setName("a1");
 
184
    m_project->accounts().insert(a1);
 
185
    Account *a2 = new Account();
 
186
    a2->setName("a2");
 
187
    m_project->accounts().insert(a2, a1);
 
188
    QCOMPARE(m_project->accounts().allAccounts().count(), 2);
 
189
 
 
190
    cmd1 = new RemoveAccountCmd(*m_project, a1);
 
191
    cmd1->redo();
 
192
    QCOMPARE(m_project->accounts().allAccounts().count(), 0);
 
193
 
 
194
    cmd1->undo();
 
195
    QCOMPARE(m_project->accounts().allAccounts().count(), 2);
 
196
 
 
197
    RemoveAccountCmd *cmd2 = new RemoveAccountCmd(*m_project, a2);
 
198
    cmd2->redo();
 
199
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
200
 
 
201
    cmd2->undo();
 
202
    QCOMPARE(m_project->accounts().allAccounts().count(), 2);
 
203
 
 
204
    cmd2->redo();
 
205
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
206
 
 
207
    delete cmd2; // should delete a2
 
208
    delete cmd1; // should not delete a1
 
209
}
 
210
 
 
211
void AccountsCommandTester::costPlace()
 
212
{
 
213
    KUndo2QStack cmds;
 
214
    Account *a1 = new Account();
 
215
    a1->setName("a1");
 
216
    cmds.push(new AddAccountCmd(*m_project, a1));
 
217
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
218
 
 
219
    Account *a2 = new Account();
 
220
    a2->setName("a2");
 
221
    cmds.push(new AddAccountCmd(*m_project, a2));
 
222
    QCOMPARE(m_project->accounts().allAccounts().count(), 2);
 
223
 
 
224
    Account *a3 = new Account();
 
225
    a3->setName("a3");
 
226
    cmds.push(new AddAccountCmd(*m_project, a3));
 
227
    QCOMPARE(m_project->accounts().allAccounts().count(), 3);
 
228
 
 
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);
 
235
 
 
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);
 
240
    cmds.undo();
 
241
    QCOMPARE(m_task->runningAccount(), a1);
 
242
    QCOMPARE(m_task->startupAccount(), a2);
 
243
    QCOMPARE(m_task->shutdownAccount(), a3);
 
244
 
 
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);
 
249
    cmds.undo();
 
250
    QCOMPARE(m_task->runningAccount(), a1);
 
251
    QCOMPARE(m_task->startupAccount(), a2);
 
252
    QCOMPARE(m_task->shutdownAccount(), a3);
 
253
 
 
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);
 
258
    cmds.undo();
 
259
    QCOMPARE(m_task->runningAccount(), a1);
 
260
    QCOMPARE(m_task->startupAccount(), a2);
 
261
    QCOMPARE(m_task->shutdownAccount(), a3);
 
262
 
 
263
    cmds.push(new ResourceModifyAccountCmd(*m_resource, 0, a1));
 
264
    QCOMPARE(m_resource->account(), a1);
 
265
 
 
266
    cmds.push(new RemoveAccountCmd(*m_project, a1));
 
267
    QVERIFY(m_task->runningAccount() == 0);
 
268
    QVERIFY(m_resource->account() == 0);
 
269
    cmds.undo();
 
270
    QCOMPARE(m_task->runningAccount(), a1);
 
271
    QCOMPARE(m_resource->account(), a1);
 
272
 
 
273
    // test when same account is used for running/startup/shutdown
 
274
    while (cmds.canUndo()) {
 
275
        cmds.undo();
 
276
    }
 
277
    a1 = new Account();
 
278
    a1->setName("a1");
 
279
    cmds.push(new AddAccountCmd(*m_project, a1));
 
280
    QCOMPARE(m_project->accounts().allAccounts().count(), 1);
 
281
 
 
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);
 
288
 
 
289
    cmds.undo();
 
290
    QCOMPARE(m_task->runningAccount(), a1);
 
291
    QCOMPARE(m_task->startupAccount(), a1);
 
292
    QVERIFY(m_task->shutdownAccount() == 0);
 
293
 
 
294
    cmds.undo();
 
295
    QCOMPARE(m_task->runningAccount(), a1);
 
296
    QVERIFY(m_task->startupAccount() == 0);
 
297
    QVERIFY(m_task->shutdownAccount() == 0);
 
298
 
 
299
    cmds.undo();
 
300
    QVERIFY(m_task->runningAccount() == 0);
 
301
    QVERIFY(m_task->startupAccount() == 0);
 
302
    QVERIFY(m_task->shutdownAccount() == 0);
 
303
 
 
304
    cmds.redo();
 
305
    QCOMPARE(m_task->runningAccount(), a1);
 
306
    QVERIFY(m_task->startupAccount() == 0);
 
307
    QVERIFY(m_task->shutdownAccount() == 0);
 
308
 
 
309
    cmds.redo();
 
310
    QCOMPARE(m_task->runningAccount(), a1);
 
311
    QCOMPARE(m_task->startupAccount(), a1);
 
312
    QVERIFY(m_task->shutdownAccount() == 0);
 
313
 
 
314
    cmds.redo();
 
315
    QCOMPARE(m_task->runningAccount(), a1);
 
316
    QCOMPARE(m_task->startupAccount(), a1);
 
317
    QCOMPARE(m_task->shutdownAccount(), a1);
 
318
}
 
319
 
 
320
} //namespace KPlato
 
321
 
 
322
QTEST_GUILESS_MAIN( KPlato::AccountsCommandTester )