Skip to content

Commit

Permalink
AWS cost calculation utils tests
Browse files Browse the repository at this point in the history
Signed-off-by: Azanul <azanulhaque@gmail.com>
  • Loading branch information
Azanul committed Sep 28, 2023
1 parent 68f85ab commit b5f6a32
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 2 deletions.
4 changes: 2 additions & 2 deletions providers/aws/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func GetCost(pds []PriceDimensions, v float64) float64 {
if pd.BeginRange < v {
if pd.EndRange != "Inf" {
endRange, _ := strconv.ParseFloat(pd.EndRange, 64)
if v < endRange {
applicableRange = v
if v > endRange {
applicableRange = endRange
}
}
total += (applicableRange - pd.BeginRange) * pd.PricePerUnit.USD
Expand Down
204 changes: 204 additions & 0 deletions providers/aws/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package utils

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/service/pricing"
)

func TestGetCost(t *testing.T) {
// Single price dimension
pd := PriceDimensions{
BeginRange: 0,
EndRange: "Inf",
PricePerUnit: struct {
USD float64 `json:"USD,string"`
}{USD: 0.1},
}
pds := []PriceDimensions{pd}
cost := GetCost(pds, 10.0)
expected := 1.0
if cost != expected {
t.Errorf("Expected cost: %f, but got: %f", expected, cost)
}

// Multiple price dimensions
pd1 := PriceDimensions{
BeginRange: 0,
EndRange: "10",
PricePerUnit: struct {
USD float64 `json:"USD,string"`
}{USD: 0.2},
}
pd2 := PriceDimensions{
BeginRange: 10,
EndRange: "Inf",
PricePerUnit: struct {
USD float64 `json:"USD,string"`
}{USD: 0.1},
}
pds = []PriceDimensions{pd1, pd2}
cost = GetCost(pds, 20)
expected = 3.0
if cost != expected {
t.Errorf("Expected cost: %f, but got: %f", expected, cost)
}
}

func TestGetPriceMap(t *testing.T) {
testCases := []struct {
inputPriceList []string
expectedNumProducts int
expectedNumPriceDims map[string]int
}{
// Minimal valid JSON input with a single product and price dimension
{
inputPriceList: []string{`
{
"product": {
"attributes": {
"group": "TestGroup"
}
},
"terms": {
"OnDemand": {
"test_term": {
"priceDimensions": {
"test_price_dimension": {
"beginRange": "0",
"endRange": "Inf",
"pricePerUnit": {
"USD": "0.1"
}
}
}
}
}
}
}`},
expectedNumProducts: 1,
expectedNumPriceDims: map[string]int{"TestGroup": 1},
},
// Multiple products with different price dimensions
{
inputPriceList: []string{
// Product 1 with 2 price dimensions
`
{
"product": {
"attributes": {
"group": "TestGroup1"
}
},
"terms": {
"OnDemand": {
"test_term": {
"priceDimensions": {
"test_price_dimension1": {
"beginRange": "0",
"endRange": "100",
"pricePerUnit": {
"USD": "0.2"
}
},
"test_price_dimension2": {
"beginRange": "100",
"endRange": "Inf",
"pricePerUnit": {
"USD": "0.3"
}
}
}
}
}
}
}`,
// Product 2 with 3 price dimensions
`
{
"product": {
"attributes": {
"group": "TestGroup2"
}
},
"terms": {
"OnDemand": {
"test_term": {
"priceDimensions": {
"test_price_dimension1": {
"beginRange": "0",
"endRange": "50",
"pricePerUnit": {
"USD": "0.1"
}
},
"test_price_dimension2": {
"beginRange": "50",
"endRange": "100",
"pricePerUnit": {
"USD": "0.15"
}
},
"test_price_dimension3": {
"beginRange": "100",
"endRange": "Inf",
"pricePerUnit": {
"USD": "0.2"
}
}
}
}
}
}
}`,
},
expectedNumProducts: 2,
expectedNumPriceDims: map[string]int{"TestGroup1": 2, "TestGroup2": 3},
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test case %d", i+1), func(t *testing.T) {
output := pricing.GetProductsOutput{
PriceList: testCase.inputPriceList,
}
priceMap, err := GetPriceMap(&output)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}

if len(priceMap) != testCase.expectedNumProducts {
t.Errorf("Expected %d products in priceMap, but got %d", testCase.expectedNumProducts, len(priceMap))
}

for group, priceDims := range priceMap {
if len(priceDims) != testCase.expectedNumPriceDims[group] {
t.Errorf("Expected %d price dimensions for group %s, but got %d", testCase.expectedNumPriceDims[group], group, len(priceDims))
}
}
})
}
}

func TestGetPriceMap_InvalidJSON(t *testing.T) {
// Invalid JSON input
invalidJSON := "invalid JSON"
output := pricing.GetProductsOutput{
PriceList: []string{invalidJSON},
}
_, err := GetPriceMap(&output)
if err == nil {
t.Error("Expected an error, but got nil")
}
}

func TestGetPriceMap_NoPricingOutput(t *testing.T) {
// PricingOutput is nil
priceMap, err := GetPriceMap(nil)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if len(priceMap) != 0 {
t.Errorf("Expected an empty priceMap, but got %v", priceMap)
}
}

0 comments on commit b5f6a32

Please sign in to comment.