module_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package public
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/pricing"
  5. "github.com/opencost/opencost/core/pkg/unit"
  6. )
  7. // TestPricingModuleConfig tests that the config struct is properly defined
  8. func TestPricingModuleConfig(t *testing.T) {
  9. config := PricingModuleConfig{
  10. Provider: pricing.AWSProvider,
  11. Currency: unit.USD,
  12. }
  13. if config.Provider != pricing.AWSProvider {
  14. t.Errorf("Provider = %v, want %v", config.Provider, pricing.AWSProvider)
  15. }
  16. if config.Currency != unit.USD {
  17. t.Errorf("Currency = %v, want %v", config.Currency, unit.USD)
  18. }
  19. }
  20. // TestProviderPricingStructure tests the nested map structure
  21. func TestProviderPricingStructure(t *testing.T) {
  22. providers := make(ProviderPricing)
  23. // Create a simple structure
  24. instanceMap := make(InstanceTypePricing)
  25. regionMap := make(RegionPricing)
  26. prices := &pricing.Prices{
  27. unit.USD: []pricing.Price{
  28. {Currency: unit.USD, Unit: unit.Hour, Price: 0.0416},
  29. },
  30. }
  31. regionMap["us-east-1"] = prices
  32. instanceMap["t3.medium"] = &regionMap
  33. providers[pricing.AWSProvider] = &instanceMap
  34. // Verify structure
  35. if providers[pricing.AWSProvider] == nil {
  36. t.Fatal("AWS provider not found")
  37. }
  38. if (*providers[pricing.AWSProvider])["t3.medium"] == nil {
  39. t.Fatal("t3.medium instance type not found")
  40. }
  41. if (*(*providers[pricing.AWSProvider])["t3.medium"])["us-east-1"] == nil {
  42. t.Fatal("us-east-1 region not found")
  43. }
  44. retrievedPrices := (*(*providers[pricing.AWSProvider])["t3.medium"])["us-east-1"]
  45. if len((*retrievedPrices)[unit.USD]) == 0 {
  46. t.Fatal("No USD prices found")
  47. }
  48. if (*retrievedPrices)[unit.USD][0].Price != 0.0416 {
  49. t.Errorf("Price = %v, want %v", (*retrievedPrices)[unit.USD][0].Price, 0.0416)
  50. }
  51. }
  52. // Made with Bob