tidb_data_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package service
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "go-common/app/infra/canal/model"
  7. pb "github.com/pingcap/tidb-tools/tidb_binlog/slave_binlog_proto/go-binlog"
  8. )
  9. func Test_tidbMakeData(t *testing.T) {
  10. insertMsg, insertData := prepareInsertData()
  11. delMsg, delData := prepareDeleteData()
  12. updateMsg, updateData := prepareUpdateData()
  13. updateMsg2, updateData2 := prepareUpdateData2()
  14. type args struct {
  15. m *msg
  16. }
  17. tests := []struct {
  18. name string
  19. args args
  20. wantData *model.Data
  21. wantErr bool
  22. }{
  23. {name: "insert", args: args{m: insertMsg}, wantData: insertData, wantErr: false},
  24. {name: "delete", args: args{m: delMsg}, wantData: delData, wantErr: false},
  25. {name: "update", args: args{m: updateMsg}, wantData: updateData, wantErr: false},
  26. {name: "update2", args: args{m: updateMsg2}, wantData: updateData2, wantErr: false},
  27. }
  28. for _, tt := range tests {
  29. t.Run(tt.name, func(t *testing.T) {
  30. gotData, err := tidbMakeData(tt.args.m)
  31. if (err != nil) != tt.wantErr {
  32. t.Errorf("tidbMakeData() error = %v, wantErr %v", err, tt.wantErr)
  33. return
  34. }
  35. gotjson, _ := json.Marshal(gotData)
  36. wantjson, _ := json.Marshal(tt.wantData)
  37. if !reflect.DeepEqual(gotjson, wantjson) {
  38. t.Errorf("tidbMakeData() = %v, want %v", gotData, tt.wantData)
  39. }
  40. })
  41. }
  42. }
  43. func prepareInsertData() (*msg, *model.Data) {
  44. insertPb := &pb.Binlog{}
  45. json.Unmarshal([]byte(`{"type":0,"commit_ts":403846216359608325,"dml_data":{"tables":[{"schema_name":"bilibili_likes","table_name":"likes","column_info":[{"name":"id","mysql_type":"bigint","is_primary_key":false},{"name":"mtime","mysql_type":"timestamp","is_primary_key":false},{"name":"ctime","mysql_type":"timestamp","is_primary_key":false},{"name":"business_id","mysql_type":"int","is_primary_key":false},{"name":"origin_id","mysql_type":"bigint","is_primary_key":false},{"name":"message_id","mysql_type":"bigint","is_primary_key":false},{"name":"mid","mysql_type":"int","is_primary_key":false},{"name":"type","mysql_type":"tinyint","is_primary_key":false}],"mutations":[{"type":0,"row":{"columns":[{"uint64_value":1},{"string_value":"2018-10-26 18:50:57"},{"string_value":"2018-10-26 18:50:57"},{"uint64_value":5},{"uint64_value":0},{"uint64_value":1},{"uint64_value":8167601},{"uint64_value":1}]}}]}]}}`), insertPb)
  46. insertMsg := &msg{
  47. db: "bilibili_likes",
  48. table: "counts",
  49. tableRegexp: "counts",
  50. mu: insertPb.DmlData.Tables[0].Mutations[0],
  51. ignore: map[string]bool{"ctime": true},
  52. keys: []string{"id", "mid"},
  53. columns: insertPb.DmlData.Tables[0].ColumnInfo,
  54. }
  55. insertData := &model.Data{
  56. Action: "insert",
  57. Table: "counts",
  58. Key: "1,8167601",
  59. New: map[string]interface{}{
  60. "id": 1,
  61. "business_id": 5,
  62. "origin_id": 0,
  63. "message_id": 1,
  64. "mid": 8167601,
  65. "type": 1,
  66. "mtime": "2018-10-26 18:50:57",
  67. },
  68. }
  69. return insertMsg, insertData
  70. }
  71. func prepareDeleteData() (*msg, *model.Data) {
  72. pbData := &pb.Binlog{}
  73. json.Unmarshal([]byte(`{"type":0,"commit_ts":403846189135953921,"dml_data":{"tables":[{"schema_name":"bilibili_likes","table_name":"likes","column_info":[{"name":"id","mysql_type":"bigint","is_primary_key":false},{"name":"mtime","mysql_type":"timestamp","is_primary_key":false},{"name":"ctime","mysql_type":"timestamp","is_primary_key":false},{"name":"business_id","mysql_type":"int","is_primary_key":false},{"name":"origin_id","mysql_type":"bigint","is_primary_key":false},{"name":"message_id","mysql_type":"bigint","is_primary_key":false},{"name":"mid","mysql_type":"int","is_primary_key":false},{"name":"type","mysql_type":"tinyint","is_primary_key":false}],"mutations":[{"type":2,"row":{"columns":[{"uint64_value":7},{"string_value":"2018-01-11 12:19:10"},{"string_value":"2018-01-11 12:19:10"},{"uint64_value":2},{"uint64_value":0},{"uint64_value":897},{"uint64_value":27515233},{"uint64_value":1}]}}]}]}}`), pbData)
  74. msg := &msg{
  75. db: "bilibili_likes",
  76. table: "counts",
  77. tableRegexp: "counts",
  78. mu: pbData.DmlData.Tables[0].Mutations[0],
  79. ignore: map[string]bool{"ctime": true},
  80. keys: []string{"message_id"},
  81. columns: pbData.DmlData.Tables[0].ColumnInfo,
  82. }
  83. data := &model.Data{
  84. Action: "delete",
  85. Table: "counts",
  86. Key: "897",
  87. New: map[string]interface{}{
  88. "id": 7,
  89. "business_id": 2,
  90. "origin_id": 0,
  91. "message_id": 897,
  92. "mid": 27515233,
  93. "type": 1,
  94. "mtime": "2018-01-11 12:19:10",
  95. },
  96. }
  97. return msg, data
  98. }
  99. func prepareUpdateData() (*msg, *model.Data) {
  100. pbData := &pb.Binlog{}
  101. // update likes type from 1 to 0
  102. json.Unmarshal([]byte(`{"type":0,"commit_ts":403846165844459523,"dml_data":{"tables":[{"schema_name":"bilibili_likes","table_name":"likes","column_info":[{"name":"id","mysql_type":"bigint","is_primary_key":false},{"name":"mtime","mysql_type":"timestamp","is_primary_key":false},{"name":"ctime","mysql_type":"timestamp","is_primary_key":false},{"name":"business_id","mysql_type":"int","is_primary_key":false},{"name":"origin_id","mysql_type":"bigint","is_primary_key":false},{"name":"message_id","mysql_type":"bigint","is_primary_key":false},{"name":"mid","mysql_type":"int","is_primary_key":false},{"name":"type","mysql_type":"tinyint","is_primary_key":false}],"mutations":[{"type":1,"row":{"columns":[{"uint64_value":4},{"string_value":"2018-10-26 18:47:44"},{"string_value":"2017-12-22 15:05:29"},{"uint64_value":5},{"uint64_value":0},{"uint64_value":46997},{"uint64_value":88895031},{"uint64_value":0}]},"change_row":{"columns":[{"uint64_value":4},{"string_value":"2017-12-22 15:55:52"},{"string_value":"2017-12-22 15:05:29"},{"uint64_value":5},{"uint64_value":0},{"uint64_value":46997},{"uint64_value":88895031},{"uint64_value":1}]}}]}]}}`), pbData)
  103. msg := &msg{
  104. db: "bilibili_likes",
  105. table: "counts",
  106. tableRegexp: "counts",
  107. mu: pbData.DmlData.Tables[0].Mutations[0],
  108. ignore: map[string]bool{"ctime": true},
  109. keys: []string{"mid"},
  110. columns: pbData.DmlData.Tables[0].ColumnInfo,
  111. }
  112. data := &model.Data{
  113. Action: "update",
  114. Table: "counts",
  115. Key: "88895031",
  116. Old: map[string]interface{}{
  117. "id": 4,
  118. "business_id": 5,
  119. "origin_id": 0,
  120. "message_id": 46997,
  121. "mid": 88895031,
  122. "type": 1,
  123. "mtime": "2017-12-22 15:55:52",
  124. },
  125. New: map[string]interface{}{
  126. "id": 4,
  127. "business_id": 5,
  128. "origin_id": 0,
  129. "message_id": 46997,
  130. "mid": 88895031,
  131. "type": 0,
  132. "mtime": "2018-10-26 18:47:44",
  133. },
  134. }
  135. return msg, data
  136. }
  137. func prepareUpdateData2() (*msg, *model.Data) {
  138. muJson := `{"type":1,"row":{"columns":[{"uint64_value":0},{"string_value":"2018-11-03 17:07:44"},{"string_value":"2018-11-03 14:55:38"},{"uint64_value":3},{"uint64_value":0},{"uint64_value":88889},{"uint64_value":3},{"uint64_value":0},{"int64_value":0},{"int64_value":0},{"uint64_value":8167601}]},"change_row":{"columns":[{"uint64_value":0},{"string_value":"2018-11-03 16:36:39"},{"string_value":"2018-11-03 14:55:38"},{"uint64_value":3},{"uint64_value":0},{"uint64_value":88889},{"uint64_value":2},{"uint64_value":0},{"int64_value":0},{"int64_value":0},{"uint64_value":8167601}]}}`
  139. columnJson := `[{"name":"id","mysql_type":"bigint","is_primary_key":false},{"name":"mtime","mysql_type":"timestamp","is_primary_key":false},{"name":"ctime","mysql_type":"timestamp","is_primary_key":false},{"name":"business_id","mysql_type":"int","is_primary_key":false},{"name":"origin_id","mysql_type":"bigint","is_primary_key":false},{"name":"message_id","mysql_type":"bigint","is_primary_key":false},{"name":"likes_count","mysql_type":"int","is_primary_key":false},{"name":"dislikes_count","mysql_type":"int","is_primary_key":false},{"name":"likes_change","mysql_type":"bigint","is_primary_key":false},{"name":"dislikes_change","mysql_type":"bigint","is_primary_key":false},{"name":"up_mid","mysql_type":"int","is_primary_key":false}]`
  140. msg := &msg{
  141. db: "bilibili_likes",
  142. table: "counts",
  143. tableRegexp: "counts",
  144. keys: []string{"message_id"},
  145. }
  146. json.Unmarshal([]byte(columnJson), &msg.columns)
  147. json.Unmarshal([]byte(muJson), &msg.mu)
  148. data := &model.Data{
  149. Action: "update",
  150. Table: "counts",
  151. Key: "88889",
  152. Old: map[string]interface{}{
  153. "ctime": "2018-11-03 14:55:38",
  154. "origin_id": 0,
  155. "dislikes_count": 0,
  156. "up_mid": 8167601,
  157. "id": 0,
  158. "mtime": "2018-11-03 16:36:39",
  159. "likes_count": 2,
  160. "likes_change": 0,
  161. "dislikes_change": 0,
  162. "business_id": 3,
  163. "message_id": 88889,
  164. },
  165. New: map[string]interface{}{
  166. "likes_count": 3,
  167. "dislikes_count": 0,
  168. "likes_change": 0,
  169. "id": 0,
  170. "mtime": "2018-11-03 17:07:44",
  171. "ctime": "2018-11-03 14:55:38",
  172. "origin_id": 0,
  173. "message_id": 88889,
  174. "business_id": 3,
  175. "dislikes_change": 0,
  176. "up_mid": 8167601,
  177. },
  178. }
  179. return msg, data
  180. }