作者 陈志颖

test:修改订单同步测试

@@ -21,7 +21,7 @@ type OrderBestshop struct { @@ -21,7 +21,7 @@ type OrderBestshop struct {
21 BuyerAddress string 21 BuyerAddress string
22 //买家备注 22 //买家备注
23 BuyerRemark string 23 BuyerRemark string
24 - // 24 + //买家id
25 BuyerId int64 25 BuyerId int64
26 //订单总数 26 //订单总数
27 OrderCount int 27 OrderCount int
@@ -31,9 +31,14 @@ type OrderBestshop struct { @@ -31,9 +31,14 @@ type OrderBestshop struct {
31 DeliveryTime string 31 DeliveryTime string
32 //创建时间 32 //创建时间
33 CreateTime time.Time 33 CreateTime time.Time
34 - PartnerId int64 34 + //合伙人id
  35 + PartnerId int64
35 //是否将数据同步到 order_base ,order_good 36 //是否将数据同步到 order_base ,order_good
36 - IsCopy bool `pg:",use_zero"` 37 + IsCopy bool `pg:",use_zero"`
  38 + //公司id
37 CompanyId int64 39 CompanyId int64
  40 + //订单区域
38 OrderArea string 41 OrderArea string
  42 + //微信小程序id
  43 + WxAppletId string
39 } 44 }
@@ -44,6 +44,7 @@ func (respository OrderBestshopRepository) transformPgModelToDomainModel(orderMo @@ -44,6 +44,7 @@ func (respository OrderBestshopRepository) transformPgModelToDomainModel(orderMo
44 IsCopy: orderModel.IsCopy, 44 IsCopy: orderModel.IsCopy,
45 CompanyId: orderModel.CompanyId, 45 CompanyId: orderModel.CompanyId,
46 OrderArea: orderModel.OrderArea, 46 OrderArea: orderModel.OrderArea,
  47 + WxAppletId: orderModel.WxAppletId,
47 }, nil 48 }, nil
48 } 49 }
49 50
@@ -67,6 +68,7 @@ func (respository OrderBestshopRepository) Add(order *domain.OrderBestShop) erro @@ -67,6 +68,7 @@ func (respository OrderBestshopRepository) Add(order *domain.OrderBestShop) erro
67 IsCopy: order.IsCopy, 68 IsCopy: order.IsCopy,
68 CompanyId: order.CompanyId, 69 CompanyId: order.CompanyId,
69 OrderArea: order.OrderArea, 70 OrderArea: order.OrderArea,
  71 + WxAppletId: order.WxAppletId,
70 } 72 }
71 _, err := tx.Model(&m).Insert() 73 _, err := tx.Model(&m).Insert()
72 order.Id = m.Id 74 order.Id = m.Id
@@ -94,6 +96,7 @@ func (respository OrderBestshopRepository) Edit(order *domain.OrderBestShop) err @@ -94,6 +96,7 @@ func (respository OrderBestshopRepository) Edit(order *domain.OrderBestShop) err
94 IsCopy: order.IsCopy, 96 IsCopy: order.IsCopy,
95 CompanyId: order.CompanyId, 97 CompanyId: order.CompanyId,
96 OrderArea: order.OrderArea, 98 OrderArea: order.OrderArea,
  99 + WxAppletId: order.WxAppletId,
97 } 100 }
98 _, err := tx.Model(&m).Where("id=?", order.Id).Update() 101 _, err := tx.Model(&m).Where("id=?", order.Id).Update()
99 order.Id = m.Id 102 order.Id = m.Id
@@ -27,13 +27,13 @@ func DataFromXiangMi(message *sarama.ConsumerMessage) error { @@ -27,13 +27,13 @@ func DataFromXiangMi(message *sarama.ConsumerMessage) error {
27 // TODO 使用小程序id作为module 27 // TODO 使用小程序id作为module
28 dataAction := msgData.Module + "/" + msgData.Action 28 dataAction := msgData.Module + "/" + msgData.Action
29 switch dataAction { 29 switch dataAction {
30 - case "xiangmi.order/ship":  
31 - err = syncBestshopOrder(msgData.Data)  
32 - if err != nil {  
33 - e := fmt.Errorf("[Consumer][SyncBestshopOrder] %s", err)  
34 - return e  
35 - }  
36 - // TODO 统一操作小程序订单 30 + //case "xiangmi.order/ship":
  31 + // err = syncBestshopOrder(msgData.Data)
  32 + // if err != nil {
  33 + // e := fmt.Errorf("[Consumer][SyncBestshopOrder] %s", err)
  34 + // return e
  35 + // }
  36 + // TODO 统一操作小程序订单
37 case "wxapplet.order/ship": 37 case "wxapplet.order/ship":
38 err = syncBestshopOrder(msgData.Data) 38 err = syncBestshopOrder(msgData.Data)
39 if err != nil { 39 if err != nil {
  1 +/**
  2 + @author: stevechan
  3 + @date: 2021/3/11
  4 + @note:
  5 +**/
  6 +
  7 +package configs
  8 +
  9 +type MqConfig struct {
  10 + Topics []string `json:"topics"`
  11 + Servers []string `json:"servers"`
  12 + ConsumerId string `json:"consumerGroup"`
  13 +}
  14 +
  15 +var Cfg = MqConfig{
  16 + Topics: []string{"xiangmi_project_dev"},
  17 + Servers: []string{"127.0.0.1:9092"},
  18 + ConsumerId: "test",
  19 +}
  1 +/**
  2 + @author: stevechan
  3 + @date: 2021/3/11
  4 + @note:
  5 +**/
  6 +
  7 +package sync_order
  8 +
  9 +import (
  10 + "encoding/json"
  11 + "fmt"
  12 + "github.com/Shopify/sarama"
  13 + "github.com/gavv/httpexpect"
  14 + "github.com/go-pg/pg"
  15 + pg2 "github.com/go-pg/pg/v10"
  16 + . "github.com/onsi/ginkgo"
  17 + . "github.com/onsi/gomega"
  18 + pG "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg"
  19 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/test/integration/consumer/sync_order/configs"
  20 + "net/http"
  21 + "strconv"
  22 + "time"
  23 +)
  24 +
  25 +var (
  26 + cfg *configs.MqConfig
  27 + producer sarama.SyncProducer
  28 +)
  29 +
  30 +func produce(topic string, key string, content interface{}) error {
  31 + data, _ := json.Marshal(content)
  32 +
  33 + msg := &sarama.ProducerMessage{
  34 + Topic: topic,
  35 + Key: sarama.StringEncoder(key),
  36 + Value: sarama.ByteEncoder(data),
  37 + Timestamp: time.Now(),
  38 + }
  39 +
  40 + _, _, err2 := producer.SendMessage(msg)
  41 + if err2 != nil {
  42 + msg1 := fmt.Sprintf("Send Error topic: %v. key: %v. content: %v", topic, key, content)
  43 + fmt.Println(msg1)
  44 + return err2
  45 + }
  46 +
  47 + fmt.Printf("Send OK topic:%s key:%s value:%s\n", topic, key, content)
  48 +
  49 + return nil
  50 +}
  51 +
  52 +var _ = Describe("同步小程序订单", func() {
  53 + var companyId int64
  54 + var partnerId int64
  55 + BeforeEach(func() {
  56 + fmt.Print("init kafka producer, it may take a few seconds to init the connection\n")
  57 + var err error
  58 + cfg = &configs.Cfg
  59 + mqConfig := sarama.NewConfig()
  60 + mqConfig.Producer.Return.Successes = true
  61 + mqConfig.Version = sarama.V0_10_2_1
  62 + if err = mqConfig.Validate(); err != nil {
  63 + msg := fmt.Sprintf("Kafka producer config invalidate. config: %v. err: %v", *cfg, err)
  64 + fmt.Println(msg)
  65 + panic(msg)
  66 + }
  67 + producer, err = sarama.NewSyncProducer(cfg.Servers, mqConfig)
  68 + if err != nil {
  69 + msg := fmt.Sprintf("Kafak producer create fail. err: %v", err)
  70 + fmt.Println(msg)
  71 + panic(msg)
  72 + }
  73 +
  74 + // 新增公司
  75 + _, err2 := pG.DB.QueryOne(
  76 + pg.Scan(&companyId),
  77 + "INSERT INTO company (name, phone, logo, admin_company, status, create_at, update_at, delete_at, remarks, enable, abbreviation, applets) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
  78 + "测试公司", "13162672794", "http://suplus-file-dev.fjmaimaimai.com/upload/image/2020091709304822611.jpg", 431, Default, "2020-09-17 09:30:05.588579+08", "2020-09-17 09:30:05.588579+08", pg2.NullTime{}, "", "测试", 1, "公司", []map[string]interface{}{{"id": "gh_18eb644002fb", "url": "pages/index/index", "name": "海鲜干货啦啦啦啦啦", "imageUrl": ""}})
  79 + Expect(err2).NotTo(HaveOccurred())
  80 +
  81 + // 新增合伙人
  82 + _, err3 := pG.DB.QueryOne(
  83 + pg.Scan(&partnerId),
  84 + "INSERT INTO partner_info (partner_name, account, password, status, partner_category, region_info, cooperate_time, create_at, update_at, salesman, partner_category_infos, company_id, remark) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
  85 + "test_partner", "19121619631", "7c4a8d09ca3762af61e59520943dc26494f8941b", 1, 1, map[string]interface{}{"regionName": "123"}, "2020-09-01 00:00:00+08", "2020-09-01 00:00:00+08", "2020-09-01 00:00:00+08", []map[string]interface{}{{"name": "123", "telephone": "13322223333"}}, []map[string]interface{}{{"id": 2, "code": "001"}}, 1, "测试合伙人")
  86 + Expect(err3).NotTo(HaveOccurred())
  87 +
  88 + key := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
  89 + value := map[string]interface{}{
  90 + "module": "wxapplet.order",
  91 + "action": "ship",
  92 + "data": map[string]interface{}{
  93 + "orderCode": "1234567895",
  94 + "orderCount": 1,
  95 + "orderAmount": 2,
  96 + "orderTime": "2021-03-05 12:01:54",
  97 + "orderState": 1,
  98 + "deliveryState": 1,
  99 + "buyerName": "陈志颖",
  100 + "buyerPhone": "19121619631",
  101 + "buyerAddress": "福建省 福州市 仓山区",
  102 + "buyerRemark": "不辣",
  103 + "buyerId": 123,
  104 + "partnerId": partnerId,
  105 + "goods": []map[string]interface{}{
  106 + {
  107 + "sn": "wertyuf",
  108 + "id": 1,
  109 + "bn": "a;slkdjgaj",
  110 + "name": "地瓜",
  111 + "price": 2,
  112 + "nums": 1,
  113 + "amount": 2,
  114 + },
  115 + },
  116 + "companyId": companyId,
  117 + "deliveryTime": "2021-03-05 12:01:54",
  118 + "orderArea": "福建省 福州市 台江区",
  119 + "wxAppletId": "gh_18eb644002fb",
  120 + },
  121 + }
  122 + err4 := produce(cfg.Topics[0], key, value)
  123 + Expect(err4).NotTo(HaveOccurred())
  124 + })
  125 + Describe("提交小程序订单消息", func() {
  126 + Context("提交正确的小程序订单", func() {
  127 + It("返回正确的订单数据", func() {
  128 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  129 + httpExpect.GET("").
  130 + Expect().
  131 + Status(http.StatusOK).
  132 + JSON().
  133 + Object().
  134 + ContainsKey("code").ValueEqual("code", 0)
  135 + })
  136 + })
  137 + })
  138 + AfterEach(func() {
  139 + _, err1 := pG.DB.Exec("DELETE FROM order_bestshop WHERE true")
  140 + Expect(err1).NotTo(HaveOccurred())
  141 + _, err2 := pG.DB.Exec("DELETE FROM company WHERE true")
  142 + Expect(err2).NotTo(HaveOccurred())
  143 + _, err3 := pG.DB.Exec("DELETE FROM partner_info WHERE true")
  144 + Expect(err3).NotTo(HaveOccurred())
  145 + })
  146 +})
  1 +/**
  2 + @author: stevechan
  3 + @date: 2021/3/11
  4 + @note:
  5 +**/
  6 +
  7 +package sync_order
  8 +
  9 +import (
  10 + "github.com/astaxie/beego"
  11 + "net/http"
  12 + "net/http/httptest"
  13 + "testing"
  14 +
  15 + . "github.com/onsi/ginkgo"
  16 + . "github.com/onsi/gomega"
  17 + _ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg"
  18 + _ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego"
  19 +)
  20 +
  21 +func TestColumnSetting(t *testing.T) {
  22 + RegisterFailHandler(Fail)
  23 + RunSpecs(t, "Kafka Consumer Sync Order Correlations Test Case Suite")
  24 +}
  25 +
  26 +var handler http.Handler
  27 +var server *httptest.Server
  28 +
  29 +// 初始化kafka
  30 +var _ = BeforeSuite(func() {
  31 + handler = beego.BeeApp.Handlers
  32 + server = httptest.NewServer(handler)
  33 +})
  34 +
  35 +var _ = AfterSuite(func() {
  36 + server.Close()
  37 +})