Skip to content

Commit 93c893b

Browse files
test: improve test coverage (#104)
1 parent de50921 commit 93c893b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+9499
-26
lines changed

arazzo/core/criterion_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,34 @@ type:
221221
})
222222
}
223223
}
224+
225+
func TestCriterionTypeUnion_Unmarshal_NilNode_Error(t *testing.T) {
226+
t.Parallel()
227+
228+
var union CriterionTypeUnion
229+
_, err := union.Unmarshal(t.Context(), "test", nil)
230+
require.Error(t, err, "should return error for nil node")
231+
require.Contains(t, err.Error(), "node is nil", "error should mention nil node")
232+
}
233+
234+
func TestCriterionTypeUnion_Unmarshal_InvalidNodeKind_Error(t *testing.T) {
235+
t.Parallel()
236+
237+
var union CriterionTypeUnion
238+
node := &yaml.Node{Kind: yaml.SequenceNode}
239+
validationErrs, err := union.Unmarshal(t.Context(), "test", node)
240+
require.NoError(t, err, "should not return fatal error")
241+
require.NotEmpty(t, validationErrs, "should have validation errors")
242+
require.Contains(t, validationErrs[0].Error(), "expected string or object", "error should mention expected types")
243+
}
244+
245+
func TestCriterionTypeUnion_SyncChanges_Int_Error(t *testing.T) {
246+
t.Parallel()
247+
248+
union := &CriterionTypeUnion{}
249+
union.SetValid(true, true)
250+
251+
_, err := union.SyncChanges(t.Context(), 42, nil)
252+
require.Error(t, err, "should return error for int model")
253+
require.Contains(t, err.Error(), "expected a struct", "error should mention struct expectation")
254+
}

arazzo/core/reusable_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,44 @@ func TestReusable_SyncChanges_NonStruct_Error(t *testing.T) {
5555
require.Error(t, err, "SyncChanges should fail")
5656
assert.Contains(t, err.Error(), "Reusable.SyncChanges expected a struct, got string", "error message should match")
5757
}
58+
59+
func TestReusable_Unmarshal_NilNode_Error(t *testing.T) {
60+
t.Parallel()
61+
62+
var reusable Reusable[*Parameter]
63+
_, err := reusable.Unmarshal(t.Context(), "test", nil)
64+
require.Error(t, err, "should return error for nil node")
65+
assert.Contains(t, err.Error(), "node is nil", "error should mention nil node")
66+
}
67+
68+
func TestReusable_Unmarshal_InlinedObject_Success(t *testing.T) {
69+
t.Parallel()
70+
71+
yamlContent := `name: petId
72+
in: path
73+
value: "123"`
74+
75+
var node yaml.Node
76+
err := yaml.Unmarshal([]byte(yamlContent), &node)
77+
require.NoError(t, err, "unmarshal should succeed")
78+
79+
var reusable Reusable[*Parameter]
80+
validationErrs, err := reusable.Unmarshal(t.Context(), "test", node.Content[0])
81+
require.NoError(t, err, "unmarshal should succeed")
82+
require.Empty(t, validationErrs, "validation errors should be empty")
83+
assert.True(t, reusable.GetValid(), "reusable should be valid")
84+
require.NotNil(t, reusable.Object, "Object should not be nil")
85+
}
86+
87+
func TestReusable_SyncChanges_Int_Error(t *testing.T) {
88+
t.Parallel()
89+
90+
var node yaml.Node
91+
err := yaml.Unmarshal([]byte(`reference: '#/test'`), &node)
92+
require.NoError(t, err, "unmarshal should succeed")
93+
94+
reusable := Reusable[*Parameter]{}
95+
_, err = reusable.SyncChanges(t.Context(), 42, node.Content[0])
96+
require.Error(t, err, "SyncChanges should fail")
97+
assert.Contains(t, err.Error(), "expected a struct", "error message should mention struct expectation")
98+
}

arazzo/find_test.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package arazzo_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/speakeasy-api/openapi/arazzo"
7+
"github.com/speakeasy-api/openapi/pointer"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestWorkflows_Find_Success(t *testing.T) {
12+
t.Parallel()
13+
14+
tests := []struct {
15+
name string
16+
workflows arazzo.Workflows
17+
id string
18+
expected *arazzo.Workflow
19+
}{
20+
{
21+
name: "empty workflows returns nil",
22+
workflows: arazzo.Workflows{},
23+
id: "test",
24+
expected: nil,
25+
},
26+
{
27+
name: "finds workflow by id",
28+
workflows: arazzo.Workflows{
29+
{WorkflowID: "workflow1"},
30+
{WorkflowID: "workflow2"},
31+
{WorkflowID: "workflow3"},
32+
},
33+
id: "workflow2",
34+
expected: &arazzo.Workflow{WorkflowID: "workflow2"},
35+
},
36+
{
37+
name: "returns nil when workflow not found",
38+
workflows: arazzo.Workflows{
39+
{WorkflowID: "workflow1"},
40+
},
41+
id: "nonexistent",
42+
expected: nil,
43+
},
44+
{
45+
name: "returns first match when multiple workflows with same id",
46+
workflows: arazzo.Workflows{
47+
{WorkflowID: "duplicate", Summary: pointer.From("first")},
48+
{WorkflowID: "duplicate", Summary: pointer.From("second")},
49+
},
50+
id: "duplicate",
51+
expected: &arazzo.Workflow{WorkflowID: "duplicate", Summary: pointer.From("first")},
52+
},
53+
}
54+
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
t.Parallel()
58+
result := tt.workflows.Find(tt.id)
59+
if tt.expected == nil {
60+
assert.Nil(t, result)
61+
} else {
62+
assert.NotNil(t, result)
63+
assert.Equal(t, tt.expected.WorkflowID, result.WorkflowID)
64+
if tt.expected.Summary != nil {
65+
assert.Equal(t, *tt.expected.Summary, *result.Summary)
66+
}
67+
}
68+
})
69+
}
70+
}
71+
72+
func TestSteps_Find_Success(t *testing.T) {
73+
t.Parallel()
74+
75+
tests := []struct {
76+
name string
77+
steps arazzo.Steps
78+
id string
79+
expected *arazzo.Step
80+
}{
81+
{
82+
name: "empty steps returns nil",
83+
steps: arazzo.Steps{},
84+
id: "test",
85+
expected: nil,
86+
},
87+
{
88+
name: "finds step by id",
89+
steps: arazzo.Steps{
90+
{StepID: "step1"},
91+
{StepID: "step2"},
92+
{StepID: "step3"},
93+
},
94+
id: "step2",
95+
expected: &arazzo.Step{StepID: "step2"},
96+
},
97+
{
98+
name: "returns nil when step not found",
99+
steps: arazzo.Steps{
100+
{StepID: "step1"},
101+
},
102+
id: "nonexistent",
103+
expected: nil,
104+
},
105+
{
106+
name: "returns first match when multiple steps with same id",
107+
steps: arazzo.Steps{
108+
{StepID: "duplicate", Description: pointer.From("first")},
109+
{StepID: "duplicate", Description: pointer.From("second")},
110+
},
111+
id: "duplicate",
112+
expected: &arazzo.Step{StepID: "duplicate", Description: pointer.From("first")},
113+
},
114+
}
115+
116+
for _, tt := range tests {
117+
t.Run(tt.name, func(t *testing.T) {
118+
t.Parallel()
119+
result := tt.steps.Find(tt.id)
120+
if tt.expected == nil {
121+
assert.Nil(t, result)
122+
} else {
123+
assert.NotNil(t, result)
124+
assert.Equal(t, tt.expected.StepID, result.StepID)
125+
if tt.expected.Description != nil {
126+
assert.Equal(t, *tt.expected.Description, *result.Description)
127+
}
128+
}
129+
})
130+
}
131+
}
132+
133+
func TestSourceDescriptions_Find_Success(t *testing.T) {
134+
t.Parallel()
135+
136+
tests := []struct {
137+
name string
138+
sourceDescriptions arazzo.SourceDescriptions
139+
findName string
140+
expected *arazzo.SourceDescription
141+
}{
142+
{
143+
name: "empty source descriptions returns nil",
144+
sourceDescriptions: arazzo.SourceDescriptions{},
145+
findName: "test",
146+
expected: nil,
147+
},
148+
{
149+
name: "finds source description by name",
150+
sourceDescriptions: arazzo.SourceDescriptions{
151+
{Name: "apiOne", URL: "https://api1.example.com"},
152+
{Name: "apiTwo", URL: "https://api2.example.com"},
153+
{Name: "apiThree", URL: "https://api3.example.com"},
154+
},
155+
findName: "apiTwo",
156+
expected: &arazzo.SourceDescription{Name: "apiTwo", URL: "https://api2.example.com"},
157+
},
158+
{
159+
name: "returns nil when source description not found",
160+
sourceDescriptions: arazzo.SourceDescriptions{
161+
{Name: "apiOne", URL: "https://api1.example.com"},
162+
},
163+
findName: "nonexistent",
164+
expected: nil,
165+
},
166+
{
167+
name: "returns first match when multiple source descriptions with same name",
168+
sourceDescriptions: arazzo.SourceDescriptions{
169+
{Name: "duplicate", URL: "https://first.example.com"},
170+
{Name: "duplicate", URL: "https://second.example.com"},
171+
},
172+
findName: "duplicate",
173+
expected: &arazzo.SourceDescription{Name: "duplicate", URL: "https://first.example.com"},
174+
},
175+
}
176+
177+
for _, tt := range tests {
178+
t.Run(tt.name, func(t *testing.T) {
179+
t.Parallel()
180+
result := tt.sourceDescriptions.Find(tt.findName)
181+
if tt.expected == nil {
182+
assert.Nil(t, result)
183+
} else {
184+
assert.NotNil(t, result)
185+
assert.Equal(t, tt.expected.Name, result.Name)
186+
assert.Equal(t, tt.expected.URL, result.URL)
187+
}
188+
})
189+
}
190+
}

arazzo/reusable_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,127 @@ func TestTypeToComponentType_Success(t *testing.T) {
4141
})
4242
}
4343
}
44+
45+
func TestComponentTypeToReusableType_Success(t *testing.T) {
46+
t.Parallel()
47+
48+
tests := []struct {
49+
name string
50+
input string
51+
expected string
52+
}{
53+
{
54+
name: "parameters converts to reusableParameter",
55+
input: "parameters",
56+
expected: "reusableParameter",
57+
},
58+
{
59+
name: "successActions converts to reusableSuccessAction",
60+
input: "successActions",
61+
expected: "reusableSuccessAction",
62+
},
63+
{
64+
name: "failureActions converts to reusableFailureAction",
65+
input: "failureActions",
66+
expected: "reusableFailureAction",
67+
},
68+
{
69+
name: "unknown type returns empty",
70+
input: "unknown",
71+
expected: "",
72+
},
73+
}
74+
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
t.Parallel()
78+
79+
actual := componentTypeToReusableType(tt.input)
80+
assert.Equal(t, tt.expected, actual, "component type conversion should match expected reusable type")
81+
})
82+
}
83+
}
84+
85+
func TestReusable_IsReference_Success(t *testing.T) {
86+
t.Parallel()
87+
88+
tests := []struct {
89+
name string
90+
reusable ReusableParameter
91+
expected bool
92+
}{
93+
{
94+
name: "nil reference returns false",
95+
reusable: ReusableParameter{},
96+
expected: false,
97+
},
98+
}
99+
100+
for _, tt := range tests {
101+
t.Run(tt.name, func(t *testing.T) {
102+
t.Parallel()
103+
104+
assert.Equal(t, tt.expected, tt.reusable.IsReference())
105+
})
106+
}
107+
}
108+
109+
func TestReusable_Get_Success(t *testing.T) {
110+
t.Parallel()
111+
112+
param := &Parameter{Name: "testParam"}
113+
114+
tests := []struct {
115+
name string
116+
reusable ReusableParameter
117+
components *Components
118+
expected *Parameter
119+
}{
120+
{
121+
name: "inline object returns object",
122+
reusable: ReusableParameter{Object: param},
123+
components: nil,
124+
expected: param,
125+
},
126+
}
127+
128+
for _, tt := range tests {
129+
t.Run(tt.name, func(t *testing.T) {
130+
t.Parallel()
131+
132+
result := tt.reusable.Get(tt.components)
133+
assert.Equal(t, tt.expected, result)
134+
})
135+
}
136+
}
137+
138+
func TestReusable_GetReferencedObject_Success(t *testing.T) {
139+
t.Parallel()
140+
141+
tests := []struct {
142+
name string
143+
reusable ReusableParameter
144+
components *Components
145+
expectNil bool
146+
}{
147+
{
148+
name: "not a reference returns nil",
149+
reusable: ReusableParameter{},
150+
components: nil,
151+
expectNil: true,
152+
},
153+
}
154+
155+
for _, tt := range tests {
156+
t.Run(tt.name, func(t *testing.T) {
157+
t.Parallel()
158+
159+
result := tt.reusable.GetReferencedObject(tt.components)
160+
if tt.expectNil {
161+
assert.Nil(t, result)
162+
} else {
163+
assert.NotNil(t, result)
164+
}
165+
})
166+
}
167+
}

0 commit comments

Comments
 (0)