Fix tests for templates without usages (#6939)

This commit is contained in:
andig 2023-03-19 12:57:14 +01:00 • committed by GitHub
parent 9a6791d0da
commit 31ab90e638
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 56 additions and 54 deletions

View file

@ -6,5 +6,4 @@ products:
params:
- preset: ocpp
render: |
type: ocpp
{{ include "ocpp" . }}

View file

@ -1,6 +1,6 @@
{{define "eebus-meter"}}
{{ define "eebus-meter" }}
type: eebus
ski: {{ .ski }}
{{ if ne .ip "" }}ip: {{ .ip }}{{ end }}
meter: true
{{end}}
{{- end }}

View file

@ -1,5 +1,5 @@
{{define "eebus-no-meter"}}
{{ define "eebus-no-meter" }}
type: eebus
ski: {{ .ski }}
{{ if ne .ip "" }}ip: {{ .ip }}{{ end }}
{{end}}
{{- end}}

View file

@ -1,5 +1,5 @@
{{define "switchsocket"}}
{{ define "switchsocket" }}
standbypower: {{ .standbypower }}
{{ if eq .integrateddevice "true" }}features: ["integrateddevice"]{{ end }}
{{ if ne .icon "" }}icon: {{ .icon }}{{ end }}
{{end}}
{{ end -}}

View file

@ -1,4 +1,4 @@
{{define "vehicle-base"}}
{{ define "vehicle-base" }}
{{- if ne .title "" }}
title: {{ .title }}
{{- end }}
@ -19,4 +19,4 @@ phases: {{ .phases }}
{{- if ne .cache "" }}
cache: {{ .cache }}
{{- end }}
{{end}}
{{- end }}

View file

@ -1,4 +1,4 @@
{{define "vehicle-identify"}}
{{ define "vehicle-identify" }}
{{- if or (ne .mode "") (ne .minSoc "") (ne .targetSoc "") (ne .minCurrent "") (ne .maxCurrent "") (ne .priority "") }}
onIdentify:
{{- if (ne .mode "") }}
@ -26,4 +26,4 @@ identifiers:
- {{ . }}
{{- end }}
{{- end }}
{{end}}
{{- end }}

View file

@ -1,3 +1,3 @@
{{define "vehicle-language"}}
{{ define "vehicle-language" }}
language: {{ .language }}
{{end}}
{{- end }}

View file

@ -9,46 +9,26 @@ import (
"gopkg.in/yaml.v3"
)
// renderTest renders and instantiates plus yaml-parses the template per usage
func renderTest(t *testing.T, tmpl Template, values map[string]interface{}, cb func(values map[string]interface{})) {
t.Run(tmpl.Template, func(t *testing.T) {
t.Parallel()
// test renders and instantiates plus yaml-parses the template per usage
func test(t *testing.T, tmpl Template, values map[string]interface{}, cb func(values map[string]interface{})) {
b, _, err := tmpl.RenderResult(TemplateRenderModeInstance, values)
if err != nil {
t.Log(string(b))
t.Error(err)
return
}
_, values, err := tmpl.RenderResult(TemplateRenderModeUnitTest, values)
if err != nil {
t.Log(tmpl.Render)
t.Error(err)
}
var instance interface{}
if err := yaml.Unmarshal(b, &instance); err != nil {
t.Log(string(b))
t.Error(err)
return
}
// instantiate all usage variants
for _, u := range tmpl.Usages() {
t.Run(u, func(t *testing.T) {
// t.Parallel()
// create a copy of the map for parallel execution
usageValues := make(map[string]interface{}, len(values)+1)
if err := copier.Copy(&usageValues, values); err != nil {
panic(err)
}
usageValues[ParamUsage] = u
b, _, err := tmpl.RenderResult(TemplateRenderModeInstance, usageValues)
if err != nil {
t.Errorf("usage: %s, result: %v", u, err)
}
var instance interface{}
if err := yaml.Unmarshal(b, &instance); err != nil {
t.Errorf("usage: %s, yaml: %v", u, err)
}
// actually run the instance if not on CI
if os.Getenv("CI") == "" {
cb(usageValues)
}
})
}
})
// actually run the instance if not on CI
if os.Getenv("CI") == "" {
cb(values)
}
}
func TestClass(t *testing.T, class Class, instantiate func(t *testing.T, values map[string]interface{})) {
@ -74,9 +54,32 @@ func TestClass(t *testing.T, class Class, instantiate func(t *testing.T, values
tmpl.ModbusValues(TemplateRenderModeInstance, values)
}
renderTest(t, tmpl, values, func(values map[string]interface{}) {
instantiate(t, values)
})
usages := tmpl.Usages()
if len(usages) == 0 {
test(t, tmpl, values, func(values map[string]interface{}) {
instantiate(t, values)
})
return
}
for _, u := range usages {
// create a copy of the map for parallel execution
usageValues := make(map[string]interface{}, len(values)+1)
if err := copier.Copy(&usageValues, values); err != nil {
panic(err)
}
usageValues[ParamUsage] = u
// subtest for each usage
t.Run(u, func(t *testing.T) {
t.Parallel()
test(t, tmpl, usageValues, func(values map[string]interface{}) {
instantiate(t, values)
})
})
}
})
}
}