package category import ( "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" "sghgogs.com/micro/shopping-service/domain/repository" "sghgogs.com/micro/shopping-service/domain/service" pb "sghgogs.com/micro/shopping-service/proto" "testing" ) func TestCategory(t *testing.T) { address := fmt.Sprintf("%v:%v@(%v:%v)/%v?charset=utf8mb4,utf8&parseTime=True&loc=Local", "root", "xugang131500", "47.56.16.206", 3306, "shopping_wenke") db, err := gorm.Open(mysql.Open(address), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), NamingStrategy: schema.NamingStrategy{ SingularTable: true, }}) if err != nil { t.Fatal(err) } repo := repository.NewRepository(db) newService := service.NewService(repo, "shoppingservice") t.Run("创建", func(t *testing.T) { // Tablet mobile_phone Computer items := []*pb.CreateCategory{ { Name: "Laptops", Description: "笔记本电脑", ParentCategoryId: 1, }, { Name: "Mobile Phones", Description: "手机", ParentCategoryId: 1, }, { Name: "Furniture", Description: "家具", ParentCategoryId: 2, }, { Name: "Home Decor", Description: "家居装饰", ParentCategoryId: 2, }, } categories := pb.CreateCategoryRequest{ Items: items, } err = newService.CreateMultipleCategories(&categories) fmt.Println(err) }) t.Run("获取列表", func(t *testing.T) { categories, err2 := newService.GetAllCategories() fmt.Println(err2) fmt.Println(categories) }) }