allowance_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package dao
  2. import (
  3. "context"
  4. "math/rand"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "go-common/app/service/main/coupon/model"
  9. "go-common/library/database/sql"
  10. xtime "go-common/library/time"
  11. "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestDaohitAllowanceInfo(t *testing.T) {
  14. var (
  15. mid = int64(1)
  16. )
  17. convey.Convey("hitAllowanceInfo", t, func(ctx convey.C) {
  18. p1 := hitAllowanceInfo(mid)
  19. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  20. ctx.So(p1, convey.ShouldNotBeNil)
  21. })
  22. })
  23. }
  24. func TestDaohitAllowanceChangeLog(t *testing.T) {
  25. var (
  26. mid = int64(1)
  27. )
  28. convey.Convey("hitAllowanceChangeLog", t, func(ctx convey.C) {
  29. p1 := hitAllowanceChangeLog(mid)
  30. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  31. ctx.So(p1, convey.ShouldNotBeNil)
  32. })
  33. })
  34. }
  35. func TestDaoByStateAndExpireAllowances(t *testing.T) {
  36. var (
  37. c = context.TODO()
  38. mid = int64(1)
  39. state = int8(0)
  40. no = int64(0)
  41. )
  42. convey.Convey("ByStateAndExpireAllowances", t, func(ctx convey.C) {
  43. res, err := d.ByStateAndExpireAllowances(c, mid, state, no)
  44. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  45. ctx.So(err, convey.ShouldBeNil)
  46. ctx.So(res, convey.ShouldNotBeNil)
  47. })
  48. })
  49. }
  50. func TestDaoAllowanceByOrderNO(t *testing.T) {
  51. var (
  52. c = context.TODO()
  53. mid = int64(0)
  54. orderNO = "1"
  55. )
  56. convey.Convey("AllowanceByOrderNO", t, func(ctx convey.C) {
  57. _, err := d.AllowanceByOrderNO(c, mid, orderNO)
  58. ctx.Convey("Then err should be nil.r should not be nil.", func(ctx convey.C) {
  59. ctx.So(err, convey.ShouldBeNil)
  60. })
  61. })
  62. }
  63. func TestDaoUsableAllowances(t *testing.T) {
  64. var (
  65. c = context.TODO()
  66. mid = int64(1)
  67. state = int8(0)
  68. no = int64(0)
  69. )
  70. convey.Convey("UsableAllowances", t, func(ctx convey.C) {
  71. _, err := d.UsableAllowances(c, mid, state, no)
  72. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  73. ctx.So(err, convey.ShouldBeNil)
  74. })
  75. })
  76. }
  77. func TestDaoAllowanceByToken(t *testing.T) {
  78. var (
  79. c = context.TODO()
  80. mid = int64(0)
  81. token = "1"
  82. )
  83. convey.Convey("AllowanceByToken", t, func(ctx convey.C) {
  84. _, err := d.AllowanceByToken(c, mid, token)
  85. ctx.Convey("Then err should be nil.r should not be nil.", func(ctx convey.C) {
  86. ctx.So(err, convey.ShouldBeNil)
  87. })
  88. })
  89. }
  90. func TestDaoUpdateAllowanceCouponInUse(t *testing.T) {
  91. var (
  92. c = context.TODO()
  93. tx = &sql.Tx{}
  94. cp = &model.CouponAllowanceInfo{}
  95. err error
  96. )
  97. convey.Convey("UpdateAllowanceCouponInUse", t, func(ctx convey.C) {
  98. tx, err = d.BeginTran(c)
  99. ctx.So(err, convey.ShouldBeNil)
  100. a, err := d.UpdateAllowanceCouponInUse(c, tx, cp)
  101. ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
  102. ctx.So(err, convey.ShouldBeNil)
  103. ctx.So(a, convey.ShouldNotBeNil)
  104. })
  105. })
  106. }
  107. func TestDaoUpdateAllowanceCouponToUse(t *testing.T) {
  108. var (
  109. c = context.Background()
  110. tx, _ = d.BeginTran(c)
  111. cp = &model.CouponAllowanceInfo{}
  112. )
  113. convey.Convey("UpdateAllowanceCouponToUse ", t, func(ctx convey.C) {
  114. a, err := d.UpdateAllowanceCouponToUse(c, tx, cp)
  115. if err == nil {
  116. if err = tx.Commit(); err != nil {
  117. tx.Rollback()
  118. }
  119. } else {
  120. tx.Rollback()
  121. }
  122. ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
  123. ctx.So(err, convey.ShouldBeNil)
  124. ctx.So(a, convey.ShouldBeGreaterThanOrEqualTo, 0)
  125. })
  126. })
  127. }
  128. func TestDaoUpdateAllowanceCouponToUsed(t *testing.T) {
  129. var (
  130. c = context.Background()
  131. tx, _ = d.BeginTran(c)
  132. cp = &model.CouponAllowanceInfo{}
  133. )
  134. convey.Convey("UpdateAllowanceCouponToUsed ", t, func(ctx convey.C) {
  135. a, err := d.UpdateAllowanceCouponToUsed(c, tx, cp)
  136. if err == nil {
  137. if err = tx.Commit(); err != nil {
  138. tx.Rollback()
  139. }
  140. } else {
  141. tx.Rollback()
  142. }
  143. ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
  144. ctx.So(err, convey.ShouldBeNil)
  145. ctx.So(a, convey.ShouldBeGreaterThanOrEqualTo, 0)
  146. })
  147. })
  148. }
  149. func TestDaoInsertCouponAllowanceHistory(t *testing.T) {
  150. var (
  151. c = context.TODO()
  152. tx = &sql.Tx{}
  153. l = &model.CouponAllowanceChangeLog{
  154. CouponToken: token(),
  155. OrderNO: "1",
  156. }
  157. err error
  158. )
  159. convey.Convey("InsertCouponAllowanceHistory", t, func(ctx convey.C) {
  160. tx, err = d.BeginTran(c)
  161. ctx.So(err, convey.ShouldBeNil)
  162. a, err := d.InsertCouponAllowanceHistory(c, tx, l)
  163. ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
  164. ctx.So(err, convey.ShouldBeNil)
  165. ctx.So(a, convey.ShouldNotBeNil)
  166. })
  167. })
  168. }
  169. func TestDaoCountByAllowanceBranchToken(t *testing.T) {
  170. var (
  171. c = context.TODO()
  172. mid = int64(0)
  173. token = "allowance_test1"
  174. )
  175. convey.Convey("CountByAllowanceBranchToken", t, func(ctx convey.C) {
  176. count, err := d.CountByAllowanceBranchToken(c, mid, token)
  177. ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
  178. ctx.So(err, convey.ShouldBeNil)
  179. ctx.So(count, convey.ShouldNotBeNil)
  180. })
  181. })
  182. }
  183. func TestDaoGetCouponByOrderNo(t *testing.T) {
  184. var (
  185. c = context.TODO()
  186. mid = int64(0)
  187. orderNo = "allowance_test1"
  188. )
  189. convey.Convey("GetCouponByOrderNo ", t, func(ctx convey.C) {
  190. res, err := d.GetCouponByOrderNo(c, mid, orderNo)
  191. ctx.Convey("Then err should be not nil.res should be nil.", func(ctx convey.C) {
  192. ctx.So(err, convey.ShouldNotBeNil)
  193. ctx.So(res, convey.ShouldNotBeNil)
  194. })
  195. })
  196. }
  197. func TestDaoTxAddAllowanceCoupon(t *testing.T) {
  198. var (
  199. c = context.Background()
  200. tx, _ = d.BeginTran(c)
  201. cp = &model.CouponAllowanceInfo{CouponToken: strconv.FormatInt(rand.Int63n(999999), 10)}
  202. )
  203. convey.Convey("TxAddAllowanceCoupon ", t, func(ctx convey.C) {
  204. err := d.TxAddAllowanceCoupon(tx, cp)
  205. if err == nil {
  206. if err = tx.Commit(); err != nil {
  207. tx.Rollback()
  208. }
  209. } else {
  210. tx.Rollback()
  211. }
  212. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  213. ctx.So(err, convey.ShouldBeNil)
  214. })
  215. })
  216. }
  217. func TestDaoBatchAddAllowanceCoupon(t *testing.T) {
  218. var (
  219. c = context.TODO()
  220. tx = &sql.Tx{}
  221. mid = int64(0)
  222. cps = []*model.CouponAllowanceInfo{}
  223. err error
  224. )
  225. convey.Convey("BatchAddAllowanceCoupon", t, func(ctx convey.C) {
  226. cps = append(cps, &model.CouponAllowanceInfo{
  227. CouponToken: token(),
  228. Mid: mid,
  229. State: model.NotUsed,
  230. StartTime: time.Now().Unix(),
  231. ExpireTime: time.Now().AddDate(0, 0, 10).Unix(),
  232. Origin: int64(1),
  233. CTime: xtime.Time(time.Now().Unix()),
  234. BatchToken: "1",
  235. Amount: float64(1),
  236. FullAmount: float64(100),
  237. AppID: int64(1),
  238. })
  239. tx, err = d.BeginTran(c)
  240. ctx.So(err, convey.ShouldBeNil)
  241. _, err = d.BatchAddAllowanceCoupon(c, tx, mid, cps)
  242. ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
  243. ctx.So(err, convey.ShouldBeNil)
  244. })
  245. })
  246. }
  247. func TestDaoAllowanceList(t *testing.T) {
  248. var (
  249. c = context.TODO()
  250. mid = int64(0)
  251. state = int8(0)
  252. no = int64(0)
  253. stime = time.Now()
  254. )
  255. convey.Convey("AllowanceList", t, func(ctx convey.C) {
  256. _, err := d.AllowanceList(c, mid, state, no, stime)
  257. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  258. ctx.So(err, convey.ShouldBeNil)
  259. })
  260. })
  261. }