diff --git a/apier/v1/accounts.go b/apier/v1/accounts.go index d294c7b25..0e7926d06 100644 --- a/apier/v1/accounts.go +++ b/apier/v1/accounts.go @@ -429,7 +429,9 @@ func (api *ApierV1) modifyBalance(aType string, attr *AttrAddBalance, reply *str return } balance.Type = utils.StringPointer(attr.BalanceType) - balance.Value = &utils.ValueFormula{Static: math.Abs(attr.Value)} + if attr.Value != 0 { + balance.Value = &utils.ValueFormula{Static: math.Abs(attr.Value)} + } accID := utils.ConcatenatedKey(attr.Tenant, attr.Account) if _, err = api.DataManager.GetAccount(accID); err != nil { @@ -500,7 +502,9 @@ func (api *ApierV1) SetBalance(attr *utils.AttrSetBalance, reply *string) (err e return } balance.Type = utils.StringPointer(attr.BalanceType) - balance.Value = &utils.ValueFormula{Static: math.Abs(attr.Value)} + if attr.Value != 0 { + balance.Value = &utils.ValueFormula{Static: math.Abs(attr.Value)} + } if (balance.ID == nil || *balance.ID == "") && (balance.Uuid == nil || *balance.Uuid == "") { return utils.NewErrMandatoryIeMissing("BalanceID", "or", "BalanceUUID") diff --git a/apier/v1/accounts_it_test.go b/apier/v1/accounts_it_test.go index 2ae3426be..1d511e669 100644 --- a/apier/v1/accounts_it_test.go +++ b/apier/v1/accounts_it_test.go @@ -50,7 +50,11 @@ var ( testAccITAddVoiceBalance, testAccITDebitBalance, testAccITAddBalance, + testAccITAddBalanceWithValue0, + testAccITAddBalanceWithValueInMap, testAccITSetBalance, + testAccITSetBalanceWithVaslue0, + testAccITSetBalanceWithVaslueInMap, testAccITSetBalanceWithExtraData, testAccITSetBalanceWithExtraData2, testAccITSetBalanceTimingIds, @@ -62,7 +66,6 @@ var ( ) func TestAccITWithRemove(t *testing.T) { - switch *dbType { case utils.MetaInternal: accTestsConfig = "tutinternal" @@ -86,7 +89,6 @@ func TestAccITWithRemove(t *testing.T) { } func TestAccITWithoutRemove(t *testing.T) { - switch *dbType { case utils.MetaInternal: accTestsConfig = "acc_balance_keep_internal" @@ -524,3 +526,153 @@ func testAccITStopCgrEngine(t *testing.T) { t.Error(err) } } + +func testAccITSetBalanceWithVaslue0(t *testing.T) { + var reply string + args := &utils.AttrSetBalance{ + Tenant: "cgrates.org", + Account: "testAccSetBalance", + BalanceType: "*monetary", + Balance: map[string]interface{}{ + utils.ID: "testAccSetBalance", + utils.Weight: 10, + }, + } + if err := accRPC.Call(utils.ApierV1SetBalance, args, &reply); err != nil { + t.Error("Got error on SetBalance: ", err.Error()) + } else if reply != "OK" { + t.Errorf("Calling SetBalance received: %s", reply) + } + + var acnt engine.Account + attrAcc := &utils.AttrGetAccount{ + Tenant: accTenant, + Account: "testAccSetBalance", + } + if err := accRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Fatal(err) + } + + for _, value := range acnt.BalanceMap[utils.MONETARY] { + // check only where balance ID is testBalanceID (SetBalance function call was made with this Balance ID) + if value.ID == "testAccSetBalance" { + if value.GetValue() != 1.5 { + t.Errorf("Expecting %+v, received: %+v", 1.5, value.GetValue()) + } + if value.Weight != 10 { + t.Errorf("Expecting %+v, received: %+v", 10, value.Weight) + } + break + } + } +} + +func testAccITSetBalanceWithVaslueInMap(t *testing.T) { + var reply string + args := &utils.AttrSetBalance{ + Tenant: "cgrates.org", + Account: "testAccSetBalance", + BalanceType: "*monetary", + Balance: map[string]interface{}{ + utils.ID: "testAccSetBalance", + utils.Weight: 10, + utils.Value: 2, + }, + } + if err := accRPC.Call(utils.ApierV1SetBalance, args, &reply); err != nil { + t.Error("Got error on SetBalance: ", err.Error()) + } else if reply != "OK" { + t.Errorf("Calling SetBalance received: %s", reply) + } + + var acnt engine.Account + attrAcc := &utils.AttrGetAccount{ + Tenant: accTenant, + Account: "testAccSetBalance", + } + if err := accRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Fatal(err) + } + + for _, value := range acnt.BalanceMap[utils.MONETARY] { + // check only where balance ID is testBalanceID (SetBalance function call was made with this Balance ID) + if value.ID == "testAccSetBalance" { + if value.GetValue() != 2 { + t.Errorf("Expecting %+v, received: %+v", 2, value.GetValue()) + } + if value.Weight != 10 { + t.Errorf("Expecting %+v, received: %+v", 10, value.Weight) + } + break + } + } +} + +func testAccITAddBalanceWithValue0(t *testing.T) { + var reply string + attrs := &AttrAddBalance{ + Tenant: "cgrates.org", + Account: "testAccAddBalance", + BalanceType: utils.MONETARY, + } + if err := accRPC.Call(utils.ApierV1AddBalance, attrs, &reply); err != nil { + t.Error("Got error on ApierV1.AddBalance: ", err.Error()) + } else if reply != "OK" { + t.Errorf("Calling ApierV1.AddBalance received: %s", reply) + } + + var acnt engine.Account + attrAcc := &utils.AttrGetAccount{ + Tenant: accTenant, + Account: "testAccAddBalance", + } + if err := accRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Fatal(err) + } + + for _, value := range acnt.BalanceMap[utils.MONETARY] { + // check only where balance ID is testBalanceID (SetBalance function call was made with this Balance ID) + if value.ID == "testAccSetBalance" { + if value.GetValue() != 1.5 { + t.Errorf("Expecting %+v, received: %+v", 1.5, value.GetValue()) + } + break + } + } +} + +func testAccITAddBalanceWithValueInMap(t *testing.T) { + var reply string + attrs := &AttrAddBalance{ + Tenant: "cgrates.org", + Account: "testAccAddBalance", + BalanceType: utils.MONETARY, + Balance: map[string]interface{}{ + utils.Value: 1.5, + }, + } + if err := accRPC.Call(utils.ApierV1AddBalance, attrs, &reply); err != nil { + t.Error("Got error on ApierV1.AddBalance: ", err.Error()) + } else if reply != "OK" { + t.Errorf("Calling ApierV1.AddBalance received: %s", reply) + } + + var acnt engine.Account + attrAcc := &utils.AttrGetAccount{ + Tenant: accTenant, + Account: "testAccAddBalance", + } + if err := accRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Fatal(err) + } + + for _, value := range acnt.BalanceMap[utils.MONETARY] { + // check only where balance ID is testBalanceID (SetBalance function call was made with this Balance ID) + if value.ID == "testAccSetBalance" { + if value.GetValue() != 3 { + t.Errorf("Expecting %+v, received: %+v", 3, value.GetValue()) + } + break + } + } +}