Skip to content

Commit d35e601

Browse files
committed
Add unit tests
1 parent fa10ddf commit d35e601

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

tools/cli/internal/openapi/paths.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const (
2727

2828
// allOperationsHaveExtension checks if all the operations in the base pat have the given extension name.
2929
func allOperationsHaveExtension(basePath *openapi3.PathItem, basePathName, extensionName string) bool {
30+
if basePath.Operations() == nil || len(basePath.Operations()) == 0 {
31+
return false
32+
}
33+
3034
if basePath.Get != nil {
3135
if result := getOperationExtensionWithName(basePath.Get, extensionName); result == nil {
3236
return false
@@ -71,6 +75,10 @@ func getOperationExtensionWithName(operation *openapi3.Operation, extensionName
7175
}
7276

7377
func allOperationsAllowDocsDiff(basePath *openapi3.PathItem, basePathName string) bool {
78+
if basePath.Operations() == nil || len(basePath.Operations()) == 0 {
79+
return false
80+
}
81+
7482
if basePath.Get != nil {
7583
prop := getOperationExtensionProperty(basePath.Get, xgenSoaMigration, allowDocsDiff)
7684
if prop != "true" {
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Copyright 2024 MongoDB Inc
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+
// http://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+
package openapi
15+
16+
import (
17+
"testing"
18+
19+
"github.com/getkin/kin-openapi/openapi3"
20+
)
21+
22+
func TestAllOperationsHaveExtension(t *testing.T) {
23+
tests := []struct {
24+
name string
25+
input *openapi3.PathItem
26+
expected bool
27+
}{
28+
{
29+
name: "All operations have extension",
30+
input: &openapi3.PathItem{
31+
Get: &openapi3.Operation{
32+
Extensions: map[string]interface{}{
33+
"x-xgen-soa-migration": map[string]interface{}{
34+
"test": "true",
35+
},
36+
},
37+
},
38+
Put: &openapi3.Operation{
39+
Extensions: map[string]interface{}{
40+
"x-xgen-soa-migration": map[string]interface{}{
41+
"test": "true",
42+
},
43+
},
44+
},
45+
Post: &openapi3.Operation{
46+
Extensions: map[string]interface{}{
47+
"x-xgen-soa-migration": map[string]interface{}{
48+
"test": "true",
49+
},
50+
},
51+
},
52+
Patch: &openapi3.Operation{
53+
Extensions: map[string]interface{}{
54+
"x-xgen-soa-migration": map[string]interface{}{
55+
"test": "true",
56+
},
57+
},
58+
},
59+
Delete: &openapi3.Operation{
60+
Extensions: map[string]interface{}{
61+
"x-xgen-soa-migration": map[string]interface{}{
62+
"test": "true",
63+
},
64+
},
65+
},
66+
},
67+
expected: true,
68+
},
69+
{
70+
name: "Not all operations have extension",
71+
input: &openapi3.PathItem{
72+
Get: &openapi3.Operation{
73+
Extensions: map[string]interface{}{
74+
"x-xgen-soa-migration": map[string]interface{}{
75+
"test": "true",
76+
},
77+
},
78+
},
79+
Put: &openapi3.Operation{
80+
Extensions: map[string]interface{}{
81+
"x-xgen-sunset": "true",
82+
},
83+
},
84+
},
85+
expected: false,
86+
},
87+
{
88+
name: "No operations",
89+
input: &openapi3.PathItem{},
90+
expected: false,
91+
},
92+
}
93+
94+
for _, test := range tests {
95+
t.Run(test.name, func(t *testing.T) {
96+
actual := allOperationsHaveExtension(test.input, "/test", "x-xgen-soa-migration")
97+
if actual != test.expected {
98+
t.Errorf("Expected %t, got %t", test.expected, actual)
99+
}
100+
})
101+
}
102+
}
103+
104+
func TestAllOperationsAllowDocsDiff(t *testing.T) {
105+
tests := []struct {
106+
name string
107+
input *openapi3.PathItem
108+
expected bool
109+
}{
110+
{
111+
name: "All operations allow docs diff",
112+
input: &openapi3.PathItem{
113+
Get: &openapi3.Operation{
114+
Extensions: map[string]interface{}{
115+
"x-xgen-soa-migration": map[string]interface{}{
116+
"allowDocsDiff": "true",
117+
},
118+
},
119+
},
120+
Put: &openapi3.Operation{
121+
Extensions: map[string]interface{}{
122+
"x-xgen-soa-migration": map[string]interface{}{
123+
"allowDocsDiff": "true",
124+
},
125+
},
126+
},
127+
Post: &openapi3.Operation{
128+
Extensions: map[string]interface{}{
129+
"x-xgen-soa-migration": map[string]interface{}{
130+
"allowDocsDiff": "true",
131+
},
132+
},
133+
},
134+
Patch: &openapi3.Operation{
135+
Extensions: map[string]interface{}{
136+
"x-xgen-soa-migration": map[string]interface{}{
137+
"allowDocsDiff": "true",
138+
},
139+
},
140+
},
141+
Delete: &openapi3.Operation{
142+
Extensions: map[string]interface{}{
143+
"x-xgen-soa-migration": map[string]interface{}{
144+
"allowDocsDiff": "true",
145+
},
146+
},
147+
},
148+
},
149+
expected: true,
150+
},
151+
{
152+
name: "Not all operations allow docs diff",
153+
input: &openapi3.PathItem{
154+
Get: &openapi3.Operation{
155+
Extensions: map[string]interface{}{
156+
"x-xgen-soa-migration": map[string]interface{}{
157+
"allowDocsDiff": "true",
158+
},
159+
},
160+
},
161+
Put: &openapi3.Operation{
162+
Extensions: map[string]interface{}{
163+
"x-xgen-soa-migration": map[string]interface{}{
164+
"allowDocsDiff": "false",
165+
},
166+
},
167+
},
168+
},
169+
expected: false,
170+
},
171+
{
172+
name: "No operations",
173+
input: &openapi3.PathItem{},
174+
expected: false,
175+
},
176+
}
177+
178+
for _, test := range tests {
179+
t.Run(test.name, func(t *testing.T) {
180+
actual := allOperationsAllowDocsDiff(test.input, "/test")
181+
if actual != test.expected {
182+
t.Errorf("Expected %t, got %t", test.expected, actual)
183+
}
184+
})
185+
}
186+
}

0 commit comments

Comments
 (0)