forked from yyoshiki41/go-radiko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_test.go
168 lines (144 loc) · 3.48 KB
/
program_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package radiko
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/chikulla/go-radiko/internal/util"
)
func client() (*Client, error) {
c, err := New("")
c.SetAreaID(areaIDTokyo)
return c, err
}
func TestClient_FindProgramByStation(t *testing.T) {
c, err := client()
if err != nil {
t.Error(err)
}
prog, err := c.FindProgramByStation(context.Background(), "TBS", time.Now())
if err != nil {
t.Error(err)
}
if prog == nil {
t.Error("CAN'T FIND THE MATCHED PROGRAM")
}
}
func TestGetStations(t *testing.T) {
if isOutsideJP() {
t.Skip("Skipping test in limited mode.")
}
c, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
c.SetAreaID(areaIDTokyo)
stations, err := c.GetStations(context.Background(), time.Now())
if err != nil {
t.Error(err)
}
if len(stations) == 0 {
t.Error("Stations is nil.")
}
}
func TestGetNowPrograms(t *testing.T) {
if isOutsideJP() {
t.Skip("Skipping test in limited mode.")
}
c, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
programs, err := c.GetNowPrograms(context.Background())
if err != nil {
t.Error(err)
}
if len(programs) == 0 {
t.Error("Programs is nil.")
}
}
func TestGetProgramByStartTime(t *testing.T) {
if isOutsideJP() {
t.Skip("Skipping test in limited mode.")
}
c, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
// Tests in ANN
stationID := "LFR"
n := time.Now()
if n.Weekday() == time.Sunday {
// If it is Sunday, ANN will not be broadcasted.
n = n.Add(-24 * time.Hour)
}
y, m, d := n.Date()
// ANN starts at 01:00 AM on Monday to Saturday in JST.
start := time.Date(y, m, d, 16, 0, 0, 0, time.UTC)
end := time.Date(y, m, d, 18, 0, 0, 0, time.UTC)
prog, err := c.GetProgramByStartTime(context.Background(), stationID, start)
if err != nil {
t.Error(err)
}
expected := util.Datetime(end)
if expected != prog.To {
t.Errorf("expected %s, but %s", expected, prog.To)
}
}
func TestGetProgramByStartTime_EmptyStationID(t *testing.T) {
c, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
_, err = c.GetProgramByStartTime(context.Background(), "", time.Now())
if err == nil {
t.Error("Should detect an error.")
}
}
func TestGetProgramByStartTime_ErrProgramNotFound(t *testing.T) {
c, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
n := time.Now()
if n.Weekday() == time.Sunday {
// If it is Sunday, ANN will not be broadcasted.
n = n.Add(-24 * time.Hour)
}
y, m, d := n.Date()
// 01:01 AM on Monday to Saturday in JST.
start := time.Date(y, m, d, 16, 1, 0, 0, time.UTC)
stationID := "LFR"
_, err = c.GetProgramByStartTime(context.Background(), stationID, start)
if err != ErrProgramNotFound {
t.Errorf("unexpected error: %s", err)
}
}
func TestGetWeeklyPrograms(t *testing.T) {
c, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
programs, err := c.GetWeeklyPrograms(context.Background(), "LFR")
if err != nil {
t.Error(err)
}
if len(programs) == 0 {
t.Error("Programs is nil.")
}
}
func TestDecodeStationsData(t *testing.T) {
file, err := os.Open(filepath.Join(testdataDir, "stations.xml"))
if err != nil {
t.Fatal(err)
}
var d stationsData
if err = decodeStationsData(file, &d); err != nil {
t.Error(err)
}
const expected = 2
if s := d.stations(); expected != len(s) {
t.Errorf("expected number of stations %d, but %d.", expected, len(s))
}
}