forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_test.go
153 lines (144 loc) · 3.25 KB
/
book_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
142
143
144
145
146
147
148
149
150
151
152
153
package runn
import (
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/goccy/go-json"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
_ "github.com/lib/pq"
)
func TestNew(t *testing.T) {
tests := []struct {
path string
wantErr bool
}{
{"testdata/book/book.yml", false},
{"testdata/book/map.yml", false},
{"testdata/notexist.yml", true},
}
for _, tt := range tests {
o, err := New(Book(tt.path))
if err != nil {
if !tt.wantErr {
t.Errorf("got %v", err)
}
continue
}
if tt.wantErr {
t.Errorf("want err")
}
if want := 1; len(o.httpRunners) != want {
t.Errorf("got %v\nwant %v", len(o.httpRunners), want)
}
if want := 1; len(o.dbRunners) != want {
t.Errorf("got %v\nwant %v", len(o.dbRunners), want)
}
if want := 6; len(o.steps) != want {
t.Errorf("got %v\nwant %v", len(o.steps), want)
}
}
}
func TestLoadBook(t *testing.T) {
tests := []struct {
path string
varsBytes []byte
}{
{
"testdata/book/env.yml",
[]byte(`{"number": 1, "string": "string", "object": {"property": "property"}, "array": [ {"property": "property"} ] }`),
},
}
debug := false
t.Setenv("DEBUG", strconv.FormatBool(debug))
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
bk, err := LoadBook(tt.path)
if err != nil {
t.Fatal(err)
}
if want := debug; bk.debug != want {
t.Errorf("got %v\nwant %v", bk.debug, want)
}
if want := "5"; bk.intervalStr != want {
t.Errorf("got %v\nwant %v", bk.intervalStr, want)
}
got := bk.vars
var want map[string]any
if err := json.Unmarshal(tt.varsBytes, &want); err != nil {
panic(err)
}
if diff := cmp.Diff(got, want, nil); diff != "" {
t.Errorf("%s", diff)
}
})
}
}
func TestApplyOptions(t *testing.T) {
tests := []struct {
opts []Option
want any
}{
{[]Option{}, url.QueryEscape},
{[]Option{Debug(true)}, url.QueryEscape},
{[]Option{Func("getEnv", os.Getenv)}, url.QueryEscape},
{[]Option{Func("urlencode", os.Getenv)}, os.Getenv},
}
for _, tt := range tests {
bk := newBook()
if err := bk.applyOptions(tt.opts...); err != nil {
t.Fatal(err)
}
got := bk.funcs["urlencode"]
if reflect.ValueOf(got).Pointer() != reflect.ValueOf(tt.want).Pointer() {
t.Errorf("got %v\nwant %v", got, tt.want)
}
}
}
func TestParseRunnerForHttpRunner(t *testing.T) {
secureUrl, _ := url.Parse("https://example.com/")
url, _ := url.Parse("http://example.com/")
client := &http.Client{Timeout: time.Duration(30000000000)}
tests := []struct {
v any
want any
}{
{
"https://example.com/",
httpRunner{
name: "req",
endpoint: secureUrl,
client: client,
validator: &nopValidator{},
},
},
{
"http://example.com/",
httpRunner{
name: "req",
endpoint: url,
client: client,
validator: &nopValidator{},
},
},
}
opts := []cmp.Option{
cmp.AllowUnexported(httpRunner{}),
cmpopts.IgnoreFields(http.Client{}, "Transport"),
}
for _, tt := range tests {
bk := newBook()
if err := bk.parseRunner("req", tt.v); err != nil {
t.Fatal(err)
}
got := bk.httpRunners["req"]
if diff := cmp.Diff(*got, tt.want, opts...); diff != "" {
t.Errorf("%s", diff)
}
}
}