Skip to content

Commit faf191b

Browse files
committed
bumped coverage on tests
Signed-off-by: quobix <[email protected]>
1 parent 198a471 commit faf191b

4 files changed

Lines changed: 259 additions & 2 deletions

File tree

datamodel/high/base/schema_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package base
55

66
import (
77
"fmt"
8+
"github.com/pb33f/libopenapi/datamodel"
89
"strings"
910
"testing"
1011

@@ -1191,3 +1192,97 @@ properties:
11911192
assert.Equal(t, true, compiled.Properties["additionalPropertiesBool"].Schema().AdditionalProperties.B)
11921193
assert.Equal(t, []string{"string"}, compiled.Properties["additionalPropertiesAnyOf"].Schema().AdditionalProperties.A.Schema().AnyOf[0].Schema().Type)
11931194
}
1195+
1196+
func TestSchema_RenderProxyWithConfig_3(t *testing.T) {
1197+
testSpec := `exclusiveMinimum: true`
1198+
1199+
var compNode yaml.Node
1200+
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
1201+
1202+
sp := new(lowbase.SchemaProxy)
1203+
err := sp.Build(nil, compNode.Content[0], nil)
1204+
assert.NoError(t, err)
1205+
1206+
config := index.CreateOpenAPIIndexConfig()
1207+
config.SpecInfo = &datamodel.SpecInfo{
1208+
VersionNumeric: 3.0,
1209+
}
1210+
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
1211+
Value: sp,
1212+
ValueNode: compNode.Content[0],
1213+
}
1214+
1215+
schemaProxy := NewSchemaProxy(&lowproxy)
1216+
compiled := schemaProxy.Schema()
1217+
1218+
// now render it out, it should be identical.
1219+
schemaBytes, _ := compiled.Render()
1220+
assert.Equal(t, testSpec, strings.TrimSpace(string(schemaBytes)))
1221+
}
1222+
1223+
func TestSchema_RenderProxyWithConfig_Corrected_31(t *testing.T) {
1224+
testSpec := `exclusiveMinimum: true`
1225+
testSpecCorrect := `exclusiveMinimum: 0`
1226+
1227+
var compNode yaml.Node
1228+
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
1229+
1230+
sp := new(lowbase.SchemaProxy)
1231+
config := index.CreateOpenAPIIndexConfig()
1232+
config.SpecInfo = &datamodel.SpecInfo{
1233+
VersionNumeric: 3.1,
1234+
}
1235+
idx := index.NewSpecIndexWithConfig(compNode.Content[0], config)
1236+
1237+
err := sp.Build(nil, compNode.Content[0], idx)
1238+
assert.NoError(t, err)
1239+
1240+
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
1241+
Value: sp,
1242+
ValueNode: compNode.Content[0],
1243+
}
1244+
1245+
schemaProxy := NewSchemaProxy(&lowproxy)
1246+
compiled := schemaProxy.Schema()
1247+
1248+
// now render it out, it should be identical.
1249+
schemaBytes, _ := compiled.Render()
1250+
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
1251+
1252+
schemaBytes, _ = compiled.RenderInline()
1253+
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
1254+
1255+
}
1256+
1257+
func TestSchema_RenderProxyWithConfig_Corrected_3(t *testing.T) {
1258+
testSpec := `exclusiveMinimum: 0`
1259+
testSpecCorrect := `exclusiveMinimum: false`
1260+
1261+
var compNode yaml.Node
1262+
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
1263+
1264+
sp := new(lowbase.SchemaProxy)
1265+
config := index.CreateOpenAPIIndexConfig()
1266+
config.SpecInfo = &datamodel.SpecInfo{
1267+
VersionNumeric: 3.0,
1268+
}
1269+
idx := index.NewSpecIndexWithConfig(compNode.Content[0], config)
1270+
1271+
err := sp.Build(nil, compNode.Content[0], idx)
1272+
assert.NoError(t, err)
1273+
1274+
lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
1275+
Value: sp,
1276+
ValueNode: compNode.Content[0],
1277+
}
1278+
1279+
schemaProxy := NewSchemaProxy(&lowproxy)
1280+
compiled := schemaProxy.Schema()
1281+
1282+
// now render it out, it should be identical.
1283+
schemaBytes, _ := compiled.Render()
1284+
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
1285+
1286+
schemaBytes, _ = compiled.RenderInline()
1287+
assert.Equal(t, testSpecCorrect, strings.TrimSpace(string(schemaBytes)))
1288+
}

datamodel/high/node_builder_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type test1 struct {
9090
Thugg *bool `yaml:"thugg,renderZero"`
9191
Thurr *int64 `yaml:"thurr,omitempty"`
9292
Thral *float64 `yaml:"thral,omitempty"`
93+
Throo *float64 `yaml:"throo,renderZero,omitempty"`
9394
Tharg []string `yaml:"tharg,omitempty"`
9495
Type []string `yaml:"type,omitempty"`
9596
Throg []*key `yaml:"throg,omitempty"`
@@ -922,6 +923,40 @@ func TestNewNodeBuilder_TestRenderZero(t *testing.T) {
922923
assert.Equal(t, desired, strings.TrimSpace(string(data)))
923924
}
924925

926+
func TestNewNodeBuilder_TestRenderZero_Float(t *testing.T) {
927+
928+
f := 0.0
929+
t1 := test1{
930+
Throo: &f,
931+
}
932+
933+
nb := NewNodeBuilder(&t1, &t1)
934+
node := nb.Render()
935+
936+
data, _ := yaml.Marshal(node)
937+
938+
desired := `throo: 0`
939+
940+
assert.Equal(t, desired, strings.TrimSpace(string(data)))
941+
}
942+
943+
func TestNewNodeBuilder_TestRenderZero_Float_NotZero(t *testing.T) {
944+
945+
f := 0.12
946+
t1 := test1{
947+
Throo: &f,
948+
}
949+
950+
nb := NewNodeBuilder(&t1, &t1)
951+
node := nb.Render()
952+
953+
data, _ := yaml.Marshal(node)
954+
955+
desired := `throo: 0.12`
956+
957+
assert.Equal(t, desired, strings.TrimSpace(string(data)))
958+
}
959+
925960
func TestNewNodeBuilder_TestRenderServerVariableSimulation(t *testing.T) {
926961

927962
t1 := test1{

datamodel/low/base/schema_test.go

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package base
22

33
import (
4-
"testing"
5-
4+
"github.com/pb33f/libopenapi/datamodel"
65
"github.com/pb33f/libopenapi/datamodel/low"
76
"github.com/pb33f/libopenapi/index"
87
"github.com/pb33f/libopenapi/resolver"
98
"github.com/pb33f/libopenapi/utils"
109
"github.com/stretchr/testify/assert"
1110
"gopkg.in/yaml.v3"
11+
"testing"
1212
)
1313

1414
func test_get_schema_blob() string {
@@ -1636,3 +1636,123 @@ func TestSchema_UnevaluatedPropertiesAsBool_Undefined(t *testing.T) {
16361636

16371637
assert.Nil(t, res.Value.Schema().UnevaluatedProperties.Value)
16381638
}
1639+
1640+
func TestSchema_ExclusiveMinimum_3_with_Config(t *testing.T) {
1641+
yml := `openapi: 3.0.3
1642+
components:
1643+
schemas:
1644+
Something:
1645+
type: integer
1646+
minimum: 3
1647+
exclusiveMinimum: true`
1648+
1649+
var iNode yaml.Node
1650+
mErr := yaml.Unmarshal([]byte(yml), &iNode)
1651+
assert.NoError(t, mErr)
1652+
1653+
config := index.CreateOpenAPIIndexConfig()
1654+
config.SpecInfo = &datamodel.SpecInfo{
1655+
VersionNumeric: 3.0,
1656+
}
1657+
1658+
idx := index.NewSpecIndexWithConfig(&iNode, config)
1659+
1660+
yml = `$ref: '#/components/schemas/Something'`
1661+
1662+
var idxNode yaml.Node
1663+
_ = yaml.Unmarshal([]byte(yml), &idxNode)
1664+
1665+
res, _ := ExtractSchema(idxNode.Content[0], idx)
1666+
1667+
assert.True(t, res.Value.Schema().ExclusiveMinimum.Value.A)
1668+
}
1669+
1670+
func TestSchema_ExclusiveMinimum_31_with_Config(t *testing.T) {
1671+
yml := `openapi: 3.1
1672+
components:
1673+
schemas:
1674+
Something:
1675+
type: integer
1676+
minimum: 3
1677+
exclusiveMinimum: 3`
1678+
1679+
var iNode yaml.Node
1680+
mErr := yaml.Unmarshal([]byte(yml), &iNode)
1681+
assert.NoError(t, mErr)
1682+
1683+
config := index.CreateOpenAPIIndexConfig()
1684+
config.SpecInfo = &datamodel.SpecInfo{
1685+
VersionNumeric: 3.1,
1686+
}
1687+
1688+
idx := index.NewSpecIndexWithConfig(&iNode, config)
1689+
1690+
yml = `$ref: '#/components/schemas/Something'`
1691+
1692+
var idxNode yaml.Node
1693+
_ = yaml.Unmarshal([]byte(yml), &idxNode)
1694+
1695+
res, _ := ExtractSchema(idxNode.Content[0], idx)
1696+
1697+
assert.Equal(t, 3.0, res.Value.Schema().ExclusiveMinimum.Value.B)
1698+
}
1699+
1700+
func TestSchema_ExclusiveMaximum_3_with_Config(t *testing.T) {
1701+
yml := `openapi: 3.0.3
1702+
components:
1703+
schemas:
1704+
Something:
1705+
type: integer
1706+
maximum: 3
1707+
exclusiveMaximum: true`
1708+
1709+
var iNode yaml.Node
1710+
mErr := yaml.Unmarshal([]byte(yml), &iNode)
1711+
assert.NoError(t, mErr)
1712+
1713+
config := index.CreateOpenAPIIndexConfig()
1714+
config.SpecInfo = &datamodel.SpecInfo{
1715+
VersionNumeric: 3.0,
1716+
}
1717+
1718+
idx := index.NewSpecIndexWithConfig(&iNode, config)
1719+
1720+
yml = `$ref: '#/components/schemas/Something'`
1721+
1722+
var idxNode yaml.Node
1723+
_ = yaml.Unmarshal([]byte(yml), &idxNode)
1724+
1725+
res, _ := ExtractSchema(idxNode.Content[0], idx)
1726+
1727+
assert.True(t, res.Value.Schema().ExclusiveMaximum.Value.A)
1728+
}
1729+
1730+
func TestSchema_ExclusiveMaximum_31_with_Config(t *testing.T) {
1731+
yml := `openapi: 3.1
1732+
components:
1733+
schemas:
1734+
Something:
1735+
type: integer
1736+
maximum: 3
1737+
exclusiveMaximum: 3`
1738+
1739+
var iNode yaml.Node
1740+
mErr := yaml.Unmarshal([]byte(yml), &iNode)
1741+
assert.NoError(t, mErr)
1742+
1743+
config := index.CreateOpenAPIIndexConfig()
1744+
config.SpecInfo = &datamodel.SpecInfo{
1745+
VersionNumeric: 3.1,
1746+
}
1747+
1748+
idx := index.NewSpecIndexWithConfig(&iNode, config)
1749+
1750+
yml = `$ref: '#/components/schemas/Something'`
1751+
1752+
var idxNode yaml.Node
1753+
_ = yaml.Unmarshal([]byte(yml), &idxNode)
1754+
1755+
res, _ := ExtractSchema(idxNode.Content[0], idx)
1756+
1757+
assert.Equal(t, 3.0, res.Value.Schema().ExclusiveMaximum.Value.B)
1758+
}

index/index_model_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ func TestSpecIndex_Children(t *testing.T) {
2323
assert.Equal(t, 1, len(idx4.GetChildren()))
2424
assert.Equal(t, 0, len(idx5.GetChildren()))
2525
}
26+
27+
func TestSpecIndex_GetConfig(t *testing.T) {
28+
idx1 := new(SpecIndex)
29+
c := SpecIndexConfig{}
30+
idx1.config = &c
31+
assert.Equal(t, &c, idx1.GetConfig())
32+
}

0 commit comments

Comments
 (0)