Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions arazzo/core/criterion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,34 @@ type:
})
}
}

func TestCriterionTypeUnion_Unmarshal_NilNode_Error(t *testing.T) {
t.Parallel()

var union CriterionTypeUnion
_, err := union.Unmarshal(t.Context(), "test", nil)
require.Error(t, err, "should return error for nil node")
require.Contains(t, err.Error(), "node is nil", "error should mention nil node")
}

func TestCriterionTypeUnion_Unmarshal_InvalidNodeKind_Error(t *testing.T) {
t.Parallel()

var union CriterionTypeUnion
node := &yaml.Node{Kind: yaml.SequenceNode}
validationErrs, err := union.Unmarshal(t.Context(), "test", node)
require.NoError(t, err, "should not return fatal error")
require.NotEmpty(t, validationErrs, "should have validation errors")
require.Contains(t, validationErrs[0].Error(), "expected string or object", "error should mention expected types")
}

func TestCriterionTypeUnion_SyncChanges_Int_Error(t *testing.T) {
t.Parallel()

union := &CriterionTypeUnion{}
union.SetValid(true, true)

_, err := union.SyncChanges(t.Context(), 42, nil)
require.Error(t, err, "should return error for int model")
require.Contains(t, err.Error(), "expected a struct", "error should mention struct expectation")
}
41 changes: 41 additions & 0 deletions arazzo/core/reusable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,44 @@ func TestReusable_SyncChanges_NonStruct_Error(t *testing.T) {
require.Error(t, err, "SyncChanges should fail")
assert.Contains(t, err.Error(), "Reusable.SyncChanges expected a struct, got string", "error message should match")
}

func TestReusable_Unmarshal_NilNode_Error(t *testing.T) {
t.Parallel()

var reusable Reusable[*Parameter]
_, err := reusable.Unmarshal(t.Context(), "test", nil)
require.Error(t, err, "should return error for nil node")
assert.Contains(t, err.Error(), "node is nil", "error should mention nil node")
}

func TestReusable_Unmarshal_InlinedObject_Success(t *testing.T) {
t.Parallel()

yamlContent := `name: petId
in: path
value: "123"`

var node yaml.Node
err := yaml.Unmarshal([]byte(yamlContent), &node)
require.NoError(t, err, "unmarshal should succeed")

var reusable Reusable[*Parameter]
validationErrs, err := reusable.Unmarshal(t.Context(), "test", node.Content[0])
require.NoError(t, err, "unmarshal should succeed")
require.Empty(t, validationErrs, "validation errors should be empty")
assert.True(t, reusable.GetValid(), "reusable should be valid")
require.NotNil(t, reusable.Object, "Object should not be nil")
}

func TestReusable_SyncChanges_Int_Error(t *testing.T) {
t.Parallel()

var node yaml.Node
err := yaml.Unmarshal([]byte(`reference: '#/test'`), &node)
require.NoError(t, err, "unmarshal should succeed")

reusable := Reusable[*Parameter]{}
_, err = reusable.SyncChanges(t.Context(), 42, node.Content[0])
require.Error(t, err, "SyncChanges should fail")
assert.Contains(t, err.Error(), "expected a struct", "error message should mention struct expectation")
}
190 changes: 190 additions & 0 deletions arazzo/find_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package arazzo_test

import (
"testing"

"github.com/speakeasy-api/openapi/arazzo"
"github.com/speakeasy-api/openapi/pointer"
"github.com/stretchr/testify/assert"
)

func TestWorkflows_Find_Success(t *testing.T) {
t.Parallel()

tests := []struct {
name string
workflows arazzo.Workflows
id string
expected *arazzo.Workflow
}{
{
name: "empty workflows returns nil",
workflows: arazzo.Workflows{},
id: "test",
expected: nil,
},
{
name: "finds workflow by id",
workflows: arazzo.Workflows{
{WorkflowID: "workflow1"},
{WorkflowID: "workflow2"},
{WorkflowID: "workflow3"},
},
id: "workflow2",
expected: &arazzo.Workflow{WorkflowID: "workflow2"},
},
{
name: "returns nil when workflow not found",
workflows: arazzo.Workflows{
{WorkflowID: "workflow1"},
},
id: "nonexistent",
expected: nil,
},
{
name: "returns first match when multiple workflows with same id",
workflows: arazzo.Workflows{
{WorkflowID: "duplicate", Summary: pointer.From("first")},
{WorkflowID: "duplicate", Summary: pointer.From("second")},
},
id: "duplicate",
expected: &arazzo.Workflow{WorkflowID: "duplicate", Summary: pointer.From("first")},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := tt.workflows.Find(tt.id)
if tt.expected == nil {
assert.Nil(t, result)
} else {
assert.NotNil(t, result)
assert.Equal(t, tt.expected.WorkflowID, result.WorkflowID)
if tt.expected.Summary != nil {
assert.Equal(t, *tt.expected.Summary, *result.Summary)
}
}
})
}
}

func TestSteps_Find_Success(t *testing.T) {
t.Parallel()

tests := []struct {
name string
steps arazzo.Steps
id string
expected *arazzo.Step
}{
{
name: "empty steps returns nil",
steps: arazzo.Steps{},
id: "test",
expected: nil,
},
{
name: "finds step by id",
steps: arazzo.Steps{
{StepID: "step1"},
{StepID: "step2"},
{StepID: "step3"},
},
id: "step2",
expected: &arazzo.Step{StepID: "step2"},
},
{
name: "returns nil when step not found",
steps: arazzo.Steps{
{StepID: "step1"},
},
id: "nonexistent",
expected: nil,
},
{
name: "returns first match when multiple steps with same id",
steps: arazzo.Steps{
{StepID: "duplicate", Description: pointer.From("first")},
{StepID: "duplicate", Description: pointer.From("second")},
},
id: "duplicate",
expected: &arazzo.Step{StepID: "duplicate", Description: pointer.From("first")},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := tt.steps.Find(tt.id)
if tt.expected == nil {
assert.Nil(t, result)
} else {
assert.NotNil(t, result)
assert.Equal(t, tt.expected.StepID, result.StepID)
if tt.expected.Description != nil {
assert.Equal(t, *tt.expected.Description, *result.Description)
}
}
})
}
}

func TestSourceDescriptions_Find_Success(t *testing.T) {
t.Parallel()

tests := []struct {
name string
sourceDescriptions arazzo.SourceDescriptions
findName string
expected *arazzo.SourceDescription
}{
{
name: "empty source descriptions returns nil",
sourceDescriptions: arazzo.SourceDescriptions{},
findName: "test",
expected: nil,
},
{
name: "finds source description by name",
sourceDescriptions: arazzo.SourceDescriptions{
{Name: "apiOne", URL: "https://api1.example.com"},
{Name: "apiTwo", URL: "https://api2.example.com"},
{Name: "apiThree", URL: "https://api3.example.com"},
},
findName: "apiTwo",
expected: &arazzo.SourceDescription{Name: "apiTwo", URL: "https://api2.example.com"},
},
{
name: "returns nil when source description not found",
sourceDescriptions: arazzo.SourceDescriptions{
{Name: "apiOne", URL: "https://api1.example.com"},
},
findName: "nonexistent",
expected: nil,
},
{
name: "returns first match when multiple source descriptions with same name",
sourceDescriptions: arazzo.SourceDescriptions{
{Name: "duplicate", URL: "https://first.example.com"},
{Name: "duplicate", URL: "https://second.example.com"},
},
findName: "duplicate",
expected: &arazzo.SourceDescription{Name: "duplicate", URL: "https://first.example.com"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := tt.sourceDescriptions.Find(tt.findName)
if tt.expected == nil {
assert.Nil(t, result)
} else {
assert.NotNil(t, result)
assert.Equal(t, tt.expected.Name, result.Name)
assert.Equal(t, tt.expected.URL, result.URL)
}
})
}
}
124 changes: 124 additions & 0 deletions arazzo/reusable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,127 @@ func TestTypeToComponentType_Success(t *testing.T) {
})
}
}

func TestComponentTypeToReusableType_Success(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input string
expected string
}{
{
name: "parameters converts to reusableParameter",
input: "parameters",
expected: "reusableParameter",
},
{
name: "successActions converts to reusableSuccessAction",
input: "successActions",
expected: "reusableSuccessAction",
},
{
name: "failureActions converts to reusableFailureAction",
input: "failureActions",
expected: "reusableFailureAction",
},
{
name: "unknown type returns empty",
input: "unknown",
expected: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

actual := componentTypeToReusableType(tt.input)
assert.Equal(t, tt.expected, actual, "component type conversion should match expected reusable type")
})
}
}

func TestReusable_IsReference_Success(t *testing.T) {
t.Parallel()

tests := []struct {
name string
reusable ReusableParameter
expected bool
}{
{
name: "nil reference returns false",
reusable: ReusableParameter{},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

assert.Equal(t, tt.expected, tt.reusable.IsReference())
})
}
}

func TestReusable_Get_Success(t *testing.T) {
t.Parallel()

param := &Parameter{Name: "testParam"}

tests := []struct {
name string
reusable ReusableParameter
components *Components
expected *Parameter
}{
{
name: "inline object returns object",
reusable: ReusableParameter{Object: param},
components: nil,
expected: param,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

result := tt.reusable.Get(tt.components)
assert.Equal(t, tt.expected, result)
})
}
}

func TestReusable_GetReferencedObject_Success(t *testing.T) {
t.Parallel()

tests := []struct {
name string
reusable ReusableParameter
components *Components
expectNil bool
}{
{
name: "not a reference returns nil",
reusable: ReusableParameter{},
components: nil,
expectNil: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

result := tt.reusable.GetReferencedObject(tt.components)
if tt.expectNil {
assert.Nil(t, result)
} else {
assert.NotNil(t, result)
}
})
}
}
Loading
Loading