mng_role_test.go 990 B

1234567891011121314151617181920212223242526272829303132333435
  1. package dao
  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. func TestRoles(t *testing.T) {
  12. convey.Convey("Roles", t, func(ctx convey.C) {
  13. ctx.Convey("When everything is correct", func(ctx convey.C) {
  14. res, err := d.Roles(context.Background())
  15. ctx.Convey("Error should be nil, res should not be empty", func(ctx convey.C) {
  16. ctx.So(err, convey.ShouldBeNil)
  17. ctx.So(res, convey.ShouldNotBeEmpty)
  18. })
  19. })
  20. ctx.Convey("When db.Query gets error", func(ctx convey.C) {
  21. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Query", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (*xsql.Rows, error) {
  22. return nil, fmt.Errorf("db.Query error")
  23. })
  24. defer guard.Unpatch()
  25. _, err := d.Roles(context.Background())
  26. ctx.Convey("Error should not be nil", func(ctx convey.C) {
  27. ctx.So(err, convey.ShouldNotBeNil)
  28. })
  29. })
  30. })
  31. }