Skip to content

Commit 0e3aae6

Browse files
author
Michael Gunkoff
committed
test case for validation
1 parent c6b9dbe commit 0e3aae6

1 file changed

Lines changed: 67 additions & 31 deletions

File tree

env_test.go

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -144,39 +144,51 @@ type badSettingsWithDuration struct {
144144
Timeout time.Duration `env:"BAD_TIMEOUT"`
145145
}
146146

147+
type BadInt32Low struct {
148+
Value int32 `env:"BAD_INT32_LOW"`
149+
}
150+
151+
type BadInt32High struct {
152+
Value int32 `env:"BAD_INT32_HIGH"`
153+
}
154+
155+
// new failing validate-tags struct
156+
type settingsWithValidateTags struct {
157+
Value int `env:"BAD_VALIDATE" validate:"min=10"`
158+
}
159+
147160
// setupTestEnvironment sets up all required environment variables for testing
148161
func setupTestEnvironment(t *testing.T) {
149162
t.Helper()
150163

151164
envVars := map[string]string{
152-
"PORT": "80",
153-
"FLOAT": "80.1",
154-
"DB": "db/file",
155-
"CACHE": "5",
156-
"BADCACHE1": "i",
157-
"BADCACHE2": "300",
158-
"BADCACHE3": "-1",
159-
"LOG_LEVEL": "debug",
160-
"SYSLOG_LEVEL": "info",
161-
"TIMEOUT": "20s",
162-
"BADPORT": "a",
163-
"STDOUT": "true",
164-
"SESSION": "session",
165-
"KEY_PATH": "/etc",
166-
"PROD": "true",
167-
"HAS_DB": "true",
168-
"DOMAIN": "example.com",
169-
"EMAIL": "email@example.com",
170-
"BIG_PORT": "25060",
171-
"STRING_SLICE": "a,b,c",
172-
173-
// new env vars for the uint8/float64 tests
174-
"UINT8": "200", // valid uint8
175-
"BAD_UINT8": "999", // out of uint8 range -> should fail
176-
"BAD_FLOAT": "notafloat", // invalid float -> should fail
177-
178-
// new env var for bad duration test
179-
"BAD_TIMEOUT": "notaduration", // invalid duration -> should fail
165+
"PORT": "80",
166+
"FLOAT": "80.1",
167+
"DB": "db/file",
168+
"CACHE": "5",
169+
"BADCACHE1": "i",
170+
"BADCACHE2": "300",
171+
"BADCACHE3": "-1",
172+
"LOG_LEVEL": "debug",
173+
"SYSLOG_LEVEL": "info",
174+
"TIMEOUT": "20s",
175+
"BADPORT": "a",
176+
"STDOUT": "true",
177+
"SESSION": "session",
178+
"KEY_PATH": "/etc",
179+
"PROD": "true",
180+
"HAS_DB": "true",
181+
"DOMAIN": "example.com",
182+
"EMAIL": "email@example.com",
183+
"BIG_PORT": "25060",
184+
"STRING_SLICE": "a,b,c",
185+
"UINT8": "200",
186+
"BAD_UINT8": "999",
187+
"BAD_FLOAT": "notafloat",
188+
"BAD_TIMEOUT": "notaduration",
189+
"BAD_INT32_LOW": "-2147483649",
190+
"BAD_INT32_HIGH": "2147483648",
191+
"BAD_VALIDATE": "5",
180192
}
181193

182194
for key, value := range envVars {
@@ -414,6 +426,27 @@ func TestLoad(t *testing.T) {
414426
expectError: true,
415427
description: "Should fail when duration cannot be parsed",
416428
},
429+
{
430+
name: "settings_int32_value_too_low_should_fail",
431+
settings: &BadInt32Low{},
432+
wantErr: NewIncorrectFieldValueError("BAD_INT32_LOW"),
433+
expectError: true,
434+
description: "Should fail when int32 value is below int32 min",
435+
},
436+
{
437+
name: "settings_int32_value_too_high_should_fail",
438+
settings: &BadInt32High{},
439+
wantErr: NewIncorrectFieldValueError("BAD_INT32_HIGH"),
440+
expectError: true,
441+
description: "Should fail when int32 value is above int32 max",
442+
},
443+
{
444+
name: "settings_with_validate_tags_fail",
445+
settings: &settingsWithValidateTags{},
446+
wantErr: nil,
447+
expectError: true,
448+
description: "Should fail when validate:min constraint is not satisfied",
449+
},
417450
}
418451

419452
for _, tt := range tests {
@@ -502,9 +535,12 @@ func createFreshInstance(original any) any {
502535
return &pigPort{}
503536
case *stringSliceConfig:
504537
return &stringSliceConfig{}
505-
// new case for the bad duration struct
506-
case *badSettingsWithDuration:
507-
return &badSettingsWithDuration{}
538+
case *BadInt32Low:
539+
return &BadInt32Low{}
540+
case *BadInt32High:
541+
return &BadInt32High{}
542+
case *settingsWithValidateTags:
543+
return &settingsWithValidateTags{}
508544
default:
509545
return original
510546
}

0 commit comments

Comments
 (0)