-
Notifications
You must be signed in to change notification settings - Fork 4
/
thermostat_test.go
105 lines (81 loc) · 2.87 KB
/
thermostat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package thermostat
import (
"errors"
"testing"
"time"
"github.com/alittlebrighter/thermostat/controller"
"github.com/alittlebrighter/thermostat/util"
)
func TestProcessTemperatureReading(t *testing.T) {
baseThermostat.Modes = map[string]*Window{"default": &Window{LowTemp: 69, HighTemp: 80}}
// order of tests matters because the process takes direction into account and it's unlikely
// and probably bad to run the AC directly following the heat
baseThermostat.ProcessTemperatureReading(68.9, util.Celsius)
if baseThermostat.control.Direction() != controller.Heating {
t.Error("Failed to set direction to HEATING.")
}
baseThermostat.ProcessTemperatureReading(72.5, util.Celsius)
if baseThermostat.control.Direction() != controller.None {
t.Error("Failed to set direction to NONE.")
}
baseThermostat.ProcessTemperatureReading(82, util.Celsius)
if baseThermostat.control.Direction() != controller.Cooling {
t.Error("Failed to set direction to COOLING.")
}
}
func TestHandleError(t *testing.T) {
baseThermostat.Modes = map[string]*Window{"default": &Window{LowTemp: 69, HighTemp: 80}}
baseThermostat.ProcessTemperatureReading(82, util.Celsius)
baseThermostat.MaxErrors = 3
for i := 0; uint8(i) < baseThermostat.MaxErrors; i++ {
baseThermostat.HandleError()
if baseThermostat.control.Direction() == controller.None {
t.Error("Shut off HVAC after too few errors.")
}
}
baseThermostat.HandleError()
if baseThermostat.control.Direction() != controller.None {
t.Error("Failed to shut off HVAC MaxErrors.")
}
}
var baseThermostat = &Thermostat{
Modes: map[string]*Window{"default": &Window{LowTemp: 69, HighTemp: 80}},
DefaultMode: "default",
Schedule: []*ScheduleEvent{},
Overshoot: 3,
PollInterval: util.Duration(time.Duration(1) * time.Minute),
UnitPreference: util.Celsius,
Events: util.NewRingBuffer(1),
control: new(MockController),
thermometer: new(MockThermometer),
}
type MockController struct {
direction controller.ThermoDirection
}
func (mc *MockController) Direction() controller.ThermoDirection {
return mc.direction
}
func (mc *MockController) Off() {
mc.direction = controller.None
}
func (mc *MockController) Fan() {
mc.direction = controller.Fan
}
func (mc *MockController) Cool() {
mc.direction = controller.Cooling
}
func (mc *MockController) Heat() {
mc.direction = controller.Heating
}
func (mc *MockController) Shutdown() {}
type MockThermometer struct{}
func (mt *MockThermometer) ReadTemperature() (float64, util.TemperatureUnits, error) {
return ambientTemp, util.Celsius, nil
}
func (mt *MockThermometer) Shutdown() {}
type MockErrorThermometer struct{}
func (mt *MockErrorThermometer) ReadTemperature() (float64, util.TemperatureUnits, error) {
return ambientTemp, util.Celsius, errors.New("Temperature not available.")
}
func (mt *MockErrorThermometer) Shutdown() {}
var ambientTemp = 72.5