| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 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 |
"encoding/json" |
| 20 |
"io" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/google/go-cmp/cmp" |
| 26 |
"gocloud.dev/blob" |
| 27 |
"gocloud.dev/pubsub" |
| 28 |
_ "gocloud.dev/pubsub/mempubsub" |
| 29 |
) |
| 30 |
|
| 31 |
func TestHandleRequest(t *testing.T) { |
| 32 |
f, p, cleanup, err := setup(testConfig("ProcessorRun")) |
| 33 |
if err != nil { |
| 34 |
t.Fatal(err) |
| 35 |
} |
| 36 |
defer cleanup() |
| 37 |
|
| 38 |
ctx := context.Background() |
| 39 |
req := &OrderRequest{ID: "x"} |
| 40 |
bytes, err := json.Marshal(req) |
| 41 |
if err != nil { |
| 42 |
t.Fatal(err) |
| 43 |
} |
| 44 |
if err := f.requestTopic.Send(ctx, &pubsub.Message{Body: bytes}); err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
if err := p.handleRequest(ctx); err != nil { |
| 48 |
t.Fatal(err) |
| 49 |
} |
| 50 |
// Just verify that there is an order "x" in the collection. |
| 51 |
order := &Order{ID: "x"} |
| 52 |
if err := p.coll.Get(ctx, order); err != nil { |
| 53 |
t.Fatal(err) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestProcessOrder(t *testing.T) { |
| 58 |
_, p, cleanup, err := setup(testConfig("ProcessOrder")) |
| 59 |
if err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
defer cleanup() |
| 63 |
|
| 64 |
ctx := context.Background() |
| 65 |
for _, test := range []struct { |
| 66 |
filename string |
| 67 |
want *Order |
| 68 |
wantErr bool |
| 69 |
}{ |
| 70 |
{ |
| 71 |
"cat1", |
| 72 |
&Order{ |
| 73 |
ID: "cat1", |
| 74 |
Email: "[email protected]", |
| 75 |
InImage: "cat1", |
| 76 |
OutImage: "cat1-out.png", |
| 77 |
Note: "converted from jpeg to png", |
| 78 |
}, |
| 79 |
false, |
| 80 |
}, |
| 81 |
{ |
| 82 |
"bad-image", |
| 83 |
nil, |
| 84 |
true, |
| 85 |
}, |
| 86 |
} { |
| 87 |
if err := copyFileToBucket("testdata/"+test.filename, p.bucket); err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
got := &Order{ |
| 91 |
ID: test.filename, |
| 92 |
Email: "[email protected]", |
| 93 |
InImage: test.filename, |
| 94 |
} |
| 95 |
err := p.processOrder(ctx, got) |
| 96 |
if err == nil && test.wantErr { |
| 97 |
t.Errorf("%s: got nil, want error", test.filename) |
| 98 |
} else if err != nil && !test.wantErr { |
| 99 |
t.Errorf("%s: got error %v, want nil", test.filename, err) |
| 100 |
} else if err == nil && !cmp.Equal(got, test.want) { |
| 101 |
t.Errorf("%s:\ngot %+v\nwant %+v", test.filename, got, test.want) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func copyFileToBucket(filename string, bucket *blob.Bucket) (err error) { |
| 107 |
f, err := os.Open(filename) |
| 108 |
if err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
defer f.Close() |
| 112 |
|
| 113 |
w, err := bucket.NewWriter(context.Background(), filepath.Base(filename), nil) |
| 114 |
if err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
defer func() { |
| 118 |
err2 := w.Close() |
| 119 |
if err == nil { |
| 120 |
err = err2 |
| 121 |
} |
| 122 |
}() |
| 123 |
|
| 124 |
_, err = io.Copy(w, f) |
| 125 |
return err |
| 126 |
} |