Rewrite tests to run without a database
- Unit tests use an in-memory fake database (internal/testutil/fakedb), temp folders and httptest; `go test ./...` needs no Docker or env vars - Integration tests (build tag "integration") start PostgreSQL with testcontainers: migrations step by step, SQL dumps (skipped if missing) and a full sync against a real database - Removed tests that only checked copies of the logic or had empty bodies, the broken root integration_test.go, compose.test.yaml and .env.test with hardcoded paths - testMusic MP3s replaced with tiny placeholders (sync never decodes audio), 194 MB -> 168 KB
This commit is contained in:
1 parent
090e794460
commit
7b144ae419
42 files changed
+2334
-40
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func env(values map[string]string) func(string) string {
|
||||
return func(key string) string { return values[key] }
|
||||
}
|
||||
|
||||
func validEnv() map[string]string {
|
||||
return map[string]string{
|
||||
"DB_HOST": "localhost",
|
||||
"DB_PORT": "5432",
|
||||
"DB_USERNAME": "user",
|
||||
"DB_PASSWORD": "pass",
|
||||
"DB_NAME": "music",
|
||||
"MUSIC_PATH": "/music",
|
||||
"CHARACTERS_PATH": "/characters",
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDefaults(t *testing.T) {
|
||||
cfg, err := load(env(validEnv()))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 8080, cfg.Port)
|
||||
assert.Equal(t, "info", cfg.LogLevel)
|
||||
assert.Equal(t, "songs", cfg.SoundCheckPath)
|
||||
assert.Equal(t, DefaultReleasesURL, cfg.ReleasesURL)
|
||||
assert.Empty(t, cfg.TrustedProxies)
|
||||
}
|
||||
|
||||
func TestLoadMissingRequired(t *testing.T) {
|
||||
values := validEnv()
|
||||
delete(values, "MUSIC_PATH")
|
||||
delete(values, "DB_HOST")
|
||||
_, err := load(env(values))
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "MUSIC_PATH is required")
|
||||
assert.Contains(t, err.Error(), "DB_HOST is required")
|
||||
}
|
||||
|
||||
func TestLoadTrustedProxies(t *testing.T) {
|
||||
values := validEnv()
|
||||
values["TRUSTED_PROXIES"] = "10.0.0.0/8, 172.16.0.0/12"
|
||||
cfg, err := load(env(values))
|
||||
require.NoError(t, err)
|
||||
require.Len(t, cfg.TrustedProxies, 2)
|
||||
assert.Equal(t, "10.0.0.0/8", cfg.TrustedProxies[0].String())
|
||||
|
||||
values["TRUSTED_PROXIES"] = "not-a-cidr"
|
||||
_, err = load(env(values))
|
||||
assert.ErrorContains(t, err, "TRUSTED_PROXIES")
|
||||
}
|
||||
|
||||
func TestLoadInvalidPort(t *testing.T) {
|
||||
values := validEnv()
|
||||
values["PORT"] = "abc"
|
||||
_, err := load(env(values))
|
||||
assert.ErrorContains(t, err, "PORT")
|
||||
}
|
||||
Reference in new issue
Block a user