mysql_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "testing"
  7. "time"
  8. "go-common/app/job/main/passport-game-data/model"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. const (
  12. _timeFormat = "2006-01-02 15:04:05"
  13. )
  14. var (
  15. _loc = time.Now().Location()
  16. )
  17. func TestDao_AddAsoAccountsCloud(t *testing.T) {
  18. once.Do(startDao)
  19. Convey("batch add aso account to cloud", t, func() {
  20. Convey("single", func() {
  21. as := make([]*model.AsoAccount, 0)
  22. a := &model.AsoAccount{
  23. Mid: 12047569,
  24. UserID: "bili_1710676855",
  25. Uname: "Bili_12047569",
  26. Pwd: "3686c9d96ae6896fe117319ba6c07087",
  27. Salt: "pdMXF856",
  28. Email: "62fe0d616162f56ecab3e12a2de83ea6",
  29. Tel: "bdb27b0300e3984e48e7aea5c672a243",
  30. CountryID: 1,
  31. MobileVerified: 1,
  32. Isleak: 0,
  33. }
  34. as = append(as, a)
  35. err := d.AddAsoAccountsCloud(context.TODO(), as)
  36. So(err, ShouldBeNil)
  37. })
  38. Convey("multiple", func() {
  39. as := make([]*model.AsoAccount, 0)
  40. a := &model.AsoAccount{
  41. Mid: 12047569,
  42. UserID: "bili_1710676855",
  43. Uname: "Bili_12047569",
  44. Pwd: "3686c9d96ae6896fe117319ba6c07087",
  45. Salt: "pdMXF856",
  46. Email: "62fe0d616162f56ecab3e12a2de83ea6",
  47. Tel: "bdb27b0300e3984e48e7aea5c672a243",
  48. CountryID: 1,
  49. MobileVerified: 1,
  50. Isleak: 0,
  51. }
  52. as = append(as, a)
  53. as = append(as, a)
  54. err := d.AddAsoAccountsCloud(context.TODO(), as)
  55. So(err, ShouldBeNil)
  56. })
  57. })
  58. }
  59. func TestDao_AsoAccountRangeCloud(t *testing.T) {
  60. once.Do(startDao)
  61. Convey("get a aso account from cloud range start time and end time", t, func() {
  62. Convey("when start time after end time", func() {
  63. st, err := time.ParseInLocation(_timeFormat, "2018-01-22 12:50:41", _loc)
  64. ed, err := time.ParseInLocation(_timeFormat, "2018-01-22 12:49:41", _loc)
  65. So(err, ShouldBeNil)
  66. res, err := d.AsoAccountRangeCloud(context.TODO(), st, ed)
  67. So(err, ShouldBeNil)
  68. So(len(res), ShouldEqual, 0)
  69. })
  70. Convey("when res is not empty", func() {
  71. st, err := time.ParseInLocation(_timeFormat, "2018-01-22 12:50:41", _loc)
  72. ed, err := time.ParseInLocation(_timeFormat, "2018-01-22 12:51:41", _loc)
  73. So(err, ShouldBeNil)
  74. res, err := d.AsoAccountRangeCloud(context.TODO(), st, ed)
  75. So(err, ShouldBeNil)
  76. So(len(res), ShouldBeGreaterThan, 0)
  77. mid := int64(88888970)
  78. ok := false
  79. var target *model.AsoAccount
  80. for _, a := range res {
  81. if a.Mid == mid {
  82. target = a
  83. ok = true
  84. break
  85. }
  86. }
  87. So(ok, ShouldBeTrue)
  88. So(target.Email, ShouldNotBeNil)
  89. So(target.Tel, ShouldBeEmpty)
  90. str, _ := json.Marshal(target)
  91. t.Logf("res: %s", str)
  92. })
  93. })
  94. }
  95. func TestDao_AsoAccountRangeLocal(t *testing.T) {
  96. once.Do(startDao)
  97. Convey("get a aso account from local range start time and end time", t, func() {
  98. Convey("when start time is after end time", func() {
  99. st, err := time.ParseInLocation(_timeFormat, "2018-01-22 12:50:41", _loc)
  100. ed, err := time.ParseInLocation(_timeFormat, "2018-01-22 12:49:41", _loc)
  101. So(err, ShouldBeNil)
  102. res, err := d.AsoAccountRangeLocal(context.TODO(), st, ed)
  103. So(err, ShouldBeNil)
  104. So(len(res), ShouldEqual, 0)
  105. })
  106. Convey("when res is not empty", func() {
  107. st, err := time.ParseInLocation(_timeFormat, "2018-01-22 12:50:41", _loc)
  108. ed, err := time.ParseInLocation(_timeFormat, "2018-01-22 12:51:41", _loc)
  109. So(err, ShouldBeNil)
  110. res, err := d.AsoAccountRangeLocal(context.TODO(), st, ed)
  111. So(err, ShouldBeNil)
  112. So(len(res), ShouldBeGreaterThan, 0)
  113. mid := int64(88888970)
  114. ok := false
  115. var target *model.OriginAsoAccount
  116. for _, a := range res {
  117. if a.Mid == mid {
  118. target = a
  119. ok = true
  120. break
  121. }
  122. }
  123. So(ok, ShouldBeTrue)
  124. So(target.Email, ShouldNotBeNil)
  125. So(target.Tel, ShouldNotBeEmpty)
  126. str, _ := json.Marshal(target)
  127. t.Logf("res: %s", str)
  128. })
  129. })
  130. }
  131. func TestDao_AsoAccountsCloud(t *testing.T) {
  132. once.Do(startDao)
  133. Convey("get aso accounts from cloud", t, func() {
  134. Convey("when res is empty", func() {
  135. res, err := d.AsoAccountsCloud(context.TODO(), []int64{10000000000})
  136. So(err, ShouldBeNil)
  137. So(len(res), ShouldEqual, 0)
  138. })
  139. Convey("when res has single item", func() {
  140. mids := []int64{88888970}
  141. res, err := d.AsoAccountsCloud(context.TODO(), mids)
  142. So(err, ShouldBeNil)
  143. So(len(res), ShouldEqual, 1)
  144. m := make(map[int64]*model.AsoAccount)
  145. for _, a := range res {
  146. m[a.Mid] = a
  147. }
  148. for _, mid := range mids {
  149. a, ok := m[mid]
  150. So(ok, ShouldBeTrue)
  151. So(a.Email, ShouldBeEmpty)
  152. So(a.Tel, ShouldNotBeEmpty)
  153. str, _ := json.Marshal(a)
  154. t.Logf("a: %s", str)
  155. }
  156. })
  157. Convey("when res has multiple items", func() {
  158. mids := []int64{88888970, 110000784}
  159. res, err := d.AsoAccountsCloud(context.TODO(), mids)
  160. So(err, ShouldBeNil)
  161. So(len(res), ShouldEqual, 2)
  162. m := make(map[int64]*model.AsoAccount)
  163. for _, a := range res {
  164. m[a.Mid] = a
  165. }
  166. for _, mid := range mids {
  167. a, ok := m[mid]
  168. So(ok, ShouldBeTrue)
  169. So(a.Email, ShouldBeEmpty)
  170. So(a.Tel, ShouldNotBeEmpty)
  171. str, _ := json.Marshal(a)
  172. t.Logf("a: %s", str)
  173. }
  174. })
  175. })
  176. }
  177. func TestDao_AsoAccountsLocal(t *testing.T) {
  178. once.Do(startDao)
  179. Convey("get aso accounts from local", t, func() {
  180. Convey("when res is empty", func() {
  181. res, err := d.AsoAccountsCloud(context.TODO(), []int64{10000000000})
  182. So(err, ShouldBeNil)
  183. So(len(res), ShouldEqual, 0)
  184. })
  185. Convey("when res has single item", func() {
  186. mids := []int64{88888970}
  187. res, err := d.AsoAccountsCloud(context.TODO(), mids)
  188. So(err, ShouldBeNil)
  189. So(len(res), ShouldEqual, 1)
  190. m := make(map[int64]*model.AsoAccount)
  191. for _, a := range res {
  192. m[a.Mid] = a
  193. }
  194. for _, mid := range mids {
  195. a, ok := m[mid]
  196. So(ok, ShouldBeTrue)
  197. So(a.Email, ShouldNotBeNil)
  198. So(a.Tel, ShouldBeEmpty)
  199. str, _ := json.Marshal(a)
  200. t.Logf("a: %s", str)
  201. }
  202. })
  203. Convey("when res has multiple items", func() {
  204. mids := []int64{88888970, 110000784}
  205. res, err := d.AsoAccountsCloud(context.TODO(), mids)
  206. So(err, ShouldBeNil)
  207. So(len(res), ShouldEqual, 2)
  208. m := make(map[int64]*model.AsoAccount)
  209. for _, a := range res {
  210. m[a.Mid] = a
  211. }
  212. for _, mid := range mids {
  213. a, ok := m[mid]
  214. So(ok, ShouldBeTrue)
  215. So(a.Email, ShouldNotBeNil)
  216. So(a.Tel, ShouldBeEmpty)
  217. str, _ := json.Marshal(a)
  218. t.Logf("a: %s", str)
  219. }
  220. })
  221. })
  222. }
  223. func TestDao_UpdateAsoAccountCloud(t *testing.T) {
  224. once.Do(startDao)
  225. Convey("update aso account", t, func() {
  226. Convey("when mtime matches", func() {
  227. mid := int64(12047569)
  228. as, err := d.AsoAccountsCloud(context.TODO(), []int64{mid})
  229. So(err, ShouldBeNil)
  230. So(len(as), ShouldEqual, 1)
  231. account := as[0]
  232. account.MobileVerified = 1 - account.MobileVerified
  233. affected, err := d.UpdateAsoAccountCloud(context.TODO(), account, account.Mtime)
  234. So(err, ShouldBeNil)
  235. So(affected, ShouldEqual, 1)
  236. })
  237. Convey("when mtime not matches", func() {
  238. mid := int64(12047569)
  239. as, err := d.AsoAccountsCloud(context.TODO(), []int64{mid})
  240. So(err, ShouldBeNil)
  241. So(len(as), ShouldEqual, 1)
  242. account := as[0]
  243. account.MobileVerified = 1 - account.MobileVerified
  244. affected, err := d.UpdateAsoAccountCloud(context.TODO(), account, time.Now())
  245. So(err, ShouldBeNil)
  246. So(affected, ShouldEqual, 0)
  247. })
  248. })
  249. }
  250. func TestDao_AddIgnoreAsoAccount(t *testing.T) {
  251. once.Do(startDao)
  252. Convey("add ignore a aso account when not exist", t, func() {
  253. //Convey("when not exists", func() {
  254. // account := &model.AsoAccount{
  255. // Mid: 12047569,
  256. // UserID: "bili_1710676855",
  257. // Uname: "Bili_12047569",
  258. // Pwd: "3686c9d96ae6896fe117319ba6c07087",
  259. // Salt: "pdMXF856",
  260. // Email: "62fe0d616162f56ecab3e12a2de83ea6",
  261. // Tel: "bdb27b0300e3984e48e7aea5c672a243",
  262. // CountryID: 1,
  263. // MobileVerified: 1,
  264. // Isleak: 0,
  265. // }
  266. // affected, err := d.AddIgnoreAsoAccount(context.TODO(), account)
  267. // So(err, ShouldBeNil)
  268. // So(affected, ShouldEqual, 1)
  269. //})
  270. Convey("when not exists", func() {
  271. account := &model.AsoAccount{
  272. Mid: 12047569,
  273. UserID: "bili_1710676855",
  274. Uname: "Bili_12047569",
  275. Pwd: "3686c9d96ae6896fe117319ba6c07087",
  276. Salt: "pdMXF856",
  277. Email: "62fe0d616162f56ecab3e12a2de83ea6",
  278. Tel: "bdb27b0300e3984e48e7aea5c672a243",
  279. CountryID: 1,
  280. MobileVerified: 1,
  281. Isleak: 0,
  282. }
  283. affected, err := d.AddIgnoreAsoAccount(context.TODO(), account)
  284. So(err, ShouldBeNil)
  285. So(affected, ShouldEqual, 0)
  286. })
  287. })
  288. }
  289. const (
  290. _pattern = "INSERT INTO aso_account (mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak) VALUES(%d,'%s','%s','%s','%s',%s,%s,%d,%d,%d) ON DUPLICATE KEY UPDATE userid='%s',uname='%s',pwd='%s',salt='%s',email=%s,tel=%s,country_id=%d,mobile_verified=%d,isleak=%d;"
  291. )
  292. func TestDao_AddAsoAccount(t *testing.T) {
  293. oldStr := `{
  294. "mid": 255554277,
  295. "userid": "bili_93079136999",
  296. "uname": "白又寻",
  297. "pwd": "8489c2cbddb7ee1438698a4f21ee1d78",
  298. "salt": "r0MHcs5M",
  299. "email": "",
  300. "tel": "ca6d0469ca340f67f4635425dcd11581",
  301. "country_id": 1,
  302. "mobile_verified": 2,
  303. "isleak": 0,
  304. "ctime": "2017-11-25T12:03:38+08:00",
  305. "mtime": "2017-12-04T14:19:59+08:00"
  306. }`
  307. old := new(model.AsoAccount)
  308. err := json.Unmarshal([]byte(oldStr), &old)
  309. if err != nil {
  310. t.Error(err)
  311. t.FailNow()
  312. }
  313. sql := getSQL(old)
  314. t.Logf("sql: %s", sql)
  315. afterStr := `{
  316. "mid": 255554277,
  317. "userid": "bili_93079136999",
  318. "uname": "白又寻",
  319. "pwd": "5f064b2ddb4d8cd5f9e01507ab1d34c6",
  320. "salt": "ggr58PEs",
  321. "email": "",
  322. "tel": "ca6d0469ca340f67f4635425dcd11581",
  323. "country_id": 1,
  324. "mobile_verified": 2,
  325. "isleak": 0,
  326. "ctime": "0001-01-01T00:00:00Z",
  327. "mtime": "2017-11-25T19:14:20+08:00"
  328. }`
  329. after := new(model.AsoAccount)
  330. err = json.Unmarshal([]byte(afterStr), &after)
  331. if err != nil {
  332. t.Error(err)
  333. t.FailNow()
  334. }
  335. afterSQL := getSQL(after)
  336. t.Logf("after sql: %s", afterSQL)
  337. }
  338. func getSQL(a *model.AsoAccount) string {
  339. email := "NULL"
  340. tel := "NULL"
  341. if len(a.Email) > 0 {
  342. email = "'" + a.Email + "'"
  343. }
  344. if len(a.Tel) > 0 {
  345. tel = "'" + a.Tel + "'"
  346. }
  347. return fmt.Sprintf(_pattern, a.Mid, a.UserID, a.Uname, a.Pwd, a.Salt, email, tel, a.CountryID, a.MobileVerified, a.Isleak, a.UserID, a.Uname, a.Pwd, a.Salt, email, tel, a.CountryID, a.MobileVerified, a.Isleak)
  348. }