-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mohamed Asif <142201466+mohamedasifs123@users.noreply.github.com> feat: adding test for config Signed-off-by: Mohamed Asif <142201466+mohamedasifs123@users.noreply.github.com> feat: adding test for config Signed-off-by: Mohamed Asif <142201466+mohamedasifs123@users.noreply.github.com> Update config_test.go Signed-off-by: Mohamed Asif <142201466+mohamedasifs123@users.noreply.github.com> feat: adding test for config Signed-off-by: mohamedasifs123 <142201466+mohamedasifs123@users.noreply.github.com>
- Loading branch information
1 parent
1b35286
commit 27e34b8
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestInitcfg_WithDefaultPath(t *testing.T) { | ||
// Ensure no custom config file is set | ||
GlobalConfig.CfgFile = "" | ||
|
||
// Set default config to avoid actual file dependencies | ||
viper.SetDefault("grpcport", 50051) | ||
viper.SetDefault("httpport", 8080) | ||
viper.SetDefault("dbaddress", "127.0.0.1:5432") | ||
|
||
Initcfg() | ||
|
||
// Validate defaults | ||
assert.Equal(t, uint16(50051), GetConfig().GRPCPort) | ||
assert.Equal(t, uint16(8080), GetConfig().HTTPPort) | ||
assert.Equal(t, "127.0.0.1:5432", GetConfig().DBAddress) | ||
} | ||
|
||
func TestSetAndGetConfig(t *testing.T) { | ||
cfg := Config{ | ||
GRPCPort: 50051, | ||
HTTPPort: 8080, | ||
DBAddress: "127.0.0.1:5432", | ||
} | ||
|
||
err := SetConfig(cfg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, &cfg, GetConfig()) | ||
} | ||
|
||
func setupViperConfig(values map[string]interface{}) { | ||
viper.Reset() | ||
for key, value := range values { | ||
viper.Set(key, value) | ||
} | ||
} | ||
|
||
func TestValidateConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config map[string]interface{} | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid Config", | ||
config: map[string]interface{}{ | ||
"grpcport": 50051, | ||
"httpport": 8080, | ||
"dbaddress": "127.0.0.1:5432", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Invalid GRPC Port", | ||
config: map[string]interface{}{ | ||
"grpcport": -1, | ||
"httpport": 8080, | ||
"dbaddress": "127.0.0.1:5432", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Invalid HTTP Port", | ||
config: map[string]interface{}{ | ||
"grpcport": 50051, | ||
"httpport": 70000, | ||
"dbaddress": "127.0.0.1:5432", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Invalid DB Address Format", | ||
config: map[string]interface{}{ | ||
"grpcport": 50051, | ||
"httpport": 8080, | ||
"dbaddress": "localhost", | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
setupViperConfig(tt.config) | ||
err := ValidateConfig() | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |