Skip to content

Commit

Permalink
Add unit tests for ControllerConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Oct 2, 2024
1 parent f380435 commit d4f0466
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
5 changes: 5 additions & 0 deletions garden-app/pkg/action/garden_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func TestGardenActionBind(t *testing.T) {
&GardenAction{},
"missing required action fields",
},
{
"ErrorMissingUpdateConfig",
&GardenAction{Update: &UpdateAction{}},
"update action must have config=true",
},
}

t.Run("SuccessfulLightAction", func(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion garden-app/pkg/controller_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ func (c *ControllerConfig) ToMessage() ControllerConfigMessage {

if c.TemperatureHumidityPin != nil {
message.TemperatureHumidityEnabled = true
message.TemperatureHumidityPin = *c.LightPin
message.TemperatureHumidityPin = *c.TemperatureHumidityPin

if c.TemperatureHumidityInterval != nil {
//nolint:gosec
message.TemperatureHumidityInterval = uint(c.TemperatureHumidityInterval.Duration.Milliseconds())
} else {
message.TemperatureHumidityInterval = 5000
}
}

Expand Down
92 changes: 92 additions & 0 deletions garden-app/pkg/controller_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pkg
import (
"errors"
"testing"
"time"

"github.com/calvinmclean/babyapi"
"github.com/stretchr/testify/assert"
Expand All @@ -26,6 +27,10 @@ func TestControllerConfigPatch(t *testing.T) {
"TemperatureHumidityPin",
&ControllerConfig{TemperatureHumidityPin: pointer(uint(1))},
},
{
"TemperatureHumidityInterval",
&ControllerConfig{TemperatureHumidityInterval: &Duration{Duration: 1 * time.Second}},
},
{
"ValvePinsEmpty",
&ControllerConfig{ValvePins: []uint{}},
Expand Down Expand Up @@ -78,3 +83,90 @@ func TestControllerConfigPatch(t *testing.T) {
assert.ElementsMatch(t, []uint{5}, c.PumpPins)
})
}

func TestToMessage(t *testing.T) {
tests := []struct {
name string
config *ControllerConfig
expected ControllerConfigMessage
}{
{
"FullConfig",
&ControllerConfig{
ValvePins: []uint{1},
PumpPins: []uint{1},
LightPin: pointer(uint(1)),
TemperatureHumidityPin: pointer(uint(1)),
TemperatureHumidityInterval: &Duration{Duration: time.Second},
},
ControllerConfigMessage{
NumZones: 1,
ValvePins: []uint{1},
PumpPins: []uint{1},
LightEnabled: true,
LightPin: uint(1),
TemperatureHumidityEnabled: true,
TemperatureHumidityPin: uint(1),
TemperatureHumidityInterval: 1000,
},
},
{
"DefaultTemperatureHumidityInterval",
&ControllerConfig{
ValvePins: []uint{1},
PumpPins: []uint{1},
LightPin: pointer(uint(1)),
TemperatureHumidityPin: pointer(uint(1)),
},
ControllerConfigMessage{
NumZones: 1,
ValvePins: []uint{1},
PumpPins: []uint{1},
LightEnabled: true,
LightPin: uint(1),
TemperatureHumidityEnabled: true,
TemperatureHumidityPin: uint(1),
TemperatureHumidityInterval: 5000,
},
},
{
"NoLightPin",
&ControllerConfig{
ValvePins: []uint{1},
PumpPins: []uint{1},
TemperatureHumidityPin: pointer(uint(1)),
TemperatureHumidityInterval: &Duration{Duration: time.Second},
},
ControllerConfigMessage{
NumZones: 1,
ValvePins: []uint{1},
PumpPins: []uint{1},
LightEnabled: false,
TemperatureHumidityEnabled: true,
TemperatureHumidityPin: uint(1),
TemperatureHumidityInterval: 1000,
},
},
{
"NoTemperatureHumidityPin",
&ControllerConfig{
ValvePins: []uint{1},
PumpPins: []uint{1},
},
ControllerConfigMessage{
NumZones: 1,
ValvePins: []uint{1},
PumpPins: []uint{1},
LightEnabled: false,
TemperatureHumidityEnabled: false,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := tt.config.ToMessage()
require.Equal(t, tt.expected, out)
})
}
}

0 comments on commit d4f0466

Please sign in to comment.