databus_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "testing"
  7. "go-common/library/queue/databus"
  8. "github.com/bouk/monkey"
  9. "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestDaoDatabusAddLog(t *testing.T) {
  12. convey.Convey("DatabusAddLog", t, func(convCtx convey.C) {
  13. var (
  14. c = context.Background()
  15. mid = int64(0)
  16. exp = int64(0)
  17. toExp = int64(0)
  18. ts = int64(0)
  19. oper = ""
  20. reason = ""
  21. ip = ""
  22. )
  23. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  24. monkey.PatchInstanceMethod(reflect.TypeOf(d.plogDatabus), "Send", func(_ *databus.Databus, _ context.Context, _ string, _ interface{}) error {
  25. return nil
  26. })
  27. defer monkey.UnpatchAll()
  28. err := d.DatabusAddLog(c, mid, exp, toExp, ts, oper, reason, ip)
  29. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  30. convCtx.So(err, convey.ShouldBeNil)
  31. })
  32. })
  33. convCtx.Convey("When everything goes negative", func(convCtx convey.C) {
  34. monkey.PatchInstanceMethod(reflect.TypeOf(d.plogDatabus), "Send", func(_ *databus.Databus, _ context.Context, _ string, _ interface{}) error {
  35. return fmt.Errorf("Failed send data err")
  36. })
  37. defer monkey.UnpatchAll()
  38. err := d.DatabusAddLog(c, mid, exp, toExp, ts, oper, reason, ip)
  39. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  40. convCtx.So(err, convey.ShouldNotBeNil)
  41. })
  42. })
  43. })
  44. }