15
15
package managedwriter
16
16
17
17
import (
18
- "bytes"
19
18
"context"
20
19
"fmt"
21
20
"testing"
@@ -24,42 +23,36 @@ import (
24
23
"cloud.google.com/go/bigquery/storage/apiv1/storagepb"
25
24
"github.com/google/go-cmp/cmp"
26
25
"google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1"
26
+ "google.golang.org/protobuf/proto"
27
27
"google.golang.org/protobuf/testing/protocmp"
28
+ "google.golang.org/protobuf/types/descriptorpb"
28
29
"google.golang.org/protobuf/types/known/wrapperspb"
29
30
)
30
31
31
- func TestAppendResult (t * testing.T ) {
32
-
33
- wantRowBytes := [][]byte {[]byte ("rowdata" )}
34
-
35
- gotAR := newAppendResult (wantRowBytes )
36
- if len (gotAR .rowData ) != len (wantRowBytes ) {
37
- t .Fatalf ("length mismatch, got %d want %d elements" , len (gotAR .rowData ), len (wantRowBytes ))
38
- }
39
- for i := 0 ; i < len (gotAR .rowData ); i ++ {
40
- if ! bytes .Equal (gotAR .rowData [i ], wantRowBytes [i ]) {
41
- t .Errorf ("mismatch in row data %d, got %q want %q" , i , gotAR .rowData , wantRowBytes )
42
- }
43
- }
44
- }
45
-
46
32
func TestPendingWrite (t * testing.T ) {
47
33
ctx := context .Background ()
48
- wantRowData := [][]byte {
49
- []byte ("row1" ),
50
- []byte ("row2" ),
51
- []byte ("row3" ),
34
+ wantReq := & storagepb.AppendRowsRequest {
35
+ Rows : & storagepb.AppendRowsRequest_ProtoRows {
36
+ ProtoRows : & storagepb.AppendRowsRequest_ProtoData {
37
+ Rows : & storagepb.ProtoRows {
38
+ SerializedRows : [][]byte {
39
+ []byte ("row1" ),
40
+ []byte ("row2" ),
41
+ []byte ("row3" ),
42
+ },
43
+ },
44
+ },
45
+ },
52
46
}
53
47
54
48
// verify no offset behavior
55
- pending := newPendingWrite (ctx , wantRowData )
56
- if pending .request .GetOffset () != nil {
57
- t .Errorf ("request should have no offset, but is present: %q" , pending .request .GetOffset ().GetValue ())
49
+ pending := newPendingWrite (ctx , nil , wantReq , nil , "" , "" )
50
+ if pending .req .GetOffset () != nil {
51
+ t .Errorf ("request should have no offset, but is present: %q" , pending .req .GetOffset ().GetValue ())
58
52
}
59
53
60
- gotRowCount := len (pending .request .GetProtoRows ().GetRows ().GetSerializedRows ())
61
- if gotRowCount != len (wantRowData ) {
62
- t .Errorf ("pendingWrite request mismatch, got %d rows, want %d rows" , gotRowCount , len (wantRowData ))
54
+ if diff := cmp .Diff (pending .req , wantReq , protocmp .Transform ()); diff != "" {
55
+ t .Errorf ("request mismatch: -got, +want:\n %s" , diff )
63
56
}
64
57
65
58
// Verify request is not acknowledged.
@@ -71,37 +64,28 @@ func TestPendingWrite(t *testing.T) {
71
64
}
72
65
73
66
// Mark completed, verify result.
74
- pending .markDone (& storage.AppendRowsResponse {}, nil , nil )
67
+ pending .markDone (& storage.AppendRowsResponse {}, nil )
75
68
if gotOff := pending .result .offset (ctx ); gotOff != NoStreamOffset {
76
69
t .Errorf ("mismatch on completed AppendResult without offset: got %d want %d" , gotOff , NoStreamOffset )
77
70
}
78
71
if pending .result .err != nil {
79
72
t .Errorf ("mismatch in error on AppendResult, got %v want nil" , pending .result .err )
80
73
}
81
- gotData := pending .result .rowData
82
- if len (gotData ) != len (wantRowData ) {
83
- t .Errorf ("length mismatch on appendresult, got %d, want %d" , len (gotData ), len (wantRowData ))
84
- }
85
- for i := 0 ; i < len (gotData ); i ++ {
86
- if ! bytes .Equal (gotData [i ], wantRowData [i ]) {
87
- t .Errorf ("row %d mismatch in data: got %q want %q" , i , gotData [i ], wantRowData [i ])
88
- }
89
- }
90
74
91
75
// Create new write to verify error result.
92
- pending = newPendingWrite (ctx , wantRowData )
76
+ pending = newPendingWrite (ctx , nil , wantReq , nil , "" , "" )
93
77
94
78
// Manually invoke option to apply offset to request.
95
79
// This would normally be appied as part of the AppendRows() method on the managed stream.
96
80
wantOffset := int64 (101 )
97
81
f := WithOffset (wantOffset )
98
82
f (pending )
99
83
100
- if pending .request .GetOffset () == nil {
84
+ if pending .req .GetOffset () == nil {
101
85
t .Errorf ("expected offset, got none" )
102
86
}
103
- if pending .request .GetOffset ().GetValue () != wantOffset {
104
- t .Errorf ("offset mismatch, got %d wanted %d" , pending .request .GetOffset ().GetValue (), wantOffset )
87
+ if pending .req .GetOffset ().GetValue () != wantOffset {
88
+ t .Errorf ("offset mismatch, got %d wanted %d" , pending .req .GetOffset ().GetValue (), wantOffset )
105
89
}
106
90
107
91
// Verify completion behavior with an error.
@@ -116,19 +100,10 @@ func TestPendingWrite(t *testing.T) {
116
100
},
117
101
},
118
102
}
119
- pending .markDone (testResp , wantErr , nil )
103
+ pending .markDone (testResp , wantErr )
120
104
121
- if pending .request != nil {
122
- t .Errorf ("expected request to be cleared, is present: %#v" , pending .request )
123
- }
124
- gotData = pending .result .rowData
125
- if len (gotData ) != len (wantRowData ) {
126
- t .Errorf ("length mismatch in data: got %d, want %d" , len (gotData ), len (wantRowData ))
127
- }
128
- for i := 0 ; i < len (gotData ); i ++ {
129
- if ! bytes .Equal (gotData [i ], wantRowData [i ]) {
130
- t .Errorf ("row %d mismatch in data: got %q want %q" , i , gotData [i ], wantRowData [i ])
131
- }
105
+ if pending .req != nil {
106
+ t .Errorf ("expected request to be cleared, is present: %#v" , pending .req )
132
107
}
133
108
134
109
select {
@@ -153,3 +128,93 @@ func TestPendingWrite(t *testing.T) {
153
128
}
154
129
}
155
130
}
131
+
132
+ func TestPendingWrite_ConstructFullRequest (t * testing.T ) {
133
+
134
+ testDP := & descriptorpb.DescriptorProto {Name : proto .String ("foo" )}
135
+ testDV := newDescriptorVersion (testDP )
136
+ testEmptyTraceID := buildTraceID (& streamSettings {})
137
+
138
+ for _ , tc := range []struct {
139
+ desc string
140
+ pw * pendingWrite
141
+ addTrace bool
142
+ want * storagepb.AppendRowsRequest
143
+ }{
144
+ {
145
+ desc : "nil request" ,
146
+ pw : & pendingWrite {
147
+ descVersion : testDV ,
148
+ },
149
+ want : & storagepb.AppendRowsRequest {
150
+ Rows : & storagepb.AppendRowsRequest_ProtoRows {
151
+ ProtoRows : & storagepb.AppendRowsRequest_ProtoData {
152
+ WriterSchema : & storagepb.ProtoSchema {
153
+ ProtoDescriptor : testDP ,
154
+ },
155
+ },
156
+ },
157
+ },
158
+ },
159
+ {
160
+ desc : "empty req w/trace" ,
161
+ pw : & pendingWrite {
162
+ req : & storagepb.AppendRowsRequest {},
163
+ descVersion : testDV ,
164
+ },
165
+ addTrace : true ,
166
+ want : & storagepb.AppendRowsRequest {
167
+ Rows : & storagepb.AppendRowsRequest_ProtoRows {
168
+ ProtoRows : & storagepb.AppendRowsRequest_ProtoData {
169
+ WriterSchema : & storagepb.ProtoSchema {
170
+ ProtoDescriptor : testDP ,
171
+ },
172
+ },
173
+ },
174
+ TraceId : testEmptyTraceID ,
175
+ },
176
+ },
177
+ {
178
+ desc : "basic req" ,
179
+ pw : & pendingWrite {
180
+ req : & storagepb.AppendRowsRequest {},
181
+ descVersion : testDV ,
182
+ },
183
+ want : & storagepb.AppendRowsRequest {
184
+ Rows : & storagepb.AppendRowsRequest_ProtoRows {
185
+ ProtoRows : & storagepb.AppendRowsRequest_ProtoData {
186
+ WriterSchema : & storagepb.ProtoSchema {
187
+ ProtoDescriptor : testDP ,
188
+ },
189
+ },
190
+ },
191
+ },
192
+ },
193
+ {
194
+ desc : "everything w/trace" ,
195
+ pw : & pendingWrite {
196
+ req : & storagepb.AppendRowsRequest {},
197
+ descVersion : testDV ,
198
+ traceID : "foo" ,
199
+ writeStreamID : "streamid" ,
200
+ },
201
+ addTrace : true ,
202
+ want : & storagepb.AppendRowsRequest {
203
+ WriteStream : "streamid" ,
204
+ Rows : & storagepb.AppendRowsRequest_ProtoRows {
205
+ ProtoRows : & storagepb.AppendRowsRequest_ProtoData {
206
+ WriterSchema : & storagepb.ProtoSchema {
207
+ ProtoDescriptor : testDP ,
208
+ },
209
+ },
210
+ },
211
+ TraceId : buildTraceID (& streamSettings {TraceID : "foo" }),
212
+ },
213
+ },
214
+ } {
215
+ got := tc .pw .constructFullRequest (tc .addTrace )
216
+ if diff := cmp .Diff (got , tc .want , protocmp .Transform ()); diff != "" {
217
+ t .Errorf ("%s diff: %s" , tc .desc , diff )
218
+ }
219
+ }
220
+ }
0 commit comments