metal_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package dao
  2. import (
  3. . "github.com/smartystreets/goconvey/convey"
  4. "go-common/library/ecode"
  5. "testing"
  6. )
  7. func TestDao_GetMetal(t *testing.T) {
  8. Convey("GetMetal", t, func() {
  9. once.Do(startService)
  10. var uid int64 = 1
  11. metal, err := d.GetMetal(ctx, uid)
  12. So(err, ShouldBeNil)
  13. So(metal, ShouldBeGreaterThanOrEqualTo, 0)
  14. })
  15. }
  16. func TestDao_ModifyMetal(t *testing.T) {
  17. Convey("ModifyMetal", t, func() {
  18. once.Do(startService)
  19. var uid int64 = 1
  20. metal, err := d.GetMetal(ctx, uid)
  21. So(err, ShouldBeNil)
  22. if metal < 10 {
  23. d.ModifyMetal(ctx, uid, 10, 0, nil)
  24. }
  25. var coins int64 = -5
  26. success, code, err := d.ModifyMetal(ctx, uid, coins, 500, "ut")
  27. So(code, ShouldEqual, 0)
  28. So(success, ShouldEqual, true)
  29. So(err, ShouldBeNil)
  30. coins = 0 - coins
  31. success, code, err = d.ModifyMetal(ctx, uid, coins, 0, nil)
  32. So(code, ShouldEqual, 0)
  33. So(success, ShouldEqual, true)
  34. So(err, ShouldBeNil)
  35. nmetal, err := d.GetMetal(ctx, uid)
  36. So(err, ShouldBeNil)
  37. So(metal, ShouldEqual, nmetal)
  38. })
  39. Convey("ModifyMetal not enough", t, func() {
  40. once.Do(startService)
  41. var uid int64 = 1
  42. metal, err := d.GetMetal(ctx, uid)
  43. So(err, ShouldBeNil)
  44. coins := 0 - int64(metal+1)
  45. _, _, err = d.ModifyMetal(ctx, uid, coins, 400, nil)
  46. So(err, ShouldEqual, ecode.CoinNotEnough)
  47. })
  48. }