goext/confext/confParser_test.go
2022-12-15 12:36:24 +01:00

117 lines
2.3 KiB
Go

package confext
import (
"testing"
"time"
)
func TestApplyEnvOverridesNoop(t *testing.T) {
type aliasint int
type aliasstring string
type testdata struct {
V1 int `env:"TEST_V1"`
VX string ``
V2 string `env:"TEST_V2"`
V3 int8 `env:"TEST_V3"`
V4 int32 `env:"TEST_V4"`
V5 int64 `env:"TEST_V5"`
V6 aliasint `env:"TEST_V6"`
VY aliasint ``
V7 aliasstring `env:"TEST_V7"`
V8 time.Duration `env:"TEST_V8"`
V9 time.Time `env:"TEST_V9"`
}
input := testdata{
V1: 1,
VX: "X",
V2: "2",
V3: 3,
V4: 4,
V5: 5,
V6: 6,
VY: 99,
V7: "7",
V8: 9,
V9: time.Unix(1671102873, 0),
}
output := input
err := ApplyEnvOverrides(&output)
if err != nil {
t.Errorf("%v", err)
t.FailNow()
}
assertEqual(t, input, output)
}
func TestApplyEnvOverridesSimple(t *testing.T) {
type aliasint int
type aliasstring string
type testdata struct {
V1 int `env:"TEST_V1"`
VX string ``
V2 string `env:"TEST_V2"`
V3 int8 `env:"TEST_V3"`
V4 int32 `env:"TEST_V4"`
V5 int64 `env:"TEST_V5"`
V6 aliasint `env:"TEST_V6"`
VY aliasint ``
V7 aliasstring `env:"TEST_V7"`
V8 time.Duration `env:"TEST_V8"`
V9 time.Time `env:"TEST_V9"`
}
data := testdata{
V1: 1,
VX: "X",
V2: "2",
V3: 3,
V4: 4,
V5: 5,
V6: 6,
VY: 99,
V7: "7",
V8: 9,
V9: time.Unix(1671102873, 0),
}
t.Setenv("TEST_V1", "846")
t.Setenv("TEST_V2", "hello_world")
t.Setenv("TEST_V3", "6")
t.Setenv("TEST_V4", "333")
t.Setenv("TEST_V5", "-937")
t.Setenv("TEST_V6", "070")
t.Setenv("TEST_V7", "AAAAAA")
t.Setenv("TEST_V8", "1min4s")
t.Setenv("TEST_V9", "2009-11-10T23:00:00Z")
err := ApplyEnvOverrides(&data)
if err != nil {
t.Errorf("%v", err)
t.FailNow()
}
assertEqual(t, data.V1, 846)
assertEqual(t, data.V2, "hello_world")
assertEqual(t, data.V3, 6)
assertEqual(t, data.V4, 333)
assertEqual(t, data.V5, -937)
assertEqual(t, data.V6, 70)
assertEqual(t, data.V7, "AAAAAA")
assertEqual(t, data.V8, time.Second*64)
assertEqual(t, data.V9, time.Unix(1257894000, 0).UTC())
}
func assertEqual[T comparable](t *testing.T, actual T, expected T) {
if actual != expected {
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
}
}