mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 20:47:44 +00:00
Continued pattern building, added info changes
tried to make some more generic patterns, but I think it's about as thin as it can be now, some boilerplate will be required. Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
393
what-changed/info_test.go
Normal file
393
what-changed/info_test.go
Normal file
@@ -0,0 +1,393 @@
|
||||
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package what_changed
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompareInfo_DescriptionAdded(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
description: this is a description
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.DescriptionLabel, extChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_TitleRemoved(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
description: this is a description
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
description: this is a description
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.TitleLabel, extChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_VersionModified(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '99.99'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, Modified, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.VersionLabel, extChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_RemoveLicense(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, ObjectRemoved, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.LicenseLabel, extChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_AddLicense(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, ObjectAdded, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.LicenseLabel, extChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_LicenseChanged(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: Apache`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, Modified, extChanges.LicenseChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.NameLabel, extChanges.LicenseChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_AddContact(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, ObjectAdded, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.ContactLabel, extChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_RemoveContact(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, ObjectRemoved, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.ContactLabel, extChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_ContactModified(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: the buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, Modified, extChanges.ContactChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, v3.NameLabel, extChanges.ContactChanges.Changes[0].Property)
|
||||
}
|
||||
|
||||
func TestCompareInfo_Equal(t *testing.T) {
|
||||
|
||||
left := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
right := `title: a nice spec
|
||||
termsOfService: https://pb33f.io/terms
|
||||
version: '1.2.3'
|
||||
contact:
|
||||
name: buckaroo
|
||||
email: buckaroo@pb33f.io
|
||||
license:
|
||||
name: MIT`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc base.Info
|
||||
var rDoc base.Info
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareInfo(&lDoc, &rDoc)
|
||||
assert.Nil(t, extChanges)
|
||||
}
|
||||
Reference in New Issue
Block a user