-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram_test.go
More file actions
146 lines (126 loc) · 3.36 KB
/
Copy pathprogram_test.go
File metadata and controls
146 lines (126 loc) · 3.36 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
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
package radikron
import (
"embed"
"io"
"strings"
"testing"
)
const testStationFMT = "FMT"
var (
//go:embed test/weekly-program-test.xml
WeeklyProgramTestXML embed.FS
)
func TestWeeklyProgramUnmarshal(t *testing.T) {
xmlFile, err := WeeklyProgramTestXML.Open("test/weekly-program-test.xml")
if err != nil {
t.Error(err)
}
progs, err := decodeWeeklyProgram(xmlFile)
if err != nil {
t.Error(err)
}
if len(progs) != 1 {
t.Errorf("unmarshal failed: %v", progs)
}
p := progs[0]
var got, want string
got = p.StationID
want = testStationFMT
if got != want {
t.Errorf("p.StationID => %v, want %v", got, want)
}
got = p.Ft
want = "20230605130000"
if got != want {
t.Errorf("p.Ft => %v, want %v", got, want)
}
got = p.To
want = "20230605145500"
if got != want {
t.Errorf("p.To => %v, want %v", got, want)
}
got = p.Title
want = "山崎怜奈の誰かに話したかったこと。"
if got != want {
t.Errorf("p.Title => %v, want %v", got, want)
}
got = p.Pfm
want = "山崎怜奈"
if got != want {
t.Errorf("p.Pfm => %v, want %v", got, want)
}
got = p.Genre.Personality
want = "タレント"
if got != want {
t.Errorf("p.Genre.Personality => %v, want %v", got, want)
}
got = p.Genre.Program
want = "トーク"
if got != want {
t.Errorf("p.Genre.Program => %v, want %v", got, want)
}
got = strings.Join(p.Tags, ",")
want = "山崎怜奈,音楽との出会いが楽しめる,作業がはかどる,気分転換におすすめ,学生におすすめ"
if got != want {
t.Errorf("p.Tags => %v, want %v", got, want)
}
}
func TestDecodeWeeklyProgram_ErrorCases(t *testing.T) {
// Test with invalid XML
invalidXML := strings.NewReader("<?xml version=\"1.0\"?><invalid></invalid>")
progs, err := decodeWeeklyProgram(io.NopCloser(invalidXML))
if err == nil {
t.Errorf("expected error for invalid XML")
}
if len(progs) != 0 {
t.Errorf("expected no programs on error")
}
// Test with empty input
emptyReader := strings.NewReader("")
progs, err = decodeWeeklyProgram(io.NopCloser(emptyReader))
if err == nil {
t.Errorf("expected error for empty input")
}
if len(progs) != 0 {
t.Errorf("expected no programs on error")
}
}
func TestFetchWeeklyPrograms(t *testing.T) {
// This test requires network access and will make a real HTTP request
// Skip if running in CI or if network is unavailable
if testing.Short() {
t.Skip("Skipping network test in short mode")
}
// Test with a real station ID (FMT is a common station)
progs, err := FetchWeeklyPrograms("FMT")
if err != nil {
// If network is unavailable, skip the test
if strings.Contains(err.Error(), "no such host") || strings.Contains(err.Error(), "connection refused") {
t.Skip("Network unavailable, skipping test")
}
t.Errorf("FetchWeeklyPrograms failed: %v", err)
return
}
// Should return some programs
if len(progs) == 0 {
t.Error("FetchWeeklyPrograms should return at least one program")
}
// Verify program structure
if len(progs) > 0 {
p := progs[0]
if p.StationID == "" {
t.Error("Program should have StationID")
}
if p.Ft == "" {
t.Error("Program should have Ft (start time)")
}
if p.To == "" {
t.Error("Program should have To (end time)")
}
}
// Test with invalid station ID
_, err = FetchWeeklyPrograms("INVALID_STATION_ID_12345")
if err == nil {
t.Error("FetchWeeklyPrograms should return error for invalid station ID")
}
}