|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + vision "cloud.google.com/go/vision/apiv1" |
| 23 | + "google.golang.org/api/option" |
| 24 | + visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1" |
| 25 | +) |
| 26 | + |
| 27 | +func TestSetEndpoint(t *testing.T) { |
| 28 | + const endpoint = "eu-vision.googleapis.com:443" |
| 29 | + |
| 30 | + // Run the code sample to check for errors. |
| 31 | + err := setEndpoint(endpoint) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("setEndpoint: %v", err) |
| 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 | + } |
| 42 | + defer client.Close() |
| 43 | + |
| 44 | + image := &visionpb.Image{ |
| 45 | + Source: &visionpb.ImageSource{ |
| 46 | + GcsImageUri: "gs://cloud-samples-data/vision/text/screen.jpg", |
| 47 | + }, |
| 48 | + } |
| 49 | + texts, err := client.DetectTexts(ctx, image, nil, 1) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("DetectTexts: %v", err) |
| 52 | + } |
| 53 | + |
| 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) |
| 57 | + } |
| 58 | + if len(text.GetBoundingPoly().GetVertices()) == 0 { |
| 59 | + t.Errorf("text.GetBoundingPoly().getVertices() must have at least one vertex") |
| 60 | + } |
| 61 | +} |
0 commit comments