Skip to content

Commit

Permalink
Add more unit test cases
Browse files Browse the repository at this point in the history
Now we're covering all JSON types:
 - string
 - number
 - object (JSON object)
 - array
 - boolean
 - null

Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Dec 22, 2024
1 parent 93aa37c commit 6264397
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
6 changes: 2 additions & 4 deletions go/mysql/binlog/binlog_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,8 @@ func ParseBinaryJSONDiff(data []byte) (sqltypes.Value, error) {
"cannot read JSON diff value for path %q", path)
}
pos += valueLen
if value.Type() == json.TypeString {
diff.WriteString(sqlparser.Utf8mb4Str)
}
diff.Write(value.MarshalTo(nil))
buf := value.MarshalSQLTo(nil)
diff.Write(buf)
diff.WriteByte(')')
}

Expand Down
79 changes: 70 additions & 9 deletions go/vt/vttablet/tabletmanager/vreplication/vplayer_flaky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1562,8 +1562,10 @@ func TestPlayerPartialImages(t *testing.T) {

testCases := []testCase{
{
input: "insert into src (id, jd, bd) values (1,'{\"key1\": \"val1\"}','blob data'), (2,'{\"key2\": \"val2\"}','blob data2'), (3,'{\"key3\": \"val3\"}','blob data3')",
output: []string{"insert into dst(id,jd,bd) values (1,JSON_OBJECT(_utf8mb4'key1', _utf8mb4'val1'),_binary'blob data'), (2,JSON_OBJECT(_utf8mb4'key2', _utf8mb4'val2'),_binary'blob data2'), (3,JSON_OBJECT(_utf8mb4'key3', _utf8mb4'val3'),_binary'blob data3')"},
input: "insert into src (id, jd, bd) values (1,'{\"key1\": \"val1\"}','blob data'), (2,'{\"key2\": \"val2\"}','blob data2'), (3,'{\"key3\": \"val3\"}','blob data3')",
output: []string{
"insert into dst(id,jd,bd) values (1,JSON_OBJECT(_utf8mb4'key1', _utf8mb4'val1'),_binary'blob data'), (2,JSON_OBJECT(_utf8mb4'key2', _utf8mb4'val2'),_binary'blob data2'), (3,JSON_OBJECT(_utf8mb4'key3', _utf8mb4'val3'),_binary'blob data3')",
},
data: [][]string{
{"1", "{\"key1\": \"val1\"}", "blob data"},
{"2", "{\"key2\": \"val2\"}", "blob data2"},
Expand All @@ -1573,8 +1575,10 @@ func TestPlayerPartialImages(t *testing.T) {
}
if runNoBlobTest {
testCases = append(testCases, testCase{
input: `update src set jd=JSON_SET(jd, '$.color', 'red') where id = 1`,
output: []string{"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.color', _utf8mb4\"red\") where id=1"},
input: `update src set jd=JSON_SET(jd, '$.color', 'red') where id = 1`,
output: []string{
"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.color', CAST(JSON_QUOTE(_utf8mb4'red') as JSON)) where id=1",
},
data: [][]string{
{"1", "{\"key1\": \"val1\", \"color\": \"red\"}", "blob data"},
{"2", "{\"key2\": \"val2\"}", "blob data2"},
Expand All @@ -1583,8 +1587,10 @@ func TestPlayerPartialImages(t *testing.T) {
})
} else {
testCases = append(testCases, testCase{
input: `update src set jd=JSON_SET(jd, '$.color', 'red') where id = 1`,
output: []string{"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.color', _utf8mb4\"red\"), bd=_binary'blob data' where id=1"},
input: `update src set jd=JSON_SET(jd, '$.color', 'red') where id = 1`,
output: []string{
"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.color', CAST(JSON_QUOTE(_utf8mb4'red') as JSON)), bd=_binary'blob data' where id=1",
},
data: [][]string{
{"1", "{\"key1\": \"val1\", \"color\": \"red\"}", "blob data"},
{"2", "{\"key2\": \"val2\"}", "blob data2"},
Expand Down Expand Up @@ -1616,6 +1622,61 @@ func TestPlayerPartialImages(t *testing.T) {
{"12", "{\"key2\": \"val2\"}", "newest blob data"},
},
},
{
input: `update src set jd=JSON_SET(jd, '$.years', 5) where id = 1`,
output: []string{
"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.years', CAST(5 as JSON)) where id=1",
},
data: [][]string{
{"1", "{\"key1\": \"val1\", \"color\": \"red\", \"years\": 5}", "blob data"},
{"3", "{\"key3\": \"val3\"}", "blob data3"},
{"12", "{\"key2\": \"val2\"}", "newest blob data"},
},
},
{
input: `update src set jd=JSON_SET(jd, '$.hobbies', JSON_ARRAY('skiing', 'video games', 'hiking')) where id = 1`,
output: []string{
"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.hobbies', JSON_ARRAY(_utf8mb4'skiing', _utf8mb4'video games', _utf8mb4'hiking')) where id=1",
},
data: [][]string{
{"1", "{\"key1\": \"val1\", \"color\": \"red\", \"years\": 5, \"hobbies\": [\"skiing\", \"video games\", \"hiking\"]}", "blob data"},
{"3", "{\"key3\": \"val3\"}", "blob data3"},
{"12", "{\"key2\": \"val2\"}", "newest blob data"},
},
},
{
input: `update src set jd=JSON_SET(jd, '$.misc', '{"address":"1012 S Park", "town":"Hastings", "state":"MI"}') where id = 12`,
output: []string{
"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.misc', CAST(JSON_QUOTE(_utf8mb4'{\"address\":\"1012 S Park\", \"town\":\"Hastings\", \"state\":\"MI\"}') as JSON)) where id=12",
},
data: [][]string{
{"1", "{\"key1\": \"val1\", \"color\": \"red\", \"years\": 5, \"hobbies\": [\"skiing\", \"video games\", \"hiking\"]}", "blob data"},
{"3", "{\"key3\": \"val3\"}", "blob data3"},
{"12", "{\"key2\": \"val2\", \"misc\": \"{\\\"address\\\":\\\"1012 S Park\\\", \\\"town\\\":\\\"Hastings\\\", \\\"state\\\":\\\"MI\\\"}\"}", "newest blob data"},
},
},
{
input: `update src set jd=JSON_SET(jd, '$.current', true) where id = 12`,
output: []string{
"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.current', CAST(_utf8mb4'true' as JSON)) where id=12",
},
data: [][]string{
{"1", "{\"key1\": \"val1\", \"color\": \"red\", \"years\": 5, \"hobbies\": [\"skiing\", \"video games\", \"hiking\"]}", "blob data"},
{"3", "{\"key3\": \"val3\"}", "blob data3"},
{"12", "{\"key2\": \"val2\", \"misc\": \"{\\\"address\\\":\\\"1012 S Park\\\", \\\"town\\\":\\\"Hastings\\\", \\\"state\\\":\\\"MI\\\"}\", \"current\": true}", "newest blob data"},
},
},
{
input: `update src set jd=JSON_SET(jd, '$.idontknow', null) where id = 3`,
output: []string{
"update dst set jd=JSON_INSERT(`jd`, _utf8mb4'$.idontknow', CAST(_utf8mb4'null' as JSON)) where id=3",
},
data: [][]string{
{"1", "{\"key1\": \"val1\", \"color\": \"red\", \"years\": 5, \"hobbies\": [\"skiing\", \"video games\", \"hiking\"]}", "blob data"},
{"3", "{\"key3\": \"val3\", \"idontknow\": null}", "blob data3"},
{"12", "{\"key2\": \"val2\", \"misc\": \"{\\\"address\\\":\\\"1012 S Park\\\", \\\"town\\\":\\\"Hastings\\\", \\\"state\\\":\\\"MI\\\"}\", \"current\": true}", "newest blob data"},
},
},
}...)
if runNoBlobTest {
testCases = append(testCases, testCase{
Expand All @@ -1630,9 +1691,9 @@ func TestPlayerPartialImages(t *testing.T) {
"insert into dst(id,jd,bd) values (13,JSON_OBJECT(_utf8mb4'key3', _utf8mb4'val3'),_binary'blob data3')",
},
data: [][]string{
{"1", "{\"key1\": \"val1\", \"color\": \"red\"}", "blob data"},
{"12", "{\"key2\": \"val2\"}", "newest blob data"},
{"13", "{\"key3\": \"val3\"}", "blob data3"},
{"1", "{\"key1\": \"val1\", \"color\": \"red\", \"years\": 5, \"hobbies\": [\"skiing\", \"video games\", \"hiking\"]}", "blob data"},
{"12", "{\"key2\": \"val2\", \"misc\": \"{\\\"address\\\":\\\"1012 S Park\\\", \\\"town\\\":\\\"Hastings\\\", \\\"state\\\":\\\"MI\\\"}\", \"current\": true}", "newest blob data"},
{"13", "{\"key3\": \"val3\", \"idontknow\": null}", "blob data3"},
},
})
}
Expand Down

0 comments on commit 6264397

Please sign in to comment.