Cleaning up some coverage and a few bits.

This commit is contained in:
Dave Shanley
2022-12-15 16:29:11 -05:00
parent 635877da1d
commit 8dff96ad90
7 changed files with 51 additions and 20 deletions

View File

@@ -6,6 +6,8 @@ package v3
import (
"fmt"
"github.com/pb33f/libopenapi/datamodel"
v2 "github.com/pb33f/libopenapi/datamodel/high/v2"
lowv2 "github.com/pb33f/libopenapi/datamodel/low/v2"
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/stretchr/testify/assert"
"io/ioutil"
@@ -364,7 +366,18 @@ func TestStripeAsDoc(t *testing.T) {
lowDoc, err = lowv3.CreateDocument(info)
assert.Len(t, err, 23)
d := NewDocument(lowDoc)
fmt.Println(d)
assert.NotNil(t, d)
}
func TestK8sAsDoc(t *testing.T) {
data, _ := ioutil.ReadFile("../../../test_specs/k8s.json")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowSwag, err := lowv2.CreateDocument(info)
d := v2.NewSwaggerDocument(lowSwag)
assert.Len(t, err, 1)
assert.NotNil(t, d)
}
func TestAsanaAsDoc(t *testing.T) {

View File

@@ -323,8 +323,8 @@ func TestSpecIndex_BurgerShopMixedRef(t *testing.T) {
index := NewSpecIndex(&rootNode)
assert.Len(t, index.allRefs, 4)
assert.Len(t, index.allMappedRefs, 4)
assert.Len(t, index.allRefs, 5)
assert.Len(t, index.allMappedRefs, 5)
assert.Equal(t, 5, index.GetPathCount())
assert.Equal(t, 5, index.GetOperationCount())
assert.Equal(t, 1, index.GetComponentSchemaCount())
@@ -631,10 +631,12 @@ func TestSpecIndex_parameterReferencesHavePaths(t *testing.T) {
yml := `paths:
/:
parameters:
- $ref: '#/components/parameters/param1'
- $ref: '#/components/parameters/param1'
- $ref: 'paramour.yaml#/components/parameters/param3'
get:
parameters:
- $ref: '#/components/parameters/param2'
- $ref: '#/components/parameters/param2'
- name: test
in: query
@@ -674,7 +676,7 @@ components:
assert.Equal(t, "$.components.parameters.param2", params["/"]["get"]["#/components/parameters/param2"].Path)
}
if assert.Contains(t, params["/"]["get"], "test") {
assert.Equal(t, "$.paths./.get.parameters[1]", params["/"]["get"]["test"].Path)
assert.Equal(t, "$.paths./.get.parameters[2]", params["/"]["get"]["test"].Path)
}
}
}

View File

@@ -156,7 +156,7 @@ func TestResolver_ResolveComponents_MixedRef(t *testing.T) {
assert.NotNil(t, resolver)
circ := resolver.Resolve()
assert.Len(t, circ, 2)
assert.Len(t, circ, 10)
}

View File

@@ -229,6 +229,12 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
"400":
description: Unexpected error. Sorry.
content:
application/json:
schema:
$ref: 'https://raw.githubusercontent.com/daveshanley/vacuum/main/model/test_files/burgershop.openapi.yaml'
components:
schemas:
Error:

View File

@@ -0,0 +1,9 @@
components:
schemas:
Thing:
type: object
description: A thing that does nothing.
properties:
message:
type: string
description: I am pointless.

View File

@@ -493,32 +493,21 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", segs[i-1], segs[i])
continue
}
cleaned = append(cleaned, fmt.Sprintf("%s%s", segs[i], segs[i]))
continue
} else {
intVal, err := strconv.ParseInt(segs[i], 10, 32)
if err == nil && intVal <= 99 {
segs[i] = fmt.Sprintf("[%d]", intVal)
if i < len(cleaned) {
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", segs[i-1], segs[i])
} else {
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", cleaned[len(cleaned)-1], segs[i])
}
continue
}
if err == nil && intVal > 99 {
segs[i] = fmt.Sprintf("['%d']", intVal)
if i < len(cleaned) {
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", segs[i-1], segs[i])
} else {
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", cleaned[len(cleaned)-1], segs[i])
}
continue
}
cleaned = append(cleaned, segs[i])
}
}
_, err := strconv.ParseInt(name, 10, 32)
var replaced string
if err != nil {

View File

@@ -621,11 +621,17 @@ func TestConvertComponentIdIntoFriendlyPathSearch_Crazy(t *testing.T) {
}
func TestConvertComponentIdIntoFriendlyPathSearch_Simple(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#//~1fresh~1pizza/get")
assert.Equal(t, "$.['/fresh/pizza'].get", path)
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/~1fresh~1pizza/get")
assert.Equal(t, "$['/fresh/pizza'].get", path)
assert.Equal(t, "get", segment)
}
func TestConvertComponentIdIntoFriendlyPathSearch_Params(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/why/0")
assert.Equal(t, "$.why[0]", path)
assert.Equal(t, "0", segment)
}
func TestConvertComponentIdIntoFriendlyPathSearch_Crazy_Github(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/paths/~1crazy~1ass~1references/get/responses/404/content/application~1xml;%20charset=utf-8/schema")
assert.Equal(t, "$.paths['/crazy/ass/references'].get.responses['404'].content['application/xml; charset=utf-8'].schema", path)
@@ -650,6 +656,12 @@ func TestConvertComponentIdIntoFriendlyPathSearch_CrazyShort(t *testing.T) {
assert.Equal(t, "/crazy/ass/references", segment)
}
func TestConvertComponentIdIntoFriendlyPathSearch_Short(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("/~1crazy~1ass~1references")
assert.Equal(t, "$['/crazy/ass/references']", path)
assert.Equal(t, "/crazy/ass/references", segment)
}
func TestConvertComponentIdIntoFriendlyPathSearch_Array(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/paths/~1crazy~1ass~1references/get/parameters/0")
assert.Equal(t, "$.paths['/crazy/ass/references'].get.parameters[0]", path)