session_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "reflect"
  7. "testing"
  8. "time"
  9. "go-common/app/interface/main/feedback/model"
  10. "go-common/library/database/sql"
  11. "github.com/bouk/monkey"
  12. "github.com/smartystreets/goconvey/convey"
  13. )
  14. func TestDaoJudgeSsnRecord(t *testing.T) {
  15. convey.Convey("JudgeSsnRecord", t, func(ctx convey.C) {
  16. var (
  17. c = context.Background()
  18. sid = int64(1)
  19. )
  20. ctx.Convey("When everything is correct", func(ctx convey.C) {
  21. cnt, err := d.JudgeSsnRecord(c, sid)
  22. ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
  23. ctx.So(err, convey.ShouldBeNil)
  24. ctx.So(cnt, convey.ShouldNotBeNil)
  25. })
  26. })
  27. ctx.Convey("When d.selSSnID.Exec gets error", func(ctx convey.C) {
  28. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.selSSnID), "Exec",
  29. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
  30. return nil, fmt.Errorf("d.selSSnID.Exec Error")
  31. })
  32. defer guard.Unpatch()
  33. _, err := d.JudgeSsnRecord(c, sid)
  34. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  35. ctx.So(err, convey.ShouldNotBeNil)
  36. })
  37. })
  38. })
  39. }
  40. func TestDaoSession(t *testing.T) {
  41. convey.Convey("Session", t, func(ctx convey.C) {
  42. var (
  43. c = context.Background()
  44. buvid = ""
  45. system = ""
  46. version = ""
  47. mid = int64(1)
  48. )
  49. ctx.Convey("When everything is correct", func(ctx convey.C) {
  50. ssn, err := d.Session(c, buvid, system, version, mid)
  51. ctx.Convey("Then err should be nil.ssn should not be nil.", func(ctx convey.C) {
  52. ctx.So(err, convey.ShouldBeNil)
  53. ctx.SkipSo(ssn, convey.ShouldNotBeNil)
  54. })
  55. })
  56. })
  57. }
  58. func TestDaoSessionCount(t *testing.T) {
  59. var (
  60. c = context.Background()
  61. mid = int64(0)
  62. )
  63. convey.Convey("SessionCount", t, func(ctx convey.C) {
  64. cnt, err := d.SessionCount(c, mid)
  65. ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
  66. ctx.So(err, convey.ShouldBeNil)
  67. ctx.So(cnt, convey.ShouldNotBeNil)
  68. })
  69. })
  70. }
  71. func TestDaoUpSsnMtime(t *testing.T) {
  72. convey.Convey("UpSsnMtime", t, func(ctx convey.C) {
  73. var (
  74. c = context.Background()
  75. now = time.Now()
  76. id = int64(1)
  77. )
  78. convey.Convey("When everything is correct", func(ctx convey.C) {
  79. err := d.UpSsnMtime(c, now, id)
  80. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  81. ctx.So(err, convey.ShouldBeNil)
  82. })
  83. })
  84. convey.Convey("When d.upSsnMtime.Exec gets error", func(ctx convey.C) {
  85. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.upSsnMtime), "Exec",
  86. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
  87. return nil, fmt.Errorf("d.upSsnMtime.Exec Error")
  88. })
  89. defer guard.Unpatch()
  90. err := d.UpSsnMtime(c, now, id)
  91. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  92. ctx.So(err, convey.ShouldNotBeNil)
  93. })
  94. })
  95. })
  96. }
  97. func TestDaoTxUpSsnMtime(t *testing.T) {
  98. convey.Convey("TxUpSsnMtime", t, func(ctx convey.C) {
  99. var (
  100. tx, _ = d.BeginTran(context.Background())
  101. now = time.Now()
  102. id = int64(0)
  103. )
  104. ctx.Convey("When everything is correct", func(ctx convey.C) {
  105. err := d.TxUpSsnMtime(tx, now, id)
  106. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  107. ctx.So(err, convey.ShouldBeNil)
  108. })
  109. })
  110. ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
  111. guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
  112. func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
  113. return nil, fmt.Errorf("tx.Exec Error")
  114. })
  115. defer guard.Unpatch()
  116. err := d.TxUpSsnMtime(tx, now, id)
  117. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  118. ctx.So(err, convey.ShouldNotBeNil)
  119. })
  120. })
  121. ctx.Reset(func() {
  122. tx.Rollback()
  123. })
  124. })
  125. }
  126. func TestDaoSessionIDByTagID(t *testing.T) {
  127. convey.Convey("SessionIDByTagID", t, func(ctx convey.C) {
  128. var (
  129. c = context.Background()
  130. tagID = []int64{1, 2}
  131. )
  132. ctx.Convey("When everything is correct", func(ctx convey.C) {
  133. sid, err := d.SessionIDByTagID(c, tagID)
  134. ctx.Convey("Then err should be nil.sid should not be nil.", func(ctx convey.C) {
  135. ctx.So(err, convey.ShouldBeNil)
  136. ctx.SkipSo(sid, convey.ShouldNotBeNil)
  137. })
  138. })
  139. ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
  140. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
  141. func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
  142. return nil, fmt.Errorf("d.dbMs.Query Error")
  143. })
  144. defer guard.Unpatch()
  145. _, err := d.SessionIDByTagID(c, tagID)
  146. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  147. ctx.So(err, convey.ShouldNotBeNil)
  148. })
  149. })
  150. })
  151. }
  152. func TestDaoSessionBySsnID(t *testing.T) {
  153. convey.Convey("SessionBySsnID", t, func(ctx convey.C) {
  154. var (
  155. c = context.Background()
  156. sid = []int64{1, 2}
  157. state = "3"
  158. start = time.Now()
  159. end = time.Now()
  160. )
  161. ctx.Convey("When everything is correct", func(ctx convey.C) {
  162. ssns, err := d.SessionBySsnID(c, sid, state, start, end)
  163. ctx.Convey("Then err should be nil.ssns should not be nil.", func(ctx convey.C) {
  164. ctx.So(err, convey.ShouldBeNil)
  165. ctx.SkipSo(ssns, convey.ShouldNotBeNil)
  166. })
  167. })
  168. ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
  169. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
  170. func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
  171. return nil, fmt.Errorf("d.dbMs.Query Error")
  172. })
  173. defer guard.Unpatch()
  174. _, err := d.SessionBySsnID(c, sid, state, start, end)
  175. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  176. ctx.So(err, convey.ShouldNotBeNil)
  177. })
  178. })
  179. })
  180. }
  181. func TestDaoSSnBySsnIDAllSate(t *testing.T) {
  182. convey.Convey("SSnBySsnIDAllSate", t, func(ctx convey.C) {
  183. var (
  184. c = context.Background()
  185. sid = []int64{1, 2}
  186. start = time.Now()
  187. end = time.Now()
  188. )
  189. ctx.Convey("When everything is correct", func(ctx convey.C) {
  190. ssns, err := d.SSnBySsnIDAllSate(c, sid, start, end)
  191. ctx.Convey("Then err should be nil.ssns should not be nil.", func(ctx convey.C) {
  192. ctx.So(err, convey.ShouldBeNil)
  193. ctx.SkipSo(ssns, convey.ShouldNotBeNil)
  194. })
  195. })
  196. ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
  197. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
  198. func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
  199. return nil, fmt.Errorf("d.dbMs.Query Error")
  200. })
  201. defer guard.Unpatch()
  202. _, err := d.SSnBySsnIDAllSate(c, sid, start, end)
  203. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  204. ctx.So(err, convey.ShouldNotBeNil)
  205. })
  206. })
  207. })
  208. }
  209. func TestDaoSessionByMid(t *testing.T) {
  210. convey.Convey("SessionByMid", t, func(ctx convey.C) {
  211. var (
  212. c = context.Background()
  213. mid = int64(1)
  214. platform = "ios"
  215. )
  216. ctx.Convey("When everything is correct", func(ctx convey.C) {
  217. ssns, err := d.SessionByMid(c, mid, platform)
  218. ctx.Convey("Then err should be nil.ssns should not be nil.", func(ctx convey.C) {
  219. ctx.So(err, convey.ShouldBeNil)
  220. ctx.So(ssns, convey.ShouldNotBeNil)
  221. })
  222. })
  223. ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
  224. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
  225. func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
  226. return nil, fmt.Errorf("d.dbMs.Query Error")
  227. })
  228. defer guard.Unpatch()
  229. _, err := d.SessionByMid(c, mid, platform)
  230. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  231. ctx.So(err, convey.ShouldNotBeNil)
  232. })
  233. })
  234. })
  235. }
  236. func TestDaoTxUpdateSessionState(t *testing.T) {
  237. convey.Convey("TxUpdateSessionState", t, func(ctx convey.C) {
  238. var (
  239. tx, _ = d.BeginTran(context.Background())
  240. state = int(0)
  241. sid = int64(0)
  242. )
  243. ctx.Convey("When everything is correct", func(ctx convey.C) {
  244. err := d.TxUpdateSessionState(tx, state, sid)
  245. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  246. ctx.So(err, convey.ShouldBeNil)
  247. })
  248. })
  249. ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
  250. guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
  251. func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
  252. return nil, fmt.Errorf("tx.Exec Error")
  253. })
  254. defer guard.Unpatch()
  255. err := d.TxUpdateSessionState(tx, state, sid)
  256. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  257. ctx.So(err, convey.ShouldNotBeNil)
  258. })
  259. })
  260. ctx.Reset(func() {
  261. tx.Rollback()
  262. })
  263. })
  264. }
  265. func TestDaoUpdateSessionState(t *testing.T) {
  266. convey.Convey("UpdateSessionState", t, func(ctx convey.C) {
  267. var (
  268. c = context.Background()
  269. state = int(0)
  270. sid = int64(0)
  271. )
  272. ctx.Convey("UpdateSessionState", func(ctx convey.C) {
  273. err := d.UpdateSessionState(c, state, sid)
  274. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  275. ctx.So(err, convey.ShouldBeNil)
  276. })
  277. })
  278. ctx.Convey("When d.upSsnSta.Exec gets error", func(ctx convey.C) {
  279. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.upSsnSta), "Exec",
  280. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
  281. return nil, fmt.Errorf("d.upSsnSta.Exec Error")
  282. })
  283. defer guard.Unpatch()
  284. err := d.UpdateSessionState(c, state, sid)
  285. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  286. ctx.So(err, convey.ShouldNotBeNil)
  287. })
  288. })
  289. })
  290. }
  291. func TestDaoTags(t *testing.T) {
  292. convey.Convey("Tags", t, func(ctx convey.C) {
  293. var (
  294. c = context.Background()
  295. mold = int(1)
  296. platform = "ios"
  297. )
  298. ctx.Convey("When everything is correct", func(ctx convey.C) {
  299. tMap, err := d.Tags(c, mold, platform)
  300. ctx.Convey("Then err should be nil.tMap should not be nil.", func(ctx convey.C) {
  301. ctx.So(err, convey.ShouldBeNil)
  302. ctx.So(tMap, convey.ShouldNotBeNil)
  303. })
  304. })
  305. ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
  306. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
  307. func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
  308. return nil, fmt.Errorf("d.dbMs.Query Error")
  309. })
  310. defer guard.Unpatch()
  311. _, err := d.Tags(c, mold, platform)
  312. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  313. ctx.So(err, convey.ShouldNotBeNil)
  314. })
  315. })
  316. })
  317. }
  318. func TestDaoAddSession(t *testing.T) {
  319. convey.Convey("AddSession", t, func(ctx convey.C) {
  320. var (
  321. c = context.Background()
  322. s = &model.Session{}
  323. )
  324. ctx.Convey("When everthing is correct", func(ctx convey.C) {
  325. id, err := d.AddSession(c, s)
  326. ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
  327. ctx.So(err, convey.ShouldBeNil)
  328. ctx.So(id, convey.ShouldNotBeNil)
  329. })
  330. })
  331. ctx.Convey("When d.inSsn.Exec gets error", func(ctx convey.C) {
  332. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.inSsn), "Exec",
  333. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
  334. return nil, fmt.Errorf("d.inSsn.Exec Error")
  335. })
  336. defer guard.Unpatch()
  337. _, err := d.AddSession(c, s)
  338. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  339. ctx.So(err, convey.ShouldNotBeNil)
  340. })
  341. })
  342. })
  343. }
  344. func TestDaoTxAddSession(t *testing.T) {
  345. convey.Convey("TxAddSession", t, func(ctx convey.C) {
  346. var (
  347. tx, _ = d.BeginTran(context.Background())
  348. s = &model.Session{}
  349. )
  350. ctx.Convey("When everthing is correct", func(ctx convey.C) {
  351. id, err := d.TxAddSession(tx, s)
  352. ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
  353. ctx.So(err, convey.ShouldBeNil)
  354. ctx.So(id, convey.ShouldNotBeNil)
  355. })
  356. })
  357. ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
  358. guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
  359. func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
  360. return nil, fmt.Errorf("tx.Exec Error")
  361. })
  362. defer guard.Unpatch()
  363. _, err := d.TxAddSession(tx, s)
  364. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  365. ctx.So(err, convey.ShouldNotBeNil)
  366. })
  367. })
  368. ctx.Reset(func() {
  369. tx.Rollback()
  370. })
  371. })
  372. }
  373. func TestDaoAddSessionTag(t *testing.T) {
  374. convey.Convey("AddSessionTag", t, func(ctx convey.C) {
  375. var (
  376. c = context.Background()
  377. sessionID = int64(0)
  378. tagID = int64(0)
  379. now = time.Now()
  380. )
  381. ctx.Convey("When everything is correct", func(ctx convey.C) {
  382. id, err := d.AddSessionTag(c, sessionID, tagID, now)
  383. ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
  384. ctx.So(err, convey.ShouldBeNil)
  385. ctx.So(id, convey.ShouldNotBeNil)
  386. })
  387. })
  388. ctx.Convey("When d.inSsnTag.Exec gets error", func(ctx convey.C) {
  389. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.inSsnTag), "Exec",
  390. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
  391. return nil, fmt.Errorf("d.inSsnTag.Exec Error")
  392. })
  393. defer guard.Unpatch()
  394. _, err := d.AddSessionTag(c, sessionID, tagID, now)
  395. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  396. ctx.So(err, convey.ShouldNotBeNil)
  397. })
  398. })
  399. })
  400. }
  401. func TestDaoTxAddSessionTag(t *testing.T) {
  402. convey.Convey("TxAddSessionTag", t, func(ctx convey.C) {
  403. var (
  404. tx, _ = d.BeginTran(context.Background())
  405. sessionID = int64(0)
  406. tagID = int64(0)
  407. now = time.Now()
  408. )
  409. ctx.Convey("When everthing is correct", func(ctx convey.C) {
  410. id, err := d.TxAddSessionTag(tx, sessionID, tagID, now)
  411. ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
  412. ctx.So(err, convey.ShouldBeNil)
  413. ctx.So(id, convey.ShouldNotBeNil)
  414. })
  415. })
  416. ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
  417. guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
  418. func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
  419. return nil, fmt.Errorf("tx.Exec Error")
  420. })
  421. defer guard.Unpatch()
  422. _, err := d.TxAddSessionTag(tx, sessionID, tagID, now)
  423. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  424. ctx.So(err, convey.ShouldNotBeNil)
  425. })
  426. })
  427. ctx.Reset(func() {
  428. tx.Rollback()
  429. })
  430. })
  431. }
  432. func TestDaoUpdateSession(t *testing.T) {
  433. convey.Convey("UpdateSession", t, func(ctx convey.C) {
  434. var (
  435. c = context.Background()
  436. s = &model.Session{}
  437. )
  438. ctx.Convey("When everything is correct", func(ctx convey.C) {
  439. affected, err := d.UpdateSession(c, s)
  440. ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
  441. ctx.So(err, convey.ShouldBeNil)
  442. ctx.So(affected, convey.ShouldNotBeNil)
  443. })
  444. })
  445. ctx.Convey("When d.upSsn.Exec gets error", func(ctx convey.C) {
  446. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.upSsn), "Exec",
  447. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
  448. return nil, fmt.Errorf("d.upSsn.Exec Error")
  449. })
  450. defer guard.Unpatch()
  451. _, err := d.UpdateSession(c, s)
  452. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  453. ctx.So(err, convey.ShouldNotBeNil)
  454. })
  455. })
  456. })
  457. }
  458. func TestDaoTagIDBySid(t *testing.T) {
  459. convey.Convey("TagIDBySid", t, func(ctx convey.C) {
  460. var (
  461. c = context.Background()
  462. sids = []int64{1, 2}
  463. )
  464. ctx.Convey("When everything is correct", func(ctx convey.C) {
  465. tsMap, err := d.TagIDBySid(c, sids)
  466. ctx.Convey("Then err should be nil.tsMap should not be nil.", func(ctx convey.C) {
  467. ctx.So(err, convey.ShouldBeNil)
  468. ctx.So(tsMap, convey.ShouldNotBeNil)
  469. })
  470. })
  471. ctx.Convey("When d.dbMs.Query gets error", func(ctx convey.C) {
  472. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.dbMs), "Query",
  473. func(_ *sql.DB, _ context.Context, _ string, _ ...interface{}) (*sql.Rows, error) {
  474. return nil, fmt.Errorf("d.dbMs.Query Error")
  475. })
  476. defer guard.Unpatch()
  477. _, err := d.TagIDBySid(c, sids)
  478. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  479. ctx.So(err, convey.ShouldNotBeNil)
  480. })
  481. })
  482. })
  483. }
  484. func TestDaoplatConvert(t *testing.T) {
  485. convey.Convey("platConvert", t, func(ctx convey.C) {
  486. var (
  487. platform = "a,b"
  488. )
  489. ctx.Convey("When the original string is 'a,b'", func(ctx convey.C) {
  490. s := platConvert(platform)
  491. ctx.Convey("Then s should equal \"a\",\"b\".", func(ctx convey.C) {
  492. ctx.So(s, convey.ShouldEqual, "\"a\",\"b\"")
  493. })
  494. })
  495. })
  496. }