-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathupgrade_test.go
More file actions
227 lines (206 loc) · 6.44 KB
/
Copy pathupgrade_test.go
File metadata and controls
227 lines (206 loc) · 6.44 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package overlay_test
import (
"testing"
"github.com/speakeasy-api/openapi/overlay"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestUpgrade_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputVersion string
inputJSONPath string
expectedVersion string
expectedJSONPath string
options []overlay.Option[overlay.UpgradeOptions]
expectUpgraded bool
}{
{
name: "upgrade 1.0.0 to 1.1.0",
inputVersion: "1.0.0",
expectedVersion: "1.1.0",
expectedJSONPath: "",
expectUpgraded: true,
},
{
name: "upgrade 1.0.0 with rfc9535 to 1.1.0 removes redundant extension",
inputVersion: "1.0.0",
inputJSONPath: "rfc9535",
expectedVersion: "1.1.0",
expectedJSONPath: "", // RFC 9535 is now default, so extension cleared
expectUpgraded: true,
},
{
name: "upgrade 1.0.0 with legacy keeps legacy",
inputVersion: "1.0.0",
inputJSONPath: "legacy",
expectedVersion: "1.1.0",
expectedJSONPath: "legacy", // User explicitly wants legacy, keep it
expectUpgraded: true,
},
{
name: "no upgrade when already at 1.1.0",
inputVersion: "1.1.0",
expectedVersion: "1.1.0",
expectedJSONPath: "",
expectUpgraded: false,
},
{
name: "upgrade with explicit target version",
inputVersion: "1.0.0",
expectedVersion: "1.1.0",
options: []overlay.Option[overlay.UpgradeOptions]{overlay.WithUpgradeTargetVersion("1.1.0")},
expectUpgraded: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
o := &overlay.Overlay{
Version: tt.inputVersion,
JSONPathVersion: tt.inputJSONPath,
Info: overlay.Info{
Title: "Test Overlay",
Version: "1.0.0",
},
Actions: []overlay.Action{
{
Target: "$.info.title",
Update: yaml.Node{Kind: yaml.ScalarNode, Value: "New Title"},
},
},
}
upgraded, err := overlay.Upgrade(ctx, o, tt.options...)
require.NoError(t, err, "upgrade should succeed")
assert.Equal(t, tt.expectUpgraded, upgraded, "upgrade status mismatch")
assert.Equal(t, tt.expectedVersion, o.Version, "version mismatch")
assert.Equal(t, tt.expectedJSONPath, o.JSONPathVersion, "JSONPath version mismatch")
})
}
}
func TestUpgrade_Error(t *testing.T) {
t.Parallel()
tests := []struct {
name string
version string
options []overlay.Option[overlay.UpgradeOptions]
wantErrs string
}{
{
name: "cannot downgrade",
version: "1.1.0",
options: []overlay.Option[overlay.UpgradeOptions]{overlay.WithUpgradeTargetVersion("1.0.0")},
wantErrs: "cannot downgrade",
},
{
name: "invalid version format",
version: "invalid",
wantErrs: "invalid current overlay version",
},
{
name: "invalid target version format",
version: "1.0.0",
options: []overlay.Option[overlay.UpgradeOptions]{overlay.WithUpgradeTargetVersion("invalid")},
wantErrs: "invalid target overlay version",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
o := &overlay.Overlay{
Version: tt.version,
Info: overlay.Info{
Title: "Test Overlay",
Version: "1.0.0",
},
Actions: []overlay.Action{
{
Target: "$.info.title",
Update: yaml.Node{Kind: yaml.ScalarNode, Value: "New Title"},
},
},
}
_, err := overlay.Upgrade(ctx, o, tt.options...)
require.Error(t, err, "upgrade should fail")
assert.Contains(t, err.Error(), tt.wantErrs, "error message mismatch")
})
}
}
func TestUpgrade_NilOverlay(t *testing.T) {
t.Parallel()
ctx := t.Context()
upgraded, err := overlay.Upgrade(ctx, nil)
require.NoError(t, err, "upgrade of nil should not error")
assert.False(t, upgraded, "nil overlay should not be upgraded")
}
func TestUpgrade_PreservesOverlayContent(t *testing.T) {
t.Parallel()
ctx := t.Context()
// Create a fully populated overlay
o := &overlay.Overlay{
Version: "1.0.0",
Info: overlay.Info{
Title: "Test Overlay",
Version: "2.0.0",
Description: "This is a test overlay with description",
},
Extends: "https://example.com/openapi.yaml",
Actions: []overlay.Action{
{
Target: "$.info.title",
Description: "Update the title",
Update: yaml.Node{Kind: yaml.ScalarNode, Value: "New Title"},
},
{
Target: "$.info.description",
Remove: true,
},
{
Target: "$.components.schemas.NewSchema",
Copy: "$.components.schemas.ExistingSchema",
},
},
}
upgraded, err := overlay.Upgrade(ctx, o)
require.NoError(t, err, "upgrade should succeed")
assert.True(t, upgraded, "overlay should be upgraded")
// Verify all content is preserved
assert.Equal(t, "1.1.0", o.Version, "version should be upgraded")
assert.Equal(t, "Test Overlay", o.Info.Title, "title should be preserved")
assert.Equal(t, "2.0.0", o.Info.Version, "info version should be preserved")
assert.Equal(t, "This is a test overlay with description", o.Info.Description, "description should be preserved")
assert.Equal(t, "https://example.com/openapi.yaml", o.Extends, "extends should be preserved")
assert.Len(t, o.Actions, 3, "all actions should be preserved")
assert.Equal(t, "$.info.title", o.Actions[0].Target, "action target should be preserved")
assert.Equal(t, "Update the title", o.Actions[0].Description, "action description should be preserved")
assert.True(t, o.Actions[1].Remove, "remove action should be preserved")
assert.Equal(t, "$.components.schemas.ExistingSchema", o.Actions[2].Copy, "copy action should be preserved")
}
func TestUpgrade_WithCopyAction(t *testing.T) {
t.Parallel()
ctx := t.Context()
// Copy action already works in 1.0.0, verify it still works after upgrade
o := &overlay.Overlay{
Version: "1.0.0",
Info: overlay.Info{
Title: "Copy Action Overlay",
Version: "1.0.0",
},
Actions: []overlay.Action{
{
Target: "$.components.schemas.Bar",
Copy: "$.components.schemas.Foo",
Description: "Copy Foo schema to Bar",
},
},
}
upgraded, err := overlay.Upgrade(ctx, o)
require.NoError(t, err, "upgrade with copy action should succeed")
assert.True(t, upgraded, "overlay should be upgraded")
assert.Equal(t, "1.1.0", o.Version, "version should be 1.1.0")
assert.Equal(t, "$.components.schemas.Foo", o.Actions[0].Copy, "copy source should be preserved")
}