sidebar_test.go 1006 B

123456789101112131415161718192021222324252627282930313233343536
  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. // Test_SideBar test dao side bar
  13. func TestDaoSideBar(t *testing.T) {
  14. convey.Convey("SidebBar", t, func(ctx convey.C) {
  15. ctx.Convey("When everyting is correct", func(ctx convey.C) {
  16. _, _, err := d.SideBar(context.Background(), time.Now())
  17. ctx.Convey("Error should be nil", func(ctx convey.C) {
  18. ctx.So(err, convey.ShouldBeNil)
  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.SideBar(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. }