Skip to content

Commit b5d6cd4

Browse files
chore: clean up some of the testing
1 parent 791ec65 commit b5d6cd4

File tree

5 files changed

+77
-171
lines changed

5 files changed

+77
-171
lines changed

arazzo/core/criterion_test.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/speakeasy-api/openapi/internal/testutils"
78
"github.com/speakeasy-api/openapi/marshaller"
89
"github.com/speakeasy-api/openapi/pointer"
9-
"github.com/speakeasy-api/openapi/testutils"
1010
"github.com/stretchr/testify/require"
1111
"gopkg.in/yaml.v3"
1212
)
@@ -135,33 +135,29 @@ type:
135135
Type: marshaller.Node[CriterionTypeUnion]{
136136
Key: "type",
137137
KeyNode: testutils.CreateStringYamlNode("type", 3, 1),
138-
Value: CriterionTypeUnion{ExpressionType: &CriterionExpressionType{
139-
Type: marshaller.Node[string]{
140-
Key: "type",
141-
KeyNode: testutils.CreateStringYamlNode("type", 4, 3),
142-
Value: "jsonpath",
143-
ValueNode: testutils.CreateStringYamlNode("jsonpath", 4, 9),
144-
Present: true,
145-
},
146-
Version: marshaller.Node[string]{
147-
Key: "version",
148-
KeyNode: testutils.CreateStringYamlNode("version", 5, 3),
149-
Value: "draft-goessner-dispatch-jsonpath-00",
150-
ValueNode: testutils.CreateStringYamlNode("draft-goessner-dispatch-jsonpath-00", 5, 12),
151-
Present: true,
152-
},
153-
}, RootNode: &yaml.Node{
154-
Kind: yaml.MappingNode,
155-
Tag: "!!map",
156-
Content: []*yaml.Node{
138+
Value: CriterionTypeUnion{
139+
ExpressionType: &CriterionExpressionType{
140+
Type: marshaller.Node[string]{
141+
Key: "type",
142+
KeyNode: testutils.CreateStringYamlNode("type", 4, 3),
143+
Value: "jsonpath",
144+
ValueNode: testutils.CreateStringYamlNode("jsonpath", 4, 9),
145+
Present: true,
146+
},
147+
Version: marshaller.Node[string]{
148+
Key: "version",
149+
KeyNode: testutils.CreateStringYamlNode("version", 5, 3),
150+
Value: "draft-goessner-dispatch-jsonpath-00",
151+
ValueNode: testutils.CreateStringYamlNode("draft-goessner-dispatch-jsonpath-00", 5, 12),
152+
Present: true,
153+
},
154+
}, RootNode: testutils.CreateMapYamlNode([]*yaml.Node{
157155
testutils.CreateStringYamlNode("type", 4, 3),
158156
testutils.CreateStringYamlNode("jsonpath", 4, 9),
159157
testutils.CreateStringYamlNode("version", 5, 3),
160158
testutils.CreateStringYamlNode("draft-goessner-dispatch-jsonpath-00", 5, 12),
161-
},
162-
Line: 4,
163-
Column: 3,
164-
}},
159+
}, 4, 3),
160+
},
165161
ValueNode: testutils.CreateMapYamlNode([]*yaml.Node{
166162
testutils.CreateStringYamlNode("type", 4, 3),
167163
testutils.CreateStringYamlNode("jsonpath", 4, 9),

testutils/utils.go renamed to internal/testutils/utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package testutils
22

33
import (
4+
"fmt"
5+
46
"gopkg.in/yaml.v3"
57
)
68

@@ -15,6 +17,26 @@ func CreateStringYamlNode(value string, line, column int) *yaml.Node {
1517
}
1618
}
1719

20+
func CreateIntYamlNode(value int, line, column int) *yaml.Node {
21+
return &yaml.Node{
22+
Value: fmt.Sprintf("%d", value),
23+
Kind: yaml.ScalarNode,
24+
Tag: "!!int",
25+
Line: line,
26+
Column: column,
27+
}
28+
}
29+
30+
func CreateBoolYamlNode(value bool, line, column int) *yaml.Node {
31+
return &yaml.Node{
32+
Value: fmt.Sprintf("%t", value),
33+
Kind: yaml.ScalarNode,
34+
Tag: "!!bool",
35+
Line: line,
36+
Column: column,
37+
}
38+
}
39+
1840
func CreateMapYamlNode(contents []*yaml.Node, line, column int) *yaml.Node {
1941
return &yaml.Node{
2042
Content: contents,

jsonschema/oas31/core/value_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/speakeasy-api/openapi/internal/testutils"
78
"github.com/speakeasy-api/openapi/marshaller"
89
"github.com/speakeasy-api/openapi/pointer"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
11-
"gopkg.in/yaml.v3"
1212
)
1313

1414
type TestEitherValue[L any, R any] struct {
@@ -25,11 +25,7 @@ func TestEitherValue_SyncChanges_Success(t *testing.T) {
2525
var target EitherValue[string, string]
2626
outNode, err := marshaller.SyncValue(ctx, source, &target, nil)
2727
require.NoError(t, err)
28-
assert.Equal(t, &yaml.Node{
29-
Value: "some-value",
30-
Kind: yaml.ScalarNode,
31-
Tag: "!!str",
32-
}, outNode)
28+
assert.Equal(t, testutils.CreateStringYamlNode("some-value", 0, 0), outNode)
3329
assert.Equal(t, "some-value", *target.Left)
3430
assert.Nil(t, target.Right)
3531
}

marshaller/syncer_test.go

Lines changed: 26 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77
"testing"
88

9+
"github.com/speakeasy-api/openapi/internal/testutils"
910
"github.com/speakeasy-api/openapi/pointer"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
@@ -16,35 +17,23 @@ func TestSyncValue_String(t *testing.T) {
1617
target := ""
1718
outNode, err := SyncValue(context.Background(), "some-value", &target, nil)
1819
require.NoError(t, err)
19-
assert.Equal(t, &yaml.Node{
20-
Value: "some-value",
21-
Kind: yaml.ScalarNode,
22-
Tag: "!!str",
23-
}, outNode)
20+
assert.Equal(t, testutils.CreateStringYamlNode("some-value", 0, 0), outNode)
2421
assert.Equal(t, "some-value", target)
2522
}
2623

2724
func TestSyncValue_StringPtrSet(t *testing.T) {
2825
target := pointer.From("")
2926
outNode, err := SyncValue(context.Background(), pointer.From("some-value"), &target, nil)
3027
require.NoError(t, err)
31-
assert.Equal(t, &yaml.Node{
32-
Value: "some-value",
33-
Kind: yaml.ScalarNode,
34-
Tag: "!!str",
35-
}, outNode)
28+
assert.Equal(t, testutils.CreateStringYamlNode("some-value", 0, 0), outNode)
3629
assert.Equal(t, "some-value", *target)
3730
}
3831

3932
func TestSyncValue_StringPtrNil(t *testing.T) {
4033
var target *string
4134
outNode, err := SyncValue(context.Background(), pointer.From("some-value"), &target, nil)
4235
require.NoError(t, err)
43-
assert.Equal(t, &yaml.Node{
44-
Value: "some-value",
45-
Kind: yaml.ScalarNode,
46-
Tag: "!!str",
47-
}, outNode)
36+
assert.Equal(t, testutils.CreateStringYamlNode("some-value", 0, 0), outNode)
4837
assert.Equal(t, "some-value", *target)
4938
}
5039

@@ -79,11 +68,7 @@ func TestSyncValue_StructPtr_CustomSyncer(t *testing.T) {
7968

8069
outNode, err := SyncValue(context.Background(), source, &target, nil)
8170
require.NoError(t, err)
82-
node := &yaml.Node{
83-
Kind: yaml.ScalarNode,
84-
Tag: "!!int",
85-
Value: "1",
86-
}
71+
node := testutils.CreateIntYamlNode(1, 0, 0)
8772
assert.Equal(t, node, outNode)
8873
assert.Equal(t, node, target.RootNode)
8974
assert.Equal(t, 1, *target.Val)
@@ -96,11 +81,7 @@ func TestSyncValue_Struct_CustomSyncer(t *testing.T) {
9681

9782
outNode, err := SyncValue(context.Background(), source, &target, nil)
9883
require.NoError(t, err)
99-
node := &yaml.Node{
100-
Kind: yaml.ScalarNode,
101-
Tag: "!!int",
102-
Value: "1",
103-
}
84+
node := testutils.CreateIntYamlNode(1, 0, 0)
10485
assert.Equal(t, node, outNode)
10586
assert.Equal(t, node, target.RootNode)
10687
}
@@ -134,52 +115,16 @@ func TestSyncChanges_Struct(t *testing.T) {
134115
outNode, err := SyncValue(context.Background(), source, &target, nil)
135116
require.NoError(t, err)
136117

137-
node := &yaml.Node{
138-
Kind: yaml.MappingNode,
139-
Tag: "!!map",
140-
Content: []*yaml.Node{
141-
{
142-
Kind: yaml.ScalarNode,
143-
Tag: "!!str",
144-
Value: "int",
145-
},
146-
{
147-
Kind: yaml.ScalarNode,
148-
Tag: "!!int",
149-
Value: "1",
150-
},
151-
{
152-
Kind: yaml.ScalarNode,
153-
Tag: "!!str",
154-
Value: "str",
155-
},
156-
{
157-
Kind: yaml.ScalarNode,
158-
Tag: "!!str",
159-
Value: "some-string",
160-
},
161-
{
162-
Kind: yaml.ScalarNode,
163-
Tag: "!!str",
164-
Value: "strPtr",
165-
},
166-
{
167-
Kind: yaml.ScalarNode,
168-
Tag: "!!str",
169-
Value: "some-string-ptr",
170-
},
171-
{
172-
Kind: yaml.ScalarNode,
173-
Tag: "!!str",
174-
Value: "boolPtr",
175-
},
176-
{
177-
Kind: yaml.ScalarNode,
178-
Tag: "!!bool",
179-
Value: "true",
180-
},
181-
},
182-
}
118+
node := testutils.CreateMapYamlNode([]*yaml.Node{
119+
testutils.CreateStringYamlNode("int", 0, 0),
120+
testutils.CreateIntYamlNode(1, 0, 0),
121+
testutils.CreateStringYamlNode("str", 0, 0),
122+
testutils.CreateStringYamlNode("some-string", 0, 0),
123+
testutils.CreateStringYamlNode("strPtr", 0, 0),
124+
testutils.CreateStringYamlNode("some-string-ptr", 0, 0),
125+
testutils.CreateStringYamlNode("boolPtr", 0, 0),
126+
testutils.CreateBoolYamlNode(true, 0, 0),
127+
}, 0, 0)
183128

184129
assert.Equal(t, node, outNode)
185130
assert.Equal(t, node, target.RootNode)
@@ -202,52 +147,16 @@ func TestSyncChanges_StructPtr(t *testing.T) {
202147
outNode, err := SyncValue(context.Background(), source, &target, nil)
203148
require.NoError(t, err)
204149

205-
node := &yaml.Node{
206-
Kind: yaml.MappingNode,
207-
Tag: "!!map",
208-
Content: []*yaml.Node{
209-
{
210-
Kind: yaml.ScalarNode,
211-
Tag: "!!str",
212-
Value: "int",
213-
},
214-
{
215-
Kind: yaml.ScalarNode,
216-
Tag: "!!int",
217-
Value: "1",
218-
},
219-
{
220-
Kind: yaml.ScalarNode,
221-
Tag: "!!str",
222-
Value: "str",
223-
},
224-
{
225-
Kind: yaml.ScalarNode,
226-
Tag: "!!str",
227-
Value: "some-string",
228-
},
229-
{
230-
Kind: yaml.ScalarNode,
231-
Tag: "!!str",
232-
Value: "strPtr",
233-
},
234-
{
235-
Kind: yaml.ScalarNode,
236-
Tag: "!!str",
237-
Value: "some-string-ptr",
238-
},
239-
{
240-
Kind: yaml.ScalarNode,
241-
Tag: "!!str",
242-
Value: "boolPtr",
243-
},
244-
{
245-
Kind: yaml.ScalarNode,
246-
Tag: "!!bool",
247-
Value: "true",
248-
},
249-
},
250-
}
150+
node := testutils.CreateMapYamlNode([]*yaml.Node{
151+
testutils.CreateStringYamlNode("int", 0, 0),
152+
testutils.CreateIntYamlNode(1, 0, 0),
153+
testutils.CreateStringYamlNode("str", 0, 0),
154+
testutils.CreateStringYamlNode("some-string", 0, 0),
155+
testutils.CreateStringYamlNode("strPtr", 0, 0),
156+
testutils.CreateStringYamlNode("some-string-ptr", 0, 0),
157+
testutils.CreateStringYamlNode("boolPtr", 0, 0),
158+
testutils.CreateBoolYamlNode(true, 0, 0),
159+
}, 0, 0)
251160

252161
assert.Equal(t, node, outNode)
253162
assert.Equal(t, node, target.RootNode)

marshaller/unmarshaller_test.go

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/speakeasy-api/openapi/extensions"
8+
"github.com/speakeasy-api/openapi/internal/testutils"
89
"github.com/speakeasy-api/openapi/pointer"
910
"github.com/speakeasy-api/openapi/sequencedmap"
1011
"github.com/stretchr/testify/assert"
@@ -78,16 +79,10 @@ x-test-2: some-value-2
7879
assertNodeField(t, "slicePrimitiveField", 4, []string{"where", "are", "you"}, 4, out.NestedModelField.Value.SlicePrimitiveField)
7980
assertNodeField(t, "sliceRequiredPrimitiveField", 5, []string{"I", "am", "here"}, 5, out.NestedModelField.Value.SliceRequiredPrimitiveField)
8081
assertNodeField(t, "mapPrimitiveField", 6, sequencedmap.New(sequencedmap.NewElem("a", 1), sequencedmap.NewElem("b", 2)), 7, out.NestedModelField.Value.MapPrimitiveField)
81-
xTestExtensionNodeNestedModelField := &yaml.Node{
82-
Value: "some-value",
83-
Kind: yaml.ScalarNode,
84-
Tag: "!!str",
85-
Line: 9,
86-
Column: 11,
87-
}
82+
xTestExtensionNodeNestedModelField := testutils.CreateStringYamlNode("some-value", 9, 11)
8883
assert.Equal(t, sequencedmap.New(sequencedmap.NewElem("x-test", Node[extensions.Extension]{
8984
Key: "x-test",
90-
KeyNode: &yaml.Node{Value: "x-test", Kind: yaml.ScalarNode, Tag: "!!str", Line: 9, Column: 3},
85+
KeyNode: testutils.CreateStringYamlNode("x-test", 9, 3),
9186
Value: xTestExtensionNodeNestedModelField,
9287
ValueNode: xTestExtensionNodeNestedModelField,
9388
})), out.NestedModelField.Value.Extensions)
@@ -105,32 +100,20 @@ x-test-2: some-value-2
105100
assertModelNodeField(t, "mapRequiredNestedModelField", 18, 19, out.MapRequiredNestedModelField)
106101
assertNodeField(t, "slicePrimitiveField", 20, []string{"p", "q", "r"}, 20, out.MapRequiredNestedModelField.Value.GetOrZero("z").SlicePrimitiveField)
107102
assertNodeField(t, "sliceRequiredPrimitiveField", 21, []string{"s", "t", "u"}, 21, out.MapRequiredNestedModelField.Value.GetOrZero("z").SliceRequiredPrimitiveField)
108-
xTestExtensionNodeMapRequiredNestedModelField := &yaml.Node{
109-
Value: "some-value",
110-
Kind: yaml.ScalarNode,
111-
Tag: "!!str",
112-
Line: 22,
113-
Column: 13,
114-
}
103+
xTestExtensionNodeMapRequiredNestedModelField := testutils.CreateStringYamlNode("some-value", 22, 13)
115104
assert.Equal(t, sequencedmap.New(sequencedmap.NewElem("x-test", Node[extensions.Extension]{
116105
Key: "x-test",
117-
KeyNode: &yaml.Node{Value: "x-test", Kind: yaml.ScalarNode, Tag: "!!str", Line: 22, Column: 5},
106+
KeyNode: testutils.CreateStringYamlNode("x-test", 22, 5),
118107
Value: xTestExtensionNodeMapRequiredNestedModelField,
119108
ValueNode: xTestExtensionNodeMapRequiredNestedModelField,
120109
})), out.MapRequiredNestedModelField.Value.GetOrZero("z").Extensions)
121110
assertNodeField(t, "slicePrimitiveField", 24, []string{"w", "x", "y"}, 24, out.MapRequiredNestedModelField.Value.GetOrZero("x").SlicePrimitiveField)
122111
assertNodeField(t, "sliceRequiredPrimitiveField", 25, []string{"1", "2", "3"}, 25, out.MapRequiredNestedModelField.Value.GetOrZero("x").SliceRequiredPrimitiveField)
123112

124-
xTestExtensionNode := &yaml.Node{
125-
Value: "some-value-2",
126-
Kind: yaml.ScalarNode,
127-
Tag: "!!str",
128-
Line: 26,
129-
Column: 11,
130-
}
113+
xTestExtensionNode := testutils.CreateStringYamlNode("some-value-2", 26, 11)
131114
assert.Equal(t, sequencedmap.New(sequencedmap.NewElem("x-test-2", Node[extensions.Extension]{
132115
Key: "x-test-2",
133-
KeyNode: &yaml.Node{Value: "x-test-2", Kind: yaml.ScalarNode, Tag: "!!str", Line: 26, Column: 1},
116+
KeyNode: testutils.CreateStringYamlNode("x-test-2", 26, 1),
134117
Value: xTestExtensionNode,
135118
ValueNode: xTestExtensionNode,
136119
})), out.Extensions)

0 commit comments

Comments
 (0)