costmodelenv_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package env
  2. import (
  3. "os"
  4. "testing"
  5. )
  6. func TestGetAPIPort(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. want int
  10. pre func()
  11. }{
  12. {
  13. name: "Ensure the default API port '9003'",
  14. want: 9003,
  15. },
  16. {
  17. name: "Ensure the default API port '9003' when API_PORT is set to ''",
  18. want: 9003,
  19. pre: func() {
  20. os.Setenv("API_PORT", "")
  21. },
  22. },
  23. {
  24. name: "Ensure the API port '9004' when API_PORT is set to '9004'",
  25. want: 9004,
  26. pre: func() {
  27. os.Setenv("API_PORT", "9004")
  28. },
  29. },
  30. }
  31. for _, tt := range tests {
  32. if tt.pre != nil {
  33. tt.pre()
  34. }
  35. t.Run(tt.name, func(t *testing.T) {
  36. if got := GetAPIPort(); got != tt.want {
  37. t.Errorf("GetAPIPort() = %v, want %v", got, tt.want)
  38. }
  39. })
  40. }
  41. }
  42. func TestIsCacheDisabled(t *testing.T) {
  43. tests := []struct {
  44. name string
  45. want bool
  46. pre func()
  47. }{
  48. {
  49. name: "Ensure the default value is false",
  50. want: false,
  51. },
  52. {
  53. name: "Ensure the value is false when DISABLE_AGGREGATE_COST_MODEL_CACHE is set to false",
  54. want: false,
  55. pre: func() {
  56. os.Setenv("DISABLE_AGGREGATE_COST_MODEL_CACHE", "false")
  57. },
  58. },
  59. {
  60. name: "Ensure the value is true when DISABLE_AGGREGATE_COST_MODEL_CACHE is set to true",
  61. want: true,
  62. pre: func() {
  63. os.Setenv("DISABLE_AGGREGATE_COST_MODEL_CACHE", "true")
  64. },
  65. },
  66. }
  67. for _, tt := range tests {
  68. if tt.pre != nil {
  69. tt.pre()
  70. }
  71. t.Run(tt.name, func(t *testing.T) {
  72. if got := IsAggregateCostModelCacheDisabled(); got != tt.want {
  73. t.Errorf("IsAggregateCostModelCacheDisabled() = %v, want %v", got, tt.want)
  74. }
  75. })
  76. }
  77. }
  78. func TestGetExportCSVMaxDays(t *testing.T) {
  79. tests := []struct {
  80. name string
  81. want int
  82. pre func()
  83. }{
  84. {
  85. name: "Ensure the default value is 90d",
  86. want: 90,
  87. },
  88. {
  89. name: "Ensure the value is 30 when EXPORT_CSV_MAX_DAYS is set to 30",
  90. want: 30,
  91. pre: func() {
  92. os.Setenv("EXPORT_CSV_MAX_DAYS", "30")
  93. },
  94. },
  95. {
  96. name: "Ensure the value is 90 when EXPORT_CSV_MAX_DAYS is set to empty string",
  97. want: 90,
  98. pre: func() {
  99. os.Setenv("EXPORT_CSV_MAX_DAYS", "")
  100. },
  101. },
  102. {
  103. name: "Ensure the value is 90 when EXPORT_CSV_MAX_DAYS is set to invalid value",
  104. want: 90,
  105. pre: func() {
  106. os.Setenv("EXPORT_CSV_MAX_DAYS", "foo")
  107. },
  108. },
  109. }
  110. for _, tt := range tests {
  111. if tt.pre != nil {
  112. tt.pre()
  113. }
  114. t.Run(tt.name, func(t *testing.T) {
  115. if got := GetExportCSVMaxDays(); got != tt.want {
  116. t.Errorf("GetExportCSVMaxDays() = %v, want %v", got, tt.want)
  117. }
  118. })
  119. }
  120. }