| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- package pricing
- import (
- "errors"
- "testing"
- "github.com/opencost/opencost/core/pkg/unit"
- )
- func TestGetPrices(t *testing.T) {
- testCases := []struct {
- name string
- prices Prices
- }{
- {
- name: "empty Prices",
- prices: Prices{},
- },
- {
- name: "single hourly price",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.Hour,
- Price: 0.096,
- },
- },
- },
- },
- {
- name: "single set of per-resource prices",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.VCPUHour,
- Price: 0.031611,
- },
- {
- Currency: unit.USD,
- Unit: unit.RAMGiBHour,
- Price: 0.004237,
- },
- },
- },
- },
- {
- name: "prices for multiple currencies",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.VCPUHour,
- Price: 0.031611,
- },
- {
- Currency: unit.USD,
- Unit: unit.RAMGiBHour,
- Price: 0.004237,
- },
- },
- unit.CNY: []Price{
- {
- Currency: unit.CNY,
- Unit: unit.VCPUHour,
- Price: 3.1611,
- },
- {
- Currency: unit.CNY,
- Unit: unit.RAMGiBHour,
- Price: 0.4237,
- },
- },
- },
- },
- }
- for _, tt := range testCases {
- t.Run(tt.name, func(t *testing.T) {
- result := tt.prices.GetPrices()
- expectedLen := 0
- for _, prices := range tt.prices {
- expectedLen += len(prices)
- }
- if len(result) != expectedLen {
- t.Errorf("expected %d prices, got %d", expectedLen, len(result))
- }
- })
- }
- }
- func TestGetPricesInCurrency(t *testing.T) {
- tests := []struct {
- name string
- prices Prices
- currency unit.Currency
- expectedCount int
- expectError bool
- errorType error
- }{
- {
- name: "empty Prices - should return NotFound error",
- prices: Prices{},
- currency: unit.USD,
- expectedCount: 0,
- expectError: true,
- errorType: NotFound,
- },
- {
- name: "currency not found - should return NotFound error",
- prices: Prices{
- unit.EUR: []Price{
- {
- Currency: unit.EUR,
- Unit: unit.VCPUHour,
- Price: 0.028,
- },
- },
- },
- currency: unit.USD,
- expectedCount: 0,
- expectError: true,
- errorType: NotFound,
- },
- {
- name: "single matching currency",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.VCPUHour,
- Price: 0.031611,
- },
- },
- },
- currency: unit.USD,
- expectedCount: 1,
- expectError: false,
- },
- {
- name: "multiple currencies - find USD",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.VCPUHour,
- Price: 0.031611,
- },
- },
- unit.EUR: []Price{
- {
- Currency: unit.EUR,
- Unit: unit.VCPUHour,
- Price: 0.028,
- },
- },
- unit.GBP: []Price{
- {
- Currency: unit.GBP,
- Unit: unit.RAMGiBHour,
- Price: 0.025,
- },
- },
- },
- currency: unit.USD,
- expectedCount: 1,
- expectError: false,
- },
- {
- name: "multiple currencies - find EUR",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.VCPUHour,
- Price: 0.031611,
- },
- },
- unit.EUR: []Price{
- {
- Currency: unit.EUR,
- Unit: unit.VCPUHour,
- Price: 0.028,
- },
- },
- },
- currency: unit.EUR,
- expectedCount: 1,
- expectError: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result, err := tt.prices.GetPricesInCurrency(tt.currency)
- if tt.expectError {
- if err == nil {
- t.Error("expected error but got none")
- }
- if !errors.Is(err, tt.errorType) {
- t.Errorf("expected error type %v, got %v", tt.errorType, err)
- }
- if len(result) != 0 {
- t.Errorf("expected nil or empty result on error, got %d prices", len(result))
- }
- } else {
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if len(result) != tt.expectedCount {
- t.Errorf("expected %d prices, got %d", tt.expectedCount, len(result))
- }
- // Verify all returned prices have the requested currency
- for _, price := range result {
- if price.Currency != tt.currency {
- t.Errorf("expected currency %v, got %v", tt.currency, price.Currency)
- }
- }
- }
- })
- }
- }
- func TestGetPricesInCurrencyWithDefault(t *testing.T) {
- testCases := []struct {
- name string
- prices Prices
- currency unit.Currency
- defaultCurrency unit.Currency
- expectedCount int
- expectError bool
- expectedCurr unit.Currency
- }{
- {
- name: "empty Prices - should return NotFound error",
- prices: Prices{},
- currency: unit.USD,
- defaultCurrency: unit.EUR,
- expectedCount: 0,
- expectError: true,
- },
- {
- name: "currency found - should return requested currency",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.VCPUHour,
- Price: 0.031611,
- },
- },
- },
- currency: unit.USD,
- defaultCurrency: unit.EUR,
- expectedCount: 1,
- expectError: false,
- expectedCurr: unit.USD,
- },
- {
- name: "currency not found - should fallback to default",
- prices: Prices{
- unit.EUR: []Price{
- {
- Currency: unit.EUR,
- Unit: unit.VCPUHour,
- Price: 0.028,
- },
- },
- },
- currency: unit.USD,
- defaultCurrency: unit.EUR,
- expectedCount: 1,
- expectError: false,
- expectedCurr: unit.EUR,
- },
- {
- name: "neither currency nor default found - should return NotFound error",
- prices: Prices{
- unit.GBP: []Price{
- {
- Currency: unit.GBP,
- Unit: unit.VCPUHour,
- Price: 0.025,
- },
- },
- },
- currency: unit.USD,
- defaultCurrency: unit.EUR,
- expectedCount: 0,
- expectError: true,
- },
- {
- name: "multiple currencies - prefer requested over default",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.VCPUHour,
- Price: 0.031611,
- },
- },
- unit.EUR: []Price{
- {
- Currency: unit.EUR,
- Unit: unit.VCPUHour,
- Price: 0.028,
- },
- },
- },
- currency: unit.USD,
- defaultCurrency: unit.EUR,
- expectedCount: 1,
- expectError: false,
- expectedCurr: unit.USD,
- },
- {
- name: "same currency and default - should return requested",
- prices: Prices{
- unit.USD: []Price{
- {
- Currency: unit.USD,
- Unit: unit.VCPUHour,
- Price: 0.031611,
- },
- },
- },
- currency: unit.USD,
- defaultCurrency: unit.USD,
- expectedCount: 1,
- expectError: false,
- expectedCurr: unit.USD,
- },
- }
- for _, tt := range testCases {
- t.Run(tt.name, func(t *testing.T) {
- result, err := tt.prices.GetPricesInCurrencyWithDefault(tt.currency, tt.defaultCurrency)
- if tt.expectError {
- if err == nil {
- t.Error("expected error but got none")
- }
- if !errors.Is(err, NotFound) {
- t.Errorf("expected NotFound error, got %v", err)
- }
- } else {
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if len(result) != tt.expectedCount {
- t.Errorf("expected %d prices, got %d", tt.expectedCount, len(result))
- }
- // Verify all returned prices have the expected currency
- for _, price := range result {
- if price.Currency != tt.expectedCurr {
- t.Errorf("expected currency %v, got %v", tt.expectedCurr, price.Currency)
- }
- }
- }
- })
- }
- }
|