canal_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package dao
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. cml "go-common/app/admin/main/apm/model/canal"
  7. "github.com/bouk/monkey"
  8. "github.com/jinzhu/gorm"
  9. "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestDaoSetConfigID(t *testing.T) {
  12. convey.Convey("SetConfigID", t, func(ctx convey.C) {
  13. var (
  14. id = int64(0)
  15. addr = "127.0.0.1:8000"
  16. db = &gorm.DB{
  17. Error: fmt.Errorf("test"),
  18. }
  19. )
  20. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  21. err := d.SetConfigID(id, addr)
  22. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  23. ctx.So(err, convey.ShouldBeNil)
  24. })
  25. })
  26. ctx.Convey("When DB update return err", func(ctx convey.C) {
  27. monkey.PatchInstanceMethod(reflect.TypeOf(d.DBCanal), "Updates", func(_ *gorm.DB, _ interface{}, _ ...bool) *gorm.DB {
  28. return db
  29. })
  30. err := d.SetConfigID(id, addr)
  31. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  32. ctx.So(err, convey.ShouldEqual, 70014)
  33. })
  34. })
  35. ctx.Reset(func() {
  36. monkey.UnpatchAll()
  37. })
  38. })
  39. }
  40. func TestDaoCanalInfoCounts(t *testing.T) {
  41. convey.Convey("CanalInfoCounts", t, func(ctx convey.C) {
  42. var (
  43. v = &cml.ConfigReq{
  44. Addr: "127.0.0.1:3308",
  45. User: "admin",
  46. Password: "admin",
  47. Project: "main.web-svr",
  48. Leader: "fss",
  49. Databases: "ada",
  50. Mark: "test",
  51. }
  52. db = &gorm.DB{
  53. Error: fmt.Errorf("test"),
  54. }
  55. )
  56. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  57. cnt, err := d.CanalInfoCounts(v)
  58. ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
  59. ctx.So(err, convey.ShouldBeNil)
  60. ctx.So(cnt, convey.ShouldNotBeNil)
  61. })
  62. })
  63. ctx.Convey("When count error", func(ctx convey.C) {
  64. monkey.PatchInstanceMethod(reflect.TypeOf(d.DBCanal), "Count", func(_ *gorm.DB, _ interface{}) *gorm.DB {
  65. return db
  66. })
  67. cnt, err := d.CanalInfoCounts(v)
  68. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  69. ctx.So(err, convey.ShouldEqual, -400)
  70. ctx.So(cnt, convey.ShouldEqual, 0)
  71. })
  72. })
  73. ctx.Reset(func() {
  74. monkey.UnpatchAll()
  75. })
  76. })
  77. }
  78. func TestDaoCanalInfoEdit(t *testing.T) {
  79. convey.Convey("CanalInfoEdit", t, func(ctx convey.C) {
  80. var (
  81. v = &cml.ConfigReq{
  82. Addr: "127.0.0.1:3308",
  83. User: "admin",
  84. Password: "admin",
  85. Project: "main.web-svr",
  86. Leader: "fss",
  87. Databases: "ada",
  88. Mark: "test",
  89. }
  90. db = &gorm.DB{
  91. Error: fmt.Errorf("test"),
  92. }
  93. )
  94. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  95. err := d.CanalInfoEdit(v)
  96. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  97. ctx.So(err, convey.ShouldBeNil)
  98. })
  99. })
  100. ctx.Convey("When edit return err", func(ctx convey.C) {
  101. monkey.PatchInstanceMethod(reflect.TypeOf(d.DBCanal), "Updates", func(_ *gorm.DB, _ interface{}, _ ...bool) *gorm.DB {
  102. return db
  103. })
  104. err := d.CanalInfoEdit(v)
  105. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  106. ctx.So(err, convey.ShouldEqual, 70005)
  107. })
  108. })
  109. ctx.Reset(func() {
  110. monkey.UnpatchAll()
  111. })
  112. })
  113. }
  114. func TestDaoCanalApplyCounts(t *testing.T) {
  115. convey.Convey("CanalApplyCounts", t, func(ctx convey.C) {
  116. var (
  117. v = &cml.ConfigReq{
  118. Addr: "127.0.0.1:3308",
  119. User: "admin",
  120. Password: "admin",
  121. Project: "main.web-svr",
  122. Leader: "fss",
  123. Databases: "ada",
  124. Mark: "test",
  125. }
  126. db = &gorm.DB{
  127. Error: fmt.Errorf("test"),
  128. }
  129. )
  130. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  131. cnt, err := d.CanalApplyCounts(v)
  132. ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
  133. ctx.So(err, convey.ShouldBeNil)
  134. ctx.So(cnt, convey.ShouldNotBeNil)
  135. })
  136. })
  137. ctx.Convey("When count error", func(ctx convey.C) {
  138. monkey.PatchInstanceMethod(reflect.TypeOf(d.DBCanal), "Count", func(_ *gorm.DB, _ interface{}) *gorm.DB {
  139. return db
  140. })
  141. cnt, err := d.CanalApplyCounts(v)
  142. ctx.Convey("Then err should not be nil.cnt should be 0.", func(ctx convey.C) {
  143. ctx.So(err, convey.ShouldEqual, -400)
  144. ctx.So(cnt, convey.ShouldEqual, 0)
  145. })
  146. })
  147. ctx.Reset(func() {
  148. monkey.UnpatchAll()
  149. })
  150. })
  151. }
  152. func TestDaoCanalApplyEdit(t *testing.T) {
  153. convey.Convey("CanalApplyEdit", t, func(ctx convey.C) {
  154. var (
  155. v = &cml.ConfigReq{
  156. Addr: "127.0.0.1:3308",
  157. User: "admin",
  158. Password: "admin",
  159. Databases: "test",
  160. Mark: "test",
  161. }
  162. db = &gorm.DB{
  163. Error: fmt.Errorf("test"),
  164. }
  165. username = "fengshanshan"
  166. )
  167. ctx.Convey("When project and leader is null", func(ctx convey.C) {
  168. err := d.CanalApplyEdit(v, username)
  169. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  170. ctx.So(err, convey.ShouldBeNil)
  171. })
  172. })
  173. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  174. v.Leader = "fengshanshan"
  175. v.Project = "main.web-svr"
  176. err := d.CanalApplyEdit(v, username)
  177. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  178. ctx.So(err, convey.ShouldBeNil)
  179. })
  180. })
  181. ctx.Convey("When edit error", func(ctx convey.C) {
  182. monkey.PatchInstanceMethod(reflect.TypeOf(d.DBCanal), "Updates", func(_ *gorm.DB, _ interface{}, _ ...bool) *gorm.DB {
  183. return db
  184. })
  185. err := d.CanalApplyEdit(v, username)
  186. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  187. ctx.So(err, convey.ShouldEqual, 70005)
  188. })
  189. })
  190. ctx.Reset(func() {
  191. monkey.UnpatchAll()
  192. })
  193. })
  194. }
  195. func TestDaoCanalApplyCreate(t *testing.T) {
  196. convey.Convey("CanalApplyCreate", t, func(ctx convey.C) {
  197. var (
  198. v = &cml.ConfigReq{
  199. Addr: "127.0.0.1:3309",
  200. User: "admin",
  201. Password: "admin",
  202. Project: "main.web-svr",
  203. Leader: "fss",
  204. Databases: "ada",
  205. Mark: "test",
  206. }
  207. db = &gorm.DB{
  208. Error: fmt.Errorf("test"),
  209. }
  210. username = "fengshanshan"
  211. )
  212. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  213. monkey.PatchInstanceMethod(reflect.TypeOf(d.DBCanal), "Create", func(_ *gorm.DB, _ interface{}) *gorm.DB {
  214. return &gorm.DB{
  215. Error: nil,
  216. }
  217. })
  218. err := d.CanalApplyCreate(v, username)
  219. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  220. ctx.So(err, convey.ShouldBeNil)
  221. })
  222. })
  223. ctx.Convey("When creater error", func(ctx convey.C) {
  224. monkey.PatchInstanceMethod(reflect.TypeOf(d.DBCanal), "Create", func(_ *gorm.DB, _ interface{}) *gorm.DB {
  225. return db
  226. })
  227. err := d.CanalApplyCreate(v, username)
  228. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  229. ctx.So(err, convey.ShouldEqual, 70006)
  230. })
  231. })
  232. ctx.Reset(func() {
  233. monkey.UnpatchAll()
  234. })
  235. })
  236. }