EventCost.Merge tests

This commit is contained in:
DanB
2017-05-26 20:02:51 +02:00
parent bfe42356b0
commit 4592523e46
3 changed files with 56 additions and 2 deletions

View File

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

View File

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

View File

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