card_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package show
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "testing"
  7. "time"
  8. xsql "go-common/library/database/sql"
  9. "github.com/bouk/monkey"
  10. "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestDaoPosRecs(t *testing.T) {
  13. convey.Convey("PosRecs", t, func(ctx convey.C) {
  14. ctx.Convey("When everything is correct", func(ctx convey.C) {
  15. res, err := d.PosRecs(context.Background(), time.Now())
  16. ctx.Convey("Error should be nil, res should not be nil", func(ctx convey.C) {
  17. ctx.So(err, convey.ShouldBeNil)
  18. ctx.So(res, convey.ShouldNotBeNil)
  19. })
  20. })
  21. ctx.Convey("When db.Query gets error", func(ctx convey.C) {
  22. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Query", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (*xsql.Rows, error) {
  23. return nil, fmt.Errorf("db.Query error")
  24. })
  25. defer guard.Unpatch()
  26. _, err := d.PosRecs(context.Background(), time.Now())
  27. ctx.Convey("Error should not be nil", func(ctx convey.C) {
  28. ctx.So(err, convey.ShouldNotBeNil)
  29. })
  30. })
  31. })
  32. }
  33. func TestDaoRecContents(t *testing.T) {
  34. convey.Convey("RecContents", t, func(ctx convey.C) {
  35. ctx.Convey("When everything is correct", func() {
  36. res, aids, err := d.RecContents(context.Background(), time.Now())
  37. ctx.Convey("Error should be nil, res, aids should not be nil", func(ctx convey.C) {
  38. ctx.So(err, convey.ShouldBeNil)
  39. ctx.So(res, convey.ShouldNotBeNil)
  40. ctx.So(aids, convey.ShouldNotBeNil)
  41. })
  42. })
  43. ctx.Convey("When db.Query gets error", func(ctx convey.C) {
  44. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Query", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (*xsql.Rows, error) {
  45. return nil, fmt.Errorf("db.Query error")
  46. })
  47. defer guard.Unpatch()
  48. _, _, err := d.RecContents(context.Background(), time.Now())
  49. ctx.Convey("Error should not be nil", func(ctx convey.C) {
  50. ctx.So(err, convey.ShouldNotBeNil)
  51. })
  52. })
  53. })
  54. }