mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 19:15:12 -06:00
add some unit tests for some of the checkers
This commit is contained in:
133
checkers_test.go
Normal file
133
checkers_test.go
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
package gomeassistant
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"saml.dev/gome-assistant/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MockState struct {
|
||||||
|
EqualsReturn bool
|
||||||
|
EqualsError bool
|
||||||
|
GetReturn EntityState
|
||||||
|
GetError bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s MockState) AfterSunrise(_ ...DurationString) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (s MockState) BeforeSunrise(_ ...DurationString) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (s MockState) AfterSunset(_ ...DurationString) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (s MockState) BeforeSunset(_ ...DurationString) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (s MockState) Get(eid string) (EntityState, error) {
|
||||||
|
if s.GetError {
|
||||||
|
return EntityState{}, errors.New("some error")
|
||||||
|
}
|
||||||
|
return s.GetReturn, nil
|
||||||
|
}
|
||||||
|
func (s MockState) Equals(eid, state string) (bool, error) {
|
||||||
|
if s.EqualsError {
|
||||||
|
return false, errors.New("some error")
|
||||||
|
}
|
||||||
|
return s.EqualsReturn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var runOnError = internal.EnabledDisabledInfo{
|
||||||
|
Entity: "eid",
|
||||||
|
State: "state",
|
||||||
|
RunOnError: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var dontRunOnError = internal.EnabledDisabledInfo{
|
||||||
|
Entity: "eid",
|
||||||
|
State: "state",
|
||||||
|
RunOnError: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
func list(infos ...internal.EnabledDisabledInfo) []internal.EnabledDisabledInfo {
|
||||||
|
ret := []internal.EnabledDisabledInfo{}
|
||||||
|
ret = append(ret, infos...)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnabledEntity_StateEqual_Passes(t *testing.T) {
|
||||||
|
state := MockState{
|
||||||
|
EqualsReturn: true,
|
||||||
|
}
|
||||||
|
c := checkEnabledEntity(state, list(runOnError))
|
||||||
|
assert.False(t, c.fail, "should pass")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnabledEntity_StateNotEqual_Fails(t *testing.T) {
|
||||||
|
state := MockState{
|
||||||
|
EqualsReturn: false,
|
||||||
|
}
|
||||||
|
c := checkEnabledEntity(state, list(runOnError))
|
||||||
|
assert.True(t, c.fail, "should fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnabledEntity_NetworkError_DontRun_Fails(t *testing.T) {
|
||||||
|
state := MockState{
|
||||||
|
EqualsError: true,
|
||||||
|
}
|
||||||
|
c := checkEnabledEntity(state, list(dontRunOnError))
|
||||||
|
assert.True(t, c.fail, "should fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnabledEntity_NetworkError_StillRun_Passes(t *testing.T) {
|
||||||
|
state := MockState{
|
||||||
|
EqualsError: true,
|
||||||
|
}
|
||||||
|
c := checkEnabledEntity(state, list(runOnError))
|
||||||
|
assert.False(t, c.fail, "should fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDisabledEntity_StateEqual_Fails(t *testing.T) {
|
||||||
|
state := MockState{
|
||||||
|
EqualsReturn: true,
|
||||||
|
}
|
||||||
|
c := checkDisabledEntity(state, list(runOnError))
|
||||||
|
assert.True(t, c.fail, "should pass")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDisabledEntity_StateNotEqual_Passes(t *testing.T) {
|
||||||
|
state := MockState{
|
||||||
|
EqualsReturn: false,
|
||||||
|
}
|
||||||
|
c := checkDisabledEntity(state, list(runOnError))
|
||||||
|
assert.False(t, c.fail, "should fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDisabledEntity_NetworkError_DontRun_Fails(t *testing.T) {
|
||||||
|
state := MockState{
|
||||||
|
EqualsError: true,
|
||||||
|
}
|
||||||
|
c := checkDisabledEntity(state, list(dontRunOnError))
|
||||||
|
assert.True(t, c.fail, "should fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDisabledEntity_NetworkError_StillRun_Passes(t *testing.T) {
|
||||||
|
state := MockState{
|
||||||
|
EqualsError: true,
|
||||||
|
}
|
||||||
|
c := checkDisabledEntity(state, list(runOnError))
|
||||||
|
assert.False(t, c.fail, "should fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStatesMatch(t *testing.T) {
|
||||||
|
c := checkStatesMatch("hey", "hey")
|
||||||
|
assert.False(t, c.fail, "should pass")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStatesDontMatch(t *testing.T) {
|
||||||
|
c := checkStatesMatch("hey", "bye")
|
||||||
|
assert.True(t, c.fail, "should fail")
|
||||||
|
}
|
||||||
5
go.mod
5
go.mod
@@ -9,9 +9,14 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/gobuffalo/envy v1.10.2 // indirect
|
github.com/gobuffalo/envy v1.10.2 // indirect
|
||||||
github.com/gobuffalo/packd v1.0.2 // indirect
|
github.com/gobuffalo/packd v1.0.2 // indirect
|
||||||
github.com/gobuffalo/packr v1.30.1 // indirect
|
github.com/gobuffalo/packr v1.30.1 // indirect
|
||||||
github.com/joho/godotenv v1.4.0 // indirect
|
github.com/joho/godotenv v1.4.0 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/rogpeppe/go-internal v1.9.0 // indirect
|
github.com/rogpeppe/go-internal v1.9.0 // indirect
|
||||||
|
github.com/stretchr/objx v0.5.0 // indirect
|
||||||
|
github.com/stretchr/testify v1.8.4 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -57,12 +57,16 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
|
|||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
||||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||||
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
|
|||||||
Reference in New Issue
Block a user