Skip to content

Commit

Permalink
Using testify, added a few tests for the home_service.go methods (#32)
Browse files Browse the repository at this point in the history
We are now using https://github.com/stretchr/testify to ease unit tests
writing.
  • Loading branch information
thibauult authored Jan 11, 2024
1 parent 56c22b0 commit 0583d85
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 247 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.20.0
)

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -22,6 +24,7 @@ require (
github.com/miekg/dns v1.1.57 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions openhue/buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package openhue

import (
"openhue-cli/openhue/test/assert"
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewTestBuildInfo(t *testing.T) {

info := NewTestBuildInfo()

assert.Equals(t, "1.0.0", info.Version)
assert.Equals(t, "1234", info.Commit)
assert.Equals(t, "now", info.Date)
assert.Equal(t, "1.0.0", info.Version)
assert.Equal(t, "1234", info.Commit)
assert.Equal(t, "now", info.Date)
}
8 changes: 4 additions & 4 deletions openhue/context_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package openhue

import (
"github.com/stretchr/testify/assert"
"openhue-cli/openhue/gen"
"openhue-cli/openhue/test/assert"
"testing"
)

func TestNewContext(t *testing.T) {
ctx := NewContext(NewTestIOStreamsDiscard(), NewTestBuildInfo(), &gen.ClientWithResponses{})
assert.NotNil(t, ctx)
assert.NotNil(t, ctx, "Context should not be nil")
}

func TestNewTestContextWithoutApi(t *testing.T) {
ctx, out := NewTestContextWithoutApi()
assert.NotNil(t, ctx)
assert.NotNil(t, out)
assert.NotNil(t, ctx, "Context should not be nil")
assert.NotNil(t, out, "Out buffer should not be nil")
}
143 changes: 143 additions & 0 deletions openhue/home_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package openhue

import (
"context"
"errors"
log "github.com/sirupsen/logrus"
"openhue-cli/openhue/gen"
)

type hueHomeCtx struct {
// api
api *gen.ClientWithResponses
// context
home *gen.BridgeHomeGet
rooms map[string]gen.RoomGet
devices map[string]gen.DeviceGet
lights map[string]gen.LightGet
groupedLights map[string]gen.GroupedLightGet
}

func loadHueHomeCtx(api *gen.ClientWithResponses) (*hueHomeCtx, error) {

hueHome, err := fetchBridgeHome(api)
if err != nil {
return nil, err
}

rooms, err := fetchRooms(api)
if err != nil {
return nil, err
}

devices, err := fetchDevices(api)
if err != nil {
return nil, err
}

lights, err := fetchLights(api)
if err != nil {
return nil, err
}

groupedLights, err := fetchGroupedLights(api)
if err != nil {
return nil, err
}

return &hueHomeCtx{
api: api,
home: hueHome,
rooms: rooms,
devices: devices,
lights: lights,
groupedLights: groupedLights,
}, nil
}

func fetchBridgeHome(api *gen.ClientWithResponses) (*gen.BridgeHomeGet, error) {
log.Info("Fetching home...")

resp, err := api.GetBridgeHomesWithResponse(context.Background())
if err != nil {
return nil, err
}

if len(*(*resp.JSON200).Data) != 1 {
return nil, errors.New("more than 1 home attached to the bridge is not supported yet")
}

return &(*(*resp.JSON200).Data)[0], nil
}

func fetchDevices(api *gen.ClientWithResponses) (map[string]gen.DeviceGet, error) {
log.Info("Fetching devices...")

resp, err := api.GetDevicesWithResponse(context.Background())
if err != nil {
return nil, err
}

devices := make(map[string]gen.DeviceGet)
hueDevices := (*resp.JSON200).Data

for _, device := range *hueDevices {
devices[*device.Id] = device
}

return devices, err
}

func fetchRooms(api *gen.ClientWithResponses) (map[string]gen.RoomGet, error) {
log.Info("Fetching rooms...")

resp, err := api.GetRoomsWithResponse(context.Background())
if err != nil {
return nil, err
}

rooms := make(map[string]gen.RoomGet)
hueRooms := (*resp.JSON200).Data

for _, room := range *hueRooms {
rooms[*room.Id] = room
}

return rooms, err
}

func fetchLights(api *gen.ClientWithResponses) (map[string]gen.LightGet, error) {
log.Info("Fetching lights...")

resp, err := api.GetLightsWithResponse(context.Background())
if err != nil {
return nil, err
}

lights := make(map[string]gen.LightGet)
hueLights := (*resp.JSON200).Data

for _, light := range *hueLights {
lights[*light.Id] = light
}

return lights, nil
}

func fetchGroupedLights(api *gen.ClientWithResponses) (map[string]gen.GroupedLightGet, error) {
log.Info("Fetching grouped lights...")

resp, err := api.GetGroupedLightsWithResponse(context.Background())
if err != nil {
return nil, err
}

groupedLights := make(map[string]gen.GroupedLightGet)
hueGroupedLights := (*resp.JSON200).Data

for _, groupedLight := range *hueGroupedLights {
groupedLights[*groupedLight.Id] = groupedLight
}

return groupedLights, err
}
90 changes: 0 additions & 90 deletions openhue/home_helper.go

This file was deleted.

Loading

0 comments on commit 0583d85

Please sign in to comment.