From 9ef324791667a6343ce194be9ae553d8ebafd84c Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Fri, 25 Feb 2022 17:46:27 -0500 Subject: [PATCH] [BEAM-13910] Improve coverage of gcsx package (#16942) --- sdks/go/pkg/beam/util/gcsx/example_test.go | 47 +++++++++++ sdks/go/pkg/beam/util/gcsx/gcs_test.go | 91 +++++++++++++++++----- 2 files changed, 118 insertions(+), 20 deletions(-) create mode 100644 sdks/go/pkg/beam/util/gcsx/example_test.go diff --git a/sdks/go/pkg/beam/util/gcsx/example_test.go b/sdks/go/pkg/beam/util/gcsx/example_test.go new file mode 100644 index 0000000000000..52664a054a878 --- /dev/null +++ b/sdks/go/pkg/beam/util/gcsx/example_test.go @@ -0,0 +1,47 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gcsx_test + +import ( + "context" + "time" + + "cloud.google.com/go/storage" + "github.com/apache/beam/sdks/v2/go/pkg/beam/util/gcsx" +) + +func Example() { + ctx := context.Background() + c, err := gcsx.NewClient(ctx, storage.ScopeReadOnly) + if err != nil { + // do something + } + + buckets, object, err := gcsx.ParseObject("gs://some-bucket/some-object") + if err != nil { + // do something + } + + ctx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + + bytes, err := gcsx.ReadObject(ctx, c, buckets, object) + if err != nil { + // do something + } + + _ = bytes +} diff --git a/sdks/go/pkg/beam/util/gcsx/gcs_test.go b/sdks/go/pkg/beam/util/gcsx/gcs_test.go index 52664a054a878..90fb4b59f2fe8 100644 --- a/sdks/go/pkg/beam/util/gcsx/gcs_test.go +++ b/sdks/go/pkg/beam/util/gcsx/gcs_test.go @@ -13,35 +13,86 @@ // See the License for the specific language governing permissions and // limitations under the License. -package gcsx_test +package gcsx import ( - "context" - "time" + "strings" + "testing" - "cloud.google.com/go/storage" - "github.com/apache/beam/sdks/v2/go/pkg/beam/util/gcsx" + "github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors" ) -func Example() { - ctx := context.Background() - c, err := gcsx.NewClient(ctx, storage.ScopeReadOnly) - if err != nil { - // do something +func TestMakeObject(t *testing.T) { + if got, want := MakeObject("some-bucket", "some/path"), "gs://some-bucket/some/path"; got != want { + t.Fatalf("MakeObject() Got: %v Want: %v", got, want) } +} - buckets, object, err := gcsx.ParseObject("gs://some-bucket/some-object") - if err != nil { - // do something +func TestParseObject(t *testing.T) { + tests := []struct { + object string + bucket string + path string + err error + }{ + { + object: "gs://some-bucket/some-object", + bucket: "some-bucket", + path: "some-object", + err: nil, + }, + { + object: "gs://some-bucket", + bucket: "some-bucket", + path: "", + err: nil, + }, + { + object: "gs://", + bucket: "", + path: "", + err: errors.Errorf("object gs:// must have bucket"), + }, + { + object: "other://some-bucket/some-object", + bucket: "", + path: "", + err: errors.Errorf("object other://some-bucket/some-object must have 'gs' scheme"), + }, } - ctx, cancel := context.WithTimeout(ctx, 30*time.Second) - defer cancel() - - bytes, err := gcsx.ReadObject(ctx, c, buckets, object) - if err != nil { - // do something + for _, test := range tests { + if bucket, path, err := ParseObject(test.object); bucket != test.bucket || path != test.path || (err != nil && test.err == nil) || (err == nil && test.err != nil) { + t.Errorf("ParseObject(%v) Got: %v, %v, %v Want: %v, %v, %v", test.object, bucket, path, err, test.bucket, test.path, test.err) + } } +} - _ = bytes +func TestJoin(t *testing.T) { + tests := []struct { + object string + elms []string + result string + }{ + { + object: "gs://some-bucket/some-object", + elms: []string{"some/path", "more/pathing"}, + result: "gs://some-bucket/some-object/some/path/more/pathing", + }, + { + object: "gs://some-bucket/some-object", + elms: []string{"some/path"}, + result: "gs://some-bucket/some-object/some/path", + }, + { + object: "gs://some-bucket/some-object", + elms: []string{}, + result: "gs://some-bucket/some-object", + }, + } + for _, test := range tests { + if got, want := Join(test.object, test.elms...), test.result; got != want { + t.Errorf("Join(%v, %v) Got: %v Want: %v", test.object, strings.Join(test.elms, ", "), got, want) + } + } }