Files
platform/region/config_test.go
T
2026-07-26 13:01:07 +02:00

166 lines
4.1 KiB
Go

package region
import (
"os"
"path/filepath"
"testing"
)
func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
if len(cfg.Regions) != 2 {
t.Fatalf("expected 2 default regions, got %d", len(cfg.Regions))
}
if got := cfg.CurrencyOf("se"); got != "SEK" {
t.Errorf("CurrencyOf(se) = %s, want SEK", got)
}
if got := cfg.LocaleOf("se"); got != "sv-se" {
t.Errorf("LocaleOf(se) = %s, want sv-se", got)
}
if got := cfg.CurrencyOf("no"); got != "NOK" {
t.Errorf("CurrencyOf(no) = %s, want NOK", got)
}
if got := cfg.LocaleOf("no"); got != "nb-no" {
t.Errorf("LocaleOf(no) = %s, want nb-no", got)
}
}
func TestCurrencyOf(t *testing.T) {
cfg := DefaultConfig()
tests := []struct {
country string
want string
}{
{"se", "SEK"},
{"SE", "SEK"},
{"no", "NOK"},
{"NO", "NOK"},
{"dk", "SEK"}, // unknown → default
{"fi", "SEK"}, // unknown → default
{"us", "SEK"}, // unknown → default
{"", "SEK"}, // empty → default
}
for _, tt := range tests {
t.Run(tt.country, func(t *testing.T) {
if got := cfg.CurrencyOf(tt.country); got != tt.want {
t.Errorf("CurrencyOf(%q) = %q, want %q", tt.country, got, tt.want)
}
})
}
}
func TestLocaleOf(t *testing.T) {
cfg := DefaultConfig()
tests := []struct {
country string
want string
}{
{"se", "sv-se"},
{"SE", "sv-se"},
{"no", "nb-no"},
{"NO", "nb-no"},
{"dk", "sv-se"}, // unknown → default
{"", "sv-se"}, // empty → default
}
for _, tt := range tests {
t.Run(tt.country, func(t *testing.T) {
if got := cfg.LocaleOf(tt.country); got != tt.want {
t.Errorf("LocaleOf(%q) = %q, want %q", tt.country, got, tt.want)
}
})
}
}
func TestCountryFromCurrency(t *testing.T) {
cfg := DefaultConfig()
tests := []struct {
currency string
want string
}{
{"SEK", "se"},
{"sek", "se"},
{"NOK", "no"},
{"nok", "no"},
{"DKK", "se"}, // unknown → default
{"EUR", "se"}, // unknown → default
{"", "se"}, // empty → default
}
for _, tt := range tests {
t.Run(tt.currency, func(t *testing.T) {
if got := cfg.CountryFromCurrency(tt.currency); got != tt.want {
t.Errorf("CountryFromCurrency(%q) = %q, want %q", tt.currency, got, tt.want)
}
})
}
}
func TestLoadConfig_FileNotFound(t *testing.T) {
cfg, err := LoadConfig("/nonexistent/regions.json")
if err != nil {
t.Fatalf("LoadConfig on missing file should not error: %v", err)
}
if cfg == nil {
t.Fatal("expected non-nil config on missing file")
}
if len(cfg.Regions) != 2 {
t.Errorf("expected 2 default regions, got %d", len(cfg.Regions))
}
}
func TestLoadConfig_CustomFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "regions.json")
content := `{
"regions": [
{"countryCode": "se", "currency": "SEK", "locale": "sv-se"},
{"countryCode": "no", "currency": "NOK", "locale": "nb-no"},
{"countryCode": "dk", "currency": "DKK", "locale": "da-dk"}
]
}`
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("write test file: %v", err)
}
cfg, err := LoadConfig(path)
if err != nil {
t.Fatalf("LoadConfig failed: %v", err)
}
if len(cfg.Regions) != 3 {
t.Fatalf("expected 3 regions, got %d", len(cfg.Regions))
}
if got := cfg.CurrencyOf("dk"); got != "DKK" {
t.Errorf("CurrencyOf(dk) = %s, want DKK", got)
}
if got := cfg.LocaleOf("dk"); got != "da-dk" {
t.Errorf("LocaleOf(dk) = %s, want da-dk", got)
}
if got := cfg.CountryFromCurrency("DKK"); got != "dk" {
t.Errorf("CountryFromCurrency(DKK) = %s, want dk", got)
}
}
func TestLoadConfig_NormalisesCountryCode(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "regions.json")
content := `{"regions": [{"countryCode": "NO", "currency": "NOK", "locale": "nb-no"}]}`
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("write test file: %v", err)
}
cfg, err := LoadConfig(path)
if err != nil {
t.Fatalf("LoadConfig failed: %v", err)
}
if got := cfg.CurrencyOf("NO"); got != "NOK" {
t.Errorf("CurrencyOf(NO) = %s, want NOK", got)
}
if got := cfg.CurrencyOf("no"); got != "NOK" {
t.Errorf("CurrencyOf(no) = %s, want NOK", got)
}
}