mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
Add test case for LoadConfig
This commit is contained in:
@@ -1,9 +1,29 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/*
|
||||
No test case for GetToken yet due to token_manager dependency
|
||||
Mocking token_manager isn't fun
|
||||
|
||||
TestLoadConfig focuses on verifying main functionality (e.g. fallback when invalid input),
|
||||
and *shouldn't* need exhaustive scenarios
|
||||
*/
|
||||
|
||||
// setupTestLogger sets up a logger that discards output and returns a function to restore the original logger
|
||||
func setupTestLogger() func() {
|
||||
originalLogger := log.Default()
|
||||
log.SetOutput(io.Discard)
|
||||
return func() {
|
||||
log.SetOutput(originalLogger.Writer())
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseRevision is a test function that verifies the behavior of the parseRevision function.
|
||||
func TestParseRevision(t *testing.T) {
|
||||
tests := []struct {
|
||||
@@ -75,3 +95,116 @@ func TestValidateURL(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadConfig is a test function that verifies the behavior of the LoadConfig function.
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
// Helper function to set environment variables
|
||||
setEnv := func(env map[string]string) {
|
||||
for k, v := range env {
|
||||
os.Setenv(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to unset environment variables
|
||||
unsetEnv := func(env map[string]string) {
|
||||
for k := range env {
|
||||
os.Unsetenv(k)
|
||||
}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string // Description of the test case
|
||||
env map[string]string // Name of the environment variable and its value
|
||||
wantErr bool // Whether an error is expected
|
||||
}{
|
||||
{
|
||||
name: "Valid configuration",
|
||||
env: map[string]string{
|
||||
"PIXIVFE_HOST": "localhost",
|
||||
"PIXIVFE_PORT": "8282",
|
||||
"PIXIVFE_TOKEN": "token1,token2",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Missing required PIXIVFE_TOKEN",
|
||||
env: map[string]string{
|
||||
"PIXIVFE_HOST": "localhost",
|
||||
"PIXIVFE_PORT": "8282",
|
||||
"PIXIVFE_IMAGEPROXY": "https://imageproxy.test",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Invalid PIXIVFE_IMAGEPROXY",
|
||||
env: map[string]string{
|
||||
"PIXIVFE_HOST": "localhost",
|
||||
"PIXIVFE_PORT": "8282",
|
||||
"PIXIVFE_TOKEN": "token1,token2",
|
||||
"PIXIVFE_IMAGEPROXY": "invalidimageproxy-test",
|
||||
},
|
||||
wantErr: false, // Should not return an error, but use fallback BuiltinProxyUrl
|
||||
},
|
||||
{
|
||||
name: "Invalid PIXIVFE_TOKEN_LOAD_BALANCING",
|
||||
env: map[string]string{
|
||||
"PIXIVFE_HOST": "localhost",
|
||||
"PIXIVFE_PORT": "8282",
|
||||
"PIXIVFE_TOKEN": "token1,token2",
|
||||
"PIXIVFE_TOKEN_LOAD_BALANCING": "invalid-load-balancing-method",
|
||||
},
|
||||
wantErr: false, // Should not return an error, but use fallback round-robin
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Set up test logger
|
||||
restoreLogger := setupTestLogger()
|
||||
defer restoreLogger()
|
||||
|
||||
// Set up environment
|
||||
setEnv(tt.env)
|
||||
defer unsetEnv(tt.env)
|
||||
|
||||
// Create a new ServerConfig instance
|
||||
config := &ServerConfig{}
|
||||
|
||||
// Call LoadConfig
|
||||
err := config.LoadConfig()
|
||||
|
||||
// Check for errors
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("LoadConfig() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.wantErr {
|
||||
// Test whether config fields were set correctly
|
||||
if config.Host != tt.env["PIXIVFE_HOST"] {
|
||||
t.Errorf("LoadConfig() Host = %v, want %v", config.Host, tt.env["PIXIVFE_HOST"])
|
||||
}
|
||||
|
||||
if config.Port != tt.env["PIXIVFE_PORT"] {
|
||||
t.Errorf("LoadConfig() Port = %v, want %v", config.Port, tt.env["PIXIVFE_PORT"])
|
||||
}
|
||||
|
||||
if len(config.Token) != 2 && tt.env["PIXIVFE_TOKEN"] == "token1,token2" {
|
||||
t.Errorf("LoadConfig() Token count = %v, want 2", len(config.Token))
|
||||
}
|
||||
|
||||
if config.TokenManager == nil {
|
||||
t.Error("LoadConfig() TokenManager is nil")
|
||||
}
|
||||
|
||||
if config.ProxyServer.String() == "" {
|
||||
t.Error("LoadConfig() ProxyServer is empty")
|
||||
}
|
||||
|
||||
if config.TokenLoadBalancing == "" {
|
||||
t.Error("LoadConfig() TokenLoadBalancing is empty")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user