调整项目架构,让框架更优化
This commit is contained in:
121
factory/factory_test.go
Normal file
121
factory/factory_test.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package factory
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.toowon.com/jimmy/go-common/config"
|
||||
"git.toowon.com/jimmy/go-common/i18n"
|
||||
"git.toowon.com/jimmy/go-common/logger"
|
||||
)
|
||||
|
||||
func TestExcelReturnsFreshInstance(t *testing.T) {
|
||||
app := New(nil)
|
||||
a := app.Excel()
|
||||
b := app.Excel()
|
||||
if a == nil || b == nil {
|
||||
t.Fatal("Excel() returned nil")
|
||||
}
|
||||
if a == b {
|
||||
t.Fatal("Excel() should return a new instance each call")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithLoggerOption(t *testing.T) {
|
||||
custom, err := logger.NewLogger(&config.LoggerConfig{
|
||||
Level: "error",
|
||||
Output: "stdout",
|
||||
Async: config.BoolPtr(false),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app := New(nil, WithLogger(custom))
|
||||
got, err := app.Logger()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != custom {
|
||||
t.Fatal("WithLogger should inject the provided logger")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithI18nAndNewHandler(t *testing.T) {
|
||||
i := i18n.NewI18n("zh-CN")
|
||||
i.LoadFromMap("zh-CN", map[string]i18n.MessageInfo{
|
||||
"common.success": {Code: 0, Message: "成功"},
|
||||
})
|
||||
app := New(nil, WithI18n(i))
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/ping", nil)
|
||||
h := app.NewHandler(rec, req)
|
||||
h.Success(map[string]string{"ok": "1"})
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
body := rec.Body.String()
|
||||
if body == "" || !strings.Contains(body, "成功") {
|
||||
t.Fatalf("response body missing i18n message: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewHandlerWithoutI18n(t *testing.T) {
|
||||
app := New(nil)
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
h := app.NewHandler(rec, req)
|
||||
h.Success(nil)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWarmupLogger(t *testing.T) {
|
||||
app := New(nil)
|
||||
if err := app.Warmup(ModuleLogger); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWarmupMissingDatabase(t *testing.T) {
|
||||
app := New(nil)
|
||||
err := app.Warmup(ModuleDatabase)
|
||||
if err == nil {
|
||||
t.Fatal("expected database warmup to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWarmupUnknownModule(t *testing.T) {
|
||||
app := New(nil)
|
||||
err := app.Warmup(Module("unknown"))
|
||||
if err == nil {
|
||||
t.Fatal("expected unknown module error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseLogger(t *testing.T) {
|
||||
app := New(nil)
|
||||
if _, err := app.Logger(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := app.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Close 后再取 logger 应可重新创建
|
||||
if _, err := app.Logger(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = app.Close()
|
||||
}
|
||||
|
||||
func TestMustLogger(t *testing.T) {
|
||||
app := New(nil)
|
||||
l := app.MustLogger()
|
||||
if l == nil {
|
||||
t.Fatal("MustLogger returned nil")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user