[app] Display 'transaction.value' after completion (bug #25)
[abstract.git] / report / aaReportTest.cpp
blob4cb07e8b3db426f343a80635e493ff2f7f3c7cc1
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
3 /*
4 * Copyright (C) 2007 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <abstract/aacore.h>
24 #include "nsCOMPtr.h"
25 #include "nsIGenericFactory.h"
26 #include "nsComponentManagerUtils.h"
27 #include "nsIArray.h"
28 #include "nsArrayUtils.h"
29 #include "nsEmbedString.h"
31 /* Unfrozen API */
32 #include "unstable/mozIStorageConnection.h"
33 #include <abstract/cxxunit/nsTestUtils.h>
34 #include <abstract/cxxunit/nsITest.h>
35 #include <abstract/cxxunit/nsITestRunner.h>
37 /* Project includes */
38 #include <abstract/base/aaIResource.h>
39 #include <abstract/base/aaIFlow.h>
40 #include <abstract/base/aaIFact.h>
41 #include <abstract/base/aaIBalance.h>
42 #include <abstract/base/aaITransaction.h>
43 #include <abstract/storage/aaBaseLoaders.h>
44 #include <abstract/storage/aaISession.h>
45 #include <abstract/storage/aaIFilter.h>
46 #include <abstract/storage/aaIStateFilter.h>
47 #include <abstract/report/aaITranscript.h>
48 #include <abstract/report/aaIBalanceSheet.h>
49 #include "aaTranscript.h"
50 #include "aaBalanceSheet.h"
51 #include <abstract/storage/aaTestConsts.h>
53 #define AA_REPORT_TEST_CID \
54 {0xca303dca, 0xd6d4, 0x4049, {0xba, 0x4c, 0x51, 0x4b, 0xf2, 0x3c, 0x82, 0x98}}
55 #define AA_REPORT_TEST_CONTRACT_ID "@aasii.org/report/unit;1"
57 class aaReportTest: public nsITest
59 public:
60 aaReportTest() {;}
61 virtual ~aaReportTest() {;}
62 NS_DECL_ISUPPORTS
63 NS_DECL_NSITEST
64 private:
65 friend class RAII;
66 nsCOMPtr<aaISession> mSession;
67 nsCOMPtr<aaILoadQuery> mFlows;
69 nsresult testTranscript(nsITestRunner *aTestRunner);
70 nsresult testPnLTranscript(nsITestRunner *aTestRunner);
71 nsresult testBalanceSheet(nsITestRunner *aTestRunner);
74 class RAII
76 public:
77 RAII(aaReportTest *t);
78 ~RAII();
80 PRBool status;
81 private:
82 aaReportTest* test;
85 NS_IMETHODIMP
86 aaReportTest::Test(nsITestRunner *aTestRunner)
88 RAII res(this);
89 if (NS_FAILED( res.status )) {
90 aTestRunner->AddError(nsITestRunner::errorJS, \
91 AA_REPORT_TEST_CONTRACT_ID " not initialized");
92 return NS_ERROR_NOT_INITIALIZED;
94 testTranscript( aTestRunner );
95 testPnLTranscript(aTestRunner);
96 testBalanceSheet(aTestRunner);
97 return NS_OK;
100 NS_IMPL_ISUPPORTS1(aaReportTest, nsITest);
102 /* Helpers */
103 PRBool
104 testFact(nsITestRunner *cxxUnitTestRunner, aaIFact *node, PRInt64 fromFlowId,
105 PRInt64 toFlowId, double sum)
107 nsresult rv;
108 PRBool equals = PR_TRUE;
110 NS_TEST_ASSERT_MSG(node, " 'fact' is null");
111 if (NS_UNLIKELY( ! node ))
112 return PR_FALSE;
113 nsCOMPtr<aaIFlow> flow;
114 PRInt64 id;
115 node->GetTakeFrom(getter_AddRefs( flow ));
116 if ( flow ) {
117 flow->GetId( &id );
118 if (NS_UNLIKELY( id != fromFlowId )) {
119 equals = PR_FALSE;
120 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.takeFrom.id' is wrong" );
122 } else if (NS_UNLIKELY( fromFlowId != 0 )) {
123 equals = PR_FALSE;
124 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.takeFrom.id' is wrong" );
127 node->GetGiveTo(getter_AddRefs( flow ));
128 if ( flow ) {
129 flow->GetId( &id );
130 if (NS_UNLIKELY( id != toFlowId )) {
131 equals = PR_FALSE;
132 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.giveTo.id' is wrong" );
134 } else if (NS_UNLIKELY( toFlowId != 0 )) {
135 equals = PR_FALSE;
136 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.giveTo.id' is wrong" );
139 double amount, diff;
140 rv = node->GetAmount(&amount);
141 diff = amount - sum;
142 if (diff > 0.0001 || diff < -0.0001) {
143 equals = PR_FALSE;
144 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.amount' is wrong" );
147 return equals;
150 PRBool
151 testTxn(nsITestRunner *cxxUnitTestRunner, aaITransaction *txn,
152 PRInt64 fromFlowId, PRInt64 toFlowId, double amount, double value,
153 PRBool status = 0, double earnings = 0.0)
155 PRBool equals = PR_TRUE;
157 NS_TEST_ASSERT_MSG(txn, " 'txn' is null");
158 if (NS_UNLIKELY( ! txn ))
159 return PR_FALSE;
160 if (! testFact(cxxUnitTestRunner, txn->PickFact(), fromFlowId, toFlowId,
161 amount))
162 return PR_FALSE;
164 double sum, diff;
165 txn->GetValue(&sum);
166 diff = value - sum;
167 if (! isZero(diff)) {
168 equals = PR_FALSE;
169 NS_TEST_ASSERT_MSG(PR_FALSE, " 'txn.value' is wrong" );
172 PRBool b;
173 txn->GetHasEarnings(&b);
174 if (b != status) {
175 equals = PR_FALSE;
176 NS_TEST_ASSERT_MSG(PR_FALSE, " 'txn.hasEarnings' is wrong" );
179 if (!b)
180 return equals;
182 return equals;
185 PRBool
186 testBalance(nsITestRunner *cxxUnitTestRunner, aaIBalance *node, PRInt64 flowId,
187 PRInt64 resourceId, double amount, double value, PRBool aSide)
189 nsresult rv;
190 PRBool equals = PR_TRUE;
191 if (NS_UNLIKELY( ! node )) {
192 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance' is null");
193 return PR_FALSE;
195 nsCOMPtr<aaIFlow> flow;
196 rv = node->GetFlow(getter_AddRefs( flow ));
197 NS_TEST_ASSERT_MSG(flow, " 'balance.flow' is null");
198 PRInt64 id;
199 if (NS_LIKELY( flow )) {
200 flow->GetId(&id);
201 if (NS_UNLIKELY( id != flowId) ) {
202 nsEmbedCString msg(" 'balance.flow.id' is wrong - ");
203 msg.AppendInt(id);
204 equals = PR_FALSE;
205 NS_TEST_ASSERT_MSG(PR_FALSE, msg.get());
207 } else {
208 equals = PR_FALSE;
211 nsCOMPtr<aaIResource> resource;
212 rv = node->GetResource(getter_AddRefs( resource ));
213 NS_TEST_ASSERT_MSG(resource, " 'balance.resource' is null" );
214 if (NS_LIKELY( resource )) {
215 resource->GetId(&id);
216 if (NS_UNLIKELY( id != resourceId) ) {
217 nsEmbedCString msg(" 'balance.resource.id' is wrong - ");
218 msg.AppendInt(id);
219 equals = PR_FALSE;
220 NS_TEST_ASSERT_MSG(PR_FALSE, msg.get());
222 } else {
223 equals = PR_FALSE;
226 double sum, diff;
227 node->GetAmount(&sum);
228 diff = amount - sum;
229 if (NS_UNLIKELY(diff > 0.0001 || diff < -0.0001)) {
230 equals = PR_FALSE;
231 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance.amount' is wrong");
234 node->GetValue(&sum);
235 diff = value - sum;
236 if (NS_UNLIKELY(diff > 0.0001 || diff < -0.0001)) {
237 equals = PR_FALSE;
238 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance.value' is wrong");
241 PRBool side;
242 node->GetSide(&side);
243 if (NS_UNLIKELY( side != aSide)) {
244 equals = PR_FALSE;
245 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance.side' is wrong");
247 return equals;
250 PRBool
251 testIncomeState(nsITestRunner *cxxUnitTestRunner, aaIBalance *node,
252 double value, PRBool aSide)
254 nsresult rv;
255 PRBool equals = PR_TRUE;
256 if (NS_UNLIKELY( ! node )) {
257 NS_TEST_ASSERT_MSG(PR_FALSE, " 'income state' is null");
258 return PR_FALSE;
260 nsCOMPtr<aaIFlow> flow;
261 rv = node->GetFlow(getter_AddRefs( flow ));
262 PRInt64 id;
263 if (NS_LIKELY( flow )) {
264 flow->GetId(&id);
265 nsEmbedCString msg(" 'income state.flow' is wrong - ");
266 msg.AppendInt(id);
267 if (NS_UNLIKELY( id )) {
268 equals = PR_FALSE;
269 NS_TEST_ASSERT_MSG(PR_FALSE, msg.get());
271 } else {
272 equals = PR_FALSE;
273 NS_TEST_ASSERT_MSG(PR_FALSE, " 'income state.flow' is null");
276 nsCOMPtr<aaIResource> resource;
277 rv = node->GetResource(getter_AddRefs( resource ));
278 if (NS_UNLIKELY( resource )) {
279 resource->GetId(&id);
280 nsEmbedCString msg(" 'income state.resource' is not null -" );
281 msg.AppendInt(id);
282 equals = PR_FALSE;
283 NS_TEST_ASSERT_MSG(PR_FALSE, msg.get());
286 double sum, diff;
287 node->GetAmount(&sum);
288 diff = sum;
289 if (!isZero(diff)) {
290 equals = PR_FALSE;
291 NS_TEST_ASSERT_MSG(PR_FALSE, " 'income state.amount' is wrong");
294 node->GetValue(&sum);
295 diff = value - sum;
296 if (NS_UNLIKELY(diff > 0.0001 || diff < -0.0001)) {
297 equals = PR_FALSE;
298 NS_TEST_ASSERT_MSG(PR_FALSE, " 'income state.value' is wrong");
301 PRBool side;
302 node->GetSide(&side);
303 if (NS_UNLIKELY( side != aSide)) {
304 equals = PR_FALSE;
305 NS_TEST_ASSERT_MSG(PR_FALSE, " 'income state.side' is wrong");
307 return equals;
310 RAII::RAII(aaReportTest *t)
311 :status(PR_FALSE), test(nsnull)
313 nsresult rv;
314 nsCOMPtr<aaISession> session(do_CreateInstance(AA_SESSION_CONTRACT_ID, &rv));
315 if (NS_UNLIKELY( ! session))
316 return;
318 status = PR_TRUE;
319 test = t;
320 test->mSession = session;
323 RAII::~RAII()
325 test->mSession = nsnull;
326 test = nsnull;
329 /* Module and Factory code */
330 NS_GENERIC_FACTORY_CONSTRUCTOR(aaReportTest)
332 static const nsModuleComponentInfo kComponents[] =
335 "Report Module Unit Test",
336 AA_REPORT_TEST_CID,
337 AA_REPORT_TEST_CONTRACT_ID,
338 aaReportTestConstructor
341 NS_IMPL_NSGETMODULE(aareportt, kComponents)
343 /* Private methods */
344 nsresult
345 aaReportTest::testTranscript(nsITestRunner *aTestRunner)
347 nsresult rv;
348 NS_TEST_BEGIN( aTestRunner );
350 nsCOMPtr<mozIStorageConnection> connection;
351 rv = mSession->GetConnection(getter_AddRefs( connection ));
352 NS_TEST_ASSERT_OK(rv);
354 nsCOMPtr<aaITranscript> transcript( do_CreateInstance(
355 AA_TRANSCRIPT_CONTRACT_ID, connection) );
356 NS_TEST_ASSERT_MSG(transcript, "[transcript] intance creation");
357 NS_ENSURE_TRUE(transcript, rv);
359 rv = mSession->CreateQuery(AA_LOADFLOW_CONTRACT_ID, getter_AddRefs(mFlows));
360 NS_TEST_ASSERT_MSG(mFlows, "[transcript] creating flow loader");
361 NS_ENSURE_TRUE(mFlows, rv);
363 rv = mFlows->Load();
364 NS_TEST_ASSERT_OK(rv); NS_ENSURE_SUCCESS(rv, rv);
366 /* Set flow to bank */
367 nsCOMPtr<aaIFlow> bank = do_QueryElementAt(mFlows, 2);
368 transcript->SetFlow( bank );
370 /* *** Transcript [1] *** */
372 /* Set start time to 2007-08-28 */
373 PRExplodedTime tm = {0,0,0,12,28,7,2007};
374 transcript->SetStart(PR_ImplodeTime(&tm));
375 /* Set end time to 2007-08-29 */
376 tm.tm_mday = 29;
377 transcript->SetEnd(PR_ImplodeTime(&tm));
379 rv = transcript->Load();
380 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transcript] [1] loading");
382 nsCOMPtr<aaIBalance> balance;
383 rv = transcript->GetOpening(getter_AddRefs( balance ));
384 NS_TEST_ASSERT_OK(rv);
385 NS_TEST_ASSERT_MSG(! balance, "[transcript] [1] opening must be null");
387 rv = transcript->GetClosing(getter_AddRefs( balance ));
388 NS_TEST_ASSERT_OK(rv);
389 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
390 + AA_EVENT_AMOUNT_3, AA_EVENT_AMOUNT_2 + AA_EVENT_AMOUNT_3, 1),
391 "[transcript] [1] closing is wrong");
393 PRUint32 count = 0;
394 rv = transcript->GetLength(&count);
395 NS_TEST_ASSERT_OK(rv);
396 NS_TEST_ASSERT_MSG(count == 2, "[transcript] [1] wrong fact count");
398 nsCOMPtr<aaITransaction> txn = do_QueryElementAt(transcript, 0);
399 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 2, 3,
400 AA_EVENT_AMOUNT_2), "[transcript] [1] 1st fact is wrong");
402 txn = do_QueryElementAt(transcript, 1);
403 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 1, 3,
404 AA_EVENT_AMOUNT_3), "[transcript] [1] 2nd fact is wrong");
406 /* *** Transcript [2] *** */
408 /* Set start time to 2007-08-28 */
409 tm.tm_mday = 28;
410 transcript->SetStart(PR_ImplodeTime(&tm));
411 /* Set end time to 2007-08-30 */
412 tm.tm_mday = 30;
413 transcript->SetEnd(PR_ImplodeTime(&tm));
415 rv = transcript->Load();
416 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transcript] [2] loading");
418 rv = transcript->GetOpening(getter_AddRefs( balance ));
419 NS_TEST_ASSERT_OK(rv);
420 NS_TEST_ASSERT_MSG(! balance, "[transcript] [2] opening must be null");
422 rv = transcript->GetClosing(getter_AddRefs( balance ));
423 NS_TEST_ASSERT_OK(rv);
424 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
425 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4- AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, AA_EVENT_AMOUNT_2
426 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4- AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, 1),
427 "[transcript] [2] closing is wrong");
429 count = 0;
430 rv = transcript->GetLength(&count);
431 NS_TEST_ASSERT_OK(rv);
432 NS_TEST_ASSERT_MSG(count == 4, "[transcript] [2] wrong fact count");
434 txn = do_QueryElementAt(transcript, 0);
435 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 2, 3,
436 AA_EVENT_AMOUNT_2), "[transcript] [2] 1st fact is wrong");
438 txn = do_QueryElementAt(transcript, 1);
439 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 1, 3,
440 AA_EVENT_AMOUNT_3), "[transcript] [2] 2nd fact is wrong");
442 txn = do_QueryElementAt(transcript, 2);
443 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 3, 4,
444 AA_EVENT_AMOUNT_4), "[update txn] pending fact is wrong");
446 /* *** Transcript [3] *** */
448 /* Set start time to 2007-08-29 */
449 tm.tm_mday = 29;
450 transcript->SetStart(PR_ImplodeTime(&tm));
451 /* Set end time to 2007-08-31 */
452 tm.tm_mday = 31;
453 transcript->SetEnd(PR_ImplodeTime(&tm));
455 rv = transcript->Load();
456 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transcript] [3] loading");
458 rv = transcript->GetOpening(getter_AddRefs( balance ));
459 NS_TEST_ASSERT_OK(rv);
460 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
461 + AA_EVENT_AMOUNT_3, AA_EVENT_AMOUNT_2 + AA_EVENT_AMOUNT_3, 1),
462 "[transcript] [3] opening is wrong");
464 rv = transcript->GetClosing(getter_AddRefs( balance ));
465 NS_TEST_ASSERT_OK(rv);
466 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
467 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4
468 - AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, AA_EVENT_AMOUNT_2
469 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4
470 - AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, 1),
471 "[transcript] [3] closing is wrong");
473 count = 0;
474 rv = transcript->GetLength(&count);
475 NS_TEST_ASSERT_OK(rv);
476 NS_TEST_ASSERT_MSG(count == 2, "[transcript] [3] wrong fact count");
478 txn = do_QueryElementAt(transcript, 0);
479 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 3, 4,
480 AA_EVENT_AMOUNT_4), "[transcript] [3] pending fact is wrong");
482 /* *** Transcript [4] *** */
484 /* Set start time to 2007-08-28 */
485 tm.tm_mday = 27;
486 transcript->SetStart(PR_ImplodeTime(&tm));
487 /* Set end time to 2007-08-29 */
488 tm.tm_mday = 28;
489 transcript->SetEnd(PR_ImplodeTime(&tm));
491 rv = transcript->Load();
492 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transcript] [4] loading");
494 rv = transcript->GetOpening(getter_AddRefs( balance ));
495 NS_TEST_ASSERT_OK(rv);
496 NS_TEST_ASSERT_MSG(! balance, "[transcript] [4] opening must be null");
498 rv = transcript->GetClosing(getter_AddRefs( balance ));
499 NS_TEST_ASSERT_OK(rv);
500 NS_TEST_ASSERT_MSG(! balance, "[transcript] [4] closing must be null");
502 count = 1;
503 rv = transcript->GetLength(&count);
504 NS_TEST_ASSERT_OK(rv);
505 NS_TEST_ASSERT_MSG(count == 0, "[transcript] [4] wrong fact count");
507 /* *** Transcript [5] *** */
509 /* Set start time to 2007-08-28 */
510 tm.tm_mday = 28;
511 transcript->SetStart(PR_ImplodeTime(&tm));
512 /* Set end time to 2007-08-30 */
513 tm.tm_mday = 30;
514 transcript->SetEnd(PR_ImplodeTime(&tm));
516 nsCOMPtr<aaIFlow> purchase = do_QueryElementAt(mFlows, 3);
517 transcript->SetFlow(purchase);
518 rv = transcript->Load();
519 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transcript] [5] loading");
521 rv = transcript->GetOpening(getter_AddRefs( balance ));
522 NS_TEST_ASSERT_OK(rv);
523 NS_TEST_ASSERT_MSG(! balance, "[transcript] [5] opening must be null");
525 rv = transcript->GetClosing(getter_AddRefs( balance ));
526 NS_TEST_ASSERT_OK(rv);
527 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 4, 3, AA_EVENT_AMOUNT_5,
528 AA_EVENT_AMOUNT_4, 1), "[transcript] [5] closing is wrong");
530 count = 0;
531 rv = transcript->GetLength(&count);
532 NS_TEST_ASSERT_OK(rv);
533 NS_TEST_ASSERT_MSG(count == 1, "[transcript] [5] wrong fact count");
535 txn = do_QueryElementAt(transcript, 0);
536 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 3, 4,
537 AA_EVENT_AMOUNT_4), "[update txn] pending fact is wrong");
539 return NS_OK;
542 nsresult
543 aaReportTest::testPnLTranscript(nsITestRunner *aTestRunner)
545 nsresult rv;
546 NS_TEST_BEGIN( aTestRunner );
548 nsCOMPtr<aaITranscript> transcript;
549 rv = mSession->CreateQuery(AA_TRANSCRIPT_CONTRACT_ID,
550 getter_AddRefs(transcript));
551 NS_TEST_ASSERT_MSG(transcript, "[transcript] intance creation");
552 NS_ENSURE_TRUE(transcript, rv);
554 /* Set flow to PnL */
555 nsCOMPtr<aaIFlow> finres = do_CreateInstance("@aasii.org/base/income-flow;1");
556 transcript->SetFlow( finres );
558 /* *** Transcript [6] *** */
560 /* Set start time to 2007-08-28 */
561 PRExplodedTime tm = {0,0,0,12,28,7,2007};
562 transcript->SetStart(PR_ImplodeTime(&tm));
563 /* Set end time to 2007-09-10 */
564 tm.tm_month = 8;
565 tm.tm_mday = 10;
566 transcript->SetEnd(PR_ImplodeTime(&tm));
568 rv = transcript->Load();
569 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transcript] [6] loading");
571 nsCOMPtr<aaIBalance> balance;
572 rv = transcript->GetOpening(getter_AddRefs( balance ));
573 NS_TEST_ASSERT_OK(rv);
574 NS_TEST_ASSERT_MSG(! balance, "[transcript] [6] opening must be null");
576 rv = transcript->GetClosing(getter_AddRefs( balance ));
577 NS_TEST_ASSERT_OK(rv);
578 NS_TEST_ASSERT_MSG(testIncomeState(aTestRunner, balance, AA_EVENT_AMOUNT_15
579 * (AA_EVENT_RATE_5 - AA_EVENT_RATE_4), 0),
580 "[transcript] [6] closing is wrong");
582 PRUint32 count = 0;
583 rv = transcript->GetLength(&count);
584 NS_TEST_ASSERT_OK(rv);
585 NS_TEST_ASSERT_MSG(count == 8, "[transcript] [8] wrong txn count");
587 nsCOMPtr<aaITransaction> txn = do_QueryElementAt(transcript, 0);
588 NS_TEST_ASSERT_MSG(testTxn(aTestRunner, txn, 6, 7, AA_EVENT_AMOUNT_6,
589 AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, 1, AA_EVENT_AMOUNT_6
590 * (AA_EVENT_RATE_3 - AA_EVENT_RATE_2)), "[transcript] [8] 1st txn is wrong");
592 txn = do_QueryElementAt(transcript, 7);
593 NS_TEST_ASSERT_MSG(testTxn(aTestRunner, txn, 0, 10, AA_EVENT_AMOUNT_15, 0.0,
594 1, AA_EVENT_AMOUNT_15 * AA_EVENT_RATE_5),
595 "[transcript] [8] 8th txn is wrong");
597 return NS_OK;
600 nsresult
601 aaReportTest::testBalanceSheet(nsITestRunner *aTestRunner)
603 nsresult rv;
604 NS_TEST_BEGIN( aTestRunner );
606 nsCOMPtr<aaIBalanceSheet> sheet;
607 rv = mSession->CreateQuery(AA_BALANCESHEET_CONTRACT_ID,
608 getter_AddRefs(sheet));
609 NS_TEST_ASSERT_MSG(sheet, "[balance sheet] intance creation");
610 NS_ENSURE_TRUE(sheet, rv);
612 rv = sheet->Load();
613 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[balance sheet] [1] loading");
615 PRUint32 count = 0;
616 rv = sheet->GetLength(&count);
617 NS_TEST_ASSERT_OK(rv);
618 NS_TEST_ASSERT_MSG(count == 7, "[balance sheet] [1] wrong line count");
620 nsCOMPtr<aaIBalance> balance = do_QueryElementAt(sheet, 6);
621 NS_TEST_ASSERT_MSG(balance, "[balance sheet] income not loaded" );
623 NS_TEST_ASSERT_MSG(testIncomeState(aTestRunner, balance, AA_EVENT_AMOUNT_15
624 * (AA_EVENT_RATE_5 - AA_EVENT_RATE_4), 0),
625 "[balance sheet] income is wrong");
627 double assets, liabilities;
628 rv = sheet->GetTotalAssets(&assets);
629 NS_TEST_ASSERT_OK(rv);
630 NS_TEST_ASSERT_MSG(isZero(assets - 242300),
631 "[balance sheet] [1] wrong total assets");
633 rv = sheet->GetTotalLiabilities(&liabilities);
634 NS_TEST_ASSERT_OK(rv);
635 NS_TEST_ASSERT_MSG(isZero(liabilities - 242300),
636 "[balance sheet] [1] wrong total liabilities");
638 /* Set date to 2007-09-07 */
639 PRExplodedTime tm = {0,0,0,12,7,8,2007};
640 rv = sheet->SetDate(PR_ImplodeTime(&tm));
642 rv = sheet->Load();
643 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[balance sheet] [2] loading");
645 sheet->GetLength(&count);
646 NS_TEST_ASSERT_MSG(count == 6, "[balance sheet] [2] wrong flow count");
648 balance = do_QueryElementAt(sheet, 0);
649 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 2, 2, AA_EVENT_AMOUNT_2
650 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_2, 0),
651 "[balance sheet] flow2 is wrong");
653 balance = do_QueryElementAt(sheet, 1);
654 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 1, 2, AA_EVENT_AMOUNT_3
655 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_3, 0),
656 "[balance sheet] flow1 is wrong");
658 balance = do_QueryElementAt(sheet, 2);
659 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 4, 3, AA_EVENT_AMOUNT_5,
660 AA_EVENT_AMOUNT_4, 1), "[balance sheet] flow4 is wrong");
662 balance = do_QueryElementAt(sheet, 3);
663 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 6, 4,
664 2400, 2400 * AA_EVENT_RATE_4, 1),
665 "[balance sheet] flow6 is wrong");
667 balance = do_QueryElementAt(sheet, 4);
668 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 9, 5,
669 1, AA_EVENT_RATE_6, 1),
670 "[balance sheet] flow9 is wrong");
672 balance = do_QueryElementAt(sheet, 5);
673 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, 87920, 87920,
674 1), "[balance sheet] flow3 is wrong");
676 rv = sheet->GetTotalAssets(&assets);
677 NS_TEST_ASSERT_OK(rv);
678 NS_TEST_ASSERT_MSG(isZero(assets - 242000),
679 "[balance sheet] [2] wrong total assets");
681 rv = sheet->GetTotalLiabilities(&liabilities);
682 NS_TEST_ASSERT_OK(rv);
683 NS_TEST_ASSERT_MSG(isZero(liabilities - 242000),
684 "[balance sheet] [2] wrong total liabilities");
686 return NS_OK;