Bumped coverage back up for model_builder

Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
Dave Shanley
2023-04-30 08:48:23 -04:00
parent 1f9a8bd185
commit f2104552f4

View File

@@ -1,53 +1,53 @@
package low package low
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"sync" "sync"
"testing" "testing"
) )
type hotdog struct { type hotdog struct {
Name NodeReference[string] Name NodeReference[string]
ValueName ValueReference[string] ValueName ValueReference[string]
Fat NodeReference[int] Fat NodeReference[int]
Ketchup NodeReference[float32] Ketchup NodeReference[float32]
Mustard NodeReference[float64] Mustard NodeReference[float64]
Grilled NodeReference[bool] Grilled NodeReference[bool]
MaxTemp NodeReference[int] MaxTemp NodeReference[int]
MaxTempHigh NodeReference[int64] MaxTempHigh NodeReference[int64]
MaxTempAlt []NodeReference[int] MaxTempAlt []NodeReference[int]
Drinks []NodeReference[string] Drinks []NodeReference[string]
Sides []NodeReference[float32] Sides []NodeReference[float32]
BigSides []NodeReference[float64] BigSides []NodeReference[float64]
Temps []NodeReference[int] Temps []NodeReference[int]
HighTemps []NodeReference[int64] HighTemps []NodeReference[int64]
Buns []NodeReference[bool] Buns []NodeReference[bool]
UnknownElements NodeReference[any] UnknownElements NodeReference[any]
LotsOfUnknowns []NodeReference[any] LotsOfUnknowns []NodeReference[any]
Where map[string]NodeReference[any] Where map[string]NodeReference[any]
There map[string]NodeReference[string] There map[string]NodeReference[string]
AllTheThings NodeReference[map[KeyReference[string]]ValueReference[string]] AllTheThings NodeReference[map[KeyReference[string]]ValueReference[string]]
} }
func TestBuildModel_Mismatch(t *testing.T) { func TestBuildModel_Mismatch(t *testing.T) {
yml := `crisps: are tasty` yml := `crisps: are tasty`
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
hd := hotdog{} hd := hotdog{}
cErr := BuildModel(&rootNode, &hd) cErr := BuildModel(&rootNode, &hd)
assert.NoError(t, cErr) assert.NoError(t, cErr)
assert.Empty(t, hd.Name) assert.Empty(t, hd.Name)
} }
func TestBuildModel(t *testing.T) { func TestBuildModel(t *testing.T) {
yml := `name: yummy yml := `name: yummy
valueName: yammy valueName: yammy
beef: true beef: true
fat: 200 fat: 200
@@ -105,306 +105,331 @@ allTheThings:
beer: isGood beer: isGood
cake: isNice` cake: isNice`
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
hd := hotdog{} hd := hotdog{}
cErr := BuildModel(rootNode.Content[0], &hd) cErr := BuildModel(rootNode.Content[0], &hd)
assert.Equal(t, 200, hd.Fat.Value) assert.Equal(t, 200, hd.Fat.Value)
assert.Equal(t, 4, hd.Fat.ValueNode.Line) assert.Equal(t, 4, hd.Fat.ValueNode.Line)
assert.Equal(t, true, hd.Grilled.Value) assert.Equal(t, true, hd.Grilled.Value)
assert.Equal(t, "yummy", hd.Name.Value) assert.Equal(t, "yummy", hd.Name.Value)
assert.Equal(t, "yammy", hd.ValueName.Value) assert.Equal(t, "yammy", hd.ValueName.Value)
assert.Equal(t, float32(200.45), hd.Ketchup.Value) assert.Equal(t, float32(200.45), hd.Ketchup.Value)
assert.Len(t, hd.Drinks, 3) assert.Len(t, hd.Drinks, 3)
assert.Len(t, hd.Sides, 4) assert.Len(t, hd.Sides, 4)
assert.Len(t, hd.BigSides, 4) assert.Len(t, hd.BigSides, 4)
assert.Len(t, hd.Temps, 2) assert.Len(t, hd.Temps, 2)
assert.Len(t, hd.HighTemps, 2) assert.Len(t, hd.HighTemps, 2)
assert.Equal(t, int64(11732849090192923), hd.HighTemps[1].Value) assert.Equal(t, int64(11732849090192923), hd.HighTemps[1].Value)
assert.Len(t, hd.MaxTempAlt, 5) assert.Len(t, hd.MaxTempAlt, 5)
assert.Equal(t, int64(7392837462032342), hd.MaxTempHigh.Value) assert.Equal(t, int64(7392837462032342), hd.MaxTempHigh.Value)
assert.Equal(t, 2, hd.Temps[1].Value) assert.Equal(t, 2, hd.Temps[1].Value)
assert.Equal(t, 27, hd.Temps[1].ValueNode.Line) assert.Equal(t, 27, hd.Temps[1].ValueNode.Line)
assert.Len(t, hd.UnknownElements.Value, 2) assert.Len(t, hd.UnknownElements.Value, 2)
assert.Len(t, hd.LotsOfUnknowns, 3) assert.Len(t, hd.LotsOfUnknowns, 3)
assert.Len(t, hd.Where, 2) assert.Len(t, hd.Where, 2)
assert.Len(t, hd.There, 2) assert.Len(t, hd.There, 2)
assert.Equal(t, "bear", hd.There["care"].Value) assert.Equal(t, "bear", hd.There["care"].Value)
assert.Equal(t, 324938249028.98234892374892374923874823974, hd.Mustard.Value) assert.Equal(t, 324938249028.98234892374892374923874823974, hd.Mustard.Value)
allTheThings := hd.AllTheThings.Value allTheThings := hd.AllTheThings.Value
for i := range allTheThings { for i := range allTheThings {
if i.Value == "beer" { if i.Value == "beer" {
assert.Equal(t, "isGood", allTheThings[i].Value) assert.Equal(t, "isGood", allTheThings[i].Value)
} }
if i.Value == "cake" { if i.Value == "cake" {
assert.Equal(t, "isNice", allTheThings[i].Value) assert.Equal(t, "isNice", allTheThings[i].Value)
} }
} }
assert.NoError(t, cErr) assert.NoError(t, cErr)
} }
func TestBuildModel_UseCopyNotRef(t *testing.T) { func TestBuildModel_UseCopyNotRef(t *testing.T) {
yml := `cake: -99999` yml := `cake: -99999`
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
hd := hotdog{} hd := hotdog{}
cErr := BuildModel(&rootNode, hd) cErr := BuildModel(&rootNode, hd)
assert.Error(t, cErr) assert.Error(t, cErr)
assert.Empty(t, hd.Name) assert.Empty(t, hd.Name)
} }
func TestBuildModel_UseUnsupportedPrimitive(t *testing.T) { func TestBuildModel_UseUnsupportedPrimitive(t *testing.T) {
type notSupported struct { type notSupported struct {
cake string cake string
} }
ns := notSupported{} ns := notSupported{}
yml := `cake: party` yml := `cake: party`
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
cErr := BuildModel(rootNode.Content[0], &ns) cErr := BuildModel(rootNode.Content[0], &ns)
assert.Error(t, cErr) assert.Error(t, cErr)
assert.Empty(t, ns.cake) assert.Empty(t, ns.cake)
} }
func TestBuildModel_UsingInternalConstructs(t *testing.T) { func TestBuildModel_UsingInternalConstructs(t *testing.T) {
type internal struct { type internal struct {
Extensions NodeReference[string] Extensions NodeReference[string]
PathItems NodeReference[string] PathItems NodeReference[string]
Thing NodeReference[string] Thing NodeReference[string]
} }
yml := `extensions: one yml := `extensions: one
pathItems: two pathItems: two
thing: yeah` thing: yeah`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
// try a null build // try a null build
try := BuildModel(nil, ins) try := BuildModel(nil, ins)
assert.NoError(t, try) assert.NoError(t, try)
cErr := BuildModel(rootNode.Content[0], ins) cErr := BuildModel(rootNode.Content[0], ins)
assert.NoError(t, cErr) assert.NoError(t, cErr)
assert.Empty(t, ins.PathItems.Value) assert.Empty(t, ins.PathItems.Value)
assert.Empty(t, ins.Extensions.Value) assert.Empty(t, ins.Extensions.Value)
assert.Equal(t, "yeah", ins.Thing.Value) assert.Equal(t, "yeah", ins.Thing.Value)
} }
func TestSetField_NodeRefAny_Error(t *testing.T) { func TestSetField_NodeRefAny_Error(t *testing.T) {
type internal struct { type internal struct {
Thing []NodeReference[any] Thing []NodeReference[any]
} }
yml := `thing: yml := `thing:
- 999 - 999
- false` - false`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
try := BuildModel(rootNode.Content[0], ins) try := BuildModel(rootNode.Content[0], ins)
assert.Error(t, try) assert.Error(t, try)
} }
func TestSetField_MapHelperWrapped(t *testing.T) { func TestSetField_MapHelperWrapped(t *testing.T) {
type internal struct { type internal struct {
Thing KeyReference[map[KeyReference[string]]ValueReference[string]] Thing KeyReference[map[KeyReference[string]]ValueReference[string]]
} }
yml := `thing: yml := `thing:
what: not what: not
chip: chop chip: chop
lip: lop` lip: lop`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
try := BuildModel(rootNode.Content[0], ins) try := BuildModel(rootNode.Content[0], ins)
assert.NoError(t, try) assert.NoError(t, try)
assert.Len(t, ins.Thing.Value, 3) assert.Len(t, ins.Thing.Value, 3)
} }
func TestSetField_MapHelper(t *testing.T) { func TestSetField_MapHelper(t *testing.T) {
type internal struct { type internal struct {
Thing map[KeyReference[string]]ValueReference[string] Thing map[KeyReference[string]]ValueReference[string]
} }
yml := `thing: yml := `thing:
what: not what: not
chip: chop chip: chop
lip: lop` lip: lop`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
try := BuildModel(rootNode.Content[0], ins) try := BuildModel(rootNode.Content[0], ins)
assert.NoError(t, try) assert.NoError(t, try)
assert.Len(t, ins.Thing, 3) assert.Len(t, ins.Thing, 3)
} }
func TestSetField_ArrayHelper(t *testing.T) { func TestSetField_ArrayHelper(t *testing.T) {
type internal struct { type internal struct {
Thing NodeReference[[]ValueReference[string]] Thing NodeReference[[]ValueReference[string]]
} }
yml := `thing: yml := `thing:
- nice - nice
- rice - rice
- slice` - slice`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
try := BuildModel(rootNode.Content[0], ins) try := BuildModel(rootNode.Content[0], ins)
assert.NoError(t, try) assert.NoError(t, try)
assert.Len(t, ins.Thing.Value, 3) assert.Len(t, ins.Thing.Value, 3)
} }
func TestSetField_Enum_Helper(t *testing.T) { func TestSetField_Enum_Helper(t *testing.T) {
type internal struct { type internal struct {
Thing NodeReference[[]ValueReference[any]] Thing NodeReference[[]ValueReference[any]]
} }
yml := `thing: yml := `thing:
- nice - nice
- rice - rice
- slice` - slice`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
try := BuildModel(rootNode.Content[0], ins) try := BuildModel(rootNode.Content[0], ins)
assert.NoError(t, try) assert.NoError(t, try)
assert.Len(t, ins.Thing.Value, 3) assert.Len(t, ins.Thing.Value, 3)
} }
func TestSetField_Default_Helper(t *testing.T) { func TestSetField_Default_Helper(t *testing.T) {
type cake struct { type cake struct {
thing int thing int
} }
// this should be ignored, no custom objects in here my friend. // this should be ignored, no custom objects in here my friend.
type internal struct { type internal struct {
Thing cake Thing cake
} }
yml := `thing: yml := `thing:
type: cake` type: cake`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
try := BuildModel(rootNode.Content[0], ins) try := BuildModel(rootNode.Content[0], ins)
assert.NoError(t, try) assert.NoError(t, try)
assert.Equal(t, 0, ins.Thing.thing) assert.Equal(t, 0, ins.Thing.thing)
}
func TestHandleSlicesOfInts(t *testing.T) {
type cake struct {
thing int
}
// this should be ignored, no custom objects in here my friend.
type internal struct {
Thing NodeReference[[]ValueReference[any]]
}
yml := `thing:
- 5
- 1.234`
ins := new(internal)
var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr)
try := BuildModel(rootNode.Content[0], ins)
assert.NoError(t, try)
assert.Equal(t, 0, ins.Thing)
} }
func TestSetField_Ignore(t *testing.T) { func TestSetField_Ignore(t *testing.T) {
type Complex struct { type Complex struct {
name string name string
} }
type internal struct { type internal struct {
Thing *Complex Thing *Complex
} }
yml := `thing: yml := `thing:
- nice - nice
- rice - rice
- slice` - slice`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
try := BuildModel(&rootNode, ins) try := BuildModel(&rootNode, ins)
assert.NoError(t, try) assert.NoError(t, try)
assert.Nil(t, ins.Thing) assert.Nil(t, ins.Thing)
} }
func TestBuildModelAsync(t *testing.T) { func TestBuildModelAsync(t *testing.T) {
type internal struct { type internal struct {
Thing KeyReference[map[KeyReference[string]]ValueReference[string]] Thing KeyReference[map[KeyReference[string]]ValueReference[string]]
} }
yml := `thing: yml := `thing:
what: not what: not
chip: chop chip: chop
lip: lop` lip: lop`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
var wg sync.WaitGroup var wg sync.WaitGroup
var errors []error var errors []error
wg.Add(1) wg.Add(1)
BuildModelAsync(rootNode.Content[0], ins, &wg, &errors) BuildModelAsync(rootNode.Content[0], ins, &wg, &errors)
wg.Wait() wg.Wait()
assert.Len(t, ins.Thing.Value, 3) assert.Len(t, ins.Thing.Value, 3)
} }
func TestBuildModelAsync_Error(t *testing.T) { func TestBuildModelAsync_Error(t *testing.T) {
type internal struct { type internal struct {
Thing []NodeReference[any] Thing []NodeReference[any]
} }
yml := `thing: yml := `thing:
- 999 - 999
- false` - false`
ins := new(internal) ins := new(internal)
var rootNode yaml.Node var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode) mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr) assert.NoError(t, mErr)
var wg sync.WaitGroup var wg sync.WaitGroup
var errors []error var errors []error
wg.Add(1) wg.Add(1)
BuildModelAsync(rootNode.Content[0], ins, &wg, &errors) BuildModelAsync(rootNode.Content[0], ins, &wg, &errors)
wg.Wait() wg.Wait()
assert.Len(t, errors, 1) assert.Len(t, errors, 1)
assert.Len(t, ins.Thing, 0) assert.Len(t, ins.Thing, 0)
} }