Skip to content

Commit f2db374

Browse files
committed
moved testing code from sample to test file
1 parent 721bfb6 commit f2db374

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

vision/detect/set_endpoint.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,23 @@ package main
1818
import (
1919
"context"
2020
"fmt"
21-
"io"
2221

2322
vision "cloud.google.com/go/vision/apiv1"
2423
"google.golang.org/api/option"
25-
visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
2624
)
2725

2826
// setEndpoint changes your endpoint.
29-
func setEndpoint(w io.Writer, endpoint string) error {
27+
func setEndpoint(ctx context.Context, endpoint string) (*vision.ImageAnnotatorClient, error) {
3028
// endpoint := "eu-vision.googleapis.com:443"
3129

32-
// Create a client with a custom endpoint.
33-
ctx := context.Background()
30+
// ctx := context.Background()
3431
client, err := vision.NewImageAnnotatorClient(ctx, option.WithEndpoint(endpoint))
3532
if err != nil {
36-
return fmt.Errorf("NewImageAnnotatorClient: %v", err)
33+
return nil, fmt.Errorf("NewImageAnnotatorClient: %v", err)
3734
}
38-
defer client.Close()
35+
// defer client.Close()
3936

40-
// Use the client with custom endpoint to detect texts on an image.
41-
image := &visionpb.Image{
42-
Source: &visionpb.ImageSource{
43-
GcsImageUri: "gs://cloud-samples-data/vision/text/screen.jpg",
44-
},
45-
}
46-
maxResults := 3
47-
texts, err := client.DetectTexts(ctx, image, nil, maxResults)
48-
if err != nil {
49-
return fmt.Errorf("DetectTexts: %v", err)
50-
}
51-
52-
fmt.Fprintf(w, "Texts:\n")
53-
for _, text := range texts {
54-
fmt.Fprintf(w, "%v\n", text.GetDescription())
55-
for _, vertex := range text.GetBoundingPoly().GetVertices() {
56-
fmt.Fprintf(w, " bounding vertex: %v, %v\n", vertex.GetX(), vertex.GetY())
57-
}
58-
}
59-
60-
return nil
37+
return client, nil
6138
}
6239

6340
// [END vision_set_endpoint]

vision/detect/set_endpoint_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,43 @@ package main
1616

1717
import (
1818
"bytes"
19+
"context"
20+
"fmt"
1921
"strings"
2022
"testing"
23+
24+
visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
2125
)
2226

2327
func TestSetEndpoint(t *testing.T) {
24-
const endpoint = "eu-vision.googleapis.com:443"
28+
ctx := context.Background()
2529

26-
var buf bytes.Buffer
30+
const endpoint = "eu-vision.googleapis.com:443"
2731

28-
if err := setEndpoint(&buf, endpoint); err != nil {
32+
client, err := setEndpoint(ctx, endpoint)
33+
if err != nil {
2934
t.Fatalf("setEndpoint: %v", err)
3035
}
36+
defer client.Close()
37+
38+
image := &visionpb.Image{
39+
Source: &visionpb.ImageSource{
40+
GcsImageUri: "gs://cloud-samples-data/vision/text/screen.jpg",
41+
},
42+
}
43+
texts, err := client.DetectTexts(ctx, image, nil, 1)
44+
if err != nil {
45+
t.Fatalf("DetectTexts: %v", err)
46+
}
47+
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+
}
3156

3257
if got, want := buf.String(), "System"; !strings.Contains(got, want) {
3358
t.Errorf("setEndpoint got:\n----\n%s----\nWant to contain:\n----\n%s\n----", got, want)

0 commit comments

Comments
 (0)