-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using testify, added a few tests for the home_service.go methods (#32)
We are now using https://github.com/stretchr/testify to ease unit tests writing.
- Loading branch information
Showing
8 changed files
with
342 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.