Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
soneda-yuya committed Jan 12, 2025
1 parent 37c935a commit 7a8bbc0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
42 changes: 42 additions & 0 deletions server/internal/adapter/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,48 @@ import (
"golang.org/x/text/language"
)

func TestAttachLang(t *testing.T) {
t.Run("Valid language tag", func(t *testing.T) {
ctx := context.Background()

lang := language.Japanese
newCtx := AttachLang(ctx, lang)

storedLang := newCtx.Value(contextLang)

assert.NotNil(t, storedLang, "Language should be stored in context")
assert.Equal(t, lang, storedLang, "Stored language should match the input")
})

t.Run("Default language (Und)", func(t *testing.T) {
ctx := context.Background()

lang := language.Und
newCtx := AttachLang(ctx, lang)

storedLang := newCtx.Value(contextLang)

assert.NotNil(t, storedLang, "Language should be stored in context")
assert.Equal(t, lang, storedLang, "Stored language should match the input")
})

t.Run("Context chaining", func(t *testing.T) {
ctx := context.Background()

lang1 := language.English
ctx1 := AttachLang(ctx, lang1)

lang2 := language.French
ctx2 := AttachLang(ctx1, lang2)

// confirm that the latest language is stored in the context
assert.Equal(t, lang2, ctx2.Value(contextLang), "Latest language should be stored in context")

// old context is not affected
assert.Equal(t, lang1, ctx1.Value(contextLang), "Old context should retain its value")
})
}

func TestLang(t *testing.T) {

// Default language for testing
Expand Down
3 changes: 0 additions & 3 deletions server/internal/app/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ func AttachLanguageMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
lang := LanguageExtractor(c.Request())
ctx := adapter.AttachLang(c.Request().Context(), lang)
if ctx == nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to attach language to context")
}
c.SetRequest(c.Request().WithContext(ctx))
return next(c)
}
Expand Down
8 changes: 5 additions & 3 deletions server/internal/locales/locales.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Error struct {

// loadLocales
// loads locales data from the cache or file
func loadLocales() {
func loadLocales(langs []string, localesFileType []string) {
cache = NewLocalesCache()
for _, lang := range langs {
for _, fileType := range localesFileType {
Expand All @@ -54,13 +54,15 @@ func loadLocales() {
// also if key is not found, it will panic
// because we want to know if the key is not found when server starts
func LoadError(key ErrorKey) (map[string]*Error, error) {
loadOnce.Do(loadLocales)
loadOnce.Do(func() {
loadLocales(langs, localesFileType)
})

localesError := make(map[string]*Error)
for _, lang := range langs {
data, ok := cache.GetFromFileCache(lang)
if !ok {
loadLocales()
loadLocales(langs, localesFileType)
data, _ = cache.GetFromFileCache(lang)
}

Expand Down
16 changes: 16 additions & 0 deletions server/internal/locales/locales_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package locales
import (
"embed"
"fmt"
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,6 +12,21 @@ import (
//go:embed en/* ja/*
var testLocales embed.FS

func TestLoadLocales_FileNotFound(t *testing.T) {
cache = nil
loadOnce = sync.Once{}

defer func() {
if r := recover(); r != nil {
assert.Contains(t, fmt.Sprintf("%v", r), "open aaa/error.json: file does not exist")
} else {
t.Fatal("Expected panic but it did not occur")
}
}()

loadLocales([]string{"aaa"}, []string{"error"})
}

func TestLoadError(t *testing.T) {
// モック用の localesJson を置き換える
localesJson = testLocales
Expand Down

0 comments on commit 7a8bbc0

Please sign in to comment.