[storage] Prepare test for 'stop transaction' (bug #101)
[abstract.git] / storage / aaAccountTest.cpp
blobb76b79fcd52bf54f7fa32759387280a2f7896600
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 "nsIGenericFactory.h"
25 #include "nsComponentManagerUtils.h"
26 #include "nsArrayUtils.h"
27 #include "nsStringAPI.h"
28 #include "nsEmbedString.h"
30 /* Unfrozen API */
31 #include <unstable/mozIStorageConnection.h>
32 #include <abstract/cxxunit/nsTestUtils.h>
33 #include <abstract/cxxunit/nsITest.h>
34 #include <abstract/cxxunit/nsITestRunner.h>
36 /* Project includes */
37 #include <abstract/base/aaIResource.h>
38 #include <abstract/base/aaIFact.h>
39 #include <abstract/base/aaIFlow.h>
40 #include <abstract/base/aaIEvent.h>
41 #include <abstract/base/aaIQuote.h>
42 #include <abstract/base/aaITransaction.h>
43 #include <abstract/base/aaIBalance.h>
44 #include <abstract/storage/aaISession.h>
45 #include <abstract/storage/aaBaseLoaders.h>
46 #include <abstract/storage/aaAccountLoaders.h>
47 #include <abstract/storage/aaIFilter.h>
48 #include <abstract/storage/aaIStateFilter.h>
49 #include <abstract/storage/aaILoadQuery.h>
50 #include "aaSaveTransaction.h"
51 /* XXX _acc This is a temporary hack to acquire connection
52 * It has to be replaced with a 'chart'
54 #include "aaSession.h"
55 #include <abstract/storage/aaTestConsts.h>
57 #define AA_ACCOUNT_TEST_CID \
58 {0x9f3b3e19, 0x2f06, 0x4a75, {0xbc, 0xb8, 0xd7, 0xd3, 0x5c, 0xaf, 0x9d, 0x36}}
59 #define AA_ACCOUNT_TEST_CONTRACT_ID "@aasii.org/storage/unit-account;1"
61 class aaAccountTest: public nsITest
63 public:
64 aaAccountTest() {;}
65 virtual ~aaAccountTest() {;}
66 NS_DECL_ISUPPORTS
67 NS_DECL_NSITEST
68 private:
69 friend class RAII;
70 nsCOMPtr<aaISession> mSession;
71 nsCOMPtr<aaILoadQuery> mFlows;
72 nsCOMPtr<aaILoadQuery> mResources;
73 nsCOMPtr<aaIFact> mPendingFact;
75 nsresult load();
76 void free();
78 nsresult testEmptyBalance(nsITestRunner *aTestRunner);
79 nsresult testPendingFacts(nsITestRunner *aTestRunner);
80 nsresult testQuote(nsITestRunner *aTestRunner);
81 nsresult testBalanceRow(nsITestRunner *aTestRunner);
82 nsresult testTransaction(nsITestRunner *aTestRunner);
83 nsresult testUpdateTransaction(nsITestRunner *aTestRunner);
84 nsresult testReplaceTransaction(nsITestRunner *aTestRunner);
85 nsresult testFlowBalance(nsITestRunner *aTestRunner);
86 nsresult testStateFilter(nsITestRunner *aTestRunner);
87 nsresult testCreditTransaction(nsITestRunner *aTestRunner);
88 nsresult testDeleteTransaction(nsITestRunner *aTestRunner);
89 nsresult testStopTransaction(nsITestRunner *aTestRunner);
92 class RAII
94 public:
95 RAII(aaAccountTest *t);
96 ~RAII();
98 PRBool status;
99 private:
100 aaAccountTest* test;
103 /* nsITest */
104 NS_IMETHODIMP
105 aaAccountTest::Test(nsITestRunner *aTestRunner)
107 RAII res(this);
108 if (NS_FAILED( res.status )) {
109 aTestRunner->AddError(nsITestRunner::errorJS, \
110 AA_ACCOUNT_TEST_CONTRACT_ID " not initialized");
111 return NS_ERROR_NOT_INITIALIZED;
113 testEmptyBalance(aTestRunner);
114 testPendingFacts(aTestRunner);
115 testQuote(aTestRunner);
116 testBalanceRow(aTestRunner);
117 testTransaction(aTestRunner);
118 testUpdateTransaction(aTestRunner);
119 testReplaceTransaction(aTestRunner);
120 testFlowBalance(aTestRunner);
121 testStateFilter(aTestRunner);
122 testCreditTransaction(aTestRunner);
123 testDeleteTransaction(aTestRunner);
124 testStopTransaction(aTestRunner);
125 return NS_OK;
128 NS_IMPL_ISUPPORTS1(aaAccountTest, nsITest);
130 /* Boilerplate - factory & module */
131 NS_GENERIC_FACTORY_CONSTRUCTOR(aaAccountTest)
133 static const nsModuleComponentInfo kComponents[] =
136 "Account Submodule Unit Test",
137 AA_ACCOUNT_TEST_CID,
138 AA_ACCOUNT_TEST_CONTRACT_ID,
139 aaAccountTestConstructor
142 NS_IMPL_NSGETMODULE(aaaccountt, kComponents)
144 /* Helpers */
145 PRBool
146 testFact(nsITestRunner *cxxUnitTestRunner, aaIFact *node, PRInt64 fromFlowId,
147 PRInt64 toFlowId, double sum)
149 nsresult rv;
150 PRBool equals = PR_TRUE;
152 nsCOMPtr<aaIFlow> flow;
153 PRInt64 id;
154 node->GetTakeFrom(getter_AddRefs( flow ));
155 if ( flow ) {
156 flow->GetId( &id );
157 if (NS_UNLIKELY( id != fromFlowId )) {
158 equals = PR_FALSE;
159 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.takeFrom.id' is wrong" );
161 } else if (NS_UNLIKELY( fromFlowId != 0 )) {
162 equals = PR_FALSE;
163 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.takeFrom.id' is wrong" );
166 node->GetGiveTo(getter_AddRefs( flow ));
167 if ( flow ) {
168 flow->GetId( &id );
169 if (NS_UNLIKELY( id != toFlowId )) {
170 equals = PR_FALSE;
171 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.giveTo.id' is wrong" );
173 } else if (NS_UNLIKELY( toFlowId != 0 )) {
174 equals = PR_FALSE;
175 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.giveTo.id' is wrong" );
178 double amount, diff;
179 rv = node->GetAmount(&amount);
180 diff = amount - sum;
181 if (diff > 0.0001 || diff < -0.0001) {
182 equals = PR_FALSE;
183 NS_TEST_ASSERT_MSG(PR_FALSE, " 'fact.amount' is wrong" );
186 return equals;
189 PRBool
190 testBalance(nsITestRunner *cxxUnitTestRunner, aaIBalance *node, PRInt64 flowId,
191 PRInt64 resourceId, double amount, double value, PRBool aSide)
193 nsresult rv;
194 PRBool equals = PR_TRUE;
195 if (NS_UNLIKELY( ! node )) {
196 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance' is null");
197 return PR_FALSE;
199 nsCOMPtr<aaIFlow> flow;
200 rv = node->GetFlow(getter_AddRefs( flow ));
201 NS_TEST_ASSERT_MSG(flow, " 'balance.flow' is null");
202 PRInt64 id;
203 if (NS_LIKELY( flow )) {
204 flow->GetId(&id);
205 if (NS_UNLIKELY( id != flowId) ) {
206 equals = PR_FALSE;
207 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance.flow.id' is wrong");
209 } else {
210 equals = PR_FALSE;
213 nsCOMPtr<aaIResource> resource;
214 rv = node->GetResource(getter_AddRefs( resource ));
215 NS_TEST_ASSERT_MSG(resource, " 'balance.resource' is null" );
216 if (NS_LIKELY( resource )) {
217 resource->GetId(&id);
218 if (NS_UNLIKELY( id != resourceId) ) {
219 nsEmbedCString msg(" 'balance.resource.id' is wrong - ");
220 msg.AppendInt(id);
221 equals = PR_FALSE;
222 NS_TEST_ASSERT_MSG(PR_FALSE, msg.get());
224 } else {
225 equals = PR_FALSE;
228 double sum, diff;
229 node->GetAmount(&sum);
230 diff = amount - sum;
231 if (NS_UNLIKELY(diff > 0.0001 || diff < -0.0001)) {
232 equals = PR_FALSE;
233 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance.amount' is wrong");
236 node->GetValue(&sum);
237 diff = value - sum;
238 if (NS_UNLIKELY(diff > 0.0001 || diff < -0.0001)) {
239 equals = PR_FALSE;
240 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance.value' is wrong");
243 PRBool side;
244 node->GetSide(&side);
245 if (NS_UNLIKELY( side != aSide)) {
246 equals = PR_FALSE;
247 NS_TEST_ASSERT_MSG(PR_FALSE, " 'balance.side' is wrong");
249 return equals;
252 RAII::RAII(aaAccountTest *t)
253 :status(PR_FALSE), test(nsnull)
255 nsresult rv;
256 nsCOMPtr<aaISession> session(do_CreateInstance(AA_SESSION_CONTRACT_ID, &rv));
257 if (NS_UNLIKELY( ! session))
258 return;
260 nsCOMPtr<aaILoadQuery> flows;
261 rv = session->CreateQuery(AA_LOADFLOW_CONTRACT_ID, getter_AddRefs( flows ));
262 if (NS_UNLIKELY( ! flows))
263 return;
264 rv = flows->Load();
265 if (NS_FAILED(rv))
266 return;
268 nsCOMPtr<aaILoadQuery> resources;
269 rv = session->CreateQuery(AA_LOADRESOURCE_CONTRACT_ID, getter_AddRefs( resources ));
270 if (NS_UNLIKELY( ! resources))
271 return;
272 rv = resources->Load();
273 if (NS_FAILED(rv))
274 return;
276 status = PR_TRUE;
277 test = t;
278 test->mSession = session;
279 test->mFlows = flows;
280 test->mResources = resources;
283 RAII::~RAII()
285 test->mPendingFact = nsnull;
286 test->mResources = nsnull;
287 test->mFlows = nsnull;
288 test->mSession = nsnull;
289 test = nsnull;
292 /* Private methods */
293 nsresult
294 aaAccountTest::testEmptyBalance(nsITestRunner *aTestRunner)
296 nsresult rv;
297 NS_TEST_BEGIN(aTestRunner);
298 aaSession *session = static_cast<aaSession *>(mSession.get());
299 NS_TEST_ASSERT_MSG(session, "[empty balance] 'session' cast" );
300 NS_ENSURE_TRUE(session, NS_ERROR_UNEXPECTED);
302 nsCOMPtr<aaILoadQuery> set(do_CreateInstance(
303 AA_LOADBALANCE_CONTRACT_ID, session->mConnection, &rv));
304 NS_TEST_ASSERT_MSG(set, "[empty balance] query instance creation" );
305 NS_ENSURE_TRUE(set, rv);
307 rv = set->Load();
308 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[empty balance] loading");
309 NS_TEST_ASSERT_MSG(set, "[empty balance] result set not loaded" );
310 NS_ENSURE_TRUE(set, rv);
312 PRUint32 count;
313 set->GetLength(&count);
314 NS_TEST_ASSERT_MSG(count == 0, "[empty balance] wrong flow count");
316 return NS_OK;
319 nsresult
320 aaAccountTest::testPendingFacts(nsITestRunner *aTestRunner)
322 nsresult rv;
323 NS_TEST_BEGIN(aTestRunner);
324 aaSession *session = static_cast<aaSession *>(mSession.get());
325 NS_TEST_ASSERT_MSG(session, "[pending facts] 'session' cast" );
326 NS_ENSURE_TRUE(session, NS_ERROR_UNEXPECTED);
328 nsCOMPtr<aaILoadQuery> set(do_CreateInstance(
329 AA_LOADPENDINGFACTS_CONTRACT_ID, session->mConnection, &rv));
330 NS_TEST_ASSERT_MSG(set, "[pending facts] query instance creation" );
331 NS_ENSURE_TRUE(set, rv);
333 rv = set->Load();
334 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[pending facts] loading");
335 NS_TEST_ASSERT_MSG(set, "[pending facts] result set not loaded" );
336 NS_ENSURE_TRUE(set, rv);
338 PRUint32 count;
339 set->GetLength(&count);
340 NS_TEST_ASSERT_MSG(count == 6, "[pending facts] wrong fact count");
342 mPendingFact = do_QueryElementAt(set, 0, &rv);
343 NS_TEST_ASSERT_MSG(mPendingFact, "[pending facts] 1st fact not loaded" );
345 NS_TEST_ASSERT_MSG(testFact(aTestRunner, mPendingFact, 2, 3,
346 AA_EVENT_AMOUNT_2), "[pending facts] 1st fact is wrong");
347 return NS_OK;
350 nsresult
351 aaAccountTest::testQuote(nsITestRunner *aTestRunner)
353 nsresult rv;
354 NS_TEST_BEGIN(aTestRunner);
355 nsCOMPtr<aaIQuote> node(do_CreateInstance("@aasii.org/base/quote;1", &rv));
356 NS_TEST_ASSERT_MSG(node, "[quote] instance not created" );
357 NS_ENSURE_TRUE(node, rv);
359 nsCOMPtr<aaIResource> rub = do_QueryElementAt(mResources, 0, &rv);
360 node->SetResource( rub );
362 /* Set time to 2007-08-29 */
363 PRExplodedTime tm = {0,0,0,12,29,7,2007};
364 node->SetTime(PR_ImplodeTime(&tm));
365 node->SetRate(1);
367 rv = mSession->Save( node, nsnull );
368 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[quote] saving");
370 aaSession *session = static_cast<aaSession *>(mSession.get());
371 NS_TEST_ASSERT_MSG(session, "[quote] 'session' cast" );
372 NS_ENSURE_TRUE(session, NS_ERROR_UNEXPECTED);
374 nsCOMPtr<aaILoadQuery> set(do_CreateInstance(
375 AA_LOADQUOTE_CONTRACT_ID, session->mConnection, &rv));
376 NS_TEST_ASSERT_MSG(set, "[quote] query instance creation" );
377 NS_ENSURE_TRUE(set, rv);
379 rv = set->Load();
380 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[quote] loading");
381 NS_TEST_ASSERT_MSG(set, "[quote] result set not loaded" );
382 NS_ENSURE_TRUE(set, rv);
384 PRUint32 count;
385 set->GetLength(&count);
386 NS_TEST_ASSERT_MSG(count == 1, "[quote] wrong quote count");
388 nsCOMPtr<aaIQuote> quote = do_QueryElementAt(set, 0, &rv);
389 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[quote] quering 1st quote");
390 NS_TEST_ASSERT_MSG(quote, "[quote] 1st quote not loaded" );
391 NS_ENSURE_TRUE(quote, NS_ERROR_FAILURE);
393 nsCOMPtr<aaIResource> obj;
394 PRInt64 objId;
395 PRTime time;
396 double rate;
398 quote->GetResource(getter_AddRefs( obj ));
399 if (NS_LIKELY( obj )) {
400 obj->GetId( &objId );
401 NS_TEST_ASSERT_MSG(objId == 1, "[quote] wrong resource id");
402 } else {
403 NS_TEST_ASSERT_MSG(obj, "[quote] resource is null");
405 quote->GetTime( &time );
406 NS_TEST_ASSERT_MSG(time == PR_ImplodeTime(&tm), "[quote] wrong start time");
407 quote->GetRate( &rate );
408 NS_TEST_ASSERT_MSG(rate < 1.00009 && rate > 0.99991, "[quote] wrong rate");
410 return NS_OK;
413 nsresult
414 aaAccountTest::testBalanceRow(nsITestRunner *aTestRunner)
416 NS_TEST_BEGIN(aTestRunner);
417 aaFullQuote q1;
418 aaBalanceRow r1, r2;
419 PRBool res;
421 /* Tesing aaBalanceRow.SetSide() */
423 res = r1.SetSide(q1);
424 NS_TEST_ASSERT_MSG( !res, "[balance row] accepts side with bad type 3");
426 q1.mType = 0;
427 res = r1.SetSide(q1);
428 NS_TEST_ASSERT_MSG( !res, "[balance row] accepts side with bad type 0");
430 q1.mType = 1;
431 q1.mRate = 0.5;
432 res = r2.SetSide(q1);
433 NS_TEST_ASSERT_MSG( res, "[balance row] side 1 not accepted");
434 NS_TEST_ASSERT_MSG( r2.mFlowRate == q1.mRate,
435 "[balance row] wrong flow rate");
436 NS_TEST_ASSERT_MSG( r2.mFactSide == 0, "[balance row] wrong fact side");
437 NS_TEST_ASSERT_MSG( r2.mSide == 1, "[balance row] wrong balance side");
440 /* Tesing aaBalanceRow.SetFact() */
442 nsresult rv;
443 nsCOMPtr<aaIFact> fact = do_CreateInstance("@aasii.org/base/fact;1", &rv);
444 NS_ENSURE_TRUE(fact, rv);
445 nsCOMPtr<aaIFlow> flow1 = do_CreateInstance("@aasii.org/base/flow;1", &rv);
446 NS_ENSURE_TRUE(flow1, rv);
447 nsCOMPtr<aaIEvent> event = do_CreateInstance("@aasii.org/base/event;1",
448 &rv);
449 NS_ENSURE_TRUE(event, rv);
450 fact->SetGiveTo(flow1);
451 fact->SetAmount(2.0);
452 event->SetTime(1);
453 event->SetId(1);
454 fact->SetEvent(event);
455 res = r2.SetFact(fact);
456 NS_TEST_ASSERT_MSG( res, "[balance row] fact not accepted");
457 NS_TEST_ASSERT_MSG( r2.mAmount == 1, "[balance row] wrong amount");
458 NS_TEST_ASSERT_MSG( r2.mStart == 1, "[balance row] wrong start time");
461 double histAmnt = 0.0, histVal = 0.0;
462 /* Testing aaBalanceRow.Merge() - Part 1
463 * Case of increasing balance (0), same day (0), to-side (0)
464 * Case code is 000
467 r1.mFactSide = 0;
468 r1.mSide = 1;
469 r1.mAmount = 2;
470 r1.mFlowRate = 0.5;
471 r1.mValue = 4;
472 r1.mStart = 1;
473 r1.mPaid = 0;
474 r1.mStatus = aaBalanceRow::SIDE | aaBalanceRow::FLOW | aaBalanceRow::VALUE;
475 res = r1.Merge(r2, histAmnt, histVal);
476 NS_TEST_ASSERT_MSG( res, "[balance row] merge type 000 failed");
477 NS_TEST_ASSERT_MSG( histAmnt == 0.0,
478 "[balance row] historical amount is not zero");
479 NS_TEST_ASSERT_MSG( histVal == 0.0,
480 "[balance row] historical value is not zero");
481 NS_TEST_ASSERT_MSG( r2.mStatus & aaBalanceRow::CLEARED,
482 "[balance row] consumed balance not cleared");
483 NS_TEST_ASSERT_MSG( r2.mAmount == 0,
484 "[balance row] consumed amount not cleared");
485 NS_TEST_ASSERT_MSG( r2.mSink == &r1,
486 "[balance row] consumed balance has no sink");
487 NS_TEST_ASSERT_MSG( r1.mStatus & aaBalanceRow::UPDATE,
488 "[balance row] updated balance has wrong status");
489 NS_TEST_ASSERT_MSG( r1.mAmount == 3,
490 "[balance row] updated amount is wrong");
491 NS_TEST_ASSERT_MSG( r1.mValue == 4,
492 "[balance row] updated value has changed");
494 r2.AddValue(2);
495 NS_TEST_ASSERT_MSG( r1.mValue == 6,
496 "[balance row] updated value not changed");
499 /* Testing aaBalanceRow.Merge() - Part 2
500 * Case of decreasing balance (1), different day (1), from-side (1)
501 * Case code is 111
504 histAmnt = 0.0, histVal = 0.0;
506 r1.mFactSide = 1;
507 r1.mSide = 1;
508 r1.mAmount = 3;
509 r1.mFlowRate = 2;
510 r1.mValue = 6;
511 r1.mStart = 1;
512 r1.mPaid = 0;
513 r1.mStatus = aaBalanceRow::SIDE | aaBalanceRow::FLOW | aaBalanceRow::VALUE;
515 r2.mFactSide = 1;
516 r2.mSide = 0;
517 r2.mAmount = 4;
518 r2.mFlowRate = 2;
519 r2.mValue = 0;
520 r2.mStart = 2;
521 r2.mPaid = 0;
522 r2.mStatus = aaBalanceRow::SIDE | aaBalanceRow::FLOW;
523 r2.mSink = nsnull;
524 res = r1.Merge(r2, histAmnt, histVal);
525 NS_TEST_ASSERT_MSG( res, "[balance row] merge type 111 failed");
526 NS_TEST_ASSERT_MSG( histAmnt == 2.0,
527 "[balance row] historical amount is wrong");
528 NS_TEST_ASSERT_MSG( histVal == 4.0,
529 "[balance row] historical value is wrong");
530 NS_TEST_ASSERT_MSG( r1.mStatus & aaBalanceRow::CLEARED,
531 "[balance row] consumed balance not cleared");
532 NS_TEST_ASSERT_MSG( r1.mAmount == 3.0,
533 "[balance row] consumed amount changed");
534 NS_TEST_ASSERT_MSG( r2.mSink == nsnull,
535 "[balance row] replacing balance has sink");
536 NS_TEST_ASSERT_MSG( r2.mAmount == 1.0,
537 "[balance row] replacing amount is wrong");
538 NS_TEST_ASSERT_MSG( r2.mValue == 2.0,
539 "[balance row] replacing value is wrong");
542 /* Testing aaBalanceRow.Merge() - Part 3
543 * Case of decreasing balance (1), same day (0), from-side (0)
544 * Case code is 100
547 histAmnt = 0.0, histVal = 0.0;
549 r1.mFactSide = 0;
550 r1.mSide = 0;
551 r1.mAmount = 34950.0;
552 r1.mFlowRate = 1 / 34.95;
553 r1.mValue = 34950.0;
554 r1.mStart = 1;
555 r1.mPaid = 0;
556 r1.mStatus = aaBalanceRow::SIDE | aaBalanceRow::FLOW | aaBalanceRow::VALUE;
558 r2.mFactSide = 0;
559 r2.mSide = 1;
560 r2.mAmount = 1000.0;
561 r2.mFlowRate = 1 / 34.95;
562 r2.mValue = 0;
563 r2.mStart = 1;
564 r2.mPaid = 0;
565 r2.mStatus = aaBalanceRow::SIDE | aaBalanceRow::FLOW;
566 r2.mSink = nsnull;
567 res = r1.Merge(r2, histAmnt, histVal);
568 NS_TEST_ASSERT_MSG( res, "[balance row] merge type 100 failed");
569 NS_TEST_ASSERT_MSG( isZero( histAmnt - 34950.0),
570 "[balance row] historical amount is wrong");
571 NS_TEST_ASSERT_MSG( isZero( histVal - 34950.0),
572 "[balance row] historical value is wrong");
573 NS_TEST_ASSERT_MSG( r2.mStatus & aaBalanceRow::CLEARED,
574 "[balance row] consumed balance not cleared");
575 NS_TEST_ASSERT_MSG( r2.mAmount == 0,
576 "[balance row] consumed amount not cleared");
577 NS_TEST_ASSERT_MSG( r2.mSink == &r1,
578 "[balance row] consumed balance has no sink");
579 NS_TEST_ASSERT_MSG( r1.mStatus & aaBalanceRow::UPDATE,
580 "[balance row] updated balance has wrong status");
581 NS_TEST_ASSERT_MSG( isZero(r1.mAmount),
582 "[balance row] updated amount is wrong");
583 NS_TEST_ASSERT_MSG( isZero(r1.mValue),
584 "[balance row] updated value wrong");
587 return NS_OK;
590 nsresult
591 aaAccountTest::testTransaction(nsITestRunner *aTestRunner)
593 nsresult rv;
594 NS_TEST_BEGIN(aTestRunner);
595 aaSession *session = static_cast<aaSession *>(mSession.get());
596 NS_TEST_ASSERT_MSG(session, "[transaction] 'session' cast" );
597 NS_ENSURE_TRUE(session, NS_ERROR_UNEXPECTED);
599 nsCOMPtr<aaISaveQuery> saver(do_CreateInstance(
600 AA_SAVETRANSACTION_CONTRACT_ID, session->mConnection, &rv));
601 NS_TEST_ASSERT_MSG(saver, "[transaction] query instance creation" );
602 NS_ENSURE_TRUE(saver, rv);
604 nsCOMPtr<aaITransaction> node(do_CreateInstance(
605 "@aasii.org/base/transaction;1", &rv));
606 NS_TEST_ASSERT_MSG(node, "[transaction] instance not created" );
607 NS_ENSURE_TRUE(node, rv);
609 node->SetFact(mPendingFact);
611 rv = saver->Save(node, nsnull);
612 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transaction] saving" );
614 nsCOMPtr<aaILoadQuery> set;
615 rv = mSession->CreateQuery(AA_LOADBALANCE_CONTRACT_ID, getter_AddRefs( set ));
616 NS_TEST_ASSERT_MSG(set, "[transaction] result set not created" );
617 NS_ENSURE_TRUE(set, rv);
618 rv = set->Load();
619 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transaction] result set not loaded" );
621 PRUint32 count;
622 set->GetLength(&count);
623 NS_TEST_ASSERT_MSG(count == 2, "[transaction] wrong flow count");
625 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(set, 0));
626 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 2, 2, AA_EVENT_AMOUNT_2
627 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_2, 0),
628 "[transaction] flow2 is wrong");
630 balance = do_QueryElementAt(set, 1);
631 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2,
632 AA_EVENT_AMOUNT_2, 1), "[transaction] flow3 is wrong");
634 rv = mSession->CreateQuery(AA_LOADPENDINGFACTS_CONTRACT_ID,
635 getter_AddRefs( set ));
636 NS_TEST_ASSERT_MSG(set, "[transaction] fact set not created" );
637 NS_ENSURE_TRUE(set, rv);
638 rv = set->Load();
639 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[transaction] fact set not loaded" );
641 set->GetLength(&count);
642 NS_TEST_ASSERT_MSG(count == AA_TXN_COUNT - 1,
643 "[transaction] wrong fact count");
645 mPendingFact = do_QueryElementAt(set, 0, &rv);
646 NS_TEST_ASSERT_MSG(mPendingFact, "[transaction] pending fact not loaded" );
648 NS_TEST_ASSERT_MSG(testFact(aTestRunner, mPendingFact, 1, 3,
649 AA_EVENT_AMOUNT_3), "[transaction] pending fact is wrong");
650 return NS_OK;
653 nsresult
654 aaAccountTest::testUpdateTransaction(nsITestRunner *aTestRunner)
656 nsresult rv;
657 NS_TEST_BEGIN(aTestRunner);
659 nsCOMPtr<aaITransaction> node(do_CreateInstance(
660 "@aasii.org/base/transaction;1", &rv));
661 NS_TEST_ASSERT_MSG(node, "[update txn] instance not created" );
662 NS_ENSURE_TRUE(node, rv);
664 node->SetFact(mPendingFact);
666 rv = mSession->Save(node, nsnull);
667 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[update txn] saving" );
669 nsCOMPtr<aaILoadQuery> set;
670 rv = mSession->CreateQuery(AA_LOADBALANCE_CONTRACT_ID, getter_AddRefs( set ));
671 NS_TEST_ASSERT_MSG(set, "[update txn] result set not created" );
672 NS_ENSURE_TRUE(set, rv);
673 rv = set->Load();
674 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[update txn] result set not loaded" );
676 PRUint32 count;
677 set->GetLength(&count);
678 NS_TEST_ASSERT_MSG(count == 3, "[update txn] wrong flow count");
680 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(set, 0));
681 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 2, 2, AA_EVENT_AMOUNT_2
682 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_2, 0),
683 "[update txn] flow2 is wrong");
685 balance = do_QueryElementAt(set, 1);
686 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
687 + AA_EVENT_AMOUNT_3, AA_EVENT_AMOUNT_2 + AA_EVENT_AMOUNT_3, 1),
688 "[update txn] flow3 is wrong");
690 balance = do_QueryElementAt(set, 2);
691 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 1, 2, AA_EVENT_AMOUNT_3
692 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_3, 0),
693 "[update txn] flow1 is wrong");
695 rv = mSession->CreateQuery(AA_LOADPENDINGFACTS_CONTRACT_ID,
696 getter_AddRefs( set ));
697 NS_TEST_ASSERT_MSG(set, "[update txn] fact set not created" );
698 NS_ENSURE_TRUE(set, rv);
699 rv = set->Load();
700 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[update txn] fact set not loaded" );
702 set->GetLength(&count);
703 NS_TEST_ASSERT_MSG(count == AA_TXN_COUNT - 2,
704 "[update txn] wrong fact count");
706 mPendingFact = do_QueryElementAt(set, 0, &rv);
707 NS_TEST_ASSERT_MSG(mPendingFact, "[update txn] pending fact not loaded" );
709 NS_TEST_ASSERT_MSG(testFact(aTestRunner, mPendingFact, 3, 4,
710 AA_EVENT_AMOUNT_4), "[update txn] pending fact is wrong");
711 return NS_OK;
714 nsresult
715 aaAccountTest::testReplaceTransaction(nsITestRunner *aTestRunner)
717 nsresult rv;
718 NS_TEST_BEGIN(aTestRunner);
720 nsCOMPtr<aaITransaction> node(do_CreateInstance(
721 "@aasii.org/base/transaction;1", &rv));
722 NS_TEST_ASSERT_MSG(node, "[replace txn] instance not created" );
723 NS_ENSURE_TRUE(node, rv);
725 node->SetFact(mPendingFact);
727 rv = mSession->Save(node, nsnull);
728 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[replace txn] saving" );
730 nsCOMPtr<aaILoadQuery> set;
731 rv = mSession->CreateQuery(AA_LOADBALANCE_CONTRACT_ID, getter_AddRefs( set ));
732 NS_TEST_ASSERT_MSG(set, "[replace txn] result set not created" );
733 NS_ENSURE_TRUE(set, rv);
734 rv = set->Load();
735 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[replace txn] result set not loaded" );
737 PRUint32 count;
738 set->GetLength(&count);
739 NS_TEST_ASSERT_MSG(count == 4, "[replace txn] wrong flow count");
741 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(set, 0));
742 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 2, 2, AA_EVENT_AMOUNT_2
743 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_2, 0),
744 "[replace txn] flow2 is wrong");
746 balance = do_QueryElementAt(set, 1);
747 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 1, 2, AA_EVENT_AMOUNT_3
748 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_3, 0),
749 "[replace txn] flow1 is wrong");
751 balance = do_QueryElementAt(set, 2);
752 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
753 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4, AA_EVENT_AMOUNT_2
754 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4, 1),
755 "[replace txn] flow3 is wrong");
757 balance = do_QueryElementAt(set, 3);
758 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 4, 3, AA_EVENT_AMOUNT_5,
759 AA_EVENT_AMOUNT_4, 1), "[replace txn] flow4 is wrong");
761 rv = mSession->CreateQuery(AA_LOADPENDINGFACTS_CONTRACT_ID,
762 getter_AddRefs( set ));
763 NS_TEST_ASSERT_MSG(set, "[replace txn] fact set not created" );
764 NS_ENSURE_TRUE(set, rv);
765 rv = set->Load();
766 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[replace txn] fact set not loaded" );
768 set->GetLength(&count);
769 NS_TEST_ASSERT_MSG(count == AA_TXN_COUNT - 3,
770 "[replace txn] wrong fact count");
772 mPendingFact = do_QueryElementAt(set, 0, &rv);
773 NS_TEST_ASSERT_MSG(mPendingFact, "[transaction] pending fact not loaded" );
775 NS_TEST_ASSERT_MSG(testFact(aTestRunner, mPendingFact, 5, 6,
776 AA_EVENT_AMOUNT_6), "[transaction] pending fact is wrong");
777 return NS_OK;
780 #define AA_FB_FILTER1 " WHERE balance.flow_id=3 AND balance.start =\
781 (SELECT MAX(start) FROM balance WHERE flow_id=3 AND start <= 2454342)\
782 AND (balance.paid > 2454342 OR balance.paid IS NULL)"
784 nsresult
785 aaAccountTest::testFlowBalance(nsITestRunner *aTestRunner)
787 nsresult rv;
788 NS_TEST_BEGIN(aTestRunner);
790 nsCOMPtr<aaILoadQuery> set;
791 rv = mSession->CreateQuery(AA_LOADBALANCE_CONTRACT_ID, getter_AddRefs( set ));
792 NS_TEST_ASSERT_MSG(set, "[flow balance] result set not created" );
793 NS_ENSURE_TRUE(set, rv);
795 nsCOMPtr<aaIStringFilter> filter(do_CreateInstance(AA_FILTER_CONTRACT_ID));
796 filter->SetExpression(NS_LITERAL_CSTRING(AA_FB_FILTER1));
797 set->SetFilter(filter);
798 rv = set->Load();
799 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[flow balance] result set not loaded" );
801 PRUint32 count;
802 set->GetLength(&count);
803 NS_TEST_ASSERT_MSG(count == 1, "[flow balance] wrong count");
805 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(set, 0));
806 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
807 + AA_EVENT_AMOUNT_3, AA_EVENT_AMOUNT_2 + AA_EVENT_AMOUNT_3, 1),
808 "[flow balance] flow3 is wrong");
810 return NS_OK;
813 nsresult
814 aaAccountTest::testStateFilter(nsITestRunner *aTestRunner)
816 nsresult rv;
817 NS_TEST_BEGIN(aTestRunner);
819 nsCOMPtr<aaILoadQuery> set;
820 rv = mSession->CreateQuery(AA_LOADBALANCE_CONTRACT_ID, getter_AddRefs( set ));
821 NS_TEST_ASSERT_MSG(set, "[state filter] result set not created" );
822 NS_ENSURE_TRUE(set, rv);
824 nsCOMPtr<aaIStateFilter> filter =
825 do_CreateInstance(AA_STATEFILTER_CONTRACT_ID);
827 nsCOMPtr<aaIFlow> bank = do_QueryElementAt(mFlows, 2);
828 filter->SetFlow( bank );
830 /* Set date to 2007-08-29 */
831 PRExplodedTime tm = {0,0,0,12,29,7,2007};
832 filter->SetDate(PR_ImplodeTime(&tm));
834 set->SetFilter(filter);
835 rv = set->Load();
836 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[state filter] result set not loaded" );
838 PRUint32 count;
839 set->GetLength(&count);
840 NS_TEST_ASSERT_MSG(count == 1, "[state filter] wrong count");
842 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(set, 0));
843 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
844 + AA_EVENT_AMOUNT_3, AA_EVENT_AMOUNT_2 + AA_EVENT_AMOUNT_3, 1),
845 "[state filter] flow3 is wrong");
847 return NS_OK;
850 nsresult
851 aaAccountTest::testCreditTransaction(nsITestRunner *aTestRunner)
853 nsresult rv;
854 NS_TEST_BEGIN(aTestRunner);
856 nsCOMPtr<aaITransaction> node(do_CreateInstance(
857 "@aasii.org/base/transaction;1", &rv));
858 NS_TEST_ASSERT_MSG(node, "[credit txn] instance not created" );
859 NS_ENSURE_TRUE(node, rv);
861 node->SetFact(mPendingFact);
863 rv = mSession->Save(node, nsnull);
864 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[credit txn] saving" );
866 nsCOMPtr<aaILoadQuery> set;
867 rv = mSession->CreateQuery(AA_LOADBALANCE_CONTRACT_ID, getter_AddRefs( set ));
868 NS_TEST_ASSERT_MSG(set, "[credit txn] result set not created" );
869 NS_ENSURE_TRUE(set, rv);
870 rv = set->Load();
871 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[credit txn] result set not loaded" );
873 PRUint32 count;
874 set->GetLength(&count);
875 NS_TEST_ASSERT_MSG(count == 6, "[credit txn] wrong flow count");
877 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(set, 0));
878 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 2, 2, AA_EVENT_AMOUNT_2
879 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_2, 0),
880 "[credit txn] flow2 is wrong");
882 balance = do_QueryElementAt(set, 1);
883 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 1, 2, AA_EVENT_AMOUNT_3
884 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_3, 0),
885 "[credit txn] flow1 is wrong");
887 balance = do_QueryElementAt(set, 2);
888 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
889 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4, AA_EVENT_AMOUNT_2
890 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4, 1),
891 "[credit txn] flow3 is wrong");
893 balance = do_QueryElementAt(set, 3);
894 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 4, 3, AA_EVENT_AMOUNT_5,
895 AA_EVENT_AMOUNT_4, 1), "[credit txn] flow4 is wrong");
897 balance = do_QueryElementAt(set, 4);
898 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 5, 1, AA_EVENT_AMOUNT_6
899 * AA_EVENT_RATE_2, AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, 0),
900 "[credit txn] flow5 is wrong");
902 balance = do_QueryElementAt(set, 5);
903 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 6, 4, AA_EVENT_AMOUNT_6,
904 AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, 1), "[credit txn] flow6 is wrong");
906 rv = mSession->CreateQuery(AA_LOADPENDINGFACTS_CONTRACT_ID,
907 getter_AddRefs( set ));
908 NS_TEST_ASSERT_MSG(set, "[credit txn] fact set not created" );
909 NS_ENSURE_TRUE(set, rv);
910 rv = set->Load();
911 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[credit txn] fact set not loaded" );
913 set->GetLength(&count);
914 NS_TEST_ASSERT_MSG(count == AA_TXN_COUNT - 4,
915 "[credit txn] wrong fact count");
917 mPendingFact = do_QueryElementAt(set, 0, &rv);
918 NS_TEST_ASSERT_MSG(mPendingFact, "[transaction] pending fact not loaded" );
920 NS_TEST_ASSERT_MSG(testFact(aTestRunner, mPendingFact, 3, 5,
921 AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2),
922 "[transaction] pending fact is wrong");
923 return NS_OK;
926 nsresult
927 aaAccountTest::testDeleteTransaction(nsITestRunner *aTestRunner)
929 nsresult rv;
930 NS_TEST_BEGIN(aTestRunner);
932 nsCOMPtr<aaITransaction> node(do_CreateInstance(
933 "@aasii.org/base/transaction;1", &rv));
934 NS_TEST_ASSERT_MSG(node, "[delete txn] instance not created" );
935 NS_ENSURE_TRUE(node, rv);
937 node->SetFact(mPendingFact);
939 rv = mSession->Save(node, nsnull);
940 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[delete txn] saving" );
942 nsCOMPtr<aaILoadQuery> list;
943 rv = mSession->CreateQuery(AA_LOADFACTLIST_CONTRACT_ID,
944 getter_AddRefs( list ));
945 NS_TEST_ASSERT_MSG(list, "[delete txn] list not created" );
946 NS_ENSURE_TRUE(list, rv);
947 rv = list->Load();
948 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[delete txn] list not loaded" );
950 PRUint32 count = 0;
951 rv = list->GetLength(&count);
952 NS_TEST_ASSERT_OK(rv);
953 NS_TEST_ASSERT_MSG(count == 5, "[delete txn] wrong fact count");
955 nsCOMPtr<aaITransaction> txn = do_QueryElementAt(list, 4);
956 NS_TEST_ASSERT_MSG(txn, "[delete txn] transaction not loaded");
957 if (NS_LIKELY(txn)) {
958 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 3, 5,
959 AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2),
960 "[delete txn] new fact is wrong");
961 NS_TEST_ASSERT_MSG(txn->PickValue() == AA_EVENT_AMOUNT_6
962 * AA_EVENT_RATE_2, "[delete txn] wrong value stored");
965 nsCOMPtr<aaILoadQuery> set;
966 rv = mSession->CreateQuery(AA_LOADBALANCE_CONTRACT_ID, getter_AddRefs( set ));
967 NS_TEST_ASSERT_MSG(set, "[delete txn] result set not created" );
968 NS_ENSURE_TRUE(set, rv);
969 rv = set->Load();
970 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[delete txn] result set not loaded" );
972 count = 0;
973 set->GetLength(&count);
974 NS_TEST_ASSERT_MSG(count == 5, "[delete txn] wrong flow count");
976 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(set, 0));
977 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 2, 2, AA_EVENT_AMOUNT_2
978 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_2, 0),
979 "[delete txn] flow2 is wrong");
981 balance = do_QueryElementAt(set, 1);
982 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 1, 2, AA_EVENT_AMOUNT_3
983 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_3, 0),
984 "[delete txn] flow1 is wrong");
986 balance = do_QueryElementAt(set, 2);
987 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
988 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4
989 - AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, AA_EVENT_AMOUNT_2
990 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4
991 - AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, 1),
992 "[delete txn] flow3 is wrong");
994 balance = do_QueryElementAt(set, 3);
995 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 4, 3, AA_EVENT_AMOUNT_5,
996 AA_EVENT_AMOUNT_4, 1), "[delete txn] flow4 is wrong");
998 balance = do_QueryElementAt(set, 4);
999 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 6, 4, AA_EVENT_AMOUNT_6,
1000 AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, 1), "[delete txn] flow6 is wrong");
1002 rv = mSession->CreateQuery(AA_LOADPENDINGFACTS_CONTRACT_ID,
1003 getter_AddRefs( set ));
1004 NS_TEST_ASSERT_MSG(set, "[delete txn] fact set not created" );
1005 NS_ENSURE_TRUE(set, rv);
1006 rv = set->Load();
1007 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[delete txn] fact set not loaded" );
1009 set->GetLength(&count);
1010 NS_TEST_ASSERT_MSG(count == AA_TXN_COUNT - 5,
1011 "[delete txn] wrong fact count");
1013 mPendingFact = do_QueryElementAt(set, 0, &rv);
1014 NS_TEST_ASSERT_MSG(mPendingFact, "[delete txn] pending fact not loaded" );
1016 NS_TEST_ASSERT_MSG(testFact(aTestRunner, mPendingFact, 6, 7,
1017 AA_EVENT_AMOUNT_6),
1018 "[transaction] pending fact is wrong");
1019 return NS_OK;
1022 nsresult
1023 aaAccountTest::testStopTransaction(nsITestRunner *aTestRunner)
1025 nsresult rv;
1026 NS_TEST_BEGIN(aTestRunner);
1028 nsCOMPtr<aaITransaction> node(do_CreateInstance(
1029 "@aasii.org/base/transaction;1", &rv));
1030 NS_TEST_ASSERT_MSG(node, "[stop txn] instance not created" );
1031 NS_ENSURE_TRUE(node, rv);
1033 node->SetFact(mPendingFact);
1035 #if 0
1036 rv = mSession->Save(node, nsnull);
1037 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[stop txn] saving" );
1039 nsCOMPtr<aaILoadQuery> list;
1040 rv = mSession->CreateQuery(AA_LOADFACTLIST_CONTRACT_ID,
1041 getter_AddRefs( list ));
1042 NS_TEST_ASSERT_MSG(list, "[stop txn] list not created" );
1043 NS_ENSURE_TRUE(list, rv);
1044 rv = list->Load();
1045 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[stop txn] list not loaded" );
1047 PRUint32 count = 0;
1048 rv = list->GetLength(&count);
1049 NS_TEST_ASSERT_OK(rv);
1050 NS_TEST_ASSERT_MSG(count == 5, "[stop txn] wrong fact count");
1052 nsCOMPtr<aaITransaction> txn = do_QueryElementAt(list, 4);
1053 NS_TEST_ASSERT_MSG(txn, "[stop txn] transaction not loaded");
1054 if (NS_LIKELY(txn)) {
1055 NS_TEST_ASSERT_MSG(testFact(aTestRunner, txn->PickFact(), 6, 7,
1056 AA_EVENT_AMOUNT_6),
1057 "[stop txn] new fact is wrong");
1058 NS_TEST_ASSERT_MSG(txn->PickValue() == AA_EVENT_AMOUNT_6
1059 * AA_EVENT_RATE_3, "[stop txn] wrong value stored");
1062 nsCOMPtr<aaILoadQuery> set;
1063 rv = mSession->CreateQuery(AA_LOADBALANCE_CONTRACT_ID, getter_AddRefs( set ));
1064 NS_TEST_ASSERT_MSG(set, "[stop txn] result set not created" );
1065 NS_ENSURE_TRUE(set, rv);
1066 rv = set->Load();
1067 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[stop txn] result set not loaded" );
1069 count = 0;
1070 set->GetLength(&count);
1071 NS_TEST_ASSERT_MSG(count == 5, "[stop txn] wrong flow count");
1073 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(set, 0));
1074 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 2, 2, AA_EVENT_AMOUNT_2
1075 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_2, 0),
1076 "[stop txn] flow2 is wrong");
1078 balance = do_QueryElementAt(set, 1);
1079 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 1, 2, AA_EVENT_AMOUNT_3
1080 / AA_FLOW_SHARE_RATE, AA_EVENT_AMOUNT_3, 0),
1081 "[stop txn] flow1 is wrong");
1083 balance = do_QueryElementAt(set, 2);
1084 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 3, 1, AA_EVENT_AMOUNT_2
1085 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4
1086 - AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, AA_EVENT_AMOUNT_2
1087 + AA_EVENT_AMOUNT_3 - AA_EVENT_AMOUNT_4
1088 - AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_2, 1),
1089 "[stop txn] flow3 is wrong");
1091 balance = do_QueryElementAt(set, 3);
1092 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 4, 3, AA_EVENT_AMOUNT_5,
1093 AA_EVENT_AMOUNT_4, 1), "[stop txn] flow4 is wrong");
1095 balance = do_QueryElementAt(set, 4);
1096 NS_TEST_ASSERT_MSG(testBalance(aTestRunner, balance, 7, 1, AA_EVENT_AMOUNT_6
1097 * AA_EVENT_RATE_3, AA_EVENT_AMOUNT_6 * AA_EVENT_RATE_3, 1),
1098 "[stop txn] flow7 is wrong");
1100 rv = mSession->CreateQuery(AA_LOADPENDINGFACTS_CONTRACT_ID,
1101 getter_AddRefs( set ));
1102 NS_TEST_ASSERT_MSG(set, "[stop txn] fact set not created" );
1103 NS_ENSURE_TRUE(set, rv);
1104 rv = set->Load();
1105 NS_TEST_ASSERT_MSG(NS_SUCCEEDED(rv), "[stop txn] fact set not loaded" );
1107 set->GetLength(&count);
1108 NS_TEST_ASSERT_MSG(count == AA_TXN_COUNT - 6,
1109 "[stop txn] wrong fact count");
1110 #endif
1111 return NS_OK;