@@ -28,6 +28,7 @@ import (
28
28
"cloud.google.com/go/bigquery/storage/managedwriter/testdata"
29
29
"cloud.google.com/go/internal/testutil"
30
30
"cloud.google.com/go/internal/uid"
31
+ "github.com/google/go-cmp/cmp"
31
32
"github.com/googleapis/gax-go/v2/apierror"
32
33
"go.opencensus.io/stats/view"
33
34
"google.golang.org/api/option"
@@ -36,6 +37,7 @@ import (
36
37
"google.golang.org/protobuf/proto"
37
38
"google.golang.org/protobuf/reflect/protodesc"
38
39
"google.golang.org/protobuf/reflect/protoreflect"
40
+ "google.golang.org/protobuf/testing/protocmp"
39
41
"google.golang.org/protobuf/types/descriptorpb"
40
42
"google.golang.org/protobuf/types/dynamicpb"
41
43
"google.golang.org/protobuf/types/known/wrapperspb"
@@ -112,6 +114,94 @@ func setupDynamicDescriptors(t *testing.T, schema bigquery.Schema) (protoreflect
112
114
return messageDescriptor , protodesc .ToDescriptorProto (messageDescriptor )
113
115
}
114
116
117
+ func TestIntegration_ClientGetWriteStream (t * testing.T ) {
118
+ ctx := context .Background ()
119
+ mwClient , bqClient := getTestClients (ctx , t )
120
+ defer mwClient .Close ()
121
+ defer bqClient .Close ()
122
+
123
+ wantLocation := "us-east1"
124
+ dataset , cleanup , err := setupTestDataset (ctx , t , bqClient , wantLocation )
125
+ if err != nil {
126
+ t .Fatalf ("failed to init test dataset: %v" , err )
127
+ }
128
+ defer cleanup ()
129
+
130
+ testTable := dataset .Table (tableIDs .New ())
131
+ if err := testTable .Create (ctx , & bigquery.TableMetadata {Schema : testdata .SimpleMessageSchema }); err != nil {
132
+ t .Fatalf ("failed to create test table %q: %v" , testTable .FullyQualifiedName (), err )
133
+ }
134
+
135
+ apiSchema , _ := adapt .BQSchemaToStorageTableSchema (testdata .SimpleMessageSchema )
136
+ parent := TableParentFromParts (testTable .ProjectID , testTable .DatasetID , testTable .TableID )
137
+ explicitStream , err := mwClient .CreateWriteStream (ctx , & storagepb.CreateWriteStreamRequest {
138
+ Parent : parent ,
139
+ WriteStream : & storagepb.WriteStream {
140
+ Type : storagepb .WriteStream_PENDING ,
141
+ },
142
+ })
143
+ if err != nil {
144
+ t .Fatalf ("CreateWriteStream: %v" , err )
145
+ }
146
+
147
+ testCases := []struct {
148
+ description string
149
+ isDefault bool
150
+ streamID string
151
+ wantType storagepb.WriteStream_Type
152
+ }{
153
+ {
154
+ description : "default" ,
155
+ isDefault : true ,
156
+ streamID : fmt .Sprintf ("%s/streams/_default" , parent ),
157
+ wantType : storagepb .WriteStream_COMMITTED ,
158
+ },
159
+ {
160
+ description : "explicit pending" ,
161
+ streamID : explicitStream .Name ,
162
+ wantType : storagepb .WriteStream_PENDING ,
163
+ },
164
+ }
165
+
166
+ for _ , tc := range testCases {
167
+ for _ , fullView := range []bool {false , true } {
168
+ info , err := mwClient .getWriteStream (ctx , tc .streamID , fullView )
169
+ if err != nil {
170
+ t .Errorf ("%s (%T): getWriteStream failed: %v" , tc .description , fullView , err )
171
+ }
172
+ if info .GetType () != tc .wantType {
173
+ t .Errorf ("%s (%T): got type %d, want type %d" , tc .description , fullView , info .GetType (), tc .wantType )
174
+ }
175
+ if info .GetLocation () != wantLocation {
176
+ t .Errorf ("%s (%T) view: got location %s, want location %s" , tc .description , fullView , info .GetLocation (), wantLocation )
177
+ }
178
+ if info .GetCommitTime () != nil {
179
+ t .Errorf ("%s (%T)expected empty commit time, got %v" , tc .description , fullView , info .GetCommitTime ())
180
+ }
181
+
182
+ if ! tc .isDefault {
183
+ if info .GetCreateTime () == nil {
184
+ t .Errorf ("%s (%T): expected create time, was empty" , tc .description , fullView )
185
+ }
186
+ } else {
187
+ if info .GetCreateTime () != nil {
188
+ t .Errorf ("%s (%T): expected empty time, got %v" , tc .description , fullView , info .GetCreateTime ())
189
+ }
190
+ }
191
+
192
+ if ! fullView {
193
+ if info .GetTableSchema () != nil {
194
+ t .Errorf ("%s (%T) basic view: expected no schema, was populated" , tc .description , fullView )
195
+ }
196
+ } else {
197
+ if diff := cmp .Diff (info .GetTableSchema (), apiSchema , protocmp .Transform ()); diff != "" {
198
+ t .Errorf ("%s (%T) schema mismatch: -got, +want:\n %s" , tc .description , fullView , diff )
199
+ }
200
+ }
201
+ }
202
+ }
203
+ }
204
+
115
205
func TestIntegration_ManagedWriter (t * testing.T ) {
116
206
mwClient , bqClient := getTestClients (context .Background (), t )
117
207
defer mwClient .Close ()
@@ -326,7 +416,7 @@ func testBufferedStream(ctx context.Context, t *testing.T, mwClient *Client, bqC
326
416
t .Fatalf ("NewManagedStream: %v" , err )
327
417
}
328
418
329
- info , err := ms .c .getWriteStream (ctx , ms .streamSettings .streamID )
419
+ info , err := ms .c .getWriteStream (ctx , ms .streamSettings .streamID , false )
330
420
if err != nil {
331
421
t .Errorf ("couldn't get stream info: %v" , err )
332
422
}
0 commit comments