Skip to content

Commit a224b80

Browse files
committed
Dividing total by PricingObject.UnitQuantity
1 parent b22e531 commit a224b80

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

pkg/pricing/basket_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ func TestBasket_Total(t *testing.T) {
4545

4646
err := basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", big.NewRat(1, 1)))
4747
So(err, ShouldBeNil)
48-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(12, 100)) // 0.12
48+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(2, 1000)) // 0.002
4949

5050
err = basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", big.NewRat(42, 1)))
5151
So(err, ShouldBeNil)
52-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(24, 100)) // 0.24
52+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(4, 1000)) // 0.004
5353

5454
err = basket.Add(NewUsageByPathWithQuantity("/compute/c1/run", big.NewRat(600, 1)))
5555
So(err, ShouldBeNil)
56-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(124, 100)) // 1.24
56+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(24, 1000)) // 0.024
5757
})
5858
Convey("1 compute instance with 2 volumes and 1 ip", func() {
5959
basket := NewBasket()
@@ -65,23 +65,23 @@ func TestBasket_Total(t *testing.T) {
6565
So(basket.Length(), ShouldEqual, 4)
6666

6767
basket.SetDuration(1 * time.Minute)
68-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(44, 100)) // 0.44
68+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(8, 1000)) // 0.008
6969

7070
basket.SetDuration(1 * time.Hour)
71-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(44, 100)) // 0.44
71+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(8, 1000)) // 0.008
7272

7373
basket.SetDuration(2 * time.Hour)
74-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(68, 100)) // 0.68
74+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(12, 1000)) // 0.012
7575

7676
basket.SetDuration(2 * 24 * time.Hour)
77-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(219, 100)) // 2.19
77+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(196, 1000)) // 0.196
7878

7979
basket.SetDuration(30 * 24 * time.Hour)
80-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(399, 100)) // 3.99
80+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(2050, 1000)) // 2.05
8181

8282
// FIXME: this test is false, the capacity is per month
8383
basket.SetDuration(365 * 24 * time.Hour)
84-
So(basket.Total(), ShouldEqualBigRat, big.NewRat(399, 100)) // 3.99
84+
So(basket.Total(), ShouldEqualBigRat, big.NewRat(2694, 1000)) // 2.694
8585
})
8686
})
8787
}

pkg/pricing/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (u *Usage) Total() *big.Rat {
7575
//return math.Min(u.PricingObject.UnitPrice * u.BillableQuantity(), u.PricingObject.UnitPriceCap)
7676

7777
total := new(big.Rat).Mul(u.BillableQuantity(), u.PricingObject.UnitPrice)
78+
total = total.Quo(total, u.PricingObject.UnitQuantity)
7879
return ratMin(total, u.PricingObject.UnitPriceCap)
7980
}
8081

pkg/pricing/usage_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,12 @@ func TestUsage_Total(t *testing.T) {
252252
So(usage.Total(), ShouldEqualBigRat, big.NewRat(0, 1))
253253

254254
usage = NewUsageWithQuantity(&object, big.NewRat(1, 1))
255-
So(usage.Total(), ShouldEqualBigRat, big.NewRat(60*12, 1000)) // 60 * 0.012
255+
So(usage.Total(), ShouldEqualBigRat, big.NewRat(12, 1000)) // 0.012
256256

257257
usage = NewUsageWithQuantity(&object, big.NewRat(61, 1))
258-
So(usage.Total(), ShouldEqualBigRat, big.NewRat(120*12, 1000)) // 120 * 0.012
258+
So(usage.Total(), ShouldEqualBigRat, big.NewRat(24, 1000)) // 0.024
259259

260260
usage = NewUsageWithQuantity(&object, big.NewRat(1000, 1))
261-
So(usage.Total(), ShouldEqualBigRat, big.NewRat(6, 1))
261+
So(usage.Total(), ShouldEqualBigRat, big.NewRat(204, 1000)) // 0.204
262262
})
263263
}

0 commit comments

Comments
 (0)