Skip to content

Commit

Permalink
#40: Covered the validation by tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Jan 21, 2024
1 parent feeb857 commit 75b7fce
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *Provider) Load(configPath string) (*Provider, error) {
for _, err := range err.(validator.ValidationErrors) {
namespace := strings.TrimLeft(err.Namespace(), "Config.")

errors = append(errors, fmt.Sprintf("- ❌ %v field is %v, %v provided", namespace, err.Tag(), err.Value()))
errors = append(errors, fmt.Sprintf("- ❌ %v field is %v, \"%v\" provided", namespace, err.Tag(), err.Value()))
}

// from here you can create your own error messages in whatever language you wish
Expand Down
20 changes: 20 additions & 0 deletions pkg/config/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ func TestConfigProvider_ValidConfigLoaded(t *testing.T) {
require.Len(t, models, 1)
}

func TestConfigProvider_InvalidConfigLoaded(t *testing.T) {
var tests = []struct {
name string
configFile string
}{
{"empty telemetry", "./testdata/provider.telnil.yaml"},
{"empty logging", "./testdata/provider.loggingnil.yaml"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
configProvider := NewProvider()
configProvider, err := configProvider.Load(tt.configFile)

require.Error(t, err)
require.ErrorContains(t, err, "failed to validate config file")
})
}
}

func TestConfigProvider_NoProvider(t *testing.T) {
configProvider := NewProvider()
_, err := configProvider.Load("./testdata/provider.nomodelprovider.yaml")
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/testdata/provider.fullconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
telemetry:
logging:
level: INFO # DEBUG, INFO, WARNING, ERROR, FATAL
level: info # debug, info, warning, error, fatal
encoding: json # console, json

routers:
Expand Down
15 changes: 15 additions & 0 deletions pkg/config/testdata/provider.loggingnil.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
telemetry:
logging:

routers:
language:
- id: simplerouter
strategy: priority
models:
- id: openai-boring
openai:
model: gpt-3.5-turbo
api_key: "ABSC@124"
default_params:
temperature: 0

14 changes: 14 additions & 0 deletions pkg/config/testdata/provider.telnil.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
telemetry:

routers:
language:
- id: simplerouter
strategy: priority
models:
- id: openai-boring
openai:
model: gpt-3.5-turbo
api_key: "ABSC@124"
default_params:
temperature: 0

4 changes: 2 additions & 2 deletions pkg/telemetry/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

type LogConfig struct {
// Level is the minimum enabled logging level.
Level zapcore.Level `yaml:"level" validate:"required"`
Level zapcore.Level `yaml:"level"`

// Encoding sets the logger's encoding. Valid values are "json", "console"
Encoding string `yaml:"encoding" validate:"required"`
Encoding string `yaml:"encoding"`

// DisableCaller stops annotating logs with the calling function's file name and line number.
// By default, all logs are annotated.
Expand Down

0 comments on commit 75b7fce

Please sign in to comment.