Skip to content

Commit 082eb34

Browse files
committed
Added new helper to initialize Usage
1 parent 661178a commit 082eb34

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed

pkg/pricing/basket_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func TestBasket_Add(t *testing.T) {
2020
So(basket, ShouldNotBeNil)
2121
So(basket.Length(), ShouldEqual, 0)
2222

23-
err := basket.Add(NewUsageByPath("/compute/c1/run", 1))
23+
err := basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", 1))
2424
So(err, ShouldBeNil)
2525
So(basket.Length(), ShouldEqual, 1)
2626

27-
err = basket.Add(NewUsageByPath("/compute/c1/run", 42))
27+
err = basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", 42))
2828
So(err, ShouldBeNil)
2929
So(basket.Length(), ShouldEqual, 2)
3030
})

pkg/pricing/usage.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,29 @@ type Usage struct {
77
Quantity *big.Rat
88
}
99

10-
func NewUsageByPath(objectPath string, quantity float64) Usage {
11-
return NewUsage(CurrentPricing.GetByPath(objectPath), quantity)
10+
func NewUsageByPath(objectPath string) Usage {
11+
return NewUsageByPathWithQuantity(objectPath, 0)
1212
}
1313

14-
func NewUsage(object *PricingObject, quantity float64) Usage {
14+
func NewUsageByPathWithQuantity(objectPath string, quantity float64) Usage {
15+
return NewUsageWithQuantity(CurrentPricing.GetByPath(objectPath), quantity)
16+
}
17+
18+
func NewUsageWithQuantity(object *PricingObject, quantity float64) Usage {
1519
return Usage{
1620
PricingObject: object,
1721
Quantity: new(big.Rat).SetFloat64(quantity),
1822
}
1923
}
2024

25+
func NewUsage(object *PricingObject) Usage {
26+
return NewUsageWithQuantity(object, 0)
27+
}
28+
29+
func (u *Usage) SetQuantity(quantity *big.Rat) {
30+
u.Quantity = quantity
31+
}
32+
2133
func (u *Usage) BillableQuantity() *big.Rat {
2234
if u.Quantity.Cmp(big.NewRat(0, 1)) < 1 {
2335
return big.NewRat(0, 1)

pkg/pricing/usage_test.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ func ShouldEqualBigRat(actual interface{}, expected ...interface{}) string {
2424
return output
2525
}
2626

27-
func TestNewUsageByPath(t *testing.T) {
28-
Convey("Testing NewUsageByPath()", t, func() {
29-
usage := NewUsageByPath("/compute/c1/run", 1)
27+
func TestNewUsageByPathWithQuantity(t *testing.T) {
28+
Convey("Testing NewUsageByPathWithQuantity()", t, func() {
29+
usage := NewUsageByPathWithQuantity("/compute/c1/run", 1)
3030
So(usage.PricingObject.Path, ShouldEqual, "/compute/c1/run")
3131
So(usage.Quantity, ShouldEqualBigRat, big.NewRat(1, 1))
3232
})
3333
}
3434

35-
func TestNewUsage(t *testing.T) {
36-
Convey("Testing NewUsage()", t, func() {
35+
func TestNewUsageWithQuantity(t *testing.T) {
36+
Convey("Testing NewUsageWithQuantity()", t, func() {
3737
object := CurrentPricing.GetByPath("/compute/c1/run")
38-
usage := NewUsage(object, 1)
38+
usage := NewUsageWithQuantity(object, 1)
3939
So(usage.PricingObject.Path, ShouldEqual, "/compute/c1/run")
4040
So(usage.Quantity, ShouldEqualBigRat, big.NewRat(1, 1))
4141
})
@@ -46,40 +46,40 @@ func TestUsage_BillableQuantity(t *testing.T) {
4646
object := &PricingObject{
4747
UnitQuantity: 60,
4848
}
49-
usage := NewUsage(object, -1)
49+
usage := NewUsageWithQuantity(object, -1)
5050
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(0, 1))
5151

52-
usage = NewUsage(object, -1000)
52+
usage = NewUsageWithQuantity(object, -1000)
5353
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(0, 1))
5454

55-
usage = NewUsage(object, 0)
55+
usage = NewUsageWithQuantity(object, 0)
5656
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(0, 1))
5757

58-
usage = NewUsage(object, 1)
58+
usage = NewUsageWithQuantity(object, 1)
5959
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60, 1))
6060

61-
usage = NewUsage(object, 59)
61+
usage = NewUsageWithQuantity(object, 59)
6262
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60, 1))
6363

64-
usage = NewUsage(object, 59.9999)
64+
usage = NewUsageWithQuantity(object, 59.9999)
6565
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60, 1))
6666

67-
usage = NewUsage(object, 60)
67+
usage = NewUsageWithQuantity(object, 60)
6868
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60, 1))
6969

70-
usage = NewUsage(object, 60.00001)
70+
usage = NewUsageWithQuantity(object, 60.00001)
7171
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60*2, 1))
7272

73-
usage = NewUsage(object, 61)
73+
usage = NewUsageWithQuantity(object, 61)
7474
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60*2, 1))
7575

76-
usage = NewUsage(object, 119)
76+
usage = NewUsageWithQuantity(object, 119)
7777
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60*2, 1))
7878

79-
usage = NewUsage(object, 121)
79+
usage = NewUsageWithQuantity(object, 121)
8080
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60*3, 1))
8181

82-
usage = NewUsage(object, 1000)
82+
usage = NewUsageWithQuantity(object, 1000)
8383
So(usage.BillableQuantity(), ShouldEqualBigRat, big.NewRat(60*17, 1))
8484
})
8585
}
@@ -89,40 +89,40 @@ func TestUsage_LostQuantity(t *testing.T) {
8989
object := &PricingObject{
9090
UnitQuantity: 60,
9191
}
92-
usage := NewUsage(object, -1)
92+
usage := NewUsageWithQuantity(object, -1)
9393
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(0, 1))
9494

95-
usage = NewUsage(object, -1000)
95+
usage = NewUsageWithQuantity(object, -1000)
9696
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(0, 1))
9797

98-
usage = NewUsage(object, 0)
98+
usage = NewUsageWithQuantity(object, 0)
9999
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(0, 1))
100100

101-
usage = NewUsage(object, 1)
101+
usage = NewUsageWithQuantity(object, 1)
102102
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(60-1, 1))
103103

104-
usage = NewUsage(object, 59)
104+
usage = NewUsageWithQuantity(object, 59)
105105
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(60-59, 1))
106106

107-
usage = NewUsage(object, 59.9999)
107+
usage = NewUsageWithQuantity(object, 59.9999)
108108
So(usage.LostQuantity(), ShouldEqualBigRat, new(big.Rat).Sub(big.NewRat(60, 1), new(big.Rat).SetFloat64(59.9999)))
109109

110-
usage = NewUsage(object, 60)
110+
usage = NewUsageWithQuantity(object, 60)
111111
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(0, 1))
112112

113-
usage = NewUsage(object, 60.00001)
113+
usage = NewUsageWithQuantity(object, 60.00001)
114114
So(usage.LostQuantity(), ShouldEqualBigRat, new(big.Rat).SetFloat64(60*2-60.00001))
115115

116-
usage = NewUsage(object, 61)
116+
usage = NewUsageWithQuantity(object, 61)
117117
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(60*2-61, 1))
118118

119-
usage = NewUsage(object, 119)
119+
usage = NewUsageWithQuantity(object, 119)
120120
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(60*2-119, 1))
121121

122-
usage = NewUsage(object, 121)
122+
usage = NewUsageWithQuantity(object, 121)
123123
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(60*3-121, 1))
124124

125-
usage = NewUsage(object, 1000)
125+
usage = NewUsageWithQuantity(object, 1000)
126126
So(usage.LostQuantity(), ShouldEqualBigRat, big.NewRat(60*17-1000, 1))
127127
})
128128
}
@@ -135,19 +135,19 @@ func TestUsage_Total(t *testing.T) {
135135
UnitPriceCap: 6,
136136
}
137137

138-
usage := NewUsage(&object, -1)
138+
usage := NewUsageWithQuantity(&object, -1)
139139
So(usage.Total(), ShouldEqualBigRat, big.NewRat(0, 1))
140140

141-
usage = NewUsage(&object, 0)
141+
usage = NewUsageWithQuantity(&object, 0)
142142
So(usage.Total(), ShouldEqualBigRat, big.NewRat(0, 1))
143143

144-
usage = NewUsage(&object, 1)
144+
usage = NewUsageWithQuantity(&object, 1)
145145
So(usage.Total(), ShouldEqualBigRat, new(big.Rat).Mul(big.NewRat(60, 1), new(big.Rat).SetFloat64(0.012)))
146146

147-
usage = NewUsage(&object, 61)
147+
usage = NewUsageWithQuantity(&object, 61)
148148
So(usage.Total(), ShouldEqualBigRat, new(big.Rat).Mul(big.NewRat(120, 1), new(big.Rat).SetFloat64(0.012)))
149149

150-
usage = NewUsage(&object, 1000)
150+
usage = NewUsageWithQuantity(&object, 1000)
151151
So(usage.Total(), ShouldEqualBigRat, big.NewRat(6, 1))
152152
})
153153
}

0 commit comments

Comments
 (0)