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()) } }