Skip to content

Commit

Permalink
Added tests for validation function
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Feb 27, 2023
1 parent 3c92941 commit ffa7481
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package configuration
import (
"errors"

"github.com/evg4b/uncors/internal/log"

"github.com/spf13/viper"
)

Expand All @@ -23,7 +25,12 @@ func readURLMapping(config *viper.Viper, configuration *UncorsConfig) error {
}

for index, key := range from {
configuration.Mappings[key] = to[index]
value := to[index]
if prev, ok := configuration.Mappings[key]; ok {
log.Warningf("Mapping for %s from (%s) replaced new value (%s)", key, prev, value)
}

configuration.Mappings[key] = value
}

return nil
Expand Down
31 changes: 31 additions & 0 deletions internal/configuration/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package configuration_test

import (
"testing"

"github.com/evg4b/uncors/internal/configuration"
"github.com/stretchr/testify/assert"
)

func TestValidate(t *testing.T) {
tests := []struct {
name string
config *configuration.UncorsConfig
expected string
}{
{
name: "invalid http-port",
config: &configuration.UncorsConfig{
Mappings: map[string]string{},
},
expected: "Key: 'UncorsConfig.HTTPPort' Error:Field validation for 'HTTPPort' failed on the 'required' tag",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
err := configuration.Validate(testCase.config)

assert.EqualError(t, err, testCase.expected)
})
}
}

0 comments on commit ffa7481

Please sign in to comment.