From 738406003e041b1b9a8963aa498301f79bd02814 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 6 Jan 2016 20:43:50 +0200 Subject: [PATCH] exclude intervals that don't contain start time' fix for integration tests --- engine/ratingprofile.go | 4 ++++ engine/ratingprofile_test.go | 33 +++++++++++++++++++++++++++- general_tests/tutorial_local_test.go | 30 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/engine/ratingprofile.go b/engine/ratingprofile.go index 5358bdae3..f802252aa 100644 --- a/engine/ratingprofile.go +++ b/engine/ratingprofile.go @@ -89,6 +89,7 @@ type RatingInfo struct { FallbackKeys []string } +// SelectRatingIntevalsForTimespan orders rate intervals in time preserving only those which aply to the specified timestamp func (ri RatingInfo) SelectRatingIntevalsForTimespan(ts *TimeSpan) (result RateIntervalList) { ri.RateIntervals.Sort() sorter := &RateIntervalTimeSorter{referenceTime: ts.TimeStart, ris: ri.RateIntervals} @@ -97,6 +98,9 @@ func (ri RatingInfo) SelectRatingIntevalsForTimespan(ts *TimeSpan) (result RateI var delta time.Duration = -1 var bestRateIntervalIndex int for index, rateInterval := range rateIntervals { + if !rateInterval.Contains(ts.TimeStart, false) { + continue + } startTime := rateInterval.Timing.getLeftMargin(ts.TimeStart) tmpDelta := ts.TimeStart.Sub(startTime) if (startTime.Before(ts.TimeStart) || diff --git a/engine/ratingprofile_test.go b/engine/ratingprofile_test.go index abae1beaa..95ef342c0 100644 --- a/engine/ratingprofile_test.go +++ b/engine/ratingprofile_test.go @@ -186,7 +186,7 @@ func TestRatingProfileRIforTSTwo(t *testing.T) { } } -func TestRatingProfileRIforTSThre(t *testing.T) { +func TestRatingProfileRIforTSThree(t *testing.T) { ri := &RatingInfo{ RateIntervals: RateIntervalList{ &RateInterval{ @@ -218,3 +218,34 @@ func TestRatingProfileRIforTSThre(t *testing.T) { t.Error("Wrong interval list: ", utils.ToIJSON(rIntervals)) } } + +func TestRatingProfileRIforTSMidnight(t *testing.T) { + ri := &RatingInfo{ + RateIntervals: RateIntervalList{ + &RateInterval{ + Timing: &RITiming{ + StartTime: "09:00:00", + }, + }, + &RateInterval{ + Timing: &RITiming{ + StartTime: "00:00:00", + }, + }, + &RateInterval{ + Timing: &RITiming{ + StartTime: "19:00:00", + }, + }, + }, + } + ts := &TimeSpan{ + TimeStart: time.Date(2016, 1, 6, 23, 40, 0, 0, time.UTC), + TimeEnd: time.Date(2016, 1, 7, 1, 1, 30, 0, time.UTC), + } + rIntervals := ri.SelectRatingIntevalsForTimespan(ts) + if len(rIntervals) != 1 || + rIntervals[0].Timing.StartTime != "19:00:00" { + t.Error("Wrong interval list: ", utils.ToIJSON(rIntervals)) + } +} diff --git a/general_tests/tutorial_local_test.go b/general_tests/tutorial_local_test.go index 3872e3a20..3e453f9bd 100644 --- a/general_tests/tutorial_local_test.go +++ b/general_tests/tutorial_local_test.go @@ -344,6 +344,36 @@ func TestTutLocalGetCosts(t *testing.T) { } else if cc.Cost != 0.327 { // t.Errorf("Calling Responder.GetCost got callcost: %s", cc.AsJSON()) } + cd = engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "1004", + TimeStart: time.Date(2016, 1, 6, 18, 31, 5, 0, time.UTC), + TimeEnd: time.Date(2016, 1, 6, 18, 32, 35, 0, time.UTC), + } + if err := tutLocalRpc.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 1.3002 { // + t.Errorf("Calling Responder.GetCost got callcost: %s", cc.AsJSON()) + } + cd = engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "1002", + TimeStart: time.Date(2014, 12, 7, 8, 42, 26, 0, time.UTC), + TimeEnd: time.Date(2014, 12, 7, 8, 44, 26, 0, time.UTC), + } + if err := tutLocalRpc.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 0.327 { // + t.Errorf("Calling Responder.GetCost got callcost: %s", cc.AsJSON()) + } } // Check call costs