Initial version of the sample live tests

This commit is contained in:
Jiri Luzny
2023-11-12 21:23:00 +01:00
parent 701ff4df58
commit 7c4e1ab8e3
3 changed files with 120 additions and 0 deletions

7
example/config.yaml Normal file
View File

@@ -0,0 +1,7 @@
app:
ip_address: 192.168.0.204
port: 8123
home_zone_entity_id: zone.home
entities:
light_entity_id: light.office_bed_lamp

View File

@@ -0,0 +1,90 @@
package example
import (
"log/slog"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v3"
ga "saml.dev/gome-assistant"
)
type (
MySuite struct {
suite.Suite
app *ga.App
config *Config
}
Config struct {
App struct {
HAAuthToken string `yaml:"ha_auth_token"`
IpAddress string `yaml:"ip_address"`
Port string `yaml:"port"`
HomeZoneEntityId string `yaml:"home_zone_entity_id"`
}
Entities struct {
LightEntityId string `yaml:"light_entity_id"`
}
}
)
func (s *MySuite) SetupTest() {
configFile, err := os.ReadFile("./config.yaml")
if err != nil {
slog.Error("Error reading config file", err)
}
s.config = &Config{}
// either env var or config file can be used to set HA auth. token
s.config.App.HAAuthToken = os.Getenv("HA_AUTH_TOKEN")
if err := yaml.Unmarshal(configFile, s.config); err != nil {
slog.Error("Error unmarshalling config file:", err)
}
s.app, err = ga.NewApp(ga.NewAppRequest{
HAAuthToken: s.config.App.HAAuthToken,
IpAddress: s.config.App.IpAddress,
HomeZoneEntityId: s.config.App.HomeZoneEntityId,
})
if err != nil {
slog.Error("Failed to createw new app:", err)
s.T().FailNow()
}
}
func (s *MySuite) TearDownSuite() {
if s.app != nil {
s.app.Cleanup()
}
}
// Basic test of ga app creation and light toggle service
func (s *MySuite) TestLightService() {
entityId := s.config.Entities.LightEntityId
initialState, err := s.app.GetState().Get(entityId)
if err != nil {
slog.Error("Error getting entity state:", err)
}
slog.Info("Initial state of entity:", "state", initialState.State)
s.app.GetService().Light.Toggle(entityId)
time.Sleep(1 * time.Second) // wait for state to update
// <-time.After(1 * time.Second)
newState, err := s.app.GetState().Get(entityId)
if err != nil {
slog.Error("Error getting entity state:", err)
}
slog.Info("New state of entity:", "state", newState.State)
assert.Equal(s.T(), initialState.State, newState.State)
}
// Run the test suite
func TestMySuite(t *testing.T) {
suite.Run(t, new(MySuite))
}

23
example/go.mod Normal file
View File

@@ -0,0 +1,23 @@
module example
go 1.21.0
require (
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
saml.dev/gome-assistant v0.2.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gobuffalo/envy v1.10.2 // indirect
github.com/gobuffalo/packd v1.0.2 // indirect
github.com/gobuffalo/packr v1.30.1 // indirect
github.com/golang-module/carbon v1.7.3 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/nathan-osman/go-sunrise v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/mod v0.9.0 // indirect
)