| 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 |
"fmt" |
| 20 |
"io" |
| 21 |
"net/http/httptest" |
| 22 |
"os" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
"gocloud.dev/docstore" |
| 28 |
) |
| 29 |
|
| 30 |
func TestOrderForm(t *testing.T) { |
| 31 |
f, _, cleanup, err := setup(testConfig("OrderForm")) |
| 32 |
if err != nil { |
| 33 |
t.Fatal(err) |
| 34 |
} |
| 35 |
defer cleanup() |
| 36 |
|
| 37 |
w := httptest.NewRecorder() |
| 38 |
r := httptest.NewRequest("GET", "/orders/new", nil) |
| 39 |
if err := f.orderForm(w, r); err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
res := w.Result() |
| 43 |
if res.StatusCode != 200 { |
| 44 |
t.Fatalf("got %d, want 200", res.StatusCode) |
| 45 |
} |
| 46 |
wantb, err := os.ReadFile("order-form.htmlt") |
| 47 |
if err != nil { |
| 48 |
t.Fatal(err) |
| 49 |
} |
| 50 |
gotb, err := io.ReadAll(res.Body) |
| 51 |
if err != nil { |
| 52 |
t.Fatal(err) |
| 53 |
} |
| 54 |
res.Body.Close() |
| 55 |
got := string(gotb) |
| 56 |
want := string(wantb) |
| 57 |
|
| 58 |
if got != want { |
| 59 |
t.Errorf("got %q, want %q", got, want) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestCreateOrder(t *testing.T) { |
| 64 |
f, _, cleanup, err := setup(testConfig("CreateOrder")) |
| 65 |
if err != nil { |
| 66 |
t.Fatal(err) |
| 67 |
} |
| 68 |
defer cleanup() |
| 69 |
|
| 70 |
ctx := context.Background() |
| 71 |
file := strings.NewReader("an image") |
| 72 |
tm := time.Date(2019, 7, 1, 0, 0, 0, 0, time.Local) |
| 73 |
id, err := f.doCreateOrder(ctx, "[email protected]", file, tm) |
| 74 |
if err != nil { |
| 75 |
t.Fatal(err) |
| 76 |
} |
| 77 |
|
| 78 |
r, err := f.bucket.NewReader(ctx, id+"-in", nil) |
| 79 |
if err != nil { |
| 80 |
t.Fatal(err) |
| 81 |
} |
| 82 |
defer r.Close() |
| 83 |
gotb, err := io.ReadAll(r) |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
gots := string(gotb) |
| 88 |
wants := "an image" |
| 89 |
if gots != wants { |
| 90 |
t.Errorf("got %q, want %q", gots, wants) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestListOrders(t *testing.T) { |
| 95 |
f, _, cleanup, err := setup(testConfig("ListOrders")) |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
defer cleanup() |
| 100 |
|
| 101 |
ctx := context.Background() |
| 102 |
// Clear the collection. |
| 103 |
if err := clearCollection(ctx, f.coll); err != nil { |
| 104 |
t.Fatal(err) |
| 105 |
} |
| 106 |
orders := []*Order{ |
| 107 |
{ |
| 108 |
ID: "a", Email: "[email protected]", InImage: "a-in", OutImage: "a-out", |
| 109 |
CreateTime: time.Now().Add(-18 * time.Second), FinishTime: time.Now(), |
| 110 |
}, |
| 111 |
{ID: "b", Email: "[email protected]", InImage: "b-in", CreateTime: time.Now()}, |
| 112 |
} |
| 113 |
actions := f.coll.Actions() |
| 114 |
for _, ord := range orders { |
| 115 |
actions.Put(ord) |
| 116 |
} |
| 117 |
if err := actions.Do(ctx); err != nil { |
| 118 |
t.Fatal(err) |
| 119 |
} |
| 120 |
|
| 121 |
w := httptest.NewRecorder() |
| 122 |
r := httptest.NewRequest("GET", "/orders", nil) |
| 123 |
if err := f.listOrders(w, r); err != nil { |
| 124 |
t.Fatal(err) |
| 125 |
} |
| 126 |
|
| 127 |
res := w.Result() |
| 128 |
if res.StatusCode != 200 { |
| 129 |
t.Fatalf("got %d, want 200", res.StatusCode) |
| 130 |
} |
| 131 |
gotb, err := io.ReadAll(res.Body) |
| 132 |
if err != nil { |
| 133 |
t.Fatal(err) |
| 134 |
} |
| 135 |
res.Body.Close() |
| 136 |
got := string(gotb) |
| 137 |
for _, ord := range orders { |
| 138 |
if !strings.Contains(got, ord.ID) { |
| 139 |
t.Errorf("got %q, should contain %q", got, ord.ID) |
| 140 |
} |
| 141 |
if !strings.Contains(got, ord.Email) { |
| 142 |
t.Errorf("got %q, should contain %q", got, ord.Email) |
| 143 |
} |
| 144 |
} |
| 145 |
url := `<a href="/show/a-out">a-out</a>` |
| 146 |
if !strings.Contains(got, url) { |
| 147 |
t.Errorf("got %q, should contain %q", got, url) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func testConfig(name string) config { |
| 152 |
reqURL := "mem://requests-" + name |
| 153 |
return config{ |
| 154 |
requestTopicURL: reqURL, |
| 155 |
requestSubURL: reqURL, |
| 156 |
bucketURL: "", // setup will use fileblob with a temporary dir |
| 157 |
collectionURL: fmt.Sprintf("mem://orders-%s/ID", name), |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
func clearCollection(ctx context.Context, coll *docstore.Collection) error { |
| 162 |
iter := coll.Query().Get(ctx, "ID") |
| 163 |
dels := coll.Actions() |
| 164 |
for { |
| 165 |
var order Order |
| 166 |
err := iter.Next(ctx, &order) |
| 167 |
if err == io.EOF { |
| 168 |
break |
| 169 |
} |
| 170 |
if err != nil { |
| 171 |
return err |
| 172 |
} |
| 173 |
dels.Delete(&order) |
| 174 |
} |
| 175 |
return dels.Do(ctx) |
| 176 |
} |