forked from tskaard/tibber-golang
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tibber_test.go
96 lines (87 loc) · 2.01 KB
/
tibber_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
package tibber
import (
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"testing"
"time"
)
type TestConfig struct {
Endpoint string `yaml:"endpoint"`
Token string `yaml:"token"`
HomeID string `yaml:"homeId"`
}
// load TestConfig from yaml file
func loadTestConfig() TestConfig {
var tc TestConfig
bytes, err := os.ReadFile("test-config.yaml")
if err != nil {
panic(err)
}
err = yaml.Unmarshal(bytes, &tc)
if err != nil {
panic(err)
}
return tc
}
func helperLoadBytes(t *testing.T, name string) []byte {
path := filepath.Join("testdata", name) // relative path
bytes, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return bytes
}
func TestGetHomes(t *testing.T) {
token := string(helperLoadBytes(t, "token.txt"))
tc := NewClient(token)
homes, _ := tc.GetHomes()
if homes == nil {
t.Fatalf("GetHomes: %v", homes)
}
}
func TestGetHomeById(t *testing.T) {
token := string(helperLoadBytes(t, "token.txt"))
tc := NewClient(token)
homeID := string(helperLoadBytes(t, "homeId.txt"))
home, _ := tc.GetHomeById(homeID)
if home.ID == "" {
t.Fatalf("GetHomeById: %s %v", homeID, home)
}
}
func TestPush(t *testing.T) {
token := string(helperLoadBytes(t, "token.txt"))
tc := NewClient(token)
_, err := tc.SendPushNotification("Golang Test", "Running golang test")
if err != nil {
t.Fatalf("Push: %v", err)
}
}
func TestStreams(t *testing.T) {
var msgCh MsgChan
testConfig := loadTestConfig()
token := testConfig.Token
homeID := testConfig.HomeID
t.Logf("homeID: %s", homeID)
stream := NewStream(homeID, token)
err := stream.StartSubscription(msgCh)
if err != nil {
t.Fatalf("Push: %v", err)
}
select {
case msg := <-msgCh:
t.Log(msg)
case <-time.After(time.Second * 7):
break
}
stream.Stop()
}
func TestGetCurrentPrice(t *testing.T) {
token := string(helperLoadBytes(t, "token.txt"))
tc := NewClient(token)
homeID := string(helperLoadBytes(t, "homeId.txt"))
priceInfo, _ := tc.GetCurrentPrice(homeID)
if priceInfo.Level == "" {
t.Fatalf("GetCurrentPrice: %v", priceInfo)
}
}