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
|
package openapi3
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func initOperation() *Operation {
operation := NewOperation()
operation.Description = "Some description"
operation.Summary = "Some summary"
operation.Tags = []string{"tag1", "tag2"}
return operation
}
func TestAddParameter(t *testing.T) {
operation := initOperation()
operation.AddParameter(NewQueryParameter("param1"))
operation.AddParameter(NewCookieParameter("param2"))
require.Equal(t, "param1", operation.Parameters.GetByInAndName("query", "param1").Name)
require.Equal(t, "param2", operation.Parameters.GetByInAndName("cookie", "param2").Name)
}
func TestAddResponse(t *testing.T) {
operation := initOperation()
operation.AddResponse(200, NewResponse())
operation.AddResponse(400, NewResponse())
require.NotNil(t, "status 200", operation.Responses.Status(200).Value)
require.NotNil(t, "status 400", operation.Responses.Status(400).Value)
}
func operationWithoutResponses() *Operation {
operation := initOperation()
return operation
}
func operationWithResponses() *Operation {
operation := initOperation()
operation.AddResponse(200, NewResponse().WithDescription("some response"))
return operation
}
func TestOperationValidation(t *testing.T) {
tests := []struct {
name string
input *Operation
expectedError error
}{
{
"when no Responses object is provided",
operationWithoutResponses(),
errors.New("value of responses must be an object"),
},
{
"when a Responses object is provided",
operationWithResponses(),
nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
c := context.Background()
validationErr := test.input.Validate(c)
require.Equal(t, test.expectedError, validationErr, "expected errors (or lack of) to match")
})
}
}
|