15
15
package main
16
16
17
17
import (
18
- "bytes"
19
18
"context"
20
- "fmt"
21
19
"strings"
22
20
"testing"
23
21
22
+ vision "cloud.google.com/go/vision/apiv1"
23
+ "google.golang.org/api/option"
24
24
visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
25
25
)
26
26
27
27
func TestSetEndpoint (t * testing.T ) {
28
- ctx := context .Background ()
29
-
30
28
const endpoint = "eu-vision.googleapis.com:443"
31
29
32
- client , err := setEndpoint (ctx , endpoint )
30
+ // Run the code sample to check for errors.
31
+ err := setEndpoint (endpoint )
33
32
if err != nil {
34
33
t .Fatalf ("setEndpoint: %v" , err )
35
34
}
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
+ }
36
42
defer client .Close ()
37
43
38
44
image := & visionpb.Image {
@@ -45,19 +51,11 @@ func TestSetEndpoint(t *testing.T) {
45
51
t .Fatalf ("DetectTexts: %v" , err )
46
52
}
47
53
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----\n Want 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----\n Want to contain:\n ----\n %s\n ----" , got , want )
59
57
}
60
- if got , want := buf . String (), "bounding vertex:" ; ! strings . Contains ( got , want ) {
61
- t .Errorf ("setEndpoint got: \n ---- \n %s---- \n Want 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" )
62
60
}
63
61
}
0 commit comments