[report] Load total debits in 'transcript' (bug 151)
[abstract.git] / report / test / aaCurrencyReport.cpp
blob7ca6bc9624e386dd7205ab4b23926778fc0e4f7d
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) 2008 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 "xpcom-config.h"
24 #include "nsCOMPtr.h"
25 #include "nsComponentManagerUtils.h"
26 #include "nsEmbedString.h"
27 #include "nsIArray.h"
28 #include "nsArrayUtils.h"
30 /* Unfrozen API */
31 #include "mozIStorageConnection.h"
32 #include "nsTestUtils.h"
33 #include "nsITest.h"
34 #include "nsITestRunner.h"
36 /* Project includes */
37 #include "aaIFlow.h"
38 #include "aaIFact.h"
39 #include "aaIBalance.h"
40 #include "aaISession.h"
41 #include "aaIManager.h"
42 #include "aaIFilter.h"
43 #include "aaBaseLoaders.h"
44 #include "aaBalanceSheet.h"
45 #include "aaTranscript.h"
47 #include "aaBaseTestUtils.h"
48 #include "aaCurrencyReport.h"
50 NS_IMPL_ISUPPORTS1(aaCurrencyReport, nsITest)
52 NS_IMETHODIMP
53 aaCurrencyReport::Test(nsITestRunner *aTestRunner)
55 RAII res(this);
56 if (NS_FAILED( res.status )) {
57 aTestRunner->AddError(nsITestRunner::errorJS, \
58 AA_CURRENCY_REPORT_CONTRACT_ID " not initialized");
59 return NS_ERROR_NOT_INITIALIZED;
61 testBalanceSheet(aTestRunner);
62 testTranscript(aTestRunner);
63 return NS_OK;
66 /* Helpers */
67 aaCurrencyReport::RAII::RAII(aaCurrencyReport *t)
68 :status(PR_FALSE), test(nsnull)
70 nsresult rv;
71 nsCOMPtr<aaIManager> manager =
72 do_CreateInstance(AA_MANAGER_CONTRACT_ID, &rv);
73 if (NS_UNLIKELY( ! manager))
74 return;
76 manager->Restore("currency.sqlite");
77 nsCOMPtr<aaISession> session(do_CreateInstance(AA_SESSION_CONTRACT_ID, &rv));
78 if (NS_UNLIKELY( ! session))
79 return;
81 status = PR_TRUE;
82 test = t;
83 test->mSession = session;
86 aaCurrencyReport::RAII::~RAII()
88 test->mFlows = nsnull;
89 test->mSession = nsnull;
90 test = nsnull;
92 nsresult rv;
93 nsCOMPtr<aaIManager> manager =
94 do_CreateInstance(AA_MANAGER_CONTRACT_ID, &rv);
95 if (NS_UNLIKELY( ! manager))
96 return;
98 manager->Backup("currency.sqlite");
101 /* Private methods */
102 nsresult
103 aaCurrencyReport::testBalanceSheet(nsITestRunner *aTestRunner)
105 nsresult rv;
106 NS_TEST_BEGIN( aTestRunner );
108 nsCOMPtr<aaIBalanceSheet> sheet;
109 rv = mSession->CreateQuery(AA_BALANCESHEET_CONTRACT_ID,
110 getter_AddRefs(sheet));
111 NS_TEST_ASSERT_MSG(sheet, "[balance sheet] intance creation");
112 NS_ENSURE_TRUE(sheet, rv);
114 rv = sheet->Load();
115 NS_TEST_ASSERT_OK(rv);
117 nsCOMPtr<aaIBalance> balance = do_QueryElementAt(sheet, 0);
118 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 6, 2, 30000, 48000, 0),
119 "[balance sheet] flow 'bx1' is wrong");
121 return NS_OK;
124 nsresult
125 aaCurrencyReport::testTranscript(nsITestRunner *aTestRunner)
127 nsresult rv;
128 NS_TEST_BEGIN( aTestRunner );
130 rv = mSession->CreateQuery(AA_LOADFLOW_CONTRACT_ID,
131 getter_AddRefs(mFlows));
132 NS_TEST_ASSERT_OK(rv);
133 NS_ENSURE_TRUE(mFlows, rv);
135 rv = mFlows->Load();
136 NS_TEST_ASSERT_OK(rv);
137 nsCOMPtr<aaIFlow> a2 = do_QueryElementAt(mFlows, 2);
139 nsCOMPtr<aaITranscript> transcript;
140 rv = mSession->CreateQuery(AA_TRANSCRIPT_CONTRACT_ID,
141 getter_AddRefs(transcript));
142 NS_TEST_ASSERT_MSG(transcript, "[transcript] intance creation");
143 NS_ENSURE_TRUE(transcript, rv);
145 PRExplodedTime tm = {0,0,0,12,25,2,2008};
146 transcript->SetStart(PR_ImplodeTime(&tm));
147 tm.tm_mday = 31;
148 transcript->SetEnd(PR_ImplodeTime(&tm));
149 transcript->SetFlow(a2);
151 rv = transcript->Load();
152 NS_TEST_ASSERT_OK(rv);
154 return NS_OK;