-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathschedule_test.go
129 lines (117 loc) · 2.78 KB
/
schedule_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
122
123
124
125
126
127
128
129
package huego
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetSchedules(t *testing.T) {
b := New(hostname, username)
schedules, err := b.GetSchedules()
if err != nil {
t.Fatal(err)
}
t.Logf("Found %d schedules", len(schedules))
for i, schedule := range schedules {
t.Logf("%d:", i)
t.Logf(" Name: %s", schedule.Name)
t.Logf(" Description: %s", schedule.Description)
t.Logf(" Command:")
t.Logf(" Address: %s", schedule.Command.Address)
t.Logf(" Method: %s", schedule.Command.Method)
t.Logf(" Body: %s", schedule.Command.Body)
t.Logf(" Time: %s", schedule.Time)
t.Logf(" LocalTime: %s", schedule.LocalTime)
t.Logf(" StartTime: %s", schedule.StartTime)
t.Logf(" Status: %s", schedule.Status)
t.Logf(" AutoDelete: %t", schedule.AutoDelete)
t.Logf(" ID: %d", schedule.ID)
}
contains := func(name string, ss []*Schedule) bool {
for _, s := range ss {
if s.Name == name {
return true
}
}
return false
}
assert.True(t, contains("Timer", schedules))
assert.True(t, contains("Alarm", schedules))
b.Host = badHostname
_, err = b.GetSchedules()
assert.NotNil(t, err)
}
func TestGetSchedule(t *testing.T) {
b := New(hostname, username)
s, err := b.GetSchedule(1)
if err != nil {
t.Fatal(err)
}
t.Logf("Time: %s", s.Time)
t.Logf("LocalTime: %s", s.LocalTime)
t.Logf("StartTime: %s", s.StartTime)
t.Logf("Status: %s", s.Status)
t.Logf("AutoDelete: %t", s.AutoDelete)
t.Logf("ID: %d", s.ID)
b.Host = badHostname
_, err = b.GetSchedule(1)
assert.NotNil(t, err)
}
func TestCreateSchedule(t *testing.T) {
b := New(hostname, username)
command := &Command{
Address: "/api/" + username + "/lights/0",
Body: &struct {
on bool
}{
false,
},
Method: "PUT",
}
schedule := &Schedule{
Name: "TestSchedule",
Description: "Huego test schedule",
Command: command,
LocalTime: "2019-09-22T13:37:00",
}
resp, err := b.CreateSchedule(schedule)
if err != nil {
t.Fatal(err)
} else {
t.Logf("Schedule created")
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}
}
b.Host = badHostname
_, err = b.CreateSchedule(schedule)
assert.NotNil(t, err)
}
func TestUpdateSchedule(t *testing.T) {
b := New(hostname, username)
id := 1
schedule := &Schedule{
Name: "New Scehdule",
Description: "Updated parameter",
}
resp, err := b.UpdateSchedule(id, schedule)
if err != nil {
t.Fatal(err)
} else {
t.Logf("Schedule %d updated", id)
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}
}
b.Host = badHostname
_, err = b.UpdateSchedule(id, schedule)
assert.NotNil(t, err)
}
func TestDeleteSchedule(t *testing.T) {
b := New(hostname, username)
id := 1
err := b.DeleteSchedule(id)
if err != nil {
t.Fatal(err)
} else {
t.Logf("Schedule %d deleted", id)
}
}