models_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  65. testCases := []testCase{}
  66. // Build one test case per-template, per-numeric field; this is obscure
  67. // but it prevents me from having to write the same test for all 10
  68. // numeric fields...
  69. for _, field := range numericFields {
  70. for _, tpl := range testCaseTemplates {
  71. testCases = append(testCases, testCase{
  72. testName: fmt.Sprintf(tpl.testName, field),
  73. fieldName: fmt.Sprintf(tpl.fieldName, field),
  74. fieldValue: tpl.fieldValue,
  75. expValue: tpl.expValue,
  76. expError: tpl.expError,
  77. })
  78. }
  79. }
  80. for _, tc := range testCases {
  81. t.Run(tc.testName, func(t *testing.T) {
  82. cp := &CustomPricing{
  83. CPU: defaultValue,
  84. SpotCPU: defaultValue,
  85. RAM: defaultValue,
  86. SpotRAM: defaultValue,
  87. GPU: defaultValue,
  88. SpotGPU: defaultValue,
  89. Storage: defaultValue,
  90. ZoneNetworkEgress: defaultValue,
  91. RegionNetworkEgress: defaultValue,
  92. InternetNetworkEgress: defaultValue,
  93. }
  94. err := SetCustomPricingField(cp, tc.fieldName, tc.fieldValue)
  95. if err != nil && tc.expError == nil {
  96. t.Errorf("unexpected error: %s", err)
  97. }
  98. if err == nil && tc.expError != nil {
  99. t.Errorf("did not find expected error: %s", tc.expError)
  100. }
  101. structValue := reflect.ValueOf(cp).Elem()
  102. structFieldValue := structValue.FieldByName(tc.fieldName)
  103. actValue := structFieldValue.String()
  104. if actValue != tc.expValue {
  105. t.Errorf("expected field '%s' to be '%s'; actual value is '%s'", tc.fieldName, tc.expValue, actValue)
  106. }
  107. })
  108. }
  109. }
  110. func TestCustomPricing_AthenaCURVersion(t *testing.T) {
  111. testCases := map[string]struct {
  112. curVersion string
  113. expected string
  114. }{
  115. "CUR version 1.0": {
  116. curVersion: "1.0",
  117. expected: "1.0",
  118. },
  119. "CUR version 2.0": {
  120. curVersion: "2.0",
  121. expected: "2.0",
  122. },
  123. "empty CUR version": {
  124. curVersion: "",
  125. expected: "",
  126. },
  127. }
  128. for name, testCase := range testCases {
  129. t.Run(name, func(t *testing.T) {
  130. cp := &CustomPricing{
  131. AthenaCURVersion: testCase.curVersion,
  132. }
  133. if cp.AthenaCURVersion != testCase.expected {
  134. t.Errorf("expected AthenaCURVersion to be '%s', got '%s'", testCase.expected, cp.AthenaCURVersion)
  135. }
  136. })
  137. }
  138. }