category_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package category
  2. import (
  3. "fmt"
  4. "gorm.io/driver/mysql"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/logger"
  7. "gorm.io/gorm/schema"
  8. "sghgogs.com/micro/shopping-service/domain/repository"
  9. "sghgogs.com/micro/shopping-service/domain/service"
  10. pb "sghgogs.com/micro/shopping-service/proto"
  11. "testing"
  12. )
  13. func TestCategory(t *testing.T) {
  14. address := fmt.Sprintf("%v:%v@(%v:%v)/%v?charset=utf8mb4,utf8&parseTime=True&loc=Local", "root", "xugang131500", "47.56.16.206", 3306, "shopping_wenke")
  15. db, err := gorm.Open(mysql.Open(address), &gorm.Config{
  16. Logger: logger.Default.LogMode(logger.Info),
  17. NamingStrategy: schema.NamingStrategy{
  18. SingularTable: true,
  19. }})
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. repo := repository.NewRepository(db)
  24. newService := service.NewService(repo, "shoppingservice")
  25. t.Run("创建", func(t *testing.T) {
  26. // Tablet mobile_phone Computer
  27. items := []*pb.CreateCategory{
  28. {
  29. Name: "Laptops",
  30. Description: "笔记本电脑",
  31. ParentCategoryId: 1,
  32. },
  33. {
  34. Name: "Mobile Phones",
  35. Description: "手机",
  36. ParentCategoryId: 1,
  37. },
  38. {
  39. Name: "Furniture",
  40. Description: "家具",
  41. ParentCategoryId: 2,
  42. },
  43. {
  44. Name: "Home Decor",
  45. Description: "家居装饰",
  46. ParentCategoryId: 2,
  47. },
  48. }
  49. categories := pb.CreateCategoryRequest{
  50. Items: items,
  51. }
  52. err = newService.CreateMultipleCategories(&categories)
  53. fmt.Println(err)
  54. })
  55. t.Run("获取列表", func(t *testing.T) {
  56. categories, err2 := newService.GetAllCategories()
  57. fmt.Println(err2)
  58. fmt.Println(categories)
  59. })
  60. }