Skip to content

Commit

Permalink
test more levels
Browse files Browse the repository at this point in the history
  • Loading branch information
MashinaMashina committed May 3, 2024
1 parent e269dff commit d9c82a1
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions cleanenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ func TestReadEnvVars(t *testing.T) {
}

func TestReadEnvErrors(t *testing.T) {
type testEnvErrors struct {
type testOneLevel struct {
Host string `env:"HOST" env-required:"true"`
}

type testTwoLevels struct {
Queue struct {
Host string `env:"HOST"`
} `env-prefix:"TEST_ERRORS_"`
Expand All @@ -338,6 +342,14 @@ func TestReadEnvErrors(t *testing.T) {
} `env-prefix:"TEST_ERRORS_THIRD_"`
}

type testThreeLevels struct {
Database struct {
URL struct {
Host string `env:"HOST" env-required:"true"`
}
}
}

tests := []struct {
name string
env map[string]string
Expand All @@ -346,9 +358,31 @@ func TestReadEnvErrors(t *testing.T) {
errorWant interface{}
}{
{
name: "required error",
name: "required error - one level",
env: nil,
cfg: &testOneLevel{},
errorAs: RequireError{},
errorWant: RequireError{
FieldPath: "",
FieldName: "Host",
EnvName: "HOST",
},
},
{
name: "required error - three levels",
env: nil,
cfg: &testThreeLevels{},
errorAs: RequireError{},
errorWant: RequireError{
FieldPath: "Database.URL.",
FieldName: "Host",
EnvName: "HOST",
},
},
{
name: "required error - two levels",
env: nil,
cfg: &testEnvErrors{},
cfg: &testTwoLevels{},
errorAs: RequireError{},
errorWant: RequireError{
FieldPath: "Database.",
Expand All @@ -362,7 +396,7 @@ func TestReadEnvErrors(t *testing.T) {
"TEST_ERRORS_DATABASE_HOST": "localhost",
"TEST_ERRORS_DATABASE_TTL": "bad-value",
},
cfg: &testEnvErrors{},
cfg: &testTwoLevels{},
errorAs: ParsingError{},
errorWant: ParsingError{
Err: fmt.Errorf("time: invalid duration \"bad-value\""),
Expand Down

0 comments on commit d9c82a1

Please sign in to comment.