Skip to content

Commit 92d9d4c

Browse files
authored
refactor: augment private getWriteStream with view support (#7196)
This PR augments the base client with fuller support for view-based resolution of GetWriteStream metadata. This PR also adds an integration test that compares behaviors between different stream types (default vs explicitly created). Towards: #7103
1 parent 3592917 commit 92d9d4c

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

bigquery/storage/managedwriter/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (c *Client) validateOptions(ctx context.Context, ms *ManagedStream) error {
169169
}
170170
if ms.streamSettings.streamID != "" {
171171
// User supplied a stream, we need to verify it exists.
172-
info, err := c.getWriteStream(ctx, ms.streamSettings.streamID)
172+
info, err := c.getWriteStream(ctx, ms.streamSettings.streamID, false)
173173
if err != nil {
174174
return fmt.Errorf("a streamname was specified, but lookup of stream failed: %v", err)
175175
}
@@ -210,10 +210,13 @@ func (c *Client) CreateWriteStream(ctx context.Context, req *storagepb.CreateWri
210210
// getWriteStream returns information about a given write stream.
211211
//
212212
// It's primarily used for setup validation, and not exposed directly to end users.
213-
func (c *Client) getWriteStream(ctx context.Context, streamName string) (*storagepb.WriteStream, error) {
213+
func (c *Client) getWriteStream(ctx context.Context, streamName string, fullView bool) (*storagepb.WriteStream, error) {
214214
req := &storagepb.GetWriteStreamRequest{
215215
Name: streamName,
216216
}
217+
if fullView {
218+
req.View = storagepb.WriteStreamView_FULL
219+
}
217220
return c.rawClient.GetWriteStream(ctx, req)
218221
}
219222

bigquery/storage/managedwriter/integration_test.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"cloud.google.com/go/bigquery/storage/managedwriter/testdata"
2929
"cloud.google.com/go/internal/testutil"
3030
"cloud.google.com/go/internal/uid"
31+
"github.com/google/go-cmp/cmp"
3132
"github.com/googleapis/gax-go/v2/apierror"
3233
"go.opencensus.io/stats/view"
3334
"google.golang.org/api/option"
@@ -36,6 +37,7 @@ import (
3637
"google.golang.org/protobuf/proto"
3738
"google.golang.org/protobuf/reflect/protodesc"
3839
"google.golang.org/protobuf/reflect/protoreflect"
40+
"google.golang.org/protobuf/testing/protocmp"
3941
"google.golang.org/protobuf/types/descriptorpb"
4042
"google.golang.org/protobuf/types/dynamicpb"
4143
"google.golang.org/protobuf/types/known/wrapperspb"
@@ -112,6 +114,94 @@ func setupDynamicDescriptors(t *testing.T, schema bigquery.Schema) (protoreflect
112114
return messageDescriptor, protodesc.ToDescriptorProto(messageDescriptor)
113115
}
114116

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+
115205
func TestIntegration_ManagedWriter(t *testing.T) {
116206
mwClient, bqClient := getTestClients(context.Background(), t)
117207
defer mwClient.Close()
@@ -326,7 +416,7 @@ func testBufferedStream(ctx context.Context, t *testing.T, mwClient *Client, bqC
326416
t.Fatalf("NewManagedStream: %v", err)
327417
}
328418

329-
info, err := ms.c.getWriteStream(ctx, ms.streamSettings.streamID)
419+
info, err := ms.c.getWriteStream(ctx, ms.streamSettings.streamID, false)
330420
if err != nil {
331421
t.Errorf("couldn't get stream info: %v", err)
332422
}

0 commit comments

Comments
 (0)