forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_test.go
135 lines (118 loc) · 3.67 KB
/
detect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"testing"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
)
func TestDetect(t *testing.T) {
testutil.SystemTest(t)
tests := []struct {
name string
local, gcs func(io.Writer, string) error
path string
wantContain string
}{
{"Labels", detectLabels, nil, "cat.jpg", "cat"},
{"Labels2", detectLabels, detectLabelsURI, "wakeupcat.jpg", "whiskers"},
{"Faces", detectFaces, detectFacesURI, "face_no_surprise.jpg", "Anger"},
{"Landmarks", detectLandmarks, detectLandmarksURI, "landmark.jpg", "Palace"},
{"Logos", detectLogos, detectLogosURI, "logos.png", "Google"},
{"Properties", detectProperties, detectPropertiesURI, "landmark.jpg", "%"},
{"SafeSearch", detectSafeSearch, detectSafeSearchURI, "wakeupcat.jpg", "Spoofed"},
{"Text", detectText, detectTextURI, "text.jpg", "Preparing to install"},
{"FullText", detectDocumentText, detectDocumentTextURI, "text.jpg", "Preparing to install"},
{"Crop", detectCropHints, detectCropHintsURI, "wakeupcat.jpg", "(0,0)"},
{"Web", detectWeb, detectWebURI, "wakeupcat.jpg", "Web properties"},
{"WebGeo", nil, detectWebGeoURI, "city.jpg", "Entities"},
{"Objects", localizeObjects, localizeObjectsURI, "puppies.jpg", "Dog"},
}
for _, tt := range tests {
if tt.local == nil {
continue
}
var buf bytes.Buffer
err := tt.local(&buf, "../testdata/"+tt.path)
if err != nil {
t.Fatalf("Local %s(%q): got %v, want nil err", tt.name, tt.path, err)
}
if got := buf.String(); !strings.Contains(got, tt.wantContain) {
t.Errorf("Local %s(%q): got %q, want to contain %q", tt.name, tt.path, got, tt.wantContain)
}
}
for _, tt := range tests {
if tt.gcs == nil {
continue
}
var buf bytes.Buffer
err := tt.gcs(&buf, "gs://python-docs-samples-tests/vision/"+tt.path)
if err != nil {
t.Fatalf("GCS %s(%q): got %v, want nil err", tt.name, tt.path, err)
}
if got := buf.String(); !strings.Contains(got, tt.wantContain) {
t.Errorf("GCS %s(%q): got %q, want to contain %q", tt.name, tt.path, got, tt.wantContain)
}
}
}
func TestDetectAsyncDocument(t *testing.T) {
tc := testutil.SystemTest(t)
ctx := context.Background()
// Create a temporary bucket
client, err := storage.NewClient(ctx)
if err != nil {
t.Fatal(err)
}
bucketName := fmt.Sprintf("%s-golang-samples-%d", tc.ProjectID, time.Now().Unix())
bucket := client.Bucket(bucketName)
if err := bucket.Create(ctx, tc.ProjectID, nil); err != nil {
t.Fatal(err)
}
// Clean and delete the bucket at the end of the test
defer func() {
it := bucket.Objects(ctx, nil)
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
t.Fatal(err)
}
if err := bucket.Object(attrs.Name).Delete(ctx); err != nil {
t.Fatal(err)
}
}
if err := bucket.Delete(ctx); err != nil {
t.Fatal(err)
}
}()
// Run the test
var buf bytes.Buffer
gcsSourceURI := "gs://python-docs-samples-tests/HodgeConj.pdf"
gcsDestinationURI := "gs://" + bucketName + "/vision/"
err = detectAsyncDocument(&buf, gcsSourceURI, gcsDestinationURI)
if err != nil {
t.Fatal(err)
}
// Check that the output files exist
expectedFiles := []string{
"vision/output-1-to-2.json",
"vision/output-3-to-4.json",
"vision/output-5-to-5.json",
}
for _, filename := range expectedFiles {
_, err = bucket.Object(filename).Attrs(ctx)
if err != nil {
t.Fatalf("wanted object %q, got error: %v", filename, err)
}
}
}