-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval_test.go
141 lines (110 loc) · 2.76 KB
/
eval_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
136
137
138
139
140
141
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
package http_test
import (
"context"
"encoding/json"
"log"
"os"
"path/filepath"
"testing"
"github.com/gdt-dev/gdt"
gdterrors "github.com/gdt-dev/gdt/errors"
gdttypes "github.com/gdt-dev/gdt/types"
gdthttp "github.com/gdt-dev/http"
"github.com/gdt-dev/http/test/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
dataFilePath = "testdata/fixtures.json"
)
type dataset struct {
Authors interface{}
Publishers interface{}
Books []*server.Book
}
func data() *dataset {
f, err := os.Open(dataFilePath)
if err != nil {
panic(err)
}
data := &dataset{}
if err = json.NewDecoder(f).Decode(&data); err != nil {
panic(err)
}
return data
}
func dataFixture() gdttypes.Fixture {
f, err := os.Open(dataFilePath)
if err != nil {
panic(err)
}
fix, err := gdt.NewJSONFixture(f)
if err != nil {
panic(err)
}
return fix
}
func TestFixturesNotSetup(t *testing.T) {
require := require.New(t)
assert := assert.New(t)
fp := filepath.Join("testdata", "create-then-get.yaml")
s, err := gdt.From(fp)
require.Nil(err)
require.NotNil(s)
err = s.Run(context.TODO(), t)
require.NotNil(err)
assert.ErrorIs(err, gdterrors.RuntimeError)
}
func setup(ctx context.Context) context.Context {
// Register an HTTP server fixture that spins up the API service on a
// random port on localhost
logger := log.New(os.Stdout, "books_api_http: ", log.LstdFlags)
srv := server.NewControllerWithBooks(logger, data().Books)
serverFixture := gdthttp.NewServerFixture(srv.Router(), false /* useTLS */)
ctx = gdt.RegisterFixture(ctx, "books_api", serverFixture)
ctx = gdt.RegisterFixture(ctx, "books_data", dataFixture())
return ctx
}
func TestCreateThenGet(t *testing.T) {
require := require.New(t)
fp := filepath.Join("testdata", "create-then-get.yaml")
ctx := gdt.NewContext()
ctx = setup(ctx)
s, err := gdt.From(fp)
require.Nil(err)
require.NotNil(s)
s.Run(ctx, t)
}
func TestFailures(t *testing.T) {
require := require.New(t)
fp := filepath.Join("testdata", "failures.yaml")
ctx := gdt.NewContext()
ctx = setup(ctx)
s, err := gdt.From(fp)
require.Nil(err)
require.NotNil(s)
s.Run(ctx, t)
}
func TestGetBooks(t *testing.T) {
require := require.New(t)
fp := filepath.Join("testdata", "get-books.yaml")
ctx := gdt.NewContext()
ctx = setup(ctx)
s, err := gdt.From(fp)
require.Nil(err)
require.NotNil(s)
s.Run(ctx, t)
}
func TestPutMultipleBooks(t *testing.T) {
require := require.New(t)
fp := filepath.Join("testdata", "put-multiple-books.yaml")
ctx := gdt.NewContext()
ctx = setup(ctx)
s, err := gdt.From(fp)
require.Nil(err)
require.NotNil(s)
s.Run(ctx, t)
}