Skip to content

Commit 6723252

Browse files
committed
uncommented boilerplate and simplified test
1 parent 3d552f4 commit 6723252

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

vision/detect/set_endpoint.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ import (
2424
)
2525

2626
// setEndpoint changes your endpoint.
27-
func setEndpoint(ctx context.Context, endpoint string) (*vision.ImageAnnotatorClient, error) {
27+
func setEndpoint(endpoint string) error {
2828
// endpoint := "eu-vision.googleapis.com:443"
2929

30-
// ctx := context.Background()
30+
ctx := context.Background()
3131
client, err := vision.NewImageAnnotatorClient(ctx, option.WithEndpoint(endpoint))
3232
if err != nil {
33-
return nil, fmt.Errorf("NewImageAnnotatorClient: %v", err)
33+
return fmt.Errorf("NewImageAnnotatorClient: %v", err)
3434
}
35-
// defer client.Close()
35+
defer client.Close()
3636

37-
return client, nil
37+
return nil
3838
}
3939

4040
// [END vision_set_endpoint]

vision/detect/set_endpoint_test.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,30 @@
1515
package main
1616

1717
import (
18-
"bytes"
1918
"context"
20-
"fmt"
2119
"strings"
2220
"testing"
2321

22+
vision "cloud.google.com/go/vision/apiv1"
23+
"google.golang.org/api/option"
2424
visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
2525
)
2626

2727
func TestSetEndpoint(t *testing.T) {
28-
ctx := context.Background()
29-
3028
const endpoint = "eu-vision.googleapis.com:443"
3129

32-
client, err := setEndpoint(ctx, endpoint)
30+
// Run the code sample to check for errors.
31+
err := setEndpoint(endpoint)
3332
if err != nil {
3433
t.Fatalf("setEndpoint: %v", err)
3534
}
35+
36+
// Since we're not returning the client from the code sample, we create an equivalent client here.
37+
ctx := context.Background()
38+
client, err := vision.NewImageAnnotatorClient(ctx, option.WithEndpoint(endpoint))
39+
if err != nil {
40+
t.Fatalf("NewImageAnnotatorClient: %v", err)
41+
}
3642
defer client.Close()
3743

3844
image := &visionpb.Image{
@@ -45,19 +51,11 @@ func TestSetEndpoint(t *testing.T) {
4551
t.Fatalf("DetectTexts: %v", err)
4652
}
4753

48-
var buf bytes.Buffer
49-
fmt.Fprintf(&buf, "Texts:\n")
50-
for _, text := range texts {
51-
fmt.Fprintf(&buf, "%v\n", text.GetDescription())
52-
for _, vertex := range text.GetBoundingPoly().GetVertices() {
53-
fmt.Fprintf(&buf, " bounding vertex: %v, %v\n", vertex.GetX(), vertex.GetY())
54-
}
55-
}
56-
57-
if got, want := buf.String(), "System"; !strings.Contains(got, want) {
58-
t.Errorf("setEndpoint got:\n----\n%s----\nWant to contain:\n----\n%s\n----", got, want)
54+
text := texts[0]
55+
if got, want := text.GetDescription(), "System"; !strings.Contains(got, want) {
56+
t.Errorf("text.GetDescription() got:\n----\n%s----\nWant to contain:\n----\n%s\n----", got, want)
5957
}
60-
if got, want := buf.String(), "bounding vertex:"; !strings.Contains(got, want) {
61-
t.Errorf("setEndpoint got:\n----\n%s----\nWant to contain:\n----\n%s\n----", got, want)
58+
if len(text.GetBoundingPoly().GetVertices()) == 0 {
59+
t.Errorf("text.GetBoundingPoly().getVertices() must have at least one vertex")
6260
}
6361
}

0 commit comments

Comments
 (0)