mng_user_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 TestUsers(t *testing.T) {
  12. convey.Convey("Users", t, func(ctx convey.C) {
  13. ctx.Convey("When everything is correct", func(ctx convey.C) {
  14. res, err := d.Users(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.Users(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 TestUser(t *testing.T) {
  34. var id = int64(67)
  35. convey.Convey("User", t, func(ctx convey.C) {
  36. convey.Convey("When everything is correct,", func(ctx convey.C) {
  37. asgs, err := d.User(context.Background(), id)
  38. ctx.Convey("Error should be nil, asgs should not be nil(No Data)", func(ctx convey.C) {
  39. ctx.So(err, convey.ShouldBeNil)
  40. ctx.So(asgs, convey.ShouldBeNil)
  41. })
  42. })
  43. convey.Convey("When set db closed", WithReopenDB(func(d *Dao) {
  44. d.Close()
  45. _, err := d.User(context.Background(), id)
  46. convey.Convey("Error should not be nil", func(ctx convey.C) {
  47. convey.So(err, convey.ShouldNotBeNil)
  48. })
  49. }))
  50. })
  51. }