|
| 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 | +} |
0 commit comments