forked from nytimes/openapi2proto
/
proto_test.go
177 lines (167 loc) · 4 KB
/
proto_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package openapi2proto_test
import (
"bytes"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"github.com/NYTimes/openapi2proto"
"github.com/NYTimes/openapi2proto/compiler"
"github.com/pmezard/go-difflib/difflib"
)
type genProtoTestCase struct {
options bool
fixturePath string
wantProto string
remoteFiles []string
wrapPrimitives bool
}
func testGenProto(t *testing.T, tests ...genProtoTestCase) {
t.Helper()
origin, _ := os.Getwd()
for _, test := range tests {
t.Run(test.fixturePath, func(t *testing.T) {
for _, remoteFile := range test.remoteFiles {
res, err := http.Get(remoteFile)
if err != nil || res.StatusCode != http.StatusOK {
t.Skip(`Remote file ` + remoteFile + ` is not available`)
}
}
var generated bytes.Buffer
var compilerOptions []compiler.Option
if test.options {
compilerOptions = append(compilerOptions, compiler.WithAnnotation(true))
}
if test.wrapPrimitives {
compilerOptions = append(compilerOptions, compiler.WithWrapPrimitives(true))
}
if err := openapi2proto.Transpile(&generated, test.fixturePath, openapi2proto.WithCompilerOptions(compilerOptions...)); err != nil {
t.Errorf(`failed to transpile: %s`, err)
return
}
os.Chdir(origin)
// if test.wantProto is empty, guess file name from the original
// fixture path
wantProtoFile := test.wantProto
if wantProtoFile == "" {
i := strings.LastIndexByte(test.fixturePath, '.')
if i > -1 {
wantProtoFile = test.fixturePath[:i] + `.proto`
} else {
t.Fatalf(`unable to guess proto file name from %s`, test.fixturePath)
}
}
want, err := ioutil.ReadFile(wantProtoFile)
if err != nil {
t.Fatal("unable to open test fixture: ", err)
}
if string(want) != generated.String() {
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(want)),
B: difflib.SplitLines(generated.String()),
FromFile: wantProtoFile,
ToFile: "Generated",
Context: 3,
}
text, _ := difflib.GetUnifiedDiffString(diff)
t.Errorf("testYaml (%s) differences:\n%s",
test.fixturePath, text)
}
})
}
}
func TestNetwork(t *testing.T) {
testGenProto(t, genProtoTestCase{
fixturePath: "fixtures/petstore/swagger.yaml",
remoteFiles: []string{
"https://raw.githubusercontent.com/NYTimes/openapi2proto/master/fixtures/petstore/Pet.yaml",
},
})
}
func TestGenerateProto(t *testing.T) {
tests := []genProtoTestCase{
{
fixturePath: "fixtures/cats.yaml",
},
{
fixturePath: "fixtures/catsanddogs.yaml",
},
{
fixturePath: "fixtures/semantic_api.json",
},
{
fixturePath: "fixtures/semantic_api.yaml",
},
{
fixturePath: "fixtures/most_popular.json",
},
{
fixturePath: "fixtures/spec.yaml",
},
{
fixturePath: "fixtures/spec.json",
},
{
options: true,
fixturePath: "fixtures/semantic_api.json",
wantProto: "fixtures/semantic_api-options.proto",
},
{
options: true,
fixturePath: "fixtures/most_popular.json",
wantProto: "fixtures/most_popular-options.proto",
},
{
options: true,
fixturePath: "fixtures/spec.yaml",
wantProto: "fixtures/spec-options.proto",
},
{
options: true,
fixturePath: "fixtures/spec.json",
wantProto: "fixtures/spec-options.proto",
},
{
fixturePath: "fixtures/includes_query.json",
},
{
fixturePath: "fixtures/lowercase_def.json",
},
{
fixturePath: "fixtures/missing_type.json",
},
/*
{
fixturePath: "fixtures/kubernetes.json",
},
*/
{
fixturePath: "fixtures/accountv1-0.json",
},
{
fixturePath: "fixtures/refs.json",
},
{
fixturePath: "fixtures/refs.yaml",
},
{
fixturePath: "fixtures/integers.yaml",
},
{
wrapPrimitives: true,
fixturePath: "fixtures/integers_required.yaml",
},
{
fixturePath: "fixtures/global_options.yaml",
},
{
fixturePath: "fixtures/naming_conversion.yaml",
},
{
options: true,
fixturePath: "fixtures/custom_options.yaml",
},
}
testGenProto(t, tests...)
}