From 294314d06cf98391fcc1c64756d3d223171aaac4 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 14 Aug 2019 14:19:45 +0300 Subject: [PATCH] Updated SQL SetCDR --- apier/v2/cdrs_it_test.go | 103 +++++++++++++++++++++++++++++++++++++-- engine/models.go | 30 ++++++++++++ engine/storage_sql.go | 2 +- 3 files changed, 130 insertions(+), 5 deletions(-) diff --git a/apier/v2/cdrs_it_test.go b/apier/v2/cdrs_it_test.go index b93f2e680..0db043616 100644 --- a/apier/v2/cdrs_it_test.go +++ b/apier/v2/cdrs_it_test.go @@ -56,6 +56,10 @@ var sTestsCDRsIT = []func(t *testing.T){ testV2CDRsRemoveRatingProfiles, testV2CDRsProcessCDRNoRattingPlan, testV2CDRsGetCdrsNoRattingPlan, + + testV2CDRsRateCDRsWithRatingPlan, + testV2CDRsGetCdrsWithRattingPlan, + testV2CDRsKillEngine, } @@ -590,8 +594,7 @@ func testV2CDRsGetCdrsNoRattingPlan(t *testing.T) { t.Errorf("Unexpected cost for CDR: %f", cdrs[0].Cost) } if cdrs[0].ExtraInfo != utils.ErrRatingPlanNotFound.Error() { - t.Errorf("PayPalAccount should be added by AttributeS, have: %s", - cdrs[0].ExtraFields["PayPalAccount"]) + t.Errorf("Expected ExtraInfo : %s received :%s", utils.ErrRatingPlanNotFound.Error(), cdrs[0].ExtraInfo) } } args = utils.RPCCDRsFilter{RunIDs: []string{"SupplierCharges"}, Accounts: []string{"testV2CDRsProcessCDR4"}} @@ -604,8 +607,100 @@ func testV2CDRsGetCdrsNoRattingPlan(t *testing.T) { t.Errorf("Unexpected cost for CDR: %f", cdrs[0].Cost) } if cdrs[0].ExtraInfo != utils.ErrRatingPlanNotFound.Error() { - t.Errorf("PayPalAccount should be added by AttributeS, have: %s", - cdrs[0].ExtraFields["PayPalAccount"]) + t.Errorf("Expected ExtraInfo : %s received :%s", utils.ErrRatingPlanNotFound.Error(), cdrs[0].ExtraInfo) + } + } +} + +// Should re-rate the supplier1 cost with RP_ANY2CNT +func testV2CDRsRateCDRsWithRatingPlan(t *testing.T) { + rpf := &utils.AttrSetRatingProfile{ + Tenant: "cgrates.org", + Category: "call", + Subject: "SUPPLIER1", + RatingPlanActivations: []*utils.TPRatingActivation{ + { + ActivationTime: "2018-01-01T00:00:00Z", + RatingPlanId: "RP_ANY1CNT"}}, + Overwrite: true, + } + var reply string + if err := cdrsRpc.Call("ApierV1.SetRatingProfile", rpf, &reply); err != nil { + t.Error("Got error on ApierV1.SetRatingProfile: ", err.Error()) + } else if reply != "OK" { + t.Error("Calling ApierV1.SetRatingProfile got reply: ", reply) + } + + rpf = &utils.AttrSetRatingProfile{ + Tenant: "cgrates.org", + Category: "call", + Subject: utils.ANY, + RatingPlanActivations: []*utils.TPRatingActivation{ + { + ActivationTime: "2018-01-01T00:00:00Z", + RatingPlanId: "RP_TESTIT1"}}, + Overwrite: true, + } + if err := cdrsRpc.Call("ApierV1.SetRatingProfile", rpf, &reply); err != nil { + t.Error("Got error on ApierV1.SetRatingProfile: ", err.Error()) + } else if reply != "OK" { + t.Error("Calling ApierV1.SetRatingProfile got reply: ", reply) + } + + if err := cdrsRpc.Call(utils.CDRsV1RateCDRs, &engine.ArgRateCDRs{ + RPCCDRsFilter: utils.RPCCDRsFilter{NotRunIDs: []string{utils.MetaRaw}, Accounts: []string{"testV2CDRsProcessCDR4"}}, + ChargerS: utils.BoolPointer(true), + }, &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 testV2CDRsGetCdrsWithRattingPlan(t *testing.T) { + var cdrCnt int64 + req := utils.AttrGetCdrs{} + if err := cdrsRpc.Call("ApierV2.CountCDRs", req, &cdrCnt); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if cdrCnt != 11 { + t.Error("Unexpected number of CDRs returned: ", cdrCnt) + } + var cdrs []*engine.ExternalCDR + args := utils.RPCCDRsFilter{RunIDs: []string{utils.MetaRaw}, Accounts: []string{"testV2CDRsProcessCDR4"}} + if err := cdrsRpc.Call(utils.ApierV2GetCDRs, args, &cdrs); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if len(cdrs) != 1 { + t.Error("Unexpected number of CDRs returned: ", len(cdrs)) + } else { + if cdrs[0].Cost != -1.0 { + t.Errorf("Unexpected cost for CDR: %f", cdrs[0].Cost) + } + } + args = utils.RPCCDRsFilter{RunIDs: []string{"CustomerCharges"}, Accounts: []string{"testV2CDRsProcessCDR4"}} + if err := cdrsRpc.Call(utils.ApierV2GetCDRs, args, &cdrs); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if len(cdrs) != 1 { + t.Error("Unexpected number of CDRs returned: ", len(cdrs)) + } else { + if cdrs[0].Cost != 0.0102 { + t.Errorf("Unexpected cost for CDR: %f", cdrs[0].Cost) + } + if cdrs[0].ExtraInfo != "" { + t.Errorf("Expected ExtraInfo : %s received :%s", "", cdrs[0].ExtraInfo) + } + } + args = utils.RPCCDRsFilter{RunIDs: []string{"SupplierCharges"}, Accounts: []string{"testV2CDRsProcessCDR4"}} + if err := cdrsRpc.Call(utils.ApierV2GetCDRs, args, &cdrs); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if len(cdrs) != 1 { + t.Error("Unexpected number of CDRs returned: ", len(cdrs)) + } else { + if cdrs[0].Cost != 0.0102 { + t.Errorf("Unexpected cost for CDR: %f", cdrs[0].Cost) + } + if cdrs[0].ExtraInfo != "" { + t.Errorf("Expected ExtraInfo : %s received :%s", "", cdrs[0].ExtraInfo) } } } diff --git a/engine/models.go b/engine/models.go index 4fd84901b..3cf37f3a5 100644 --- a/engine/models.go +++ b/engine/models.go @@ -308,6 +308,36 @@ func (t CDRsql) TableName() string { return utils.CDRsTBL } +func (t CDRsql) AsMapStringInterface() (out map[string]interface{}) { + out = make(map[string]interface{}) + // out["id"] = t.ID // ignore ID + out["cgrid"] = t.Cgrid + out["run_id"] = t.RunID + out["origin_host"] = t.OriginHost + out["source"] = t.Source + out["origin_id"] = t.OriginID + out["tor"] = t.TOR + out["request_type"] = t.RequestType + out["tenant"] = t.Tenant + out["category"] = t.Category + out["account"] = t.Account + out["subject"] = t.Subject + out["destination"] = t.Destination + out["setup_time"] = t.SetupTime + out["answer_time"] = t.AnswerTime + out["usage"] = t.Usage + out["extra_fields"] = t.ExtraFields + out["cost_source"] = t.CostSource + out["cost"] = t.Cost + out["cost_details"] = t.CostDetails + out["extra_info"] = t.ExtraInfo + out["created_at"] = t.CreatedAt + out["updated_at"] = t.UpdatedAt + // out["deleted_at"] = t.DeletedAt // ignore DeletedAt + return + +} + type SessionCostsSQL struct { ID int64 Cgrid string diff --git a/engine/storage_sql.go b/engine/storage_sql.go index ad6359357..d0bdc13a0 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -890,7 +890,7 @@ func (self *SQLStorage) SetCDR(cdr *CDR, allowUpdate bool) error { tx = self.db.Begin() cdrSql.UpdatedAt = time.Now() updated := tx.Model(&CDRsql{}).Where( - &CDRsql{Cgrid: cdr.CGRID, RunID: cdr.RunID, OriginID: cdr.OriginID}).Updates(cdrSql) + &CDRsql{Cgrid: cdr.CGRID, RunID: cdr.RunID, OriginID: cdr.OriginID}).Updates(cdrSql.AsMapStringInterface()) if updated.Error != nil { tx.Rollback() return updated.Error