-
Notifications
You must be signed in to change notification settings - Fork 36
/
rule_test.go
121 lines (110 loc) · 2.09 KB
/
rule_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package huego
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetRules(t *testing.T) {
b := New(hostname, username)
rules, err := b.GetRules()
if err != nil {
t.Fatal(err)
}
t.Logf("Found %d rules", len(rules))
for _, rule := range rules {
t.Log(rule)
}
contains := func(name string, ss []*Rule) bool {
for _, s := range ss {
if s.Name == name {
return true
}
}
return false
}
assert.True(t, contains("Wall Switch Rule", rules))
assert.True(t, contains("Wall Switch Rule 2", rules))
b.Host = badHostname
_, err = b.GetRules()
assert.NotNil(t, err)
}
func TestGetRule(t *testing.T) {
b := New(hostname, username)
l, err := b.GetRule(1)
if err != nil {
t.Fatal(err)
} else {
t.Log(l)
}
b.Host = badHostname
_, err = b.GetRule(1)
assert.NotNil(t, err)
}
func TestCreateRule(t *testing.T) {
b := New(hostname, username)
conditions := []*Condition{
{
Address: "/sensors/2/state/buttonevent",
Operator: "eq",
Value: "16",
},
}
actions := []*RuleAction{
{
Address: "/groups/0/action",
Method: "PUT",
Body: &State{On: true},
},
}
rule := &Rule{
Name: "Huego Test Rule",
Conditions: conditions,
Actions: actions,
}
resp, err := b.CreateRule(rule)
if err != nil {
t.Fatal(err)
} else {
t.Logf("Rule created")
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}
}
b.Host = badHostname
_, err = b.CreateRule(rule)
assert.NotNil(t, err)
}
func TestUpdateRule(t *testing.T) {
b := New(hostname, username)
id := 1
rule := &Rule{
Actions: []*RuleAction{
{
Address: "/groups/1/action",
Method: "PUT",
Body: &State{On: true},
},
},
}
resp, err := b.UpdateRule(id, rule)
if err != nil {
t.Fatal(err)
} else {
t.Logf("Rule %d updated", id)
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}
}
b.Host = badHostname
_, err = b.UpdateRule(id, rule)
assert.NotNil(t, err)
}
func TestDeleteRule(t *testing.T) {
b := New(hostname, username)
id := 1
err := b.DeleteRule(id)
if err != nil {
t.Fatal(err)
} else {
t.Logf("Rule %d deleted", id)
}
}