feat: storage, language rules and migration
This commit is contained in:
1 parent
8c4a1825ab
commit
0806733d5c
70 files changed
+1771
-132
No files matched your search
@@ -0,0 +1,103 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/language"
|
||||
entity "RenewCMS/internal/infrastructure/persistence/models"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
func TestLanguageNormalization(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
want string
|
||||
err error
|
||||
}{
|
||||
{"en", "en", nil},
|
||||
{"EN", "en", nil},
|
||||
{"pt-br", "pt-BR", nil},
|
||||
{"PT_BR", "", language.ErrInvalidLanguage},
|
||||
{"fr-ca", "fr-CA", nil},
|
||||
{" fr", "fr", nil},
|
||||
{"e", "", language.ErrInvalidLanguage},
|
||||
{"english", "", language.ErrInvalidLanguage},
|
||||
{"en-ZZZZ", "", language.ErrInvalidLanguage},
|
||||
{"", "", language.ErrInvalidLanguage},
|
||||
{"xx", "", language.ErrUnsupportedLanguage},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run("Language tag normalization "+c.input, func(t *testing.T) {
|
||||
got, err := language.Normalize(c.input)
|
||||
if got != c.want || !errors.Is(err, c.err) {
|
||||
t.Errorf("Normalize(%q) = %q, %v; want %q, %v", c.input, got, err, c.want, c.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLanguageSuggestion(t *testing.T) {
|
||||
text := func(value string) *string { return &value }
|
||||
cases := []struct {
|
||||
locale *string
|
||||
suggested string
|
||||
substituted bool
|
||||
}{
|
||||
{text("pt-BR"), "pt-BR", false},
|
||||
{text("en-GB"), "en-GB", false},
|
||||
{text("fr-BE"), "fr", true},
|
||||
{text("fr"), "fr", false},
|
||||
{text("xx-YY"), "", false},
|
||||
{nil, "", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
name := "none"
|
||||
if c.locale != nil {
|
||||
name = *c.locale
|
||||
}
|
||||
t.Run("Suggestion for a site's default language "+name, func(t *testing.T) {
|
||||
got := language.Suggest(c.locale)
|
||||
if got.Tag != c.suggested || got.Substituted != c.substituted || got.Found != (c.suggested != "") {
|
||||
t.Errorf("Suggest = %+v; want %q substituted %v", got, c.suggested, c.substituted)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLanguageSupportedReturnsIndependentCopy(t *testing.T) {
|
||||
first := language.Supported()
|
||||
if len(first) == 0 {
|
||||
t.Fatal("expected supported languages")
|
||||
}
|
||||
first[0] = "xx"
|
||||
second := language.Supported()
|
||||
if second[0] == "xx" {
|
||||
t.Fatal("Supported must return a copy")
|
||||
}
|
||||
if _, err := language.Normalize(second[0]); err != nil {
|
||||
t.Fatalf("supported tag %q must normalize: %v", second[0], err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLanguageColumnsMatchTheDomainTagLength(t *testing.T) {
|
||||
columns := []struct {
|
||||
model any
|
||||
field string
|
||||
column string
|
||||
}{
|
||||
{&entity.Article{}, "Lang", "lang"},
|
||||
{&entity.SiteLanguage{}, "Lang", "lang"},
|
||||
{&entity.Site{}, "Locale", "locale"},
|
||||
}
|
||||
for _, c := range columns {
|
||||
parsed, err := schema.Parse(c.model, &sync.Map{}, schema.NamingStrategy{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if size := parsed.LookUpField(c.field).Size; size != language.MaxTagLength {
|
||||
t.Errorf("%s.%s has size %d, expected %d", parsed.Table, c.column, size, language.MaxTagLength)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user