|
| 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_GetBasho(t *testing.T) { |
| 16 | + t.Run("get basho by ID", func(t *testing.T) { |
| 17 | + g := NewWithT(t) |
| 18 | + |
| 19 | + mockResp := `{ |
| 20 | + "date": "202501", |
| 21 | + "startDate": "2025-01-12T00:00:00Z", |
| 22 | + "endDate": "2025-01-26T00:00:00Z", |
| 23 | + "yusho": [ |
| 24 | + { |
| 25 | + "type": "Makuuchi", |
| 26 | + "rikishiId": 45, |
| 27 | + "shikonaEn": "Terunofuji", |
| 28 | + "shikonaJp": "照ノ富士" |
| 29 | + } |
| 30 | + ], |
| 31 | + "specialPrizes": [ |
| 32 | + { |
| 33 | + "type": "Shukun-sho", |
| 34 | + "rikishiId": 123, |
| 35 | + "shikonaEn": "Takakeisho", |
| 36 | + "shikonaJp": "貴景勝" |
| 37 | + } |
| 38 | + ] |
| 39 | + }` |
| 40 | + |
| 41 | + transport := &mockTransport{ |
| 42 | + validateRequest: func(req *http.Request) error { |
| 43 | + g.Expect(req.Method).To(Equal(http.MethodGet)) |
| 44 | + g.Expect(req.URL.Scheme).To(Equal("https")) |
| 45 | + g.Expect(req.URL.Host).To(Equal("sumo-api.com")) |
| 46 | + g.Expect(req.URL.Path).To(Equal("/api/basho/202501")) |
| 47 | + g.Expect(req.URL.Query()).To(BeEmpty()) |
| 48 | + return nil |
| 49 | + }, |
| 50 | + response: &http.Response{ |
| 51 | + StatusCode: http.StatusOK, |
| 52 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 57 | + resp, err := client.GetBasho(context.Background(), sumoapi.GetBashoRequest{ |
| 58 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 1}, |
| 59 | + }) |
| 60 | + |
| 61 | + g.Expect(err).ToNot(HaveOccurred()) |
| 62 | + g.Expect(resp).ToNot(BeNil()) |
| 63 | + g.Expect(resp.ID).To(Equal(sumoapi.BashoID{Year: 2025, Month: 1})) |
| 64 | + g.Expect(resp.StartDate).ToNot(BeNil()) |
| 65 | + g.Expect(resp.EndDate).ToNot(BeNil()) |
| 66 | + g.Expect(resp.Yusho).To(HaveLen(1)) |
| 67 | + g.Expect(resp.Yusho[0].Type).To(Equal("Makuuchi")) |
| 68 | + g.Expect(resp.Yusho[0].RikishiID).To(Equal(45)) |
| 69 | + g.Expect(resp.Yusho[0].ShikonaEnglish).To(Equal("Terunofuji")) |
| 70 | + g.Expect(resp.SpecialPrizes).To(HaveLen(1)) |
| 71 | + g.Expect(resp.SpecialPrizes[0].Type).To(Equal("Shukun-sho")) |
| 72 | + g.Expect(resp.SpecialPrizes[0].RikishiID).To(Equal(123)) |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("get basho with minimal data", func(t *testing.T) { |
| 76 | + g := NewWithT(t) |
| 77 | + |
| 78 | + mockResp := `{ |
| 79 | + "date": "199903" |
| 80 | + }` |
| 81 | + |
| 82 | + transport := &mockTransport{ |
| 83 | + validateRequest: func(req *http.Request) error { |
| 84 | + g.Expect(req.URL.Path).To(Equal("/api/basho/199903")) |
| 85 | + return nil |
| 86 | + }, |
| 87 | + response: &http.Response{ |
| 88 | + StatusCode: http.StatusOK, |
| 89 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 94 | + resp, err := client.GetBasho(context.Background(), sumoapi.GetBashoRequest{ |
| 95 | + BashoID: sumoapi.BashoID{Year: 1999, Month: 3}, |
| 96 | + }) |
| 97 | + |
| 98 | + g.Expect(err).ToNot(HaveOccurred()) |
| 99 | + g.Expect(resp).ToNot(BeNil()) |
| 100 | + g.Expect(resp.ID).To(Equal(sumoapi.BashoID{Year: 1999, Month: 3})) |
| 101 | + g.Expect(resp.StartDate).To(BeNil()) |
| 102 | + g.Expect(resp.EndDate).To(BeNil()) |
| 103 | + g.Expect(resp.Yusho).To(BeEmpty()) |
| 104 | + g.Expect(resp.SpecialPrizes).To(BeEmpty()) |
| 105 | + g.Expect(resp.Torikumi).To(BeEmpty()) |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("get basho with torikumi", func(t *testing.T) { |
| 109 | + g := NewWithT(t) |
| 110 | + |
| 111 | + mockResp := `{ |
| 112 | + "date": "202501", |
| 113 | + "torikumi": [ |
| 114 | + { |
| 115 | + "bashoId": "202501", |
| 116 | + "division": "Makuuchi", |
| 117 | + "day": 1, |
| 118 | + "matchNo": 1, |
| 119 | + "eastId": 45, |
| 120 | + "eastShikona": "Terunofuji", |
| 121 | + "eastRank": "Yokozuna 1 East", |
| 122 | + "westId": 123, |
| 123 | + "westShikona": "Takakeisho", |
| 124 | + "westRank": "Ozeki 1 West", |
| 125 | + "kimarite": "Yorikiri", |
| 126 | + "winnerId": 45, |
| 127 | + "winnerEn": "Terunofuji" |
| 128 | + } |
| 129 | + ] |
| 130 | + }` |
| 131 | + |
| 132 | + transport := &mockTransport{ |
| 133 | + response: &http.Response{ |
| 134 | + StatusCode: http.StatusOK, |
| 135 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 140 | + resp, err := client.GetBasho(context.Background(), sumoapi.GetBashoRequest{ |
| 141 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 1}, |
| 142 | + }) |
| 143 | + |
| 144 | + g.Expect(err).ToNot(HaveOccurred()) |
| 145 | + g.Expect(resp).ToNot(BeNil()) |
| 146 | + g.Expect(resp.Torikumi).To(HaveLen(1)) |
| 147 | + g.Expect(resp.Torikumi[0].BashoID).To(Equal(sumoapi.BashoID{Year: 2025, Month: 1})) |
| 148 | + g.Expect(resp.Torikumi[0].Division).To(Equal("Makuuchi")) |
| 149 | + g.Expect(resp.Torikumi[0].Day).To(Equal(1)) |
| 150 | + }) |
| 151 | + |
| 152 | + t.Run("context is propagated", func(t *testing.T) { |
| 153 | + g := NewWithT(t) |
| 154 | + |
| 155 | + mockResp := `{ |
| 156 | + "date": "202501" |
| 157 | + }` |
| 158 | + |
| 159 | + type testKey struct{} |
| 160 | + ctx := context.WithValue(context.Background(), testKey{}, "test-value") |
| 161 | + |
| 162 | + transport := &mockTransport{ |
| 163 | + validateRequest: func(req *http.Request) error { |
| 164 | + g.Expect(req.Context().Value(testKey{})).To(Equal("test-value")) |
| 165 | + return nil |
| 166 | + }, |
| 167 | + response: &http.Response{ |
| 168 | + StatusCode: http.StatusOK, |
| 169 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 174 | + _, err := client.GetBasho(ctx, sumoapi.GetBashoRequest{ |
| 175 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 1}, |
| 176 | + }) |
| 177 | + |
| 178 | + g.Expect(err).ToNot(HaveOccurred()) |
| 179 | + }) |
| 180 | + |
| 181 | + t.Run("http request error", func(t *testing.T) { |
| 182 | + g := NewWithT(t) |
| 183 | + |
| 184 | + transport := &mockTransport{ |
| 185 | + validateRequest: func(req *http.Request) error { |
| 186 | + return http.ErrAbortHandler |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 191 | + resp, err := client.GetBasho(context.Background(), sumoapi.GetBashoRequest{ |
| 192 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 1}, |
| 193 | + }) |
| 194 | + |
| 195 | + g.Expect(err).To(HaveOccurred()) |
| 196 | + g.Expect(err.Error()).To(ContainSubstring("error making http request")) |
| 197 | + g.Expect(resp).To(BeNil()) |
| 198 | + }) |
| 199 | + |
| 200 | + t.Run("invalid JSON response", func(t *testing.T) { |
| 201 | + g := NewWithT(t) |
| 202 | + |
| 203 | + transport := &mockTransport{ |
| 204 | + response: &http.Response{ |
| 205 | + StatusCode: http.StatusOK, |
| 206 | + Body: io.NopCloser(strings.NewReader("not valid json")), |
| 207 | + }, |
| 208 | + } |
| 209 | + |
| 210 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 211 | + resp, err := client.GetBasho(context.Background(), sumoapi.GetBashoRequest{ |
| 212 | + BashoID: sumoapi.BashoID{Year: 2025, Month: 1}, |
| 213 | + }) |
| 214 | + |
| 215 | + g.Expect(err).To(HaveOccurred()) |
| 216 | + g.Expect(err.Error()).To(ContainSubstring("error unmarshaling response body")) |
| 217 | + g.Expect(resp).To(BeNil()) |
| 218 | + }) |
| 219 | +} |
0 commit comments