mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 04:20:17 +00:00
v2 and v3 response model now in place.
Closing in around the branch soon, all the leaves and twigs are just about in place.
This commit is contained in:
227
what-changed/model/response_test.go
Normal file
227
what-changed/model/response_test.go
Normal file
@@ -0,0 +1,227 @@
|
||||
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/v2"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompareResponse_V2(t *testing.T) {
|
||||
|
||||
left := `description: response
|
||||
schema:
|
||||
type: string
|
||||
headers:
|
||||
thing:
|
||||
description: a header
|
||||
examples:
|
||||
bam: alam
|
||||
x-toot: poot`
|
||||
right := left
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.Response
|
||||
var rDoc v2.Response
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareResponse(&lDoc, &rDoc)
|
||||
assert.Nil(t, extChanges)
|
||||
|
||||
}
|
||||
|
||||
func TestCompareResponse_V2_Modify(t *testing.T) {
|
||||
|
||||
left := `description: response
|
||||
schema:
|
||||
type: string
|
||||
headers:
|
||||
thing:
|
||||
description: a header
|
||||
examples:
|
||||
bam: alam`
|
||||
|
||||
right := `description: response changed
|
||||
schema:
|
||||
type: int
|
||||
headers:
|
||||
thing:
|
||||
description: a header changed
|
||||
examples:
|
||||
bam: alabama
|
||||
x-toot: poot`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.Response
|
||||
var rDoc v2.Response
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareResponse(&lDoc, &rDoc)
|
||||
assert.Equal(t, 5, extChanges.TotalChanges())
|
||||
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
|
||||
}
|
||||
|
||||
func TestCompareResponse_V2_Add(t *testing.T) {
|
||||
|
||||
left := `description: response
|
||||
headers:
|
||||
thing:
|
||||
description: a header`
|
||||
|
||||
right := `description: response
|
||||
schema:
|
||||
type: int
|
||||
headers:
|
||||
thing:
|
||||
description: a header
|
||||
examples:
|
||||
bam: alam`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.Response
|
||||
var rDoc v2.Response
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareResponse(&lDoc, &rDoc)
|
||||
assert.Equal(t, 2, extChanges.TotalChanges())
|
||||
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
|
||||
}
|
||||
|
||||
func TestCompareResponse_V2_Remove(t *testing.T) {
|
||||
|
||||
left := `description: response
|
||||
headers:
|
||||
thing:
|
||||
description: a header`
|
||||
|
||||
right := `description: response
|
||||
schema:
|
||||
type: int
|
||||
headers:
|
||||
thing:
|
||||
description: a header
|
||||
examples:
|
||||
bam: alabama`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v2.Response
|
||||
var rDoc v2.Response
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareResponse(&rDoc, &lDoc)
|
||||
assert.Equal(t, 2, extChanges.TotalChanges())
|
||||
assert.Equal(t, 1, extChanges.TotalBreakingChanges())
|
||||
}
|
||||
|
||||
func TestCompareResponse_V3(t *testing.T) {
|
||||
|
||||
left := `description: response
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
headers:
|
||||
thing:
|
||||
description: a header
|
||||
links:
|
||||
aLink:
|
||||
operationId: oneTwoThree
|
||||
x-toot: poot`
|
||||
right := left
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v3.Response
|
||||
var rDoc v3.Response
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareResponse(&lDoc, &rDoc)
|
||||
assert.Nil(t, extChanges)
|
||||
}
|
||||
|
||||
func TestCompareResponse_V3_Modify(t *testing.T) {
|
||||
|
||||
left := `description: response
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
headers:
|
||||
thing:
|
||||
description: a header
|
||||
links:
|
||||
aLink:
|
||||
operationId: oneTwoThree
|
||||
server:
|
||||
url: https://pb33f.io
|
||||
x-toot: poot`
|
||||
|
||||
right := `links:
|
||||
aLink:
|
||||
operationId: oneTwoThreeFour
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: int
|
||||
description: response change
|
||||
headers:
|
||||
thing:
|
||||
description: a header changed
|
||||
x-toot: pooty`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc v3.Response
|
||||
var rDoc v3.Response
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
extChanges := CompareResponse(&lDoc, &rDoc)
|
||||
|
||||
assert.Equal(t, 5, extChanges.TotalChanges())
|
||||
assert.Equal(t, 2, extChanges.TotalBreakingChanges())
|
||||
}
|
||||
Reference in New Issue
Block a user