order_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "strings"
  7. "testing"
  8. "time"
  9. "go-common/app/service/openplatform/ticket-sales/api/grpc/type"
  10. "go-common/app/service/openplatform/ticket-sales/model"
  11. "go-common/app/service/openplatform/ticket-sales/model/consts"
  12. "github.com/smartystreets/goconvey/convey"
  13. )
  14. var (
  15. oids []int64
  16. )
  17. func TestTxInsertOrders(t *testing.T) {
  18. convey.Convey("OrderID", t, func() {
  19. oids, _ = d.OrderID(context.TODO(), 3)
  20. convey.So(len(oids), convey.ShouldEqual, 3)
  21. })
  22. convey.Convey("TxInsertOrders", t, func() {
  23. o1 := &model.OrderMain{
  24. OrderID: oids[0],
  25. UID: "TEST",
  26. OrderType: 1,
  27. ItemID: 1,
  28. ItemInfo: &_type.OrderItemInfo{
  29. Name: "TEST",
  30. Img: "//img.bilibili.com",
  31. ScreenID: 1,
  32. ScreenName: "TEST",
  33. ScreenType: 1,
  34. DeliverType: 1,
  35. ExpressFee: 1,
  36. VIPExpressFree: 1,
  37. VerID: 1,
  38. },
  39. Count: 1,
  40. TotalMoney: 2,
  41. ExpressFee: 1,
  42. PayMoney: 2,
  43. PayChannel: 1,
  44. PayTime: time.Now().Unix(),
  45. Source: "TEST",
  46. Status: 1,
  47. SubStatus: 1,
  48. RefundStatus: 1,
  49. }
  50. tx, _ := d.BeginTx(context.TODO())
  51. c, _ := d.TxInsertOrders(tx, []*model.OrderMain{o1})
  52. convey.So(c, convey.ShouldEqual, 1)
  53. o2, o3 := &model.OrderMain{}, &model.OrderMain{}
  54. *o2 = *o1
  55. o2.OrderID = oids[1]
  56. o2.ItemID = 2
  57. *o3 = *o1
  58. o3.OrderID = oids[2]
  59. o3.ItemID = 2
  60. o3.RefundStatus = consts.RefundStatusPtRefunded
  61. c, _ = d.TxInsertOrders(tx, []*model.OrderMain{o2, o3})
  62. convey.So(c, convey.ShouldEqual, 2)
  63. tx.Commit()
  64. })
  65. }
  66. func TestOrders(t *testing.T) {
  67. convey.Convey("UserOrders", t, func() {
  68. orders, _ := d.CacheOrders(context.TODO(), &model.OrderMainQuerier{UID: "TEST", OrderID: oids[:1]})
  69. convey.So(len(orders), convey.ShouldEqual, 0)
  70. orders, _ = d.Orders(context.TODO(), &model.OrderMainQuerier{UID: "TEST"})
  71. convey.So(len(orders), convey.ShouldEqual, 3)
  72. for i := 0; i < 10; i++ {
  73. orders, _ = d.CacheOrders(context.TODO(), &model.OrderMainQuerier{UID: "TEST"})
  74. if len(orders) > 0 {
  75. break
  76. }
  77. time.Sleep(100 * time.Millisecond)
  78. }
  79. convey.So(len(orders), convey.ShouldEqual, 3)
  80. for i := 0; i < 10; i++ {
  81. orders, _ = d.CacheOrders(context.TODO(), &model.OrderMainQuerier{UID: "TEST", OrderID: oids[1:2]})
  82. if len(orders) > 0 {
  83. break
  84. }
  85. time.Sleep(100 * time.Millisecond)
  86. }
  87. convey.So(len(orders), convey.ShouldEqual, 1)
  88. orders, _ = d.Orders(context.TODO(), &model.OrderMainQuerier{UID: "TEST", ItemID: 2, Status: []int16{1}})
  89. convey.So(len(orders), convey.ShouldEqual, 2)
  90. })
  91. }
  92. func TestOrderCount(t *testing.T) {
  93. convey.Convey("OrderCount", t, func() {
  94. cnt, _ := d.CacheOrderCount(context.TODO(), &model.OrderMainQuerier{UID: "TEST"})
  95. convey.So(cnt, convey.ShouldEqual, 0)
  96. cnt, _ = d.OrderCount(context.TODO(), &model.OrderMainQuerier{UID: "TEST"})
  97. convey.So(cnt, convey.ShouldEqual, 3)
  98. for i := 0; i < 10; i++ {
  99. cnt, _ = d.CacheOrderCount(context.TODO(), &model.OrderMainQuerier{UID: "TEST"})
  100. if cnt > 0 {
  101. break
  102. }
  103. time.Sleep(100 * time.Millisecond)
  104. }
  105. convey.So(cnt, convey.ShouldEqual, 3)
  106. })
  107. }
  108. func TestTxInsertOrderDetails(t *testing.T) {
  109. convey.Convey("TxInsertOrderDetails", t, func() {
  110. dt1 := &model.OrderDetail{
  111. OrderID: oids[0],
  112. Buyer: "TEST",
  113. Tel: "13800138000",
  114. PersonalID: "342921",
  115. ExpressCO: "shunfeng",
  116. ExpressNO: "000",
  117. Remark: "TEST",
  118. DeviceType: 1,
  119. IP: net.ParseIP("127.0.0.1"),
  120. DeliverDetail: &_type.OrderDeliver{
  121. AddrID: 1,
  122. Name: "张三",
  123. Tel: "13810559189",
  124. Addr: "北京市",
  125. },
  126. Detail: &_type.OrderExtra{
  127. AutoRecvTime: time.Now().Unix(),
  128. DelayRecvTimes: 1,
  129. },
  130. }
  131. dt2 := &model.OrderDetail{}
  132. *dt2 = *dt1
  133. dt2.OrderID = oids[1]
  134. tx, _ := d.BeginTx(context.TODO())
  135. c, _ := d.TxInsertOrderDetails(tx, []*model.OrderDetail{dt1, dt2})
  136. tx.Commit()
  137. convey.So(c, convey.ShouldEqual, 2)
  138. })
  139. }
  140. func TestOrderDetails(t *testing.T) {
  141. convey.Convey("OrderDetails", t, func() {
  142. dts, _ := d.CacheOrderDetails(context.TODO(), oids)
  143. convey.So(len(dts), convey.ShouldEqual, 0)
  144. dts, _ = d.OrderDetails(context.TODO(), oids)
  145. convey.So(len(dts), convey.ShouldEqual, 2)
  146. for i := 0; i < 10; i++ {
  147. dts, _ = d.CacheOrderDetails(context.TODO(), oids)
  148. if len(dts) > 0 {
  149. break
  150. }
  151. time.Sleep(100 * time.Millisecond)
  152. }
  153. convey.So(len(dts), convey.ShouldEqual, 3)
  154. dts, _ = d.OrderDetails(context.TODO(), oids)
  155. convey.So(len(dts), convey.ShouldEqual, 2)
  156. })
  157. }
  158. func TestTxInsertOrderSKUs(t *testing.T) {
  159. convey.Convey("TxInsertOrderSKUs", t, func() {
  160. sku1 := &model.OrderSKU{
  161. OrderID: oids[0],
  162. SKUID: 1,
  163. Count: 1,
  164. OriginPrice: 1,
  165. Price: 1,
  166. TicketType: 1,
  167. Discounts: &_type.OrderSKUDiscounts{
  168. Platform: map[int32]int64{1: 1},
  169. },
  170. }
  171. sku2 := &model.OrderSKU{
  172. OrderID: oids[0],
  173. SKUID: 2,
  174. Count: 1,
  175. OriginPrice: 2,
  176. Price: 2,
  177. TicketType: 1,
  178. }
  179. tx, _ := d.BeginTx(context.TODO())
  180. cnt, _ := d.TxInsertOrderSKUs(tx, []*model.OrderSKU{sku1, sku2})
  181. tx.Commit()
  182. convey.So(cnt, convey.ShouldEqual, 2)
  183. })
  184. }
  185. func TestOrderSKUs(t *testing.T) {
  186. convey.Convey("OrderSKUs", t, func() {
  187. skus, _ := d.CacheOrderSKUs(context.TODO(), oids)
  188. convey.So(len(skus), convey.ShouldEqual, 0)
  189. skus, _ = d.OrderSKUs(context.TODO(), oids)
  190. convey.So(len(skus), convey.ShouldEqual, 1)
  191. for i := 0; i < 10; i++ {
  192. skus, _ = d.CacheOrderSKUs(context.TODO(), oids)
  193. if len(skus) > 0 {
  194. break
  195. }
  196. time.Sleep(100 * time.Millisecond)
  197. }
  198. convey.So(len(skus), convey.ShouldEqual, 3)
  199. skus, _ = d.OrderSKUs(context.TODO(), oids)
  200. convey.So(len(skus), convey.ShouldEqual, 1)
  201. })
  202. }
  203. func TestOrderPayCharges(t *testing.T) {
  204. convey.Convey("OrderPayCharges", t, func() {
  205. var oid int64 = 1519626683221383
  206. chs, _ := d.OrderPayCharges(context.TODO(), []int64{oid})
  207. convey.So(chs, convey.ShouldContainKey, oid)
  208. })
  209. }
  210. func TestGetBoughtCount(t *testing.T) {
  211. convey.Convey("GetBoughtCount", t, func() {
  212. c, _ := d.RawBoughtCount(context.TODO(), "TEST", 2, nil)
  213. convey.So(c, convey.ShouldEqual, 2)
  214. c, _ = d.RawBoughtCount(context.TODO(), "TEST", 1, []int64{1})
  215. convey.So(c, convey.ShouldEqual, 1)
  216. })
  217. }
  218. //删除测试数据
  219. func TestDelOrders(t *testing.T) {
  220. tx, _ := d.BeginTx(ctx)
  221. tables := []string{"order_main", "order_detail", "order_sku", "order_pay_charge", "ticket"}
  222. w := strings.Repeat(",?", len(oids))[1:]
  223. a := make([]interface{}, len(oids))
  224. for k, v := range oids {
  225. a[k] = v
  226. }
  227. for _, t := range tables {
  228. f := "order_id"
  229. if t == "ticket" {
  230. f = "oid"
  231. }
  232. _, err := tx.Exec(fmt.Sprintf("DELETE FROM `%s` WHERE %s IN (%s)", t, f, w), a...)
  233. if err != nil {
  234. tx.Rollback()
  235. return
  236. }
  237. }
  238. tx.Commit()
  239. d.DelCacheOrders(context.TODO(), &model.OrderMainQuerier{UID: "TEST"})
  240. }