Skip to content

Commit

Permalink
testsss
Browse files Browse the repository at this point in the history
  • Loading branch information
hspitzley-czi committed Mar 6, 2024
1 parent 202aa8d commit 1bc7e2e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
99 changes: 97 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestEnvToMap(t *testing.T) {
r.Contains(m, "ENV2")
}

func TesEvaluateConfigWithEnv(t *testing.T) {
func TestEvaluateConfigWithEnv(t *testing.T) {
r := require.New(t)
test := `blah={{.ENV1}}
blah2={{.ENV2}}`
Expand All @@ -40,7 +40,7 @@ blah2=test2`
r.Equal(expected, string(b))
}

func TesEvaluateConfigWithMissingEnv(t *testing.T) {
func TestEvaluateConfigWithMissingEnv(t *testing.T) {
r := require.New(t)
test := `blah={{.ENV1}}
blah2={{.ENV2}}`
Expand All @@ -55,3 +55,98 @@ blah2=`
r.NoError(err)
r.Equal(expected, string(b))
}

type nestedConfig struct {
Value1 string `json:"value1"`
}

type testConfig struct {
Value1 string `json:"value1"`
Value2 string `json:"value2"`
Nested nestedConfig `json:"nested"`
}

func TestLoadConfigurationBasic(t *testing.T) {
r := require.New(t)

cfg := &testConfig{}
err := LoadConfiguration(cfg, WithConfigYamlDir[testConfig]("./testData/basic"))
r.NoError(err)

r.Equal("foo", cfg.Value1)
r.Equal("bar", cfg.Value2)
r.Equal("zap", cfg.Nested.Value1)
}

func TestLoadConfigurationOverlay(t *testing.T) {
r := require.New(t)

// Set the deployment stage to test so it overlays the app-config.test.yaml file
os.Setenv("DEPLOYMENT_STAGE", "test")
defer os.Unsetenv("DEPLOYMENT_STAGE")

cfg := &testConfig{}
err := LoadConfiguration(cfg, WithConfigYamlDir[testConfig]("./testData/overlay"))
r.NoError(err)

r.Equal("testval1", cfg.Value1)
r.Equal("testval2", cfg.Value2)
r.Equal("zap", cfg.Nested.Value1)
}

type validatedConfig struct {
Value1 string `json:"value1" validate:"required"`
Value2 string `json:"value2" validate:"required"`
Value3 string `json:"value3"`
}

// this should fail because the values are missing from the yaml file
func TestLoadConfigurationValidatedFail(t *testing.T) {
r := require.New(t)

cfg := &validatedConfig{}
err := LoadConfiguration(cfg, WithConfigYamlDir[validatedConfig]("./testData/validation"))
r.Error(err)

r.Contains(err.Error(), "Value1")
r.Contains(err.Error(), "Value2")
r.Equal("zap", cfg.Value3)
}

// this should succeed because the values are supplied by the editor function
func TestLoadConfigurationValidatedSucceedWithConfigEditor(t *testing.T) {
r := require.New(t)

cfg := &validatedConfig{}
err := LoadConfiguration(
cfg,
WithConfigYamlDir[validatedConfig]("./testData/validation"),
WithConfigEditorFn(func(cfg *validatedConfig) error {
cfg.Value1 = "edited-value1"
cfg.Value2 = "edited-value2"
return nil
}),
)
r.NoError(err)

r.Equal("edited-value1", cfg.Value1)
r.Equal("edited-value2", cfg.Value2)
r.Equal("zap", cfg.Value3)
}

// this should succeed because the values are supplied by the overlay file
func TestLoadConfigurationValidatedSucceedWithOverlay(t *testing.T) {
r := require.New(t)

// Set the deployment stage to test so it overlays the app-config.test.yaml file
os.Setenv("DEPLOYMENT_STAGE", "withvalues")
defer os.Unsetenv("DEPLOYMENT_STAGE")

cfg := &validatedConfig{}
err := LoadConfiguration(cfg, WithConfigYamlDir[validatedConfig]("./testData/validation"))
r.NoError(err)

r.Equal("foo", cfg.Value1)
r.Equal("bar", cfg.Value2)
r.Equal("zap", cfg.Value3)
}
4 changes: 4 additions & 0 deletions config/testData/basic/app-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
value1: foo
value2: bar
nested:
value1: zap
2 changes: 2 additions & 0 deletions config/testData/overlay/app-config.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
value1: testval1
value2: testval2
3 changes: 3 additions & 0 deletions config/testData/overlay/app-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
value1: foo
nested:
value1: zap
4 changes: 4 additions & 0 deletions config/testData/validation/app-config.withvalues.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
value1: foo
value2: bar
value3: zap

1 change: 1 addition & 0 deletions config/testData/validation/app-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
value3: zap

0 comments on commit 1bc7e2e

Please sign in to comment.