diff --git a/apier/v1/accountsv1_it_test.go b/apier/v1/accountsv1_it_test.go index e1f5c9c1e..7d72502e2 100644 --- a/apier/v1/accountsv1_it_test.go +++ b/apier/v1/accountsv1_it_test.go @@ -56,7 +56,8 @@ func TestAccountSv1IT(t *testing.T) { testAccountSv1SimpleDebit, testAccountSv1DebitMultipleAcc, testAccountSv1DebitMultipleAccLimited, - testAccountSv1DebitWithAttributeS, + testAccountSv1DebitWithAttributeSandRateS, + testAccountSv1DebitWithRateS, } switch *dbType { case utils.MetaInternal: @@ -730,7 +731,7 @@ func testAccountSv1DebitMultipleAccLimited(t *testing.T) { } } -func testAccountSv1DebitWithAttributeS(t *testing.T) { +func testAccountSv1DebitWithAttributeSandRateS(t *testing.T) { accPrfAPI := &APIAccountProfileWithCache{ APIAccountProfile: &utils.APIAccountProfile{ Tenant: "cgrates.org", @@ -827,3 +828,101 @@ func testAccountSv1DebitWithAttributeS(t *testing.T) { t.Errorf("Expecting : %+v, received: %s", decimal.New(99, 0), reply2.Balances["Balance1"].Units) } } + +func testAccountSv1DebitWithRateS(t *testing.T) { + accPrfAPI := &APIAccountProfileWithCache{ + APIAccountProfile: &utils.APIAccountProfile{ + Tenant: "cgrates.org", + ID: "ACC_WITH_RATES", + FilterIDs: []string{"*string:~*req.Account:ACC_WITH_RATES"}, + Weights: ";10", + Balances: map[string]*utils.APIBalance{ + "Balance1": &utils.APIBalance{ + ID: "Balance1", + Weights: ";10", + Type: utils.MetaConcrete, + Units: 100, + CostIncrements: []*utils.APICostIncrement{ + { + Increment: utils.Float64Pointer(1), + RecurrentFee: utils.Float64Pointer(-1), + }, + }, + }, + }, + ThresholdIDs: []string{utils.MetaNone}, + }, + } + var reply string + if err := acntSRPC.Call(utils.APIerSv1SetAccountProfile, accPrfAPI, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Error("Unexpected reply returned", reply) + } + var err error + var convAcc *utils.AccountProfile + if convAcc, err = accPrfAPI.AsAccountProfile(); err != nil { + t.Error(err) + } + var reply2 *utils.AccountProfile + if err := acntSRPC.Call(utils.APIerSv1GetAccountProfile, &utils.TenantIDWithOpts{ + TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ACC_WITH_RATES"}}, &reply2); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(convAcc, reply2) { + t.Errorf("Expecting : %+v, received: %+v", convAcc, reply2) + } + + //set a rate profile to be used in case of debit + apiRPrf := &engine.APIRateProfile{ + Tenant: "cgrates.org", + ID: "RP_Test2", + FilterIDs: []string{"*string:~*req.Account:ACC_WITH_RATES"}, + Weights: ";20", + Rates: map[string]*engine.APIRate{ + "RT_ALWAYS": { + ID: "RT_ALWAYS", + Weights: ";0", + ActivationTimes: "* * * * *", + IntervalRates: []*engine.APIIntervalRate{ + { + IntervalStart: "0", + RecurrentFee: utils.Float64Pointer(0.5), + Increment: utils.Float64Pointer(2), + Unit: utils.Float64Pointer(2), + }, + }, + }, + }, + } + + if err := acntSRPC.Call(utils.APIerSv1SetRateProfile, + &APIRateProfileWithCache{ + APIRateProfileWithOpts: &engine.APIRateProfileWithOpts{ + APIRateProfile: apiRPrf}, + }, &reply); err != nil { + t.Fatal(err) + } else if reply != utils.OK { + t.Errorf("Expecting: %+v, received: %+v", utils.OK, reply) + } + + var eEc *utils.ExtEventCharges + if err := acntSRPC.Call(utils.AccountSv1DebitUsage, + &utils.ArgsAccountsForEvent{CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "testAccountSv1DebitWithAttributeS", + Event: map[string]interface{}{ + utils.AccountField: "ACC_WITH_RATES", + utils.Usage: "20", + }}}, &eEc); err != nil { + t.Error(err) + } else if eEc.Usage == nil || *eEc.Usage != 20.0 { + t.Fatalf("received usage: %v", *eEc.Usage) + } + + if err := acntSRPC.Call(utils.APIerSv1GetAccountProfile, &utils.TenantIDWithOpts{ + TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ACC_WITH_RATES"}}, &reply2); err != nil { + t.Error(err) + } else if reply2.Balances["Balance1"].Units.Cmp(decimal.New(95, 0)) != 0 { + t.Errorf("Expecting : %+v, received: %s", decimal.New(95, 0), reply2.Balances["Balance1"].Units) + } +}