@@ -12057,37 +12057,79 @@ func TestDatabaseLevelChangefeed(t *testing.T) {
12057
12057
cdcTest (t , testFn )
12058
12058
}
12059
12059
12060
- func TestChangefeedBareFullProtobuf (t * testing.T ) {
12060
+ func TestChangefeedProtobuf (t * testing.T ) {
12061
12061
defer leaktest .AfterTest (t )()
12062
12062
defer log .Scope (t ).Close (t )
12063
12063
12064
- testFn := func (t * testing.T , s TestServer , f cdctest.TestFeedFactory ) {
12065
- sqlDB := sqlutils .MakeSQLRunner (s .DB )
12064
+ type testCase struct {
12065
+ envelope string
12066
+ withDiff bool
12067
+ expectedRows []string
12068
+ }
12066
12069
12067
- sqlDB .Exec (t , `
12068
- CREATE TABLE pricing (
12069
- id INT PRIMARY KEY,
12070
- name STRING,
12071
- discount FLOAT,
12072
- tax DECIMAL,
12073
- options STRING[]
12074
- )` )
12075
- sqlDB .Exec (t , `
12076
- INSERT INTO pricing VALUES
12077
- (1, 'Chair', 15.75, 2.500, ARRAY['Brown', 'Black']),
12078
- (2, 'Table', 20.00, 1.23456789, ARRAY['Brown', 'Black'])
12079
- ` )
12080
- pricingFeed := feed (t , f ,
12081
- `CREATE CHANGEFEED FOR pricing WITH envelope='bare', format='protobuf', key_in_value, topic_in_value` )
12082
- defer closeFeed (t , pricingFeed )
12070
+ tests := []testCase {
12071
+ {
12072
+ envelope : "bare" ,
12073
+ withDiff : false ,
12074
+ expectedRows : []string {
12075
+ `pricing: {"id":1}->{"values":{"discount":15.75,"id":1,"name":"Chair","options":["Brown","Black"],"tax":"2.500"},"__crdb__":{"key":{"id":1},"topic":"pricing"}}` ,
12076
+ `pricing: {"id":2}->{"values":{"discount":20,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"},"__crdb__":{"key":{"id":2},"topic":"pricing"}}` ,
12077
+ `pricing: {"id":2}->{"values":{"discount":25.5,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"},"__crdb__":{"key":{"id":2},"topic":"pricing"}}` ,
12078
+ `pricing: {"id":1}->{"values":{"discount":10,"id":1,"name":"Armchair","options":["Red"],"tax":"1.000"},"__crdb__":{"key":{"id":1},"topic":"pricing"}}` ,
12079
+ `pricing: {"id":3}->{"values":{"discount":50,"id":3,"name":"Sofa","options":["Gray"],"tax":"4.250"},"__crdb__":{"key":{"id":3},"topic":"pricing"}}` ,
12080
+ `pricing: {"id":2}->{"values":{"discount":null,"id":2,"name":null,"options":null,"tax":null},"__crdb__":{"key":{"id":2},"topic":"pricing"}}` ,
12081
+ },
12082
+ },
12083
+ {
12084
+ envelope : "wrapped" ,
12085
+ withDiff : true ,
12086
+ expectedRows : []string {
12087
+ `pricing: {"id":1}->{"after":{"values":{"discount":15.75,"id":1,"name":"Chair","options":["Brown","Black"],"tax":"2.500"}},"before":{},"key":{"id":1},"topic":"pricing"}` ,
12088
+ `pricing: {"id":2}->{"after":{"values":{"discount":20,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"}},"before":{},"key":{"id":2},"topic":"pricing"}` ,
12089
+ `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"}` ,
12090
+ `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"}` ,
12091
+ `pricing: {"id":3}->{"after":{"values":{"discount":50,"id":3,"name":"Sofa","options":["Gray"],"tax":"4.250"}},"before":{},"key":{"id":3},"topic":"pricing"}` ,
12092
+ `pricing: {"id":2}->{"after":{},"before":{"values":{"discount":25.5,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"}},"key":{"id":2},"topic":"pricing"}` ,
12093
+ },
12094
+ },
12095
+ }
12083
12096
12084
- expected := [] string {
12085
- `pricing: {"id":1}->{"values":{"discount":15.75,"id":1,"name":"Chair","options":["Brown","Black"],"tax":"2.500"},"__crdb__":{"key":{"id":1},"topic":"pricing"}}` ,
12086
- `pricing: {"id":2}->{"values":{"discount":20,"id":2,"name":"Table","options":["Brown","Black"],"tax":"1.23456789"},"__crdb__":{"key":{"id":2},"topic":"pricing"}}` ,
12087
- }
12097
+ for _ , tc := range tests {
12098
+ t . Run ( fmt . Sprintf ( "envelope=%s" , tc . envelope ), func ( t * testing. T ) {
12099
+ testFn := func ( t * testing. T , s TestServer , f cdctest. TestFeedFactory ) {
12100
+ sqlDB := sqlutils . MakeSQLRunner ( s . DB )
12088
12101
12089
- assertPayloads (t , pricingFeed , expected )
12090
- }
12102
+ sqlDB .Exec (t , `
12103
+ CREATE TABLE pricing (
12104
+ id INT PRIMARY KEY,
12105
+ name STRING,
12106
+ discount FLOAT,
12107
+ tax DECIMAL,
12108
+ options STRING[]
12109
+ )` )
12110
+ sqlDB .Exec (t , `
12111
+ INSERT INTO pricing VALUES
12112
+ (1, 'Chair', 15.75, 2.500, ARRAY['Brown', 'Black']),
12113
+ (2, 'Table', 20.00, 1.23456789, ARRAY['Brown', 'Black'])` )
12114
+
12115
+ var opts []string
12116
+ opts = append (opts , fmt .Sprintf ("envelope='%s'" , tc .envelope ))
12117
+ opts = append (opts , "format='protobuf'" , "key_in_value" , "topic_in_value" )
12118
+ if tc .withDiff {
12119
+ opts = append (opts , "diff" )
12120
+ }
12091
12121
12092
- cdcTest (t , testFn , feedTestForceSink ("kafka" ))
12122
+ feed := feed (t , f , fmt .Sprintf ("CREATE CHANGEFEED FOR pricing WITH %s" , strings .Join (opts , ", " )))
12123
+ defer closeFeed (t , feed )
12124
+
12125
+ sqlDB .Exec (t , `UPDATE pricing SET discount = 25.50 WHERE id = 2` )
12126
+ sqlDB .Exec (t , `UPSERT INTO pricing (id, name, discount, tax, options) VALUES (1, 'Armchair', 10.00, 1.000, ARRAY['Red'])` )
12127
+ sqlDB .Exec (t , `INSERT INTO pricing VALUES (3, 'Sofa', 50.00, 4.250, ARRAY['Gray'])` )
12128
+ sqlDB .Exec (t , `DELETE FROM pricing WHERE id = 2` )
12129
+
12130
+ assertPayloads (t , feed , tc .expectedRows )
12131
+ }
12132
+ cdcTest (t , testFn , feedTestForceSink ("kafka" ))
12133
+ })
12134
+ }
12093
12135
}
0 commit comments