Skip to content

Commit

Permalink
Merge pull request #30 from kripsy/implement-unit-test-09112023
Browse files Browse the repository at this point in the history
add test
  • Loading branch information
kripsy authored Nov 9, 2023
2 parents b549bc7 + 2505308 commit 9b084aa
Showing 1 changed file with 75 additions and 6 deletions.
81 changes: 75 additions & 6 deletions internal/server/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package config_test

import (
"fmt"
"os"
"reflect"
"strings"
"testing"

"github.com/kripsy/GophKeeper/internal/server/config"
Expand All @@ -10,6 +13,8 @@ import (
)

func TestInitConfig(t *testing.T) {
originalEnv := saveCurrentEnv()
defer restoreEnv(originalEnv)
tests := []struct {
name string
envVars map[string]string
Expand Down Expand Up @@ -46,31 +51,95 @@ func TestInitConfig(t *testing.T) {
DatabaseDsn: "postgres://gophkeeperdb:gophkeeperdbpwd@localhost:5432/gophkeeperdb?sslmode=disable",
Secret: "newsecret",
TokenExp: config.TOKENEXP,
IsSecure: false,
IsSecure: true,
EndpointMinio: "localhost:9000",
AccessKeyIDMinio: "masoud",
SecretAccessKeyMinio: "Strong#Pass#2022",
BucketNameMinio: "secrets",
IsUseSSLMinio: false,
},
},
{
name: "Test Case 3 - Minio and DatabaseDsn Environment Variables",
envVars: map[string]string{
"ENDPOINTMINIO": "minio.example.com:9000",
"ACCESSKEYIDMINIO": "minioadmin",
"SECRETACCESSKEYMINIO": "minioadminpassword",
"BUCKETNAMEMINIO": "newbucket",
"ISUSESSLMINIO": "true",
"DATABASE_DSN": "postgres://user:password@localhost:5432/mydb?sslmode=enable",
},
expectedConfig: &config.Config{
URLServer: "localhost:8080",
LoggerLevel: "Info",
DatabaseDsn: "postgres://user:password@localhost:5432/mydb?sslmode=enable",
Secret: "supersecret",
TokenExp: config.TOKENEXP,
IsSecure: true,
EndpointMinio: "minio.example.com:9000",
AccessKeyIDMinio: "minioadmin",
SecretAccessKeyMinio: "minioadminpassword",
BucketNameMinio: "newbucket",
IsUseSSLMinio: true,
},
},
}

tests[1].expectedConfig.IsSecure = false
tests[2].expectedConfig.IsSecure = false
tests[2].expectedConfig.URLServer = "localhost:9090"
tests[2].expectedConfig.LoggerLevel = "Debug"
tests[2].expectedConfig.Secret = "newsecret"
tests[2].expectedConfig.DatabaseDsn = "postgres://user:password@localhost:5432/mydb?sslmode=enable"
tests[2].expectedConfig.EndpointMinio = "minio.example.com:9000"
tests[2].expectedConfig.AccessKeyIDMinio = "minioadmin"
tests[2].expectedConfig.SecretAccessKeyMinio = "minioadminpassword"
tests[2].expectedConfig.BucketNameMinio = "newbucket"
tests[2].expectedConfig.IsUseSSLMinio = true

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for key, value := range tt.envVars {
os.Setenv(key, value)
}
defer unsetEnvVars(tt.envVars)

got, err := config.InitConfig()

require.NoError(t, err)

assert.Equal(t, tt.expectedConfig, got)

for key := range tt.envVars {
os.Unsetenv(key)
if !reflect.DeepEqual(tt.expectedConfig, got) {
fmt.Println(tt.expectedConfig)
fmt.Println(got)
}
assert.True(t, reflect.DeepEqual(tt.expectedConfig, got), "Configs do not match")
})
}
}

func setEnvVars(envVars map[string]string) {
for key, value := range envVars {
os.Setenv(key, value)
}
}

func unsetEnvVars(envVars map[string]string) {
for key := range envVars {
os.Unsetenv(key)
}
}

func saveCurrentEnv() map[string]string {
env := os.Environ()
savedEnv := make(map[string]string, len(env))
for _, e := range env {
pair := strings.SplitN(e, "=", 2)
savedEnv[pair[0]] = pair[1]
}

return savedEnv
}

func restoreEnv(env map[string]string) {
unsetEnvVars(env) // Удаляем текущие значения
setEnvVars(env) // Устанавливаем сохраненные значения
}

0 comments on commit 9b084aa

Please sign in to comment.