working on dimed balances

This commit is contained in:
Radu Ioan Fericean
2013-07-15 11:31:36 +03:00
parent 45fd41a1a2
commit 6aafc8cf5f
3 changed files with 44 additions and 2 deletions

View File

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

View File

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

View File

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