From 4592523e46b0a553dd191120af94daea3e8930aa Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 26 May 2017 20:02:51 +0200 Subject: [PATCH] EventCost.Merge tests --- engine/eventcost.go | 3 +-- engine/eventcost_test.go | 50 ++++++++++++++++++++++++++++++++++++++++ engine/libeventcost.go | 5 ++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/engine/eventcost.go b/engine/eventcost.go index 42b70d6b8..d7e9b71e6 100644 --- a/engine/eventcost.go +++ b/engine/eventcost.go @@ -353,8 +353,7 @@ func (ec *EventCost) Merge(ecs ...*EventCost) { ec.AppendChargingIntervalFromEventCost(newEC, cIlIdx) } } - ec.Usage = nil // Reset them - ec.Cost = nil + ec.ResetCounters() } // RemoveStaleReferences iterates through cached data and makes sure it is still referenced from Charging diff --git a/engine/eventcost_test.go b/engine/eventcost_test.go index ac6ad4a1e..08ad0a18f 100644 --- a/engine/eventcost_test.go +++ b/engine/eventcost_test.go @@ -1086,3 +1086,53 @@ func TestECTrimMUsage(t *testing.T) { t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC)) } } + +func TestECMerge(t *testing.T) { + ec := NewBareEventCost() + ec.CGRID = testEC.CGRID + ec.RunID = testEC.RunID + ec.StartTime = testEC.StartTime + newEC := &EventCost{ + Charges: []*ChargingInterval{testEC.Charges[0]}, + AccountSummary: testEC.AccountSummary, + Rating: testEC.Rating, + Accounting: testEC.Accounting, + RatingFilters: testEC.RatingFilters, + Rates: testEC.Rates, + Timings: testEC.Timings, + } + ec.Merge(newEC) + if len(ec.Charges) != len(newEC.Charges) || + !reflect.DeepEqual(ec.Charges[0].TotalUsage(), newEC.Charges[0].TotalUsage()) || + !reflect.DeepEqual(ec.Charges[0].TotalCost(), newEC.Charges[0].TotalCost()) { + t.Errorf("Unexpected EC after merge: %s", utils.ToJSON(ec)) + } + // Add second charging interval with compress factor 1 + newEC = &EventCost{ + Charges: []*ChargingInterval{ + &ChargingInterval{ + RatingUUID: testEC.Charges[1].RatingUUID, + Increments: testEC.Charges[1].Increments, + CompressFactor: 1}}, + AccountSummary: testEC.AccountSummary, + Rating: testEC.Rating, + Accounting: testEC.Accounting, + RatingFilters: testEC.RatingFilters, + Rates: testEC.Rates, + Timings: testEC.Timings, + } + ec.Merge(newEC) + if len(ec.Charges) != 2 || + !reflect.DeepEqual(ec.Charges[1].TotalUsage(), newEC.Charges[0].TotalUsage()) || + !reflect.DeepEqual(ec.Charges[1].TotalCost(), newEC.Charges[0].TotalCost()) { + t.Errorf("Unexpected EC after merge: %s", utils.ToJSON(ec)) + } + newEC.Charges[0].CompressFactor = 1 + ec.Merge(newEC) + if len(ec.Charges) != 2 || + ec.Charges[1].CompressFactor != 2 || + *ec.Charges[1].Usage() != time.Duration(1*time.Minute) || // only equal at charging interval level + *ec.Charges[1].TotalUsage() != time.Duration(2*time.Minute) { + t.Errorf("Unexpected EC after merge: %s", utils.ToJSON(ec)) + } +} diff --git a/engine/libeventcost.go b/engine/libeventcost.go index a43e25fa5..82dacbf7b 100644 --- a/engine/libeventcost.go +++ b/engine/libeventcost.go @@ -104,6 +104,11 @@ func (cIl *ChargingInterval) Cost() float64 { return *cIl.cost } +func (cIl *ChargingInterval) TotalCost() float64 { + return utils.Round((cIl.Cost() * float64(cIl.CompressFactor)), + globalRoundingDecimals, utils.ROUNDING_MIDDLE) +} + // Clone returns a new instance of ChargingInterval with independent data func (cIl *ChargingInterval) Clone() (cln *ChargingInterval) { cln = new(ChargingInterval)