-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Azanul <azanulhaque@gmail.com>
- Loading branch information
Showing
2 changed files
with
206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |