Skip to content

Commit 0cc7fe3

Browse files
committed
Added Basket.SetDuration, testing Basket.Total
1 parent b2a63b0 commit 0cc7fe3

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

pkg/pricing/basket.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pricing
22

3-
import "math/big"
3+
import (
4+
"math/big"
5+
"time"
6+
)
47

58
type Basket []Usage
69

@@ -17,6 +20,18 @@ func (b *Basket) Length() int {
1720
return len(*b)
1821
}
1922

23+
func (b *Basket) SetDuration(duration time.Duration) error {
24+
var err error
25+
for i, usage := range *b {
26+
err = usage.SetDuration(duration)
27+
if err != nil {
28+
return err
29+
}
30+
(*b)[i] = usage
31+
}
32+
return nil
33+
}
34+
2035
func (b *Basket) Total() *big.Rat {
2136
total := new(big.Rat)
2237
for _, usage := range *b {

pkg/pricing/basket_test.go

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pricing
33
import (
44
"math/big"
55
"testing"
6+
"time"
67

78
. "github.com/smartystreets/goconvey/convey"
89
)
@@ -37,28 +38,55 @@ func TestBasket_Add(t *testing.T) {
3738

3839
func TestBasket_Total(t *testing.T) {
3940
Convey("Testing Basket.Total", t, FailureContinues, func() {
40-
basket := NewBasket()
41-
So(basket, ShouldNotBeNil)
42-
current := ratZero
43-
difference := ratZero
44-
So(basket.Total(), ShouldEqualBigRat, current)
41+
Convey("3 compute instances", func() {
42+
basket := NewBasket()
43+
So(basket, ShouldNotBeNil)
44+
current := ratZero
45+
difference := ratZero
46+
So(basket.Total(), ShouldEqualBigRat, current)
4547

46-
err := basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", 1))
47-
So(err, ShouldBeNil)
48-
difference = new(big.Rat).Mul(big.NewRat(60, 1), new(big.Rat).SetFloat64(0.012))
49-
current = new(big.Rat).Add(current, difference)
50-
So(basket.Total(), ShouldEqualBigRat, current)
48+
err := basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", 1))
49+
So(err, ShouldBeNil)
50+
difference = new(big.Rat).Mul(big.NewRat(60, 1), new(big.Rat).SetFloat64(0.012))
51+
current = new(big.Rat).Add(current, difference)
52+
So(basket.Total(), ShouldEqualBigRat, current)
5153

52-
err = basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", 42))
53-
So(err, ShouldBeNil)
54-
difference = new(big.Rat).Mul(big.NewRat(60, 1), new(big.Rat).SetFloat64(0.012))
55-
current = new(big.Rat).Add(current, difference)
56-
So(basket.Total(), ShouldEqualBigRat, current)
54+
err = basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", 42))
55+
So(err, ShouldBeNil)
56+
difference = new(big.Rat).Mul(big.NewRat(60, 1), new(big.Rat).SetFloat64(0.012))
57+
//difference = big.NewRat(72, 100)
58+
current = new(big.Rat).Add(current, difference)
59+
So(basket.Total(), ShouldEqualBigRat, current)
5760

58-
err = basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", 600))
59-
So(err, ShouldBeNil)
60-
difference = big.NewRat(6, 1)
61-
current = new(big.Rat).Add(current, difference)
62-
So(basket.Total(), ShouldEqualBigRat, current)
61+
err = basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", 600))
62+
So(err, ShouldBeNil)
63+
difference = big.NewRat(6, 1)
64+
current = new(big.Rat).Add(current, difference)
65+
So(basket.Total(), ShouldEqualBigRat, current)
66+
})
67+
Convey("1 compute instance with 2 volumes and 1 ip", func() {
68+
basket := NewBasket()
69+
70+
basket.Add(NewUsageByPath("/compute/c1/run"))
71+
basket.Add(NewUsageByPath("/ip/dynamic"))
72+
basket.Add(NewUsageByPath("/storage/local/ssd/storage"))
73+
basket.Add(NewUsageByPath("/storage/local/ssd/storage"))
74+
So(basket.Length(), ShouldEqual, 4)
75+
76+
basket.SetDuration(1 * time.Minute)
77+
So(basket.Total(), ShouldEqualBigRat, new(big.Rat).SetFloat64(1.36))
78+
79+
basket.SetDuration(1 * time.Hour)
80+
So(basket.Total(), ShouldEqualBigRat, new(big.Rat).SetFloat64(1.36))
81+
82+
basket.SetDuration(2 * time.Hour)
83+
So(basket.Total(), ShouldEqualBigRat, new(big.Rat).SetFloat64(2.32))
84+
85+
basket.SetDuration(2 * 24 * time.Hour)
86+
So(basket.Total(), ShouldEqualBigRat, new(big.Rat).SetFloat64(8.39))
87+
88+
basket.SetDuration(30 * 24 * time.Hour)
89+
So(basket.Total(), ShouldEqualBigRat, new(big.Rat).SetFloat64(1.99))
90+
})
6391
})
6492
}

0 commit comments

Comments
 (0)