From 6aafc8cf5fca3f7cb84d1c1f2cdfab64880d842c Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 15 Jul 2013 11:31:36 +0300 Subject: [PATCH] working on dimed balances --- rater/minute_buckets.go | 2 +- rater/timespans.go | 2 +- rater/timespans_test.go | 42 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/rater/minute_buckets.go b/rater/minute_buckets.go index d5338a50e..9cefc79bc 100644 --- a/rater/minute_buckets.go +++ b/rater/minute_buckets.go @@ -31,7 +31,7 @@ type MinuteBucket struct { Price float64 Percent float64 // percentage from standard price DestinationIds []string - ExpirationTime time.Time + ExpirationDate time.Time precision int } diff --git a/rater/timespans.go b/rater/timespans.go index ecad9ac89..00a5a9c57 100644 --- a/rater/timespans.go +++ b/rater/timespans.go @@ -160,7 +160,7 @@ Splits the given timespan on minute bucket's duration. */ func (ts *TimeSpan) SplitByMinuteBucket(mb *MinuteBucket) (newTs *TimeSpan) { // if mb expired skip it - if ts.TimeStart.Equal(mb.ExpirationTime) || ts.TimeStart.After(mb.ExpirationTime) { + if !mb.ExpirationTime.IsZero() && (ts.TimeStart.Equal(mb.ExpirationTime) || ts.TimeStart.After(mb.ExpirationTime)) { return nil } s := ts.GetDuration().Seconds() diff --git a/rater/timespans_test.go b/rater/timespans_test.go index 2e19e7e7a..626e4e75a 100644 --- a/rater/timespans_test.go +++ b/rater/timespans_test.go @@ -217,3 +217,45 @@ func TestSetInterval(t *testing.T) { t.Error("Bigger ponder interval should win") } } + +func TestTimespanSplitByMinuteBucketPlenty(t *testing.T) { + t1 := time.Date(2013, time.July, 15, 10, 40, 0, 0, time.UTC) + t2 := time.Date(2013, time.July, 15, 10, 42, 0, 0, time.UTC) + mb := &MinuteBucket{Seconds: 180} + ts := TimeSpan{TimeStart: t1, TimeEnd: t2} + newTs := ts.SplitByMinuteBucket(mb) + if ts.MinuteInfo == nil || ts.MinuteInfo.Quantity != 120 { + t.Error("Not enough minutes on minute bucket split") + } + if newTs != nil { + t.Error("Bad extra timespan on minute bucket split") + } +} + +func TestTimespanSplitByMinuteBucketScarce(t *testing.T) { + t1 := time.Date(2013, time.July, 15, 10, 40, 0, 0, time.UTC) + t2 := time.Date(2013, time.July, 15, 10, 42, 0, 0, time.UTC) + mb := &MinuteBucket{Seconds: 60} + ts := TimeSpan{TimeStart: t1, TimeEnd: t2} + newTs := ts.SplitByMinuteBucket(mb) + if ts.MinuteInfo == nil || ts.MinuteInfo.Quantity != 60 { + t.Error("Not enough minutes on minute bucket split") + } + if newTs == nil || newTs.MinuteInfo != nil { + t.Error("Missing extra timespan on minute bucket split") + } +} + +func TestTimespanSplitByMinuteBucketPlantyExpired(t *testing.T) { + t1 := time.Date(2013, time.July, 15, 10, 40, 0, 0, time.UTC) + t2 := time.Date(2013, time.July, 15, 10, 42, 0, 0, time.UTC) + mb := &MinuteBucket{Seconds: 180, ExpirationDate: time.Date(2013, time.July, 15, 10, 40, 0, 0, time.UTC)} + ts := TimeSpan{TimeStart: t1, TimeEnd: t2} + newTs := ts.SplitByMinuteBucket(mb) + if ts.MinuteInfo == nil || ts.MinuteInfo.Quantity != 120 { + t.Error("Not enough minutes on minute bucket split") + } + if newTs != nil { + t.Error("Bad extra timespan on minute bucket split") + } +}