catface_backend_go/test/models_test.go

184 lines
3.7 KiB
Go
Raw Normal View History

2024-10-17 11:39:05 +08:00
// add_test.go
package test
import (
"catface/app/model"
2024-10-23 10:05:06 +08:00
"fmt"
2024-11-02 11:49:49 +08:00
"reflect"
"strconv"
"strings"
2024-10-17 11:39:05 +08:00
"testing"
)
func TestUsers(t *testing.T) {
Init()
user := model.UsersModel{}
err := DB.AutoMigrate(&user)
if err != nil {
t.Error(err)
}
}
2024-10-20 16:30:29 +08:00
func TestEncouner(t *testing.T) {
Init()
encounter := model.Encounter{}
err := DB.AutoMigrate(&encounter)
if err != nil {
t.Error(err)
}
}
2024-10-21 01:24:01 +08:00
func TestEncounterLike(t *testing.T) {
Init()
encounterLike := model.EncounterLike{}
err := DB.AutoMigrate(&encounterLike)
if err != nil {
t.Error(err)
}
}
2024-10-23 02:08:36 +08:00
2024-10-23 09:05:05 +08:00
func TestEncounterLevel_Insert(t *testing.T) {
2024-10-23 02:08:36 +08:00
Init()
encounterLevel := model.EncounerLevel{}
err := DB.AutoMigrate(&encounterLevel)
if err != nil {
t.Error(err)
}
ZH := []string{"日常", "重大", "标志", "代办", "日程"}
EN := []string{"daily", "serious", "flag", "todo", "schedule"}
2024-10-23 02:54:56 +08:00
colorbg := []string{"#F0F0F0", "#FFD700", "#FF69B4", "#87CEFA", "#32CD32"}
colorfont := []string{"#333333", "#000000", "#FFFFFF", "#000000", "#FFFFFF"}
2024-10-23 02:08:36 +08:00
for i := 0; i < len(ZH); i++ {
encounterLevel := model.EncounerLevel{
BriefModel: model.BriefModel{
NameZh: ZH[i],
NameEn: EN[i],
},
2024-10-23 02:54:56 +08:00
Color: model.Color{
ColorBackground: colorbg[i],
ColorFont: colorfont[i],
},
2024-10-23 02:08:36 +08:00
}
DB.Create(&encounterLevel)
}
}
2024-10-23 09:05:05 +08:00
func TestEncounterLike_Create_and_Delete(t *testing.T) {
Init()
encounterLike := model.EncounterLike{
UsersModelId: 1,
EncounterId: 15,
}
2024-10-23 10:05:06 +08:00
// DB.Create(&encounterLike)
// DB.Delete(&encounterLike)
// time.Sleep(2 * time.Second)
// encounterLike = model.EncounterLike{
// UsersModelId: 1,
// EncounterId: 15,
// }
DB.Unscoped().Where("encounter_id", encounterLike.EncounterId).First(&encounterLike)
fmt.Println(encounterLike.Id, encounterLike.IsDel)
// INFO 这样操作无效
res := DB.Where("id = ?", encounterLike.Id).Update("is_del", nil)
fmt.Println(res.RowsAffected)
// INFO 这样有效。
encounterLike.IsDel = 0
res2 := DB.Save(&encounterLike)
fmt.Println(res2.RowsAffected)
2024-10-23 09:05:05 +08:00
}
2024-10-23 12:27:38 +08:00
func TestAnimalLike(t *testing.T) {
Init()
animalLike := model.AnimalLike{}
err := DB.AutoMigrate(&animalLike)
if err != nil {
t.Error(err)
}
}
2024-11-02 11:49:49 +08:00
func TestEaLink(t *testing.T) {
Init()
eaLink := model.EncounterAnimalLink{}
err := DB.AutoMigrate(&eaLink)
if err != nil {
t.Error(err)
}
}
// 测试函数
func TestInsertEncounterAnimalLinks(t *testing.T) {
Init()
// 定义要插入的数据
data := []struct {
EncounterId int
AnimalIds string
}{
{10, "4"},
{11, "2,3"},
{13, "4"},
{14, "4"},
{15, "4"},
{16, "4"},
{17, "4"},
{18, "4"},
{19, "4"},
{20, "4"},
}
// 插入数据
for _, item := range data {
animalIds := strings.Split(item.AnimalIds, ",")
for _, animalIdStr := range animalIds {
animalId, err := strconv.Atoi(animalIdStr)
if err != nil {
t.Errorf("Failed to convert animal Id: %v", err)
continue
}
link := model.EncounterAnimalLink{
EncounterId: item.EncounterId,
AnimalId: animalId,
}
if err := DB.Create(&link).Error; err != nil {
t.Errorf("Failed to insert link: %v", err)
}
}
}
// 验证数据是否正确插入
var links []model.EncounterAnimalLink
if err := DB.Find(&links).Error; err != nil {
t.Errorf("Failed to fetch links: %v", err)
}
expectedLinks := []model.EncounterAnimalLink{
{EncounterId: 10, AnimalId: 4},
{EncounterId: 11, AnimalId: 2},
{EncounterId: 11, AnimalId: 3},
{EncounterId: 13, AnimalId: 4},
{EncounterId: 14, AnimalId: 4},
{EncounterId: 15, AnimalId: 4},
{EncounterId: 16, AnimalId: 4},
{EncounterId: 17, AnimalId: 4},
{EncounterId: 18, AnimalId: 4},
{EncounterId: 19, AnimalId: 4},
{EncounterId: 20, AnimalId: 4},
}
if !reflect.DeepEqual(links, expectedLinks) {
t.Errorf("Expected links: %v, but got: %v", expectedLinks, links)
}
}