From 47e7e67f42dfd0d142f1ce08d10b6eb6f3df4dfb Mon Sep 17 00:00:00 2001 From: TeoV Date: Tue, 7 May 2019 16:28:43 +0300 Subject: [PATCH] Add test for special cases for account with multiple balance of the same type --- apier/v2/cdrs_offline_it_test.go | 56 +++++++++++++++++++- engine/account_test.go | 87 ++++++++++++++++++++++++++++++++ engine/balances.go | 1 - engine/balances_test.go | 22 ++++++++ 4 files changed, 164 insertions(+), 2 deletions(-) diff --git a/apier/v2/cdrs_offline_it_test.go b/apier/v2/cdrs_offline_it_test.go index 0fe2e713a..f91ef4973 100644 --- a/apier/v2/cdrs_offline_it_test.go +++ b/apier/v2/cdrs_offline_it_test.go @@ -49,7 +49,7 @@ var sTestsCDRsOfflineIT = []func(t *testing.T){ testV2CDRsOfflineLoadData, testV2CDRsOfflineBalanceUpdate, testV2CDRsOfflineExpiryBalance, - + testV2CDRsSpecialCase, testV2CDRsOfflineKillEngine, } @@ -329,6 +329,60 @@ func testV2CDRsOfflineExpiryBalance(t *testing.T) { time.Sleep(time.Duration(150) * time.Millisecond) // Give time for CDR to be rated } +func testV2CDRsSpecialCase(t *testing.T) { + //add a test account with balance type monetary and value 10 + attrs := &utils.AttrSetBalance{ + Tenant: "cgrates.org", + Account: "specialTest", + BalanceType: utils.MONETARY, + BalanceID: utils.StringPointer("SpecialBalance1"), + Value: utils.Float64Pointer(10.0), + Weight: utils.Float64Pointer(10.0), + } + var reply string + if err := cdrsOfflineRpc.Call("ApierV2.SetBalance", attrs, &reply); err != nil { + t.Fatal(err) + } + attrs.BalanceID = utils.StringPointer("SpecialBalance2") + if err := cdrsOfflineRpc.Call("ApierV2.SetBalance", attrs, &reply); err != nil { + t.Fatal(err) + } + var acnt *engine.Account + if err := cdrsOfflineRpc.Call("ApierV2.GetAccount", &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "specialTest"}, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap) != 1 || len(acnt.BalanceMap[utils.MONETARY]) != 2 { + t.Errorf("Unexpected balance received: %+v", acnt.BalanceMap[utils.MONETARY]) + } + + cgrEv := &utils.CGREvent{ + Tenant: "cgrates.org", + Event: map[string]interface{}{ + utils.OriginID: "testV2CDRsSpecialCase", + utils.OriginHost: "192.168.1.1", + utils.Source: "testV2CDRsSpecialCase", + utils.RequestType: utils.META_POSTPAID, + utils.Category: "call", + utils.Account: "specialTest", + utils.Subject: "specialTest", + utils.Destination: "1002", + utils.AnswerTime: time.Date(2018, 8, 24, 16, 00, 26, 0, time.UTC), + utils.Usage: time.Duration(1) * time.Minute, + }, + } + mapEv := engine.NewMapEvent(cgrEv.Event) + cdr, err := mapEv.AsCDR(nil, "cgrates.org", "") + if err != nil { + t.Error("Unexpected error received: ", err) + } + //process cdr should trigger balance update event + if err := cdrsOfflineRpc.Call(utils.CDRsV1ProcessCDR, cdr, &reply); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if reply != utils.OK { + t.Error("Unexpected reply received: ", reply) + } + time.Sleep(time.Duration(150) * time.Millisecond) // Give time for CDR to be rated +} + func testV2CDRsOfflineKillEngine(t *testing.T) { if err := engine.KillEngine(*waitRater); err != nil { t.Error(err) diff --git a/engine/account_test.go b/engine/account_test.go index f874da2b2..3fa5b3d31 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -2186,6 +2186,93 @@ func TestAccountAsAccountDigest(t *testing.T) { } } +func TestAccountGetBalancesForPrefixSpecialCases(t *testing.T) { + acc := &Account{ + BalanceMap: map[string]Balances{ + utils.MONETARY: Balances{ + &Balance{ + ID: "SpecialBalance1", + Value: 10, + Weight: 10.0, + }, + &Balance{ + ID: "SpecialBalance2", + Value: 10, + Weight: 10.0, + }, + }, + }, + } + bcs := acc.getBalancesForPrefix("", "", utils.MONETARY, "") + if len(bcs) != 2 && bcs[0].ID != "SpecialBalance1" && bcs[1].ID != "SpecialBalance2" { + t.Errorf("Unexpected order balances : %+v", utils.ToJSON(bcs)) + } +} + +func TestAccountGetBalancesForPrefixSpecialCases2(t *testing.T) { + acc := &Account{ + BalanceMap: map[string]Balances{ + utils.MONETARY: Balances{ + &Balance{ + ID: "SpecialBalance1", + Value: 10, + Weight: 10.0, + }, + &Balance{ + ID: "SpecialBalance2", + Value: 10, + Weight: 20.0, + }, + }, + }, + } + bcs := acc.getBalancesForPrefix("", "", utils.MONETARY, "") + if len(bcs) != 2 && bcs[0].ID != "SpecialBalance2" && bcs[0].Weight != 20.0 { + t.Errorf("Unexpected order balances : %+v", utils.ToJSON(bcs)) + } +} + +func TestAccountGetBalancesForPrefixSpecialCases3(t *testing.T) { + acc := &Account{ + BalanceMap: map[string]Balances{ + utils.MONETARY: Balances{ + &Balance{ + ID: "SpecialBalance1", + Value: 10, + Weight: 10.0, + }, + &Balance{ + ID: "SpecialBalance2", + Value: 10, + Weight: 10.0, + }, + &Balance{ + ID: "SpecialBalance3", + Value: 10, + Weight: 10.0, + }, + &Balance{ + ID: "SpecialBalance4", + Value: 10, + Weight: 10.0, + }, + &Balance{ + ID: "SpecialBalance5", + Value: 10, + Weight: 10.0, + }, + }, + }, + } + bcs := acc.getBalancesForPrefix("", "", utils.MONETARY, "") + if len(bcs) != 5 && + bcs[0].ID != "SpecialBalance1" && bcs[1].ID != "SpecialBalance2" && + bcs[2].ID != "SpecialBalance3" && bcs[3].ID != "SpecialBalance4" && + bcs[4].ID != "SpecialBalance5" { + t.Errorf("Unexpected order balances : %+v", utils.ToJSON(bcs)) + } +} + /*********************************** Benchmarks *******************************/ func BenchmarkGetSecondForPrefix(b *testing.B) { diff --git a/engine/balances.go b/engine/balances.go index d11e8a0a6..3a0e7ef1f 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -740,7 +740,6 @@ func (bc Balances) Swap(i, j int) { func (bc Balances) Less(j, i int) bool { return bc[i].precision < bc[j].precision || (bc[i].precision == bc[j].precision && bc[i].Weight < bc[j].Weight) - } func (bc Balances) Sort() { diff --git a/engine/balances_test.go b/engine/balances_test.go index a657217c3..fea4d1bf6 100644 --- a/engine/balances_test.go +++ b/engine/balances_test.go @@ -67,6 +67,28 @@ func TestBalanceSortWeight(t *testing.T) { } } +func TestBalanceSortWeight2(t *testing.T) { + bs := Balances{ + &Balance{ID: "B1", Weight: 2, precision: 1}, + &Balance{ID: "B2", Weight: 1, precision: 1}, + } + bs.Sort() + if bs[0].ID != "B1" && bs[1].ID != "B2" { + t.Error("Buckets not sorted by precision!") + } +} + +func TestBalanceSortWeight3(t *testing.T) { + bs := Balances{ + &Balance{ID: "B1", Weight: 2, Value: 10.0}, + &Balance{ID: "B2", Weight: 4, Value: 10.0}, + } + bs.Sort() + if bs[0].ID != "B2" && bs[1].ID != "B1" { + t.Error(utils.ToJSON(bs)) + } +} + func TestBalanceSortWeightLess(t *testing.T) { mb1 := &Balance{Weight: 1, precision: 1} mb2 := &Balance{Weight: 2, precision: 1}