316 lines
6.9 KiB
Go
316 lines
6.9 KiB
Go
package repository
|
||
|
||
import (
|
||
"testing"
|
||
|
||
"github.com/topfans/backend/pkg/models"
|
||
)
|
||
|
||
func TestStarRepository_GetByID(t *testing.T) {
|
||
db := setupTestDB(t)
|
||
defer cleanupTestDB(t, db)
|
||
|
||
repo := NewStarRepository()
|
||
|
||
// 创建一个明星用于测试
|
||
star := &models.Star{
|
||
Name: "测试明星",
|
||
IdentityID: "test_star_001",
|
||
IsActive: true,
|
||
}
|
||
db.Create(star)
|
||
|
||
// 测试正常查询
|
||
retrieved, err := repo.GetByID(star.StarID)
|
||
if err != nil {
|
||
t.Fatalf("GetByID failed: %v", err)
|
||
}
|
||
|
||
if retrieved.StarID != star.StarID {
|
||
t.Errorf("StarID mismatch: expected %d, got %d", star.StarID, retrieved.StarID)
|
||
}
|
||
|
||
if retrieved.Name != star.Name {
|
||
t.Errorf("Name mismatch: expected %s, got %s", star.Name, retrieved.Name)
|
||
}
|
||
|
||
// 测试不存在的明星
|
||
_, err = repo.GetByID(99999)
|
||
if err == nil {
|
||
t.Fatal("Expected error for non-existent star")
|
||
}
|
||
|
||
// 测试无效ID
|
||
_, err = repo.GetByID(0)
|
||
if err == nil {
|
||
t.Fatal("Expected error for invalid star_id")
|
||
}
|
||
}
|
||
|
||
func TestStarRepository_GetByIdentityID(t *testing.T) {
|
||
db := setupTestDB(t)
|
||
defer cleanupTestDB(t, db)
|
||
|
||
repo := NewStarRepository()
|
||
|
||
// 创建一个明星用于测试
|
||
star := &models.Star{
|
||
Name: "测试明星2",
|
||
IdentityID: "test_star_002",
|
||
IsActive: true,
|
||
}
|
||
db.Create(star)
|
||
|
||
// 测试正常查询
|
||
retrieved, err := repo.GetByIdentityID(star.IdentityID)
|
||
if err != nil {
|
||
t.Fatalf("GetByIdentityID failed: %v", err)
|
||
}
|
||
|
||
if retrieved.IdentityID != star.IdentityID {
|
||
t.Errorf("IdentityID mismatch: expected %s, got %s", star.IdentityID, retrieved.IdentityID)
|
||
}
|
||
|
||
// 测试不存在的identity_id
|
||
_, err = repo.GetByIdentityID("non_existent")
|
||
if err == nil {
|
||
t.Fatal("Expected error for non-existent identity_id")
|
||
}
|
||
|
||
// 测试空identity_id
|
||
_, err = repo.GetByIdentityID("")
|
||
if err == nil {
|
||
t.Fatal("Expected error for empty identity_id")
|
||
}
|
||
}
|
||
|
||
func TestStarRepository_GetAllActive(t *testing.T) {
|
||
db := setupTestDB(t)
|
||
defer cleanupTestDB(t, db)
|
||
|
||
repo := NewStarRepository()
|
||
|
||
// 创建多个明星
|
||
star1 := &models.Star{
|
||
Name: "明星A",
|
||
IdentityID: "test_star_a",
|
||
IsActive: true,
|
||
}
|
||
db.Create(star1)
|
||
|
||
star2 := &models.Star{
|
||
Name: "明星B",
|
||
IdentityID: "test_star_b",
|
||
IsActive: true,
|
||
}
|
||
db.Create(star2)
|
||
|
||
// 创建一个非激活的明星(不应该被查询到)
|
||
star3 := &models.Star{
|
||
Name: "明星C",
|
||
IdentityID: "test_star_c",
|
||
IsActive: false,
|
||
}
|
||
db.Create(star3)
|
||
// 确保设置为非激活状态(因为GORM可能会使用默认值)
|
||
db.Model(star3).Update("is_active", false)
|
||
|
||
// 查询所有激活的明星
|
||
stars, err := repo.GetAllActive()
|
||
if err != nil {
|
||
t.Fatalf("GetAllActive failed: %v", err)
|
||
}
|
||
|
||
// 应该至少有2个激活的明星(可能数据库中有其他数据)
|
||
if len(stars) < 2 {
|
||
t.Errorf("Expected at least 2 active stars, got %d", len(stars))
|
||
}
|
||
|
||
// 验证所有返回的明星都是激活的
|
||
for _, s := range stars {
|
||
if !s.IsActive {
|
||
t.Errorf("Found inactive star: %s (ID: %d)", s.Name, s.StarID)
|
||
}
|
||
}
|
||
|
||
// 验证star3不在结果中
|
||
for _, s := range stars {
|
||
if s.IdentityID == star3.IdentityID {
|
||
t.Errorf("Found inactive star in results: %s", star3.IdentityID)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestStarRepository_Search(t *testing.T) {
|
||
db := setupTestDB(t)
|
||
defer cleanupTestDB(t, db)
|
||
|
||
repo := NewStarRepository()
|
||
|
||
// 创建测试明星
|
||
star1 := &models.Star{
|
||
Name: "张三",
|
||
IdentityID: "test_search_1",
|
||
IsActive: true,
|
||
}
|
||
db.Create(star1)
|
||
|
||
star2 := &models.Star{
|
||
Name: "李四",
|
||
IdentityID: "test_search_2",
|
||
IsActive: true,
|
||
}
|
||
db.Create(star2)
|
||
|
||
star3 := &models.Star{
|
||
Name: "张三丰",
|
||
IdentityID: "test_search_3",
|
||
IsActive: true,
|
||
}
|
||
db.Create(star3)
|
||
|
||
// 测试搜索"张三"(应该匹配star1和star3)
|
||
results, err := repo.Search("张三")
|
||
if err != nil {
|
||
t.Fatalf("Search failed: %v", err)
|
||
}
|
||
|
||
foundCount := 0
|
||
for _, s := range results {
|
||
if s.IdentityID == star1.IdentityID || s.IdentityID == star3.IdentityID {
|
||
foundCount++
|
||
}
|
||
}
|
||
|
||
if foundCount < 2 {
|
||
t.Errorf("Expected to find at least 2 stars matching '张三', but found %d", foundCount)
|
||
}
|
||
|
||
// 测试搜索"李"(应该匹配star2)
|
||
results, err = repo.Search("李")
|
||
if err != nil {
|
||
t.Fatalf("Search failed: %v", err)
|
||
}
|
||
|
||
found := false
|
||
for _, s := range results {
|
||
if s.IdentityID == star2.IdentityID {
|
||
found = true
|
||
break
|
||
}
|
||
}
|
||
|
||
if !found {
|
||
t.Error("Expected to find star2 when searching for '李'")
|
||
}
|
||
|
||
// 测试搜索不存在的关键词
|
||
results, err = repo.Search("不存在")
|
||
if err != nil {
|
||
t.Fatalf("Search failed: %v", err)
|
||
}
|
||
|
||
found = false
|
||
for _, s := range results {
|
||
if s.IdentityID == star1.IdentityID || s.IdentityID == star2.IdentityID || s.IdentityID == star3.IdentityID {
|
||
found = true
|
||
break
|
||
}
|
||
}
|
||
|
||
if found {
|
||
t.Error("Expected no matches for '不存在', but found some")
|
||
}
|
||
|
||
// 测试空关键词(应该返回所有激活的明星)
|
||
results, err = repo.Search("")
|
||
if err != nil {
|
||
t.Fatalf("Search with empty keyword failed: %v", err)
|
||
}
|
||
|
||
if len(results) == 0 {
|
||
t.Error("Expected at least some results for empty keyword")
|
||
}
|
||
}
|
||
|
||
func TestStarRepository_Search_EnglishName(t *testing.T) {
|
||
db := setupTestDB(t)
|
||
defer cleanupTestDB(t, db)
|
||
|
||
repo := NewStarRepository()
|
||
|
||
// 创建带英文名的明星
|
||
nameEn := "Test Star"
|
||
star := &models.Star{
|
||
Name: "测试明星",
|
||
NameEn: &nameEn,
|
||
IdentityID: "test_search_en",
|
||
IsActive: true,
|
||
}
|
||
db.Create(star)
|
||
|
||
// 测试搜索英文名
|
||
results, err := repo.Search("Test")
|
||
if err != nil {
|
||
t.Fatalf("Search failed: %v", err)
|
||
}
|
||
|
||
found := false
|
||
for _, s := range results {
|
||
if s.IdentityID == star.IdentityID {
|
||
found = true
|
||
break
|
||
}
|
||
}
|
||
|
||
if !found {
|
||
t.Error("Expected to find star when searching for English name")
|
||
}
|
||
}
|
||
|
||
func TestStarRepository_GetByID_Inactive(t *testing.T) {
|
||
db := setupTestDB(t)
|
||
defer cleanupTestDB(t, db)
|
||
|
||
repo := NewStarRepository()
|
||
|
||
// 创建一个非激活的明星
|
||
star := &models.Star{
|
||
Name: "非激活明星",
|
||
IdentityID: "test_inactive",
|
||
IsActive: false,
|
||
}
|
||
db.Create(star)
|
||
// 确保设置为非激活状态(因为GORM可能会使用默认值)
|
||
db.Model(star).Update("is_active", false)
|
||
|
||
// 查询时应该返回错误(因为过滤了is_active=false)
|
||
_, err := repo.GetByID(star.StarID)
|
||
if err == nil {
|
||
t.Fatal("Expected error for inactive star")
|
||
}
|
||
}
|
||
|
||
func TestStarRepository_GetByIdentityID_Inactive(t *testing.T) {
|
||
db := setupTestDB(t)
|
||
defer cleanupTestDB(t, db)
|
||
|
||
repo := NewStarRepository()
|
||
|
||
// 创建一个非激活的明星
|
||
star := &models.Star{
|
||
Name: "非激活明星2",
|
||
IdentityID: "test_inactive_2",
|
||
IsActive: false,
|
||
}
|
||
db.Create(star)
|
||
// 确保设置为非激活状态(因为GORM可能会使用默认值)
|
||
db.Model(star).Update("is_active", false)
|
||
|
||
// 查询时应该返回错误(因为过滤了is_active=false)
|
||
_, err := repo.GetByIdentityID(star.IdentityID)
|
||
if err == nil {
|
||
t.Fatal("Expected error for inactive star")
|
||
}
|
||
}
|