166 lines
4.2 KiB
Go
166 lines
4.2 KiB
Go
package propsstore
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"chatapp3-golang/internal/model"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestMapBySourceIDsReturnsLatestActiveCommodity(t *testing.T) {
|
|
db := newPropsStoreTestDB(t)
|
|
now := time.Date(2026, 5, 18, 12, 0, 0, 0, time.UTC)
|
|
older := now.Add(-time.Hour)
|
|
delFalse := false
|
|
delTrue := true
|
|
shelfFalse := false
|
|
shelfTrue := true
|
|
discount := 0.0
|
|
|
|
mustCreatePropsStore(t, db, model.PropsCommodityStore{
|
|
ID: 1,
|
|
SysOrigin: "LIKEI",
|
|
SourceID: 100,
|
|
PropsType: "AVATAR_FRAME",
|
|
CurrencyTypes: "FREE",
|
|
ValidDays: "1",
|
|
Discount: &discount,
|
|
ShelfStatus: &shelfFalse,
|
|
Sort: 1,
|
|
Del: nil,
|
|
UpdateTime: &older,
|
|
})
|
|
mustCreatePropsStore(t, db, model.PropsCommodityStore{
|
|
ID: 2,
|
|
SysOrigin: "LIKEI",
|
|
SourceID: 100,
|
|
PropsType: "AVATAR_FRAME",
|
|
CurrencyTypes: "GOLD",
|
|
ValidDays: "7",
|
|
Discount: &discount,
|
|
ShelfStatus: &shelfTrue,
|
|
Sort: 2,
|
|
Del: &delFalse,
|
|
UpdateTime: &now,
|
|
})
|
|
mustCreatePropsStore(t, db, model.PropsCommodityStore{
|
|
ID: 3,
|
|
SysOrigin: "LIKEI",
|
|
SourceID: 200,
|
|
PropsType: "AVATAR_FRAME",
|
|
CurrencyTypes: "GOLD",
|
|
ValidDays: "7",
|
|
Discount: &discount,
|
|
ShelfStatus: &shelfTrue,
|
|
Sort: 1,
|
|
Del: &delTrue,
|
|
UpdateTime: &now,
|
|
})
|
|
|
|
service := NewService(db, nil)
|
|
got, err := service.MapBySourceIDs(context.Background(), MapBySourceIDsRequest{
|
|
SysOrigin: "likei",
|
|
SourceIDs: []flexibleInt64{
|
|
flexibleInt64(100),
|
|
flexibleInt64(200),
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("MapBySourceIDs() error = %v", err)
|
|
}
|
|
if len(got) != 1 {
|
|
t.Fatalf("MapBySourceIDs() len = %d, want 1", len(got))
|
|
}
|
|
item, ok := got["100"]
|
|
if !ok {
|
|
t.Fatalf("MapBySourceIDs() missing source 100: %#v", got)
|
|
}
|
|
if item.ID != "2" || item.CurrencyTypes != "GOLD" || item.ShelfStatus == nil || !*item.ShelfStatus {
|
|
t.Fatalf("MapBySourceIDs() item = %#v", item)
|
|
}
|
|
}
|
|
|
|
func TestSaveCommodityUpdatesExistingAndRestoresDelFalse(t *testing.T) {
|
|
db := newPropsStoreTestDB(t)
|
|
now := time.Date(2026, 5, 18, 12, 0, 0, 0, time.UTC)
|
|
delTrue := true
|
|
shelfFalse := false
|
|
discount := 0.0
|
|
mustCreatePropsStore(t, db, model.PropsCommodityStore{
|
|
ID: 1,
|
|
SysOrigin: "LIKEI",
|
|
SourceID: 100,
|
|
PropsType: "AVATAR_FRAME",
|
|
CurrencyTypes: "FREE",
|
|
ValidDays: "1",
|
|
Discount: &discount,
|
|
ShelfStatus: &shelfFalse,
|
|
Sort: 1,
|
|
Label: "old",
|
|
Del: &delTrue,
|
|
UpdateTime: &now,
|
|
})
|
|
|
|
shelfTrue := true
|
|
service := NewService(db, nil)
|
|
service.now = func() time.Time { return now.Add(time.Hour) }
|
|
got, err := service.SaveCommodity(context.Background(), SaveCommodityRequest{
|
|
SysOrigin: "LIKEI",
|
|
SourceID: flexibleInt64(100),
|
|
PropsType: "AVATAR_FRAME",
|
|
CurrencyTypes: "GOLD",
|
|
ValidDays: "7",
|
|
Discount: &discount,
|
|
ShelfStatus: &shelfTrue,
|
|
Sort: 0,
|
|
Label: "",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("SaveCommodity() error = %v", err)
|
|
}
|
|
if got.ID != "1" || got.CurrencyTypes != "GOLD" || got.Label != "" {
|
|
t.Fatalf("SaveCommodity() = %#v", got)
|
|
}
|
|
|
|
var count int64
|
|
if err := db.Model(&model.PropsCommodityStore{}).Count(&count).Error; err != nil {
|
|
t.Fatalf("Count() error = %v", err)
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("row count = %d, want 1", count)
|
|
}
|
|
var row model.PropsCommodityStore
|
|
if err := db.First(&row, "id = ?", int64(1)).Error; err != nil {
|
|
t.Fatalf("First() error = %v", err)
|
|
}
|
|
if row.Del == nil || *row.Del {
|
|
t.Fatalf("row.Del = %v, want false", row.Del)
|
|
}
|
|
if row.ShelfStatus == nil || !*row.ShelfStatus {
|
|
t.Fatalf("row.ShelfStatus = %v, want true", row.ShelfStatus)
|
|
}
|
|
}
|
|
|
|
func newPropsStoreTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.PropsCommodityStore{}); err != nil {
|
|
t.Fatalf("auto migrate: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func mustCreatePropsStore(t *testing.T, db *gorm.DB, row model.PropsCommodityStore) {
|
|
t.Helper()
|
|
if err := db.Create(&row).Error; err != nil {
|
|
t.Fatalf("create props store: %v", err)
|
|
}
|
|
}
|