bindbox-game/internal/api/app/store_test.go
邹方成 45815bfb7d chore: 清理无用文件与优化代码结构
refactor(utils): 修复密码哈希比较逻辑错误
feat(user): 新增按状态筛选优惠券接口
docs: 添加虚拟发货与任务中心相关文档
fix(wechat): 修正Code2Session上下文传递问题
test: 补充订单折扣与积分转换测试用例
build: 更新配置文件与构建脚本
style: 清理多余的空行与注释
2025-12-18 17:35:55 +08:00

85 lines
3.0 KiB
Go

package app
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/logger"
"bindbox-game/internal/repository/mysql"
// no model import for sqlite DDL
)
func Test_ListStoreItemsForApp_Kinds(t *testing.T) {
repo, err := mysql.NewSQLiteRepoForTest()
if err != nil {
t.Fatal(err)
}
// create sqlite-compatible tables
if err := repo.GetDbW().Exec(`CREATE TABLE products (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, category_id INTEGER, images_json TEXT, price INTEGER NOT NULL, stock INTEGER NOT NULL, sales INTEGER NOT NULL, status INTEGER NOT NULL, deleted_at TEXT)`).Error; err != nil {
t.Fatal(err)
}
if err := repo.GetDbW().Exec(`CREATE TABLE system_item_cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, status INTEGER NOT NULL, price INTEGER NOT NULL)`).Error; err != nil {
t.Fatal(err)
}
if err := repo.GetDbW().Exec(`CREATE TABLE system_coupons (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, scope_type INTEGER, discount_type INTEGER NOT NULL, discount_value INTEGER NOT NULL, min_spend INTEGER NOT NULL, status INTEGER NOT NULL)`).Error; err != nil {
t.Fatal(err)
}
// seed rows
if err := repo.GetDbW().Exec(`INSERT INTO products (name,images_json,price,stock,sales,status) VALUES ('P1','["a.jpg"]',1000,5,0,1)`).Error; err != nil {
t.Fatal(err)
}
if err := repo.GetDbW().Exec(`INSERT INTO system_item_cards (name,status,price) VALUES ('IC1',1,500)`).Error; err != nil {
t.Fatal(err)
}
if err := repo.GetDbW().Exec(`INSERT INTO system_coupons (name,scope_type,discount_type,discount_value,min_spend,status) VALUES ('C1',1,1,300,1000,1)`).Error; err != nil {
t.Fatal(err)
}
// logger & mux
lg, err := logger.NewCustomLogger(nil, logger.WithOutputInConsole())
if err != nil {
t.Fatal(err)
}
mux, err := core.New(lg)
if err != nil {
t.Fatal(err)
}
mux.Group("/api/app").GET("/store/items", NewStore(lg, repo).ListStoreItemsForApp())
// product
rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/app/store/items?kind=product&page=1&page_size=10", bytes.NewBufferString(""))
mux.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("product code=%d body=%s", rr.Code, rr.Body.String())
}
var rsp map[string]interface{}
if err := json.Unmarshal([]byte(rr.Body.String()), &rsp); err != nil {
t.Fatal(err)
}
if rsp["total"].(float64) < 1 {
t.Fatalf("expect product total>=1 got %v", rsp["total"])
}
// item_card
rr2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/api/app/store/items?kind=item_card&page=1&page_size=10", bytes.NewBufferString(""))
mux.ServeHTTP(rr2, req2)
if rr2.Code != http.StatusOK {
t.Fatalf("item_card code=%d body=%s", rr2.Code, rr2.Body.String())
}
// coupon
rr3 := httptest.NewRecorder()
req3, _ := http.NewRequest("GET", "/api/app/store/items?kind=coupon&page=1&page_size=10", bytes.NewBufferString(""))
mux.ServeHTTP(rr3, req3)
if rr3.Code != http.StatusOK {
t.Fatalf("coupon code=%d body=%s", rr3.Code, rr3.Body.String())
}
}