-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheating_manager_test.go
56 lines (48 loc) · 1.34 KB
/
heating_manager_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
package main
import (
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
func TestNewHeatingManager(t *testing.T) {
manager, err := NewHeatingManager()
if err != nil {
t.Fatalf("Failed to create HeatingManager: %v", err)
}
if manager == nil {
t.Fatal("Expected non-nil HeatingManager instance")
}
}
func TestCheckTemperature(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("25"))
}))
defer ts.Close()
manager, _ := NewHeatingManager()
manager.Config.ShellyURL = ts.URL
manager.checkTemperature(manager.Config.ShellyURL)
if manager.TemperatureExceeded {
t.Error("TemperatureExceeded should be false for temperature 25")
}
}
func TestWeeklyCheck(t *testing.T) {
manager, _ := NewHeatingManager()
manager.weeklyCheck("someURL", "someOtherURL")
}
func TestGetTemperature(t *testing.T) {
expectedTemp := 25.0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(strconv.FormatFloat(expectedTemp, 'f', -1, 64)))
}))
defer ts.Close()
temp, err := getTemperature(ts.URL)
if err != nil {
t.Errorf("getTemperature returned an error: %v", err)
}
if temp != expectedTemp {
t.Errorf("Expected %v, got %v", expectedTemp, temp)
}
}