97 lines
2.9 KiB
Go
97 lines
2.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"fashionapi/internal/dto"
|
|
"fashionapi/internal/model"
|
|
"fashionapi/internal/pkg/imgurl"
|
|
"fashionapi/internal/repository"
|
|
)
|
|
|
|
// fakeSnapRepo 只实现测试路径用到的仓储方法;其余由嵌入的接口兜底(测试中不会调到未实现的方法)。
|
|
type fakeSnapRepo struct {
|
|
repository.StreetSnapRepository
|
|
findByID *model.StreetSnap
|
|
listImages []model.StreetSnapImage
|
|
rows []model.StreetSnap
|
|
}
|
|
|
|
func (f *fakeSnapRepo) FindByID(_ context.Context, _ string) (*model.StreetSnap, error) {
|
|
return f.findByID, nil
|
|
}
|
|
|
|
func (f *fakeSnapRepo) ListImages(_ context.Context, _ string) ([]model.StreetSnapImage, error) {
|
|
return f.listImages, nil
|
|
}
|
|
|
|
func (f *fakeSnapRepo) List(_ context.Context, q dto.StreetSnapQuery) ([]model.StreetSnap, int64, error) {
|
|
return f.rows, int64(len(f.rows)), nil
|
|
}
|
|
|
|
var _ repository.StreetSnapRepository = (*fakeSnapRepo)(nil)
|
|
|
|
func newSnapService(f *fakeSnapRepo) StreetSnapService {
|
|
return NewStreetSnapService(f, nil, imgurl.New("", "", ""), nil, nil)
|
|
}
|
|
|
|
// TestStreetSnapDetailLocalizesTitle 详情按 locale 返回中/英标题。
|
|
func TestStreetSnapDetailLocalizesTitle(t *testing.T) {
|
|
repo := &fakeSnapRepo{
|
|
findByID: &model.StreetSnap{ID: 1, Title: "London Street Style", TitleCn: "伦敦街拍", Cover: "c.jpg"},
|
|
listImages: []model.StreetSnapImage{{ID: 1, Image: "a.jpg"}},
|
|
}
|
|
svc := newSnapService(repo)
|
|
|
|
cn, err := svc.Detail(context.Background(), "1", "cn")
|
|
if err != nil {
|
|
t.Fatalf("Detail cn err: %v", err)
|
|
}
|
|
if cn.Title != "伦敦街拍" {
|
|
t.Errorf("cn 标题应为中文,实际 %q", cn.Title)
|
|
}
|
|
|
|
en, err := svc.Detail(context.Background(), "1", "en")
|
|
if err != nil {
|
|
t.Fatalf("Detail en err: %v", err)
|
|
}
|
|
if en.Title != "London Street Style" {
|
|
t.Errorf("en 标题应为英文,实际 %q", en.Title)
|
|
}
|
|
}
|
|
|
|
// TestStreetSnapDetailFallsBackToEn 中文为空时回落英文。
|
|
func TestStreetSnapDetailFallsBackToEn(t *testing.T) {
|
|
repo := &fakeSnapRepo{findByID: &model.StreetSnap{ID: 1, Title: "Only English", TitleCn: ""}}
|
|
svc := newSnapService(repo)
|
|
|
|
cn, err := svc.Detail(context.Background(), "1", "cn")
|
|
if err != nil {
|
|
t.Fatalf("Detail cn err: %v", err)
|
|
}
|
|
if cn.Title != "Only English" {
|
|
t.Errorf("中文为空应回落英文,实际 %q", cn.Title)
|
|
}
|
|
}
|
|
|
|
// TestStreetSnapListLocalizesTitle 列表按 locale 取标题,中文为空回落英文。
|
|
func TestStreetSnapListLocalizesTitle(t *testing.T) {
|
|
repo := &fakeSnapRepo{rows: []model.StreetSnap{
|
|
{ID: 1, Title: "EN A", TitleCn: "中 A"},
|
|
{ID: 2, Title: "EN B", TitleCn: ""},
|
|
}}
|
|
svc := newSnapService(repo)
|
|
|
|
got, _, err := svc.List(context.Background(), dto.StreetSnapQuery{Locale: "cn", Images: 0})
|
|
if err != nil {
|
|
t.Fatalf("List err: %v", err)
|
|
}
|
|
if len(got) != 2 {
|
|
t.Fatalf("期望 2 条,实际 %d", len(got))
|
|
}
|
|
if got[0].Title != "中 A" || got[1].Title != "EN B" {
|
|
t.Errorf("列表本地化错误: %#v", got)
|
|
}
|
|
}
|