summaryrefslogtreecommitdiffstats
path: root/kmymoney2/reports/pivotgridtest.cpp
blob: 397491d853d7f3c45224e48580d9f520d7325770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/***************************************************************************
                          pivotgridtest.cpp
                          -------------------
    copyright            : (C) 2002-2005 by Thomas Baumgart
    email                : ipwizard@users.sourceforge.net
                           Ace Jones <ace.j@hotpop.com>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/*
#include <qvaluelist.h>
#include <qvaluevector.h>
#include <qdom.h>
#include <qfile.h>

#include <kdebug.h>
#include <kdeversion.h>
#include <kglobal.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kstandarddirs.h>

// DOH, mmreport.h uses this without including it!!
#include "../mymoney/mymoneyaccount.h"

#include "../mymoney/mymoneysecurity.h"
#include "../mymoney/mymoneyprice.h"
#include "../mymoney/mymoneyreport.h"
#include "../mymoney/mymoneystatement.h"
#include "../mymoney/storage/mymoneystoragedump.h"
#include "../mymoney/storage/mymoneystoragexml.h"
*/

#include "pivotgridtest.h"

#include "reportstestcommon.h"

#define private public
#include "../reports/pivotgrid.h"
#undef private

using namespace reports;
using namespace test;

PivotGridTest::PivotGridTest()
{
}

void PivotGridTest::setUp ()
{
  storage = new MyMoneySeqAccessMgr;
  file = MyMoneyFile::instance();
  file->attachStorage(storage);

  MyMoneyFileTransaction ft;
  file->addCurrency(MyMoneySecurity("CAD", "Canadian Dollar",        "C$"));
  file->addCurrency(MyMoneySecurity("USD", "US Dollar",              "$"));
  file->addCurrency(MyMoneySecurity("JPY", "Japanese Yen",           QChar(0x00A5), 100, 1));
  file->addCurrency(MyMoneySecurity("GBP", "British Pound",           "#"));
  file->setBaseCurrency(file->currency("USD"));

  MyMoneyPayee payeeTest("Test Payee");
  file->addPayee(payeeTest);
  MyMoneyPayee payeeTest2("Thomas Baumgart");
  file->addPayee(payeeTest2);

  acAsset = (MyMoneyFile::instance()->asset().id());
  acLiability = (MyMoneyFile::instance()->liability().id());
  acExpense = (MyMoneyFile::instance()->expense().id());
  acIncome = (MyMoneyFile::instance()->income().id());
  acChecking = makeAccount(QString("Checking Account"),MyMoneyAccount::Checkings,moCheckingOpen,QDate(2004,5,15),acAsset);
  acCredit = makeAccount(QString("Credit Card"),MyMoneyAccount::CreditCard,moCreditOpen,QDate(2004,7,15),acLiability);
  acSolo = makeAccount(QString("Solo"),MyMoneyAccount::Expense,0,QDate(2004,1,11),acExpense);
  acParent = makeAccount(QString("Parent"),MyMoneyAccount::Expense,0,QDate(2004,1,11),acExpense);
  acChild = makeAccount(QString("Child"),MyMoneyAccount::Expense,0,QDate(2004,2,11),acParent);
  acForeign = makeAccount(QString("Foreign"),MyMoneyAccount::Expense,0,QDate(2004,1,11),acExpense);

  acSecondChild = makeAccount(QString("Second Child"),MyMoneyAccount::Expense,0,QDate(2004,2,11),acParent);
  acGrandChild1 = makeAccount(QString("Grand Child 1"),MyMoneyAccount::Expense,0,QDate(2004,2,11),acChild);
  acGrandChild2 = makeAccount(QString("Grand Child 2"),MyMoneyAccount::Expense,0,QDate(2004,2,11),acChild);

  MyMoneyInstitution i("Bank of the World","","","","","","");
  file->addInstitution(i);
  inBank = i.id();
  ft.commit();
}

void PivotGridTest::tearDown ()
{
  file->detachStorage(storage);
  delete storage;
}

void PivotGridTest::testCellAddValue(void)
{
  PivotCell a;
  CPPUNIT_ASSERT(a == MyMoneyMoney(0,1));
  CPPUNIT_ASSERT(a.m_stockSplit == MyMoneyMoney(1,1));
  CPPUNIT_ASSERT(a.m_postSplit == MyMoneyMoney(0,1));
  CPPUNIT_ASSERT(a.formatMoney("", 2) == MyMoneyMoney(0,1).formatMoney("", 2));

  PivotCell b(MyMoneyMoney(13,10));
  CPPUNIT_ASSERT(b == MyMoneyMoney(13,10));
  CPPUNIT_ASSERT(b.m_stockSplit == MyMoneyMoney(1,1));
  CPPUNIT_ASSERT(b.m_postSplit == MyMoneyMoney(0,1));
  CPPUNIT_ASSERT(b.formatMoney("", 2) == MyMoneyMoney(13,10).formatMoney("", 2));

  PivotCell s(b);
  CPPUNIT_ASSERT(s == MyMoneyMoney(13,10));
  CPPUNIT_ASSERT(s.m_stockSplit == MyMoneyMoney(1,1));
  CPPUNIT_ASSERT(s.m_postSplit == MyMoneyMoney(0,1));
  CPPUNIT_ASSERT(s.formatMoney("", 2) == MyMoneyMoney(13,10).formatMoney("", 2));

  s = PivotCell::stockSplit(MyMoneyMoney(1,2));
  CPPUNIT_ASSERT(s == MyMoneyMoney(0,1));
  CPPUNIT_ASSERT(s.m_stockSplit == MyMoneyMoney(1,2));
  CPPUNIT_ASSERT(s.m_postSplit == MyMoneyMoney(0,1));
  CPPUNIT_ASSERT(s.formatMoney("", 2) == MyMoneyMoney(0,1).formatMoney("", 2));
  
  a += MyMoneyMoney(1,1);
  a += MyMoneyMoney(2,1);
  CPPUNIT_ASSERT(a == MyMoneyMoney(3,1));
  CPPUNIT_ASSERT(a.m_stockSplit == MyMoneyMoney(1,1));
  CPPUNIT_ASSERT(a.m_postSplit == MyMoneyMoney(0,1));
  CPPUNIT_ASSERT(a.formatMoney("", 2) == MyMoneyMoney(3,1).formatMoney("", 2));

  a += s;
  CPPUNIT_ASSERT(a == MyMoneyMoney(3,1));
  CPPUNIT_ASSERT(a.m_stockSplit == MyMoneyMoney(1,2));
  CPPUNIT_ASSERT(a.m_postSplit == MyMoneyMoney(0,1));
  CPPUNIT_ASSERT(a.formatMoney("", 2) == MyMoneyMoney(15,10).formatMoney("", 2));

  a += MyMoneyMoney(3,1);
  a += MyMoneyMoney(3,1);
  CPPUNIT_ASSERT(a == MyMoneyMoney(3,1));
  CPPUNIT_ASSERT(a.m_stockSplit == MyMoneyMoney(1,2));
  CPPUNIT_ASSERT(a.m_postSplit == MyMoneyMoney(6,1));
  CPPUNIT_ASSERT(a.formatMoney("", 2) == MyMoneyMoney(75,10).formatMoney("", 2));
}

void PivotGridTest::testCellAddCell(void)
{
  PivotCell a,b;

  a += MyMoneyMoney(3,1);
  a += PivotCell::stockSplit(MyMoneyMoney(2,1));
  a += MyMoneyMoney(4,1);

  CPPUNIT_ASSERT(a == MyMoneyMoney(3,1));
  CPPUNIT_ASSERT(a.m_stockSplit == MyMoneyMoney(2,1));
  CPPUNIT_ASSERT(a.m_postSplit == MyMoneyMoney(4,1));
  CPPUNIT_ASSERT(a.formatMoney("", 2) == MyMoneyMoney(10,1).formatMoney("", 2));

  b += MyMoneyMoney(4,1);
  b += PivotCell::stockSplit(MyMoneyMoney(4,1));
  b += MyMoneyMoney(16,1);
  
  CPPUNIT_ASSERT(b == MyMoneyMoney(4,1));
  CPPUNIT_ASSERT(b.m_stockSplit == MyMoneyMoney(4,1));
  CPPUNIT_ASSERT(b.m_postSplit == MyMoneyMoney(16,1));
  CPPUNIT_ASSERT(b.formatMoney("", 2) == MyMoneyMoney(32,1).formatMoney("", 2));

  a += b;

  CPPUNIT_ASSERT(a == MyMoneyMoney(3,1));
  CPPUNIT_ASSERT(a.m_stockSplit == MyMoneyMoney(8,1));
  CPPUNIT_ASSERT(a.m_postSplit == MyMoneyMoney(48,1));
  CPPUNIT_ASSERT(a.formatMoney("", 2) == MyMoneyMoney(72,1).formatMoney("", 2));
}

void PivotGridTest::testCellRunningSum(void)
{
  PivotCell a;
  MyMoneyMoney runningSum(12,10);

  a += MyMoneyMoney(3,1);
  a += PivotCell::stockSplit(MyMoneyMoney(125,100));
  a += MyMoneyMoney(134,10);

  CPPUNIT_ASSERT(a.m_stockSplit != MyMoneyMoney(1,1));
  CPPUNIT_ASSERT(a.m_postSplit != MyMoneyMoney(0,1));

  runningSum = a.calculateRunningSum(runningSum);

  CPPUNIT_ASSERT(runningSum == MyMoneyMoney(1865,100));
  CPPUNIT_ASSERT(a.formatMoney("", 2) == MyMoneyMoney(1865,100).formatMoney("", 2));
  CPPUNIT_ASSERT(a.m_stockSplit == MyMoneyMoney(1,1));
  CPPUNIT_ASSERT(a.m_postSplit == MyMoneyMoney(0,1));
}

// vim:cin:si:ai:et:ts=2:sw=2: