audit_test.go 1018 B

123456789101112131415161718192021222324252627282930313233343536
  1. package show
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "testing"
  7. xsql "go-common/library/database/sql"
  8. "github.com/bouk/monkey"
  9. "github.com/smartystreets/goconvey/convey"
  10. )
  11. // TestDaoAudit test audit.
  12. func TestDaoAudit(t *testing.T) {
  13. convey.Convey("Audit", t, func(ctx convey.C) {
  14. ctx.Convey("When everything is correct", func(ctx convey.C) {
  15. res, err := d.Audit(context.Background())
  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.Audit(context.Background())
  27. ctx.Convey("Error should not be nil", func(ctx convey.C) {
  28. ctx.So(err, convey.ShouldNotBeNil)
  29. })
  30. })
  31. })
  32. }