-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathha_metric_test.go
More file actions
73 lines (62 loc) · 1.57 KB
/
ha_metric_test.go
File metadata and controls
73 lines (62 loc) · 1.57 KB
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
package main
import (
"context"
_ "embed"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/fagnercarvalho/ha-influx-grafana/ha"
"github.com/fagnercarvalho/ha-influx-grafana/metrics"
"github.com/fagnercarvalho/ha-influx-grafana/metrics/metertest"
)
//go:embed testdata/state.json
var state string
func TestAddMetric(t *testing.T) {
tests := []struct {
name string
expectMetrics []metrics.Metric
}{
{
name: "happy path",
expectMetrics: []metrics.Metric{
{
Name: "binary_sensor.motion_sensor",
Value: 0,
Unit: "",
Attributes: map[string]interface{}{
"device_class": "motion",
"friendly_name": "Motion sensor",
},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mockMeter, err := metertest.NewMock()
if err != nil {
t.Fatal(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(state))
}))
homeAssistant := ha.NewHomeAssistant(server.URL, "")
err = addMetric(context.Background(), "entityID", homeAssistant, mockMeter)
if err != nil {
t.Fatal(err)
}
metrics, err := mockMeter.Collect()
if err != nil {
t.Fatal(err)
}
if len(metrics) != len(test.expectMetrics) {
t.Errorf("got %d metrics, want %d", len(metrics), len(test.expectMetrics))
}
if !reflect.DeepEqual(test.expectMetrics, metrics) {
t.Errorf("got metrics %v, want %v", metrics, test.expectMetrics)
}
})
}
}