models_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package models
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestSetSetCustomPricingField(t *testing.T) {
  8. defaultValue := "1.0"
  9. type testCase struct {
  10. testName string
  11. fieldName string
  12. fieldValue string
  13. expValue string
  14. expError error
  15. }
  16. testCaseTemplates := []testCase{
  17. {
  18. testName: "valid number for %s",
  19. fieldName: "%s",
  20. fieldValue: "0.04321",
  21. expValue: "0.04321",
  22. expError: nil,
  23. },
  24. {
  25. testName: "long number for %s",
  26. fieldName: "%s",
  27. fieldValue: "0.04321234321231234",
  28. expValue: "0.04321234321231234",
  29. expError: nil,
  30. },
  31. {
  32. testName: "illegal number for %s",
  33. fieldName: "%s",
  34. fieldValue: "0.123.123",
  35. expValue: defaultValue, // assert that the default value is not mutated
  36. expError: fmt.Errorf("invalid numeric value for field"),
  37. },
  38. {
  39. testName: "NaN for %s",
  40. fieldName: "%s",
  41. fieldValue: "NaN",
  42. expValue: defaultValue, // assert that the default value is not mutated
  43. expError: fmt.Errorf("invalid numeric value for field"),
  44. },
  45. {
  46. testName: "empty string for %s",
  47. fieldName: "%s",
  48. fieldValue: "",
  49. expValue: defaultValue, // assert that the default value is not mutated
  50. expError: nil,
  51. },
  52. }
  53. numericFields := []string{
  54. "CPU",
  55. "GPU",
  56. "RAM",
  57. "SpotCPU",
  58. "SpotGPU",
  59. "SpotRAM",
  60. "Storage",
  61. "ZoneNetworkEgress",
  62. "RegionNetworkEgress",
  63. "InternetNetworkEgress",
  64. "NatGatewayEgress",
  65. "NatGatewayIngress",
  66. }
  67. testCases := []testCase{}
  68. // Build one test case per-template, per-numeric field; this is obscure
  69. // but it prevents me from having to write the same test for all 10
  70. // numeric fields...
  71. for _, field := range numericFields {
  72. for _, tpl := range testCaseTemplates {
  73. testCases = append(testCases, testCase{
  74. testName: fmt.Sprintf(tpl.testName, field),
  75. fieldName: fmt.Sprintf(tpl.fieldName, field),
  76. fieldValue: tpl.fieldValue,
  77. expValue: tpl.expValue,
  78. expError: tpl.expError,
  79. })
  80. }
  81. }
  82. for _, tc := range testCases {
  83. t.Run(tc.testName, func(t *testing.T) {
  84. cp := &CustomPricing{
  85. CPU: defaultValue,
  86. SpotCPU: defaultValue,
  87. RAM: defaultValue,
  88. SpotRAM: defaultValue,
  89. GPU: defaultValue,
  90. SpotGPU: defaultValue,
  91. Storage: defaultValue,
  92. ZoneNetworkEgress: defaultValue,
  93. RegionNetworkEgress: defaultValue,
  94. InternetNetworkEgress: defaultValue,
  95. NatGatewayEgress: defaultValue,
  96. NatGatewayIngress: defaultValue,
  97. }
  98. err := SetCustomPricingField(cp, tc.fieldName, tc.fieldValue)
  99. if err != nil && tc.expError == nil {
  100. t.Errorf("unexpected error: %s", err)
  101. }
  102. if err == nil && tc.expError != nil {
  103. t.Errorf("did not find expected error: %s", tc.expError)
  104. }
  105. structValue := reflect.ValueOf(cp).Elem()
  106. structFieldValue := structValue.FieldByName(tc.fieldName)
  107. actValue := structFieldValue.String()
  108. if actValue != tc.expValue {
  109. t.Errorf("expected field '%s' to be '%s'; actual value is '%s'", tc.fieldName, tc.expValue, actValue)
  110. }
  111. })
  112. }
  113. }
  114. func TestCustomPricing_AthenaCURVersion(t *testing.T) {
  115. testCases := map[string]struct {
  116. curVersion string
  117. expected string
  118. }{
  119. "CUR version 1.0": {
  120. curVersion: "1.0",
  121. expected: "1.0",
  122. },
  123. "CUR version 2.0": {
  124. curVersion: "2.0",
  125. expected: "2.0",
  126. },
  127. "empty CUR version": {
  128. curVersion: "",
  129. expected: "",
  130. },
  131. }
  132. for name, testCase := range testCases {
  133. t.Run(name, func(t *testing.T) {
  134. cp := &CustomPricing{
  135. AthenaCURVersion: testCase.curVersion,
  136. }
  137. if cp.AthenaCURVersion != testCase.expected {
  138. t.Errorf("expected AthenaCURVersion to be '%s', got '%s'", testCase.expected, cp.AthenaCURVersion)
  139. }
  140. })
  141. }
  142. }