mng_auth_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 TestAuths(t *testing.T) {
  12. convey.Convey("Auths", t, func(ctx convey.C) {
  13. ctx.Convey("When everything is correct", func(ctx convey.C) {
  14. res, err := d.Auths(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.Auths(context.Background())
  26. ctx.Convey("Error should not be nil", func(ctx convey.C) {
  27. ctx.So(err, convey.ShouldNotBeNil)
  28. })
  29. })
  30. })
  31. }
  32. // Set d.close() to get reversal case
  33. func TestAuthRelation(t *testing.T) {
  34. convey.Convey("AuthRelation", t, func(ctx convey.C) {
  35. convey.Convey("When everything is correct,", func(ctx convey.C) {
  36. asgs, err := d.AuthRelation(context.Background())
  37. ctx.Convey("Error should be nil, asgs should not be nil(No Data)", func(ctx convey.C) {
  38. ctx.So(err, convey.ShouldBeNil)
  39. ctx.So(asgs, convey.ShouldBeNil)
  40. })
  41. })
  42. convey.Convey("When set db closed", WithReopenDB(func(d *Dao) {
  43. d.Close()
  44. _, err := d.AuthRelation(context.Background())
  45. convey.Convey("Error should not be nil", func(ctx convey.C) {
  46. convey.So(err, convey.ShouldNotBeNil)
  47. })
  48. }))
  49. })
  50. }