echoswagger/internal_test.go

285 lines
5.0 KiB
Go
Raw Permalink Normal View History

/*
* Copyright (c) 2019 Alex aka mailoman <alex@webz.asia>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Alex aka mailoman <alex@webz.asia>
* @copyright Copyright (c) 2019 Alex aka mailoman <alex@webz.asia>
* @since 18.12.2019
*
*/
2018-09-04 14:09:11 +02:00
package echoswagger
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParamTypes(t *testing.T) {
var pa interface{}
var pb *int64
var pc map[string]string
var pd [][]float64
type Parent struct {
Child struct {
Name string
}
}
var pe Parent
2018-09-04 14:09:11 +02:00
tests := []struct {
p interface{}
panic bool
name string
}{
{
p: pa,
panic: true,
name: "Interface type",
},
{
p: &pa,
panic: true,
name: "Interface pointer type",
},
{
p: &pb,
panic: false,
name: "Int type",
},
{
p: &pc,
panic: true,
name: "Map type",
},
{
p: nil,
panic: true,
name: "Nil type",
},
{
p: 0,
panic: false,
name: "Int type",
},
{
p: &pd,
panic: false,
name: "Array float64 type",
},
{
p: &pe,
panic: true,
name: "Struct type",
},
2018-09-04 14:09:11 +02:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := prepareApi()
if tt.panic {
assert.Panics(t, func() {
a.AddParamPath(tt.p, tt.name, "")
})
} else {
a.AddParamPath(tt.p, tt.name, "")
sapi, ok := a.(*api)
assert.Equal(t, ok, true)
assert.Equal(t, len(sapi.operation.Parameters), 1)
assert.Equal(t, tt.name, sapi.operation.Parameters[0].Name)
}
})
}
}
func TestNestedParamTypes(t *testing.T) {
var pa struct {
Name string
}
var pb struct {
User struct {
Name string
}
}
tests := []struct {
p interface{}
panic bool
name string
}{
{
p: 0,
panic: true,
name: "Basic type",
},
{
p: pa,
panic: false,
name: "Struct type",
},
{
p: pb,
panic: true,
name: "Nested struct type",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := prepareApi()
if tt.panic {
assert.Panics(t, func() {
a.AddParamPathNested(tt.p)
})
} else {
a.AddParamPathNested(tt.p)
sapi, ok := a.(*api)
assert.Equal(t, ok, true)
assert.Equal(t, len(sapi.operation.Parameters), 1)
}
})
}
}
2018-09-04 14:09:11 +02:00
func TestSchemaTypes(t *testing.T) {
var pa interface{}
var pb map[string]string
type PT struct {
Name string
ExpiredAt time.Time
}
var pc map[PT]string
var pd PT
var pe map[time.Time]string
var pf map[*int]string
type PU struct {
Any interface{}
2018-09-04 14:09:11 +02:00
}
var pg PU
var ph map[string]interface{}
2018-09-04 14:09:11 +02:00
tests := []struct {
p interface{}
panic bool
name string
}{
{
p: pa,
panic: true,
name: "Interface type",
},
{
p: nil,
panic: true,
name: "Nil type",
},
{
p: "",
panic: false,
name: "String type",
},
{
p: &pb,
panic: false,
name: "Map type",
},
{
p: &pc,
panic: true,
name: "Map struct type",
},
{
p: pd,
panic: false,
name: "Struct type",
},
{
p: &pd,
panic: false,
name: "Struct pointer type",
},
{
p: &pe,
panic: false,
name: "Map time.Time key type",
},
{
p: &pf,
panic: false,
name: "Map pointer key type",
},
{
p: &pg,
panic: false,
name: "Struct interface field type",
},
{
p: &ph,
panic: false,
name: "Map interface value type",
2018-09-04 14:09:11 +02:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := prepareApi()
if tt.panic {
assert.Panics(t, func() {
a.AddParamBody(tt.p, tt.name, "", true)
})
} else {
a.AddParamBody(tt.p, tt.name, "", true)
sapi, ok := a.(*api)
assert.Equal(t, ok, true)
assert.Equal(t, len(sapi.operation.Parameters), 1)
assert.Equal(t, tt.name, sapi.operation.Parameters[0].Name)
}
})
}
}
type testUser struct {
Id int64
Name string
Pets []testPet
}
type testPet struct {
Id int64
Masters []testUser
}
func TestRecursiveTypes(t *testing.T) {
tests := []struct {
p interface{}
name string
}{
{
p: &testUser{},
name: "User",
},
{
p: &testPet{},
name: "Pet",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := prepareApi()
a.AddParamBody(tt.p, tt.name, "", true)
sapi, ok := a.(*api)
assert.Equal(t, ok, true)
assert.Equal(t, len(sapi.operation.Parameters), 1)
assert.Equal(t, len(*sapi.defs), 2)
assert.Equal(t, tt.name, sapi.operation.Parameters[0].Name)
})
}
}