-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefecturejp_test.go
193 lines (168 loc) · 4.44 KB
/
prefecturejp_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package prefecturejp_test
import (
"testing"
"github.com/theplant/prefecturejp"
"github.com/theplant/testingutils/errorassert"
)
func TestGetPrefectures(t *testing.T) {
prefectures := prefecturejp.GetPrefectures()
if got, want := len(prefectures), 47; got != want {
t.Fatalf("got %d of prefectures, but want %d", got, want)
}
}
func TestGetPrefectureJapaneses(t *testing.T) {
prefectures := prefecturejp.GetPrefectures()
japaneses := prefecturejp.GetPrefectureJapaneses()
for i, _ := range japaneses {
if got, want := japaneses[i], prefectures[i].Japanese; got != want {
t.Errorf("cases[%d] failed: got %q, but want %q", i, got, want)
}
}
}
func TestGetPrefectureNames(t *testing.T) {
prefectures := prefecturejp.GetPrefectures()
names := prefecturejp.GetPrefectureNames()
for i, _ := range names {
if got, want := names[i], prefectures[i].Name; got != want {
t.Errorf("cases[%d] failed: got %q, but want %q", i, got, want)
}
}
}
func TestGetPrefectureShortNames(t *testing.T) {
prefectures := prefecturejp.GetPrefectures()
shortNames := prefecturejp.GetPrefectureShortNames()
for i, _ := range shortNames {
if got, want := shortNames[i], prefectures[i].ShortName; got != want {
t.Errorf("cases[%d] failed: got %q, but want %q", i, got, want)
}
}
}
func TestGetPrefecturesJSON(t *testing.T) {
prefectures, err := prefecturejp.GetPrefecturesJSON()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := len(prefectures), 47; got != want {
t.Fatalf("got %d of prefectures, but want %d", got, want)
}
}
func TestGetJapaneseByName(t *testing.T) {
var cases = []struct {
name string
japanese string
}{
{"Hokkaido", "北海道"},
{"Aomori", "青森県"},
{"Iwate", "岩手県"},
{"", ""},
}
for i, c := range cases {
got := prefecturejp.GetJapaneseByName(c.name)
if got != c.japanese {
t.Errorf("cases[%d] failed: got %q, but want %q", i, got, c.japanese)
}
}
}
func TestGetNameByJapanese(t *testing.T) {
var cases = []struct {
name string
japanese string
}{
{"Hokkaido", "北海道"},
{"Aomori", "青森県"},
{"Iwate", "岩手県"},
{"", ""},
}
for i, c := range cases {
got := prefecturejp.GetNameByJapanese(c.japanese)
if want := c.name; got != want {
t.Errorf("cases[%d] failed: got %q, but want %q", i, got, want)
}
}
}
func TestGetShortNameByName(t *testing.T) {
var cases = []struct {
name string
shortName string
}{
{"Hokkaido", "北海道"},
{"Aomori", "青森"},
{"Iwate", "岩手"},
{"", ""},
}
for i, c := range cases {
got := prefecturejp.GetShortNameByName(c.name)
if want := c.shortName; got != want {
t.Errorf("cases[%d] failed: got %q, but want %q", i, got, want)
}
}
}
func TestGetNameByShortName(t *testing.T) {
var cases = []struct {
name string
shortName string
}{
{"Hokkaido", "北海道"},
{"Aomori", "青森"},
{"Iwate", "岩手"},
{"", ""},
}
for i, c := range cases {
got := prefecturejp.GetNameByShortName(c.shortName)
if want := c.name; got != want {
t.Errorf("cases[%d] failed: got %q, but want %q", i, got, want)
}
}
}
func TestGetCodeByJapanese(t *testing.T) {
var cases = []struct {
code string
japanese string
}{
{"JP-01", "北海道"},
{"JP-02", "青森県"},
{"JP-03", "岩手県"},
{"", ""},
{"", "xxx"},
}
for i, c := range cases {
got := prefecturejp.GetCodeByJapanese(c.japanese)
if want := c.code; got != want {
t.Errorf("cases[%d] failed: got %q, but want %q", i, got, want)
}
}
}
func TestQueryPrefectureByFields(t *testing.T) {
tests := []struct {
name string
fields []prefecturejp.PrefectureField
value string
expectedPref *prefecturejp.Prefecture
}{
{
name: "found",
fields: []prefecturejp.PrefectureField{
prefecturejp.PrefectureField_CODE,
prefecturejp.PrefectureField_NAME,
prefecturejp.PrefectureField_JAPANESE,
prefecturejp.PrefectureField_SHORT_NAME,
},
value: "Hokkaido",
expectedPref: &prefecturejp.Prefecture{Code: "JP-01", Name: "Hokkaido", Japanese: "北海道", ShortName: "北海道", Region: "北海道"},
},
{
name: "don't found",
fields: []prefecturejp.PrefectureField{
prefecturejp.PrefectureField_CODE,
},
value: "Aomori",
expectedPref: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pref := prefecturejp.QueryPrefectureByFields(test.fields, test.value)
errorassert.Equal(t, test.expectedPref, pref)
})
}
}