|
| 1 | +package sumoapi_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + |
| 12 | + "github.com/sumo-mcp/sumoapi-go" |
| 13 | +) |
| 14 | + |
| 15 | +func TestClient_GetBanzuke(t *testing.T) { |
| 16 | + t.Run("get banzuke for division", func(t *testing.T) { |
| 17 | + g := NewWithT(t) |
| 18 | + |
| 19 | + mockResp := `{ |
| 20 | + "bashoId": "202511", |
| 21 | + "division": "Makuuchi", |
| 22 | + "east": [ |
| 23 | + { |
| 24 | + "side": "East", |
| 25 | + "rikishiID": 8850, |
| 26 | + "shikonaEn": "Onosato", |
| 27 | + "shikonaJp": "大の里", |
| 28 | + "rankValue": 101, |
| 29 | + "rank": "Yokozuna 1 East", |
| 30 | + "record": [ |
| 31 | + { |
| 32 | + "result": "win", |
| 33 | + "opponentShikonaEn": "Takayasu", |
| 34 | + "opponentShikonaJp": "高安", |
| 35 | + "opponentID": 44, |
| 36 | + "kimarite": "yorikiri" |
| 37 | + } |
| 38 | + ], |
| 39 | + "wins": 11, |
| 40 | + "losses": 4, |
| 41 | + "absences": 0 |
| 42 | + } |
| 43 | + ], |
| 44 | + "west": [ |
| 45 | + { |
| 46 | + "side": "West", |
| 47 | + "rikishiID": 19, |
| 48 | + "shikonaEn": "Hoshoryu", |
| 49 | + "shikonaJp": "豊昇龍", |
| 50 | + "rankValue": 201, |
| 51 | + "rank": "Ozeki 1 West", |
| 52 | + "record": [], |
| 53 | + "wins": 12, |
| 54 | + "losses": 3, |
| 55 | + "absences": 0 |
| 56 | + } |
| 57 | + ] |
| 58 | + }` |
| 59 | + |
| 60 | + transport := &mockTransport{ |
| 61 | + validateRequest: func(req *http.Request) error { |
| 62 | + g.Expect(req.Method).To(Equal(http.MethodGet)) |
| 63 | + g.Expect(req.URL.Scheme).To(Equal("https")) |
| 64 | + g.Expect(req.URL.Host).To(Equal("sumo-api.com")) |
| 65 | + g.Expect(req.URL.Path).To(Equal("/api/basho/202511/banzuke/Makuuchi")) |
| 66 | + return nil |
| 67 | + }, |
| 68 | + response: &http.Response{ |
| 69 | + StatusCode: http.StatusOK, |
| 70 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 75 | + bashoID := sumoapi.BashoID{Year: 2025, Month: 11} |
| 76 | + resp, err := client.GetBanzuke(context.Background(), sumoapi.GetBanzukeRequest{ |
| 77 | + BashoID: bashoID, |
| 78 | + Division: "Makuuchi", |
| 79 | + }) |
| 80 | + |
| 81 | + g.Expect(err).ToNot(HaveOccurred()) |
| 82 | + g.Expect(resp).ToNot(BeNil()) |
| 83 | + g.Expect(resp.BashoID).To(Equal(bashoID)) |
| 84 | + g.Expect(resp.Division).To(Equal("Makuuchi")) |
| 85 | + |
| 86 | + // Check east side |
| 87 | + g.Expect(resp.East).To(HaveLen(1)) |
| 88 | + g.Expect(resp.East[0].Side).To(Equal("East")) |
| 89 | + g.Expect(resp.East[0].RikishiID).To(Equal(8850)) |
| 90 | + g.Expect(resp.East[0].ShikonaEnglish).To(Equal("Onosato")) |
| 91 | + g.Expect(resp.East[0].ShikonaJapanese).To(Equal("大の里")) |
| 92 | + g.Expect(resp.East[0].HumanReadableRankName).To(Equal("Yokozuna 1 East")) |
| 93 | + g.Expect(resp.East[0].NumericRankName).To(Equal(101)) |
| 94 | + g.Expect(resp.East[0].Wins).To(Equal(11)) |
| 95 | + g.Expect(resp.East[0].Losses).To(Equal(4)) |
| 96 | + g.Expect(resp.East[0].Absences).To(Equal(0)) |
| 97 | + |
| 98 | + // Check match record |
| 99 | + g.Expect(resp.East[0].Matches).To(HaveLen(1)) |
| 100 | + g.Expect(resp.East[0].Matches[0].Result).To(Equal("win")) |
| 101 | + g.Expect(resp.East[0].Matches[0].OpponentShikonaEnglish).To(Equal("Takayasu")) |
| 102 | + g.Expect(resp.East[0].Matches[0].OpponentShikonaJapanese).To(Equal("高安")) |
| 103 | + g.Expect(resp.East[0].Matches[0].OpponentID).To(Equal(44)) |
| 104 | + g.Expect(resp.East[0].Matches[0].Kimarite).To(Equal("yorikiri")) |
| 105 | + |
| 106 | + // Check west side |
| 107 | + g.Expect(resp.West).To(HaveLen(1)) |
| 108 | + g.Expect(resp.West[0].Side).To(Equal("West")) |
| 109 | + g.Expect(resp.West[0].RikishiID).To(Equal(19)) |
| 110 | + g.Expect(resp.West[0].ShikonaEnglish).To(Equal("Hoshoryu")) |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("get banzuke for different division", func(t *testing.T) { |
| 114 | + g := NewWithT(t) |
| 115 | + |
| 116 | + mockResp := `{ |
| 117 | + "bashoId": "202501", |
| 118 | + "division": "Juryo", |
| 119 | + "east": [], |
| 120 | + "west": [] |
| 121 | + }` |
| 122 | + |
| 123 | + transport := &mockTransport{ |
| 124 | + validateRequest: func(req *http.Request) error { |
| 125 | + g.Expect(req.URL.Path).To(Equal("/api/basho/202501/banzuke/Juryo")) |
| 126 | + return nil |
| 127 | + }, |
| 128 | + response: &http.Response{ |
| 129 | + StatusCode: http.StatusOK, |
| 130 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 135 | + bashoID := sumoapi.BashoID{Year: 2025, Month: 1} |
| 136 | + resp, err := client.GetBanzuke(context.Background(), sumoapi.GetBanzukeRequest{ |
| 137 | + BashoID: bashoID, |
| 138 | + Division: "Juryo", |
| 139 | + }) |
| 140 | + |
| 141 | + g.Expect(err).ToNot(HaveOccurred()) |
| 142 | + g.Expect(resp).ToNot(BeNil()) |
| 143 | + g.Expect(resp.BashoID).To(Equal(bashoID)) |
| 144 | + g.Expect(resp.Division).To(Equal("Juryo")) |
| 145 | + }) |
| 146 | + |
| 147 | + t.Run("banzuke with all result types", func(t *testing.T) { |
| 148 | + g := NewWithT(t) |
| 149 | + |
| 150 | + mockResp := `{ |
| 151 | + "bashoId": "202511", |
| 152 | + "division": "Makuuchi", |
| 153 | + "east": [ |
| 154 | + { |
| 155 | + "side": "East", |
| 156 | + "rikishiID": 1, |
| 157 | + "shikonaEn": "TestRikishi", |
| 158 | + "shikonaJp": "テスト力士", |
| 159 | + "rankValue": 101, |
| 160 | + "rank": "Yokozuna 1 East", |
| 161 | + "record": [ |
| 162 | + {"result": "win", "opponentShikonaEn": "A", "opponentShikonaJp": "あ", "opponentID": 2, "kimarite": "yorikiri"}, |
| 163 | + {"result": "loss", "opponentShikonaEn": "B", "opponentShikonaJp": "い", "opponentID": 3, "kimarite": "oshidashi"}, |
| 164 | + {"result": "absent", "opponentShikonaEn": "C", "opponentShikonaJp": "う", "opponentID": 4}, |
| 165 | + {"result": "fusen win", "opponentShikonaEn": "D", "opponentShikonaJp": "え", "opponentID": 5, "kimarite": "fusen"}, |
| 166 | + {"result": "fusen loss", "opponentShikonaEn": "E", "opponentShikonaJp": "お", "opponentID": 6, "kimarite": "fusen"} |
| 167 | + ], |
| 168 | + "wins": 2, |
| 169 | + "losses": 2, |
| 170 | + "absences": 1 |
| 171 | + } |
| 172 | + ], |
| 173 | + "west": [] |
| 174 | + }` |
| 175 | + |
| 176 | + transport := &mockTransport{ |
| 177 | + response: &http.Response{ |
| 178 | + StatusCode: http.StatusOK, |
| 179 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 184 | + resp, err := client.GetBanzuke(context.Background(), sumoapi.GetBanzukeRequest{ |
| 185 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 11}, |
| 186 | + Division: "Makuuchi", |
| 187 | + }) |
| 188 | + |
| 189 | + g.Expect(err).ToNot(HaveOccurred()) |
| 190 | + g.Expect(resp).ToNot(BeNil()) |
| 191 | + g.Expect(resp.East[0].Matches).To(HaveLen(5)) |
| 192 | + g.Expect(resp.East[0].Matches[0].Result).To(Equal("win")) |
| 193 | + g.Expect(resp.East[0].Matches[1].Result).To(Equal("loss")) |
| 194 | + g.Expect(resp.East[0].Matches[2].Result).To(Equal("absent")) |
| 195 | + g.Expect(resp.East[0].Matches[3].Result).To(Equal("fusen win")) |
| 196 | + g.Expect(resp.East[0].Matches[4].Result).To(Equal("fusen loss")) |
| 197 | + }) |
| 198 | + |
| 199 | + t.Run("context is propagated", func(t *testing.T) { |
| 200 | + g := NewWithT(t) |
| 201 | + |
| 202 | + mockResp := `{ |
| 203 | + "bashoId": "202511", |
| 204 | + "division": "Makuuchi", |
| 205 | + "east": [], |
| 206 | + "west": [] |
| 207 | + }` |
| 208 | + |
| 209 | + type testKey struct{} |
| 210 | + ctx := context.WithValue(context.Background(), testKey{}, "test-value") |
| 211 | + |
| 212 | + transport := &mockTransport{ |
| 213 | + validateRequest: func(req *http.Request) error { |
| 214 | + g.Expect(req.Context().Value(testKey{})).To(Equal("test-value")) |
| 215 | + return nil |
| 216 | + }, |
| 217 | + response: &http.Response{ |
| 218 | + StatusCode: http.StatusOK, |
| 219 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 220 | + }, |
| 221 | + } |
| 222 | + |
| 223 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 224 | + _, err := client.GetBanzuke(ctx, sumoapi.GetBanzukeRequest{ |
| 225 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 11}, |
| 226 | + Division: "Makuuchi", |
| 227 | + }) |
| 228 | + |
| 229 | + g.Expect(err).ToNot(HaveOccurred()) |
| 230 | + }) |
| 231 | + |
| 232 | + t.Run("http request error", func(t *testing.T) { |
| 233 | + g := NewWithT(t) |
| 234 | + |
| 235 | + transport := &mockTransport{ |
| 236 | + validateRequest: func(req *http.Request) error { |
| 237 | + return http.ErrAbortHandler |
| 238 | + }, |
| 239 | + } |
| 240 | + |
| 241 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 242 | + resp, err := client.GetBanzuke(context.Background(), sumoapi.GetBanzukeRequest{ |
| 243 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 11}, |
| 244 | + Division: "Makuuchi", |
| 245 | + }) |
| 246 | + |
| 247 | + g.Expect(err).To(HaveOccurred()) |
| 248 | + g.Expect(err.Error()).To(ContainSubstring("error making http request")) |
| 249 | + g.Expect(resp).To(BeNil()) |
| 250 | + }) |
| 251 | + |
| 252 | + t.Run("invalid JSON response", func(t *testing.T) { |
| 253 | + g := NewWithT(t) |
| 254 | + |
| 255 | + transport := &mockTransport{ |
| 256 | + response: &http.Response{ |
| 257 | + StatusCode: http.StatusOK, |
| 258 | + Body: io.NopCloser(strings.NewReader("not valid json")), |
| 259 | + }, |
| 260 | + } |
| 261 | + |
| 262 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 263 | + resp, err := client.GetBanzuke(context.Background(), sumoapi.GetBanzukeRequest{ |
| 264 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 11}, |
| 265 | + Division: "Makuuchi", |
| 266 | + }) |
| 267 | + |
| 268 | + g.Expect(err).To(HaveOccurred()) |
| 269 | + g.Expect(err.Error()).To(ContainSubstring("error unmarshaling response body")) |
| 270 | + g.Expect(resp).To(BeNil()) |
| 271 | + }) |
| 272 | +} |
0 commit comments