member_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "reflect"
  7. "testing"
  8. "time"
  9. "go-common/app/job/main/member/model"
  10. xsql "go-common/library/database/sql"
  11. "github.com/bouk/monkey"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. func TestDao_SetBaseInfo(t *testing.T) {
  15. r := &model.BaseInfo{Mid: 100, Sex: 1, Name: "lgs", Face: "face", Sign: "sign", Rank: 3}
  16. Convey("SetBaseInfo", t, func() {
  17. Convey("SetBaseInfo success", func() {
  18. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  19. return nil, nil
  20. })
  21. defer monkey.UnpatchAll()
  22. err := d.SetBaseInfo(context.TODO(), r)
  23. So(err, ShouldBeNil)
  24. })
  25. Convey("SetBaseInfo failed", func() {
  26. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  27. return nil, fmt.Errorf("cannot connect database error")
  28. })
  29. defer monkey.UnpatchAll()
  30. err := d.SetBaseInfo(context.TODO(), r)
  31. So(err, ShouldNotBeNil)
  32. })
  33. })
  34. }
  35. func TestDao_UpdateSign(t *testing.T) {
  36. Convey("SetSign", t, func() {
  37. Convey("SetSign success", func() {
  38. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  39. return nil, nil
  40. })
  41. defer monkey.UnpatchAll()
  42. err := d.SetSign(context.TODO(), 1, "my sign")
  43. So(err, ShouldBeNil)
  44. })
  45. Convey("SetSign failed", func() {
  46. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  47. return nil, fmt.Errorf("cannot connect database error")
  48. })
  49. defer monkey.UnpatchAll()
  50. err := d.SetSign(context.TODO(), 1, "my sign")
  51. So(err, ShouldNotBeNil)
  52. })
  53. })
  54. }
  55. func TestDao_SetName(t *testing.T) {
  56. Convey("SetName", t, func() {
  57. Convey("SetName success", func() {
  58. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  59. return nil, nil
  60. })
  61. defer monkey.UnpatchAll()
  62. err := d.SetName(context.TODO(), 1, "myname")
  63. So(err, ShouldBeNil)
  64. })
  65. Convey("SetName failed", func() {
  66. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  67. return nil, fmt.Errorf("cannot connect database error")
  68. })
  69. defer monkey.UnpatchAll()
  70. err := d.SetName(context.TODO(), 1, "myname")
  71. So(err, ShouldNotBeNil)
  72. })
  73. })
  74. }
  75. func TestDao_UpdateFace(t *testing.T) {
  76. Convey("SetFaceRank", t, func() {
  77. Convey("SetFaceRank success", func() {
  78. err := d.SetFace(context.TODO(), int64(1), "http://face")
  79. So(err, ShouldBeNil)
  80. })
  81. Convey("SetFaceRank failed", func() {
  82. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  83. return nil, fmt.Errorf("cannot connect database error")
  84. })
  85. defer monkey.UnpatchAll()
  86. err := d.SetFace(context.TODO(), int64(1), "http://face")
  87. So(err, ShouldNotBeNil)
  88. })
  89. })
  90. }
  91. func TestDao_SelMoral(t *testing.T) {
  92. Convey("SelMoral", t, func() {
  93. Convey("SelMoral success", func() {
  94. monkey.PatchInstanceMethod(reflect.TypeOf(d.accdb.QueryRow(context.TODO(), _SelMoral, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  95. return nil
  96. })
  97. defer monkey.UnpatchAll()
  98. moral, err := d.SelMoral(context.TODO(), int64(8))
  99. So(err, ShouldBeNil)
  100. So(moral, ShouldNotBeNil)
  101. })
  102. Convey("SelMoral error", func() {
  103. monkey.PatchInstanceMethod(reflect.TypeOf(d.accdb.QueryRow(context.TODO(), _SelMoral, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  104. return fmt.Errorf("row.Scan error")
  105. })
  106. defer monkey.UnpatchAll()
  107. moral, err := d.SelMoral(context.TODO(), int64(8))
  108. So(err, ShouldNotBeNil)
  109. So(moral, ShouldNotBeNil)
  110. })
  111. Convey("SelMoral no record", func() {
  112. monkey.PatchInstanceMethod(reflect.TypeOf(d.accdb.QueryRow(context.TODO(), _SelMoral, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  113. return sql.ErrNoRows
  114. })
  115. defer monkey.UnpatchAll()
  116. moral, err := d.SelMoral(context.TODO(), int64(8))
  117. So(err, ShouldBeNil)
  118. So(moral, ShouldBeNil)
  119. })
  120. })
  121. }
  122. func TestDao_IncrMoral(t *testing.T) {
  123. Convey("IncrMoral", t, func() {
  124. Convey("AuditQueuingFace success", func() {
  125. _, err := d.RecoverMoral(context.TODO(), 2, 100, 1, time.Now().Format("2006-01-02"))
  126. So(err, ShouldBeNil)
  127. })
  128. Convey("AuditQueuingFace failed", func() {
  129. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  130. return nil, fmt.Errorf("cannot connect database error")
  131. })
  132. defer monkey.UnpatchAll()
  133. _, err := d.RecoverMoral(context.TODO(), 2, 100, 1, time.Now().Format("2006-01-02"))
  134. So(err, ShouldNotBeNil)
  135. })
  136. })
  137. }
  138. func TestDao_AuditQueuingFace(t *testing.T) {
  139. Convey("AuditQueuingFace", t, func() {
  140. Convey("AuditQueuingFace success", func() {
  141. err := d.AuditQueuingFace(context.TODO(), "/bfs/face/7e68723b9d3664ac3773c1f3c26d5e2bfabc0f22.jpg", "", 0)
  142. So(err, ShouldBeNil)
  143. })
  144. Convey("AuditQueuingFace failed", func() {
  145. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  146. return nil, fmt.Errorf("cannot connect database error")
  147. })
  148. defer monkey.UnpatchAll()
  149. err := d.AuditQueuingFace(context.TODO(), "/bfs/face/7e68723b9d3664ac3773c1f3c26d5e2bfabc0f22.jpg", "", 0)
  150. So(err, ShouldNotBeNil)
  151. })
  152. })
  153. }
  154. func TestDao_BaseInfo(t *testing.T) {
  155. Convey("BaseInfo", t, func() {
  156. Convey("BaseInfo success", func() {
  157. monkey.PatchInstanceMethod(reflect.TypeOf(d.accdb.QueryRow(context.TODO(), _SelBaseInfo, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  158. return nil
  159. })
  160. defer monkey.UnpatchAll()
  161. r, err := d.BaseInfo(context.TODO(), 100)
  162. So(err, ShouldBeNil)
  163. So(r, ShouldNotBeNil)
  164. })
  165. Convey("BaseInfo error", func() {
  166. monkey.PatchInstanceMethod(reflect.TypeOf(d.accdb.QueryRow(context.TODO(), _SelBaseInfo, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  167. return fmt.Errorf("row.Scan error")
  168. })
  169. defer monkey.UnpatchAll()
  170. r, err := d.BaseInfo(context.TODO(), 11)
  171. So(err, ShouldNotBeNil)
  172. So(r, ShouldBeNil)
  173. })
  174. Convey("BaseInfo no record", func() {
  175. monkey.PatchInstanceMethod(reflect.TypeOf(d.accdb.QueryRow(context.TODO(), _SelBaseInfo, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  176. return sql.ErrNoRows
  177. })
  178. defer monkey.UnpatchAll()
  179. r, err := d.BaseInfo(context.TODO(), 11)
  180. So(err, ShouldBeNil)
  181. So(r, ShouldBeNil)
  182. })
  183. })
  184. }
  185. // TestDao_RecoverMoral update
  186. func TestDao_RecoverMoral(t *testing.T) {
  187. Convey("RecoverMoral", t, func() {
  188. Convey("RecoverMoral success", func() {
  189. _, err := d.RecoverMoral(context.TODO(), 121, 1, 1, time.Now().Format("2006-01-02"))
  190. So(err, ShouldBeNil)
  191. })
  192. Convey("RecoverMoral failed", func() {
  193. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  194. return nil, fmt.Errorf("cannot connect database error")
  195. })
  196. defer monkey.UnpatchAll()
  197. r, err := d.RecoverMoral(context.TODO(), 121, 1, 1, time.Now().Format("2006-01-02"))
  198. So(err, ShouldNotBeNil)
  199. So(r, ShouldEqual, 0)
  200. })
  201. })
  202. }
  203. func TestDao_SetSign(t *testing.T) {
  204. Convey("SetSign", t, func() {
  205. Convey("SetSign success", func() {
  206. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  207. return nil, nil
  208. })
  209. defer monkey.UnpatchAll()
  210. err := d.SetSign(context.TODO(), 100, fmt.Sprintf("sign%s", time.Minute.String()))
  211. So(err, ShouldBeNil)
  212. })
  213. Convey("SetSign failed", func() {
  214. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  215. return nil, fmt.Errorf("cannot connect database error")
  216. })
  217. defer monkey.UnpatchAll()
  218. err := d.SetSign(context.TODO(), 100, fmt.Sprintf("sign%s", time.Minute.String()))
  219. So(err, ShouldNotBeNil)
  220. })
  221. })
  222. }
  223. func TestDao_SetOfficial(t *testing.T) {
  224. Convey("SetOfficial", t, func() {
  225. Convey("SetOfficial success", func() {
  226. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  227. return nil, nil
  228. })
  229. defer monkey.UnpatchAll()
  230. err := d.SetOfficial(context.TODO(), 100, 1, fmt.Sprintf("title%s", time.Minute.String()), "it's a test")
  231. So(err, ShouldBeNil)
  232. })
  233. Convey("SetOfficial failed", func() {
  234. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  235. return nil, fmt.Errorf("cannot connect database error")
  236. })
  237. defer monkey.UnpatchAll()
  238. err := d.SetOfficial(context.TODO(), 100, 1, fmt.Sprintf("title%s", time.Minute.String()), "it's a test")
  239. So(err, ShouldNotBeNil)
  240. })
  241. })
  242. }
  243. func TestDao_SetFace(t *testing.T) {
  244. Convey("SetFace", t, func() {
  245. err := d.SetFace(context.TODO(), 100, fmt.Sprintf("face%s", time.Minute.String()))
  246. So(err, ShouldBeNil)
  247. })
  248. Convey("SetFace", t, func() {
  249. Convey("SetFace success", func() {
  250. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  251. return nil, nil
  252. })
  253. defer monkey.UnpatchAll()
  254. err := d.SetFace(context.TODO(), 100, fmt.Sprintf("face%s", time.Minute.String()))
  255. So(err, ShouldBeNil)
  256. })
  257. Convey("SetFace failed", func() {
  258. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  259. return nil, fmt.Errorf("cannot connect database error")
  260. })
  261. defer monkey.UnpatchAll()
  262. err := d.SetFace(context.TODO(), 100, fmt.Sprintf("face%s", time.Minute.String()))
  263. So(err, ShouldNotBeNil)
  264. })
  265. })
  266. }
  267. func TestDao_InitBase(t *testing.T) {
  268. Convey("InitBase", t, func() {
  269. Convey("InitBase success", func() {
  270. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  271. return nil, nil
  272. })
  273. defer monkey.UnpatchAll()
  274. err := d.InitBase(context.TODO(), 1)
  275. So(err, ShouldBeNil)
  276. })
  277. Convey("InitBase failed", func() {
  278. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  279. return nil, fmt.Errorf("cannot connect database error")
  280. })
  281. defer monkey.UnpatchAll()
  282. err := d.InitBase(context.TODO(), 1)
  283. So(err, ShouldNotBeNil)
  284. })
  285. })
  286. }