Add test for special cases for account with multiple balance of the same type

This commit is contained in:
TeoV
2019-05-07 16:28:43 +03:00
committed by Dan Christian Bogos
parent d9de22cb55
commit 47e7e67f42
4 changed files with 164 additions and 2 deletions

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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() {

View File

@@ -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}