|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TestGetEnvAsInt tests the getEnvAsInt helper function. |
| 9 | +func TestGetEnvAsInt(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + envVarName string |
| 13 | + envVarVal string |
| 14 | + defaultVal int |
| 15 | + expected int |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "Env var exists and is valid int", |
| 19 | + envVarName: "TEST_INT_VAR_1", |
| 20 | + envVarVal: "123", |
| 21 | + defaultVal: 0, |
| 22 | + expected: 123, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "Env var exists but is invalid int", |
| 26 | + envVarName: "TEST_INT_VAR_2", |
| 27 | + envVarVal: "abc", |
| 28 | + defaultVal: 10, |
| 29 | + expected: 10, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Env var does not exist", |
| 33 | + envVarName: "TEST_INT_VAR_3", |
| 34 | + envVarVal: "", |
| 35 | + defaultVal: 20, |
| 36 | + expected: 20, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Env var is zero", |
| 40 | + envVarName: "TEST_INT_VAR_4", |
| 41 | + envVarVal: "0", |
| 42 | + defaultVal: 5, |
| 43 | + expected: 0, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + // Set environment variable for the test case |
| 50 | + if tt.envVarVal != "" { |
| 51 | + os.Setenv(tt.envVarName, tt.envVarVal) |
| 52 | + } else { |
| 53 | + os.Unsetenv(tt.envVarName) // Ensure it's not set |
| 54 | + } |
| 55 | + |
| 56 | + // Call the function |
| 57 | + result := getEnvAsInt(tt.envVarName, tt.defaultVal) |
| 58 | + |
| 59 | + // Assert the result |
| 60 | + if result != tt.expected { |
| 61 | + t.Errorf("getEnvAsInt(%s, %d) = %d; want %d", tt.envVarName, tt.defaultVal, result, tt.expected) |
| 62 | + } |
| 63 | + |
| 64 | + // Clean up environment variable |
| 65 | + os.Unsetenv(tt.envVarName) |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestGetEnvAsFloat64 tests the getEnvAsFloat64 helper function. |
| 71 | +func TestGetEnvAsFloat64(t *testing.T) { |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + envVarName string |
| 75 | + envVarVal string |
| 76 | + defaultVal float64 |
| 77 | + expected float64 |
| 78 | + }{ |
| 79 | + {"Valid float", "TEST_FLOAT_VAR_1", "123.45", 0.0, 123.45}, |
| 80 | + {"Invalid float", "TEST_FLOAT_VAR_2", "xyz", 10.5, 10.5}, |
| 81 | + {"Not exists", "TEST_FLOAT_VAR_3", "", 20.0, 20.0}, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tt := range tests { |
| 85 | + t.Run(tt.name, func(t *testing.T) { |
| 86 | + if tt.envVarVal != "" { |
| 87 | + os.Setenv(tt.envVarName, tt.envVarVal) |
| 88 | + } else { |
| 89 | + os.Unsetenv(tt.envVarName) |
| 90 | + } |
| 91 | + result := getEnvAsFloat64(tt.envVarName, tt.defaultVal) |
| 92 | + if result != tt.expected { |
| 93 | + t.Errorf("getEnvAsFloat64(%s, %f) = %f; want %f", tt.envVarName, tt.defaultVal, result, tt.expected) |
| 94 | + } |
| 95 | + os.Unsetenv(tt.envVarName) |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// TestGetEnvAsStr tests the getEnvAsStr helper function. |
| 101 | +func TestGetEnvAsStr(t *testing.T) { |
| 102 | + tests := []struct { |
| 103 | + name string |
| 104 | + envVarName string |
| 105 | + envVarVal string |
| 106 | + defaultVal string |
| 107 | + expected string |
| 108 | + }{ |
| 109 | + {"Valid string", "TEST_STR_VAR_1", "hello", "default", "hello"}, |
| 110 | + {"Empty string", "TEST_STR_VAR_2", "", "default", "default"}, // Unset |
| 111 | + {"Not exists", "TEST_STR_VAR_3", "", "default", "default"}, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tt := range tests { |
| 115 | + t.Run(tt.name, func(t *testing.T) { |
| 116 | + if tt.envVarVal != "" { |
| 117 | + os.Setenv(tt.envVarName, tt.envVarVal) |
| 118 | + } else { |
| 119 | + os.Unsetenv(tt.envVarName) |
| 120 | + } |
| 121 | + result := getEnvAsStr(tt.envVarName, tt.defaultVal) |
| 122 | + if result != tt.expected { |
| 123 | + t.Errorf("getEnvAsStr(%s, %s) = %s; want %s", tt.envVarName, tt.defaultVal, result, tt.expected) |
| 124 | + } |
| 125 | + os.Unsetenv(tt.envVarName) |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// TestGetEnvAsBool tests the getEnvAsBool helper function. |
| 131 | +func TestGetEnvAsBool(t *testing.T) { |
| 132 | + tests := []struct { |
| 133 | + name string |
| 134 | + envVarName string |
| 135 | + envVarVal string |
| 136 | + defaultVal bool |
| 137 | + expected bool |
| 138 | + }{ |
| 139 | + {"Valid true", "TEST_BOOL_VAR_1", "true", false, true}, |
| 140 | + {"Valid false", "TEST_BOOL_VAR_2", "false", true, false}, |
| 141 | + {"Invalid bool", "TEST_BOOL_VAR_3", "notbool", false, false}, // Should return default |
| 142 | + {"Not exists", "TEST_BOOL_VAR_4", "", true, true}, |
| 143 | + } |
| 144 | + |
| 145 | + for _, tt := range tests { |
| 146 | + t.Run(tt.name, func(t *testing.T) { |
| 147 | + if tt.envVarVal != "" { |
| 148 | + os.Setenv(tt.envVarName, tt.envVarVal) |
| 149 | + } else { |
| 150 | + os.Unsetenv(tt.envVarName) |
| 151 | + } |
| 152 | + result := getEnvAsBool(tt.envVarName, tt.defaultVal) |
| 153 | + if result != tt.expected { |
| 154 | + t.Errorf("getEnvAsBool(%s, %t) = %t; want %t", tt.envVarName, tt.defaultVal, result, tt.expected) |
| 155 | + } |
| 156 | + os.Unsetenv(tt.envVarName) |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
0 commit comments