|
| 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_GetRikishiStats(t *testing.T) { |
| 16 | + t.Run("get rikishi stats by ID", func(t *testing.T) { |
| 17 | + g := NewWithT(t) |
| 18 | + |
| 19 | + mockResp := `{ |
| 20 | + "basho": 81, |
| 21 | + "yusho": 13, |
| 22 | + "totalMatches": 798, |
| 23 | + "totalWins": 523, |
| 24 | + "totalLosses": 275, |
| 25 | + "totalAbsences": 231, |
| 26 | + "sansho": { |
| 27 | + "Gino-sho": 3, |
| 28 | + "Kanto-sho": 3, |
| 29 | + "Shukun-sho": 3 |
| 30 | + }, |
| 31 | + "bashoByDivision": { |
| 32 | + "Makuuchi": 52, |
| 33 | + "Juryo": 7 |
| 34 | + }, |
| 35 | + "yushoByDivision": { |
| 36 | + "Makuuchi": 10, |
| 37 | + "Juryo": 2 |
| 38 | + }, |
| 39 | + "winsByDivision": { |
| 40 | + "Makuuchi": 366, |
| 41 | + "Juryo": 61 |
| 42 | + }, |
| 43 | + "lossByDivision": { |
| 44 | + "Makuuchi": 207, |
| 45 | + "Juryo": 38 |
| 46 | + }, |
| 47 | + "absenceByDivision": { |
| 48 | + "Makuuchi": 197, |
| 49 | + "Juryo": 6 |
| 50 | + }, |
| 51 | + "totalByDivision": { |
| 52 | + "Makuuchi": 573, |
| 53 | + "Juryo": 99 |
| 54 | + } |
| 55 | + }` |
| 56 | + |
| 57 | + transport := &mockTransport{ |
| 58 | + validateRequest: func(req *http.Request) error { |
| 59 | + g.Expect(req.Method).To(Equal(http.MethodGet)) |
| 60 | + g.Expect(req.URL.Scheme).To(Equal("https")) |
| 61 | + g.Expect(req.URL.Host).To(Equal("sumo-api.com")) |
| 62 | + g.Expect(req.URL.Path).To(Equal("/api/rikishi/45/stats")) |
| 63 | + g.Expect(req.URL.RawQuery).To(BeEmpty()) |
| 64 | + return nil |
| 65 | + }, |
| 66 | + response: &http.Response{ |
| 67 | + StatusCode: http.StatusOK, |
| 68 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 73 | + resp, err := client.GetRikishiStats(context.Background(), sumoapi.GetRikishiStatsRequest{ |
| 74 | + RikishiID: 45, |
| 75 | + }) |
| 76 | + |
| 77 | + g.Expect(err).ToNot(HaveOccurred()) |
| 78 | + g.Expect(resp).ToNot(BeNil()) |
| 79 | + g.Expect(resp.Basho).To(Equal(81)) |
| 80 | + g.Expect(resp.Yusho).To(Equal(13)) |
| 81 | + g.Expect(resp.TotalMatches).To(Equal(798)) |
| 82 | + g.Expect(resp.TotalWins).To(Equal(523)) |
| 83 | + g.Expect(resp.TotalLosses).To(Equal(275)) |
| 84 | + g.Expect(resp.TotalAbsences).To(Equal(231)) |
| 85 | + |
| 86 | + // Check sansho |
| 87 | + g.Expect(resp.Sansho).To(HaveLen(3)) |
| 88 | + g.Expect(resp.Sansho["Gino-sho"]).To(Equal(3)) |
| 89 | + g.Expect(resp.Sansho["Kanto-sho"]).To(Equal(3)) |
| 90 | + g.Expect(resp.Sansho["Shukun-sho"]).To(Equal(3)) |
| 91 | + |
| 92 | + // Check division stats |
| 93 | + g.Expect(resp.BashoByDivision["Makuuchi"]).To(Equal(52)) |
| 94 | + g.Expect(resp.YushoByDivision["Makuuchi"]).To(Equal(10)) |
| 95 | + g.Expect(resp.WinsByDivision["Makuuchi"]).To(Equal(366)) |
| 96 | + g.Expect(resp.LossByDivision["Makuuchi"]).To(Equal(207)) |
| 97 | + g.Expect(resp.AbsenceByDivision["Makuuchi"]).To(Equal(197)) |
| 98 | + g.Expect(resp.TotalMatchesByDivision["Makuuchi"]).To(Equal(573)) |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("context is propagated", func(t *testing.T) { |
| 102 | + g := NewWithT(t) |
| 103 | + |
| 104 | + mockResp := `{ |
| 105 | + "basho": 10, |
| 106 | + "yusho": 1 |
| 107 | + }` |
| 108 | + |
| 109 | + type testKey struct{} |
| 110 | + ctx := context.WithValue(context.Background(), testKey{}, "test-value") |
| 111 | + |
| 112 | + transport := &mockTransport{ |
| 113 | + validateRequest: func(req *http.Request) error { |
| 114 | + g.Expect(req.Context().Value(testKey{})).To(Equal("test-value")) |
| 115 | + return nil |
| 116 | + }, |
| 117 | + response: &http.Response{ |
| 118 | + StatusCode: http.StatusOK, |
| 119 | + Body: io.NopCloser(strings.NewReader(mockResp)), |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 124 | + _, err := client.GetRikishiStats(ctx, sumoapi.GetRikishiStatsRequest{ |
| 125 | + RikishiID: 45, |
| 126 | + }) |
| 127 | + |
| 128 | + g.Expect(err).ToNot(HaveOccurred()) |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("http request error", func(t *testing.T) { |
| 132 | + g := NewWithT(t) |
| 133 | + |
| 134 | + transport := &mockTransport{ |
| 135 | + validateRequest: func(req *http.Request) error { |
| 136 | + return http.ErrAbortHandler |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 141 | + resp, err := client.GetRikishiStats(context.Background(), sumoapi.GetRikishiStatsRequest{ |
| 142 | + RikishiID: 45, |
| 143 | + }) |
| 144 | + |
| 145 | + g.Expect(err).To(HaveOccurred()) |
| 146 | + g.Expect(err.Error()).To(ContainSubstring("error making http request")) |
| 147 | + g.Expect(resp).To(BeNil()) |
| 148 | + }) |
| 149 | + |
| 150 | + t.Run("invalid JSON response", func(t *testing.T) { |
| 151 | + g := NewWithT(t) |
| 152 | + |
| 153 | + transport := &mockTransport{ |
| 154 | + response: &http.Response{ |
| 155 | + StatusCode: http.StatusOK, |
| 156 | + Body: io.NopCloser(strings.NewReader("not valid json")), |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + client := sumoapi.New(sumoapi.WithHTTPClient(&http.Client{Transport: transport})) |
| 161 | + resp, err := client.GetRikishiStats(context.Background(), sumoapi.GetRikishiStatsRequest{ |
| 162 | + RikishiID: 45, |
| 163 | + }) |
| 164 | + |
| 165 | + g.Expect(err).To(HaveOccurred()) |
| 166 | + g.Expect(err.Error()).To(ContainSubstring("error unmarshaling response body")) |
| 167 | + g.Expect(resp).To(BeNil()) |
| 168 | + }) |
| 169 | +} |
0 commit comments