@@ -12105,37 +12105,79 @@ func TestDatabaseLevelChangefeed(t *testing.T) {
12105
12105
cdcTest (t , testFn )
12106
12106
}
12107
12107
12108
- func TestChangefeedBareFullProtobuf (t * testing.T ) {
12108
+ func TestChangefeedProtobuf (t * testing.T ) {
12109
12109
defer leaktest .AfterTest (t )()
12110
12110
defer log .Scope (t ).Close (t )
12111
12111
12112
- testFn := func (t * testing.T , s TestServer , f cdctest.TestFeedFactory ) {
12113
- sqlDB := sqlutils .MakeSQLRunner (s .DB )
12112
+ type testCase struct {
12113
+ envelope string
12114
+ withDiff bool
12115
+ expectedRows []string
12116
+ }
12114
12117
12115
- sqlDB .Exec (t , `
12116
- CREATE TABLE pricing (
12117
- id INT PRIMARY KEY,
12118
- name STRING,
12119
- discount FLOAT,
12120
- tax DECIMAL,
12121
- options STRING[]
12122
- )` )
12123
- sqlDB .Exec (t , `
12124
- INSERT INTO pricing VALUES
12125
- (1, 'Chair', 15.75, 2.500, ARRAY['Brown', 'Black']),
12126
- (2, 'Table', 20.00, 1.23456789, ARRAY['Brown', 'Black'])
12127
- ` )
12128
- pricingFeed := feed (t , f ,
12129
- `CREATE CHANGEFEED FOR pricing WITH envelope='bare', format='protobuf', key_in_value, topic_in_value` )
12130
- defer closeFeed (t , pricingFeed )
12118
+ tests := []testCase {
12119
+ {
12120
+ envelope : "bare" ,
12121
+ withDiff : false ,
12122
+ expectedRows : []string {
12123
+ `pricing: {"id":1}->{"values":{"discount":15.75,"id":1,"name":"Chair","options":["Brown","Black"],"tax":"2.500"},"__crdb__":{"key":{"id":1},"topic":"pricing"}}` ,
12124
+ `pricing: {"id":2}->{"values":{"discount":20,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"},"__crdb__":{"key":{"id":2},"topic":"pricing"}}` ,
12125
+ `pricing: {"id":2}->{"values":{"discount":25.5,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"},"__crdb__":{"key":{"id":2},"topic":"pricing"}}` ,
12126
+ `pricing: {"id":1}->{"values":{"discount":10,"id":1,"name":"Armchair","options":["Red"],"tax":"1.000"},"__crdb__":{"key":{"id":1},"topic":"pricing"}}` ,
12127
+ `pricing: {"id":3}->{"values":{"discount":50,"id":3,"name":"Sofa","options":["Gray"],"tax":"4.250"},"__crdb__":{"key":{"id":3},"topic":"pricing"}}` ,
12128
+ `pricing: {"id":2}->{"values":{"discount":null,"id":2,"name":null,"options":null,"tax":null},"__crdb__":{"key":{"id":2},"topic":"pricing"}}` ,
12129
+ },
12130
+ },
12131
+ {
12132
+ envelope : "wrapped" ,
12133
+ withDiff : true ,
12134
+ expectedRows : []string {
12135
+ `pricing: {"id":1}->{"after":{"values":{"discount":15.75,"id":1,"name":"Chair","options":["Brown","Black"],"tax":"2.500"}},"before":{},"key":{"id":1},"topic":"pricing"}` ,
12136
+ `pricing: {"id":2}->{"after":{"values":{"discount":20,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"}},"before":{},"key":{"id":2},"topic":"pricing"}` ,
12137
+ `pricing: {"id":2}->{"after":{"values":{"discount":25.5,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"}},"before":{"values":{"discount":20,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"}},"key":{"id":2},"topic":"pricing"}` ,
12138
+ `pricing: {"id":1}->{"after":{"values":{"discount":10,"id":1,"name":"Armchair","options":["Red"],"tax":"1.000"}},"before":{"values":{"discount":15.75,"id":1,"name":"Chair","options":["Brown","Black"],"tax":"2.500"}},"key":{"id":1},"topic":"pricing"}` ,
12139
+ `pricing: {"id":3}->{"after":{"values":{"discount":50,"id":3,"name":"Sofa","options":["Gray"],"tax":"4.250"}},"before":{},"key":{"id":3},"topic":"pricing"}` ,
12140
+ `pricing: {"id":2}->{"after":{},"before":{"values":{"discount":25.5,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"}},"key":{"id":2},"topic":"pricing"}` ,
12141
+ },
12142
+ },
12143
+ }
12131
12144
12132
- expected := [] string {
12133
- `pricing: {"id":1}->{"values":{"discount":15.75,"id":1,"name":"Chair","options":["Brown","Black"],"tax":"2.500"},"__crdb__":{"key":{"id":1},"topic":"pricing"}}` ,
12134
- `pricing: {"id":2}->{"values":{"discount":20,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"},"__crdb__":{"key":{"id":2},"topic":"pricing"}}` ,
12135
- }
12145
+ for _ , tc := range tests {
12146
+ t . Run ( fmt . Sprintf ( "envelope=%s" , tc . envelope ), func ( t * testing. T ) {
12147
+ testFn := func ( t * testing. T , s TestServer , f cdctest. TestFeedFactory ) {
12148
+ sqlDB := sqlutils . MakeSQLRunner ( s . DB )
12136
12149
12137
- assertPayloads (t , pricingFeed , expected )
12138
- }
12150
+ sqlDB .Exec (t , `
12151
+ CREATE TABLE pricing (
12152
+ id INT PRIMARY KEY,
12153
+ name STRING,
12154
+ discount FLOAT,
12155
+ tax DECIMAL,
12156
+ options STRING[]
12157
+ )` )
12158
+ sqlDB .Exec (t , `
12159
+ INSERT INTO pricing VALUES
12160
+ (1, 'Chair', 15.75, 2.500, ARRAY['Brown', 'Black']),
12161
+ (2, 'Table', 20.00, 1.23456789, ARRAY['Brown', 'Black'])` )
12162
+
12163
+ var opts []string
12164
+ opts = append (opts , fmt .Sprintf ("envelope='%s'" , tc .envelope ))
12165
+ opts = append (opts , "format='protobuf'" , "key_in_value" , "topic_in_value" )
12166
+ if tc .withDiff {
12167
+ opts = append (opts , "diff" )
12168
+ }
12139
12169
12140
- cdcTest (t , testFn , feedTestForceSink ("kafka" ))
12170
+ feed := feed (t , f , fmt .Sprintf ("CREATE CHANGEFEED FOR pricing WITH %s" , strings .Join (opts , ", " )))
12171
+ defer closeFeed (t , feed )
12172
+
12173
+ sqlDB .Exec (t , `UPDATE pricing SET discount = 25.50 WHERE id = 2` )
12174
+ sqlDB .Exec (t , `UPSERT INTO pricing (id, name, discount, tax, options) VALUES (1, 'Armchair', 10.00, 1.000, ARRAY['Red'])` )
12175
+ sqlDB .Exec (t , `INSERT INTO pricing VALUES (3, 'Sofa', 50.00, 4.250, ARRAY['Gray'])` )
12176
+ sqlDB .Exec (t , `DELETE FROM pricing WHERE id = 2` )
12177
+
12178
+ assertPayloads (t , feed , tc .expectedRows )
12179
+ }
12180
+ cdcTest (t , testFn , feedTestForceSink ("kafka" ))
12181
+ })
12182
+ }
12141
12183
}
0 commit comments