|
| 1 | +package sumoapi_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + |
| 9 | + "github.com/sumo-mcp/sumoapi-go" |
| 10 | +) |
| 11 | + |
| 12 | +func TestMarshalBashoRikishiIDToJSON(t *testing.T) { |
| 13 | + for _, tt := range []struct { |
| 14 | + name string |
| 15 | + bashoRikishiID sumoapi.BashoRikishiID |
| 16 | + expectedJSON string |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "double digit month", |
| 20 | + bashoRikishiID: sumoapi.BashoRikishiID{BashoID: sumoapi.BashoID{Year: 2023, Month: 11}, RikishiID: 1}, |
| 21 | + expectedJSON: `"202311-1"`, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "single digit month", |
| 25 | + bashoRikishiID: sumoapi.BashoRikishiID{BashoID: sumoapi.BashoID{Year: 1999, Month: 3}, RikishiID: 1}, |
| 26 | + expectedJSON: `"199903-1"`, |
| 27 | + }, |
| 28 | + } { |
| 29 | + t.Run(tt.name, func(t *testing.T) { |
| 30 | + g := NewWithT(t) |
| 31 | + b, err := json.Marshal(tt.bashoRikishiID) |
| 32 | + g.Expect(err).ToNot(HaveOccurred()) |
| 33 | + g.Expect(string(b)).To(Equal(tt.expectedJSON)) |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + t.Run("marshal inside struct", func(t *testing.T) { |
| 38 | + g := NewWithT(t) |
| 39 | + type wrapper struct { |
| 40 | + Basho sumoapi.BashoRikishiID `json:"basho"` |
| 41 | + } |
| 42 | + w := wrapper{Basho: sumoapi.BashoRikishiID{BashoID: sumoapi.BashoID{Year: 2023, Month: 11}, RikishiID: 1}} |
| 43 | + b, err := json.Marshal(w) |
| 44 | + g.Expect(err).ToNot(HaveOccurred()) |
| 45 | + g.Expect(string(b)).To(Equal(`{"basho":"202311-1"}`)) |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func TestUnmarshalBashoRikishiIDFromJSON(t *testing.T) { |
| 50 | + for _, tt := range []struct { |
| 51 | + name string |
| 52 | + jsonData string |
| 53 | + expectedBashoRikishiID sumoapi.BashoRikishiID |
| 54 | + expectError bool |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "double digit month", |
| 58 | + jsonData: `"202311-1"`, |
| 59 | + expectedBashoRikishiID: sumoapi.BashoRikishiID{BashoID: sumoapi.BashoID{Year: 2023, Month: 11}, RikishiID: 1}, |
| 60 | + expectError: false, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "single digit month", |
| 64 | + jsonData: `"199903-1"`, |
| 65 | + expectedBashoRikishiID: sumoapi.BashoRikishiID{BashoID: sumoapi.BashoID{Year: 1999, Month: 3}, RikishiID: 1}, |
| 66 | + expectError: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "invalid format", |
| 70 | + jsonData: `"20A911"`, |
| 71 | + expectedBashoRikishiID: sumoapi.BashoRikishiID{}, |
| 72 | + expectError: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "wrong length", |
| 76 | + jsonData: `"20231"`, |
| 77 | + expectedBashoRikishiID: sumoapi.BashoRikishiID{}, |
| 78 | + expectError: true, |
| 79 | + }, |
| 80 | + } { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + g := NewWithT(t) |
| 83 | + var b sumoapi.BashoRikishiID |
| 84 | + err := json.Unmarshal([]byte(tt.jsonData), &b) |
| 85 | + if tt.expectError { |
| 86 | + g.Expect(err).To(HaveOccurred()) |
| 87 | + } else { |
| 88 | + g.Expect(err).ToNot(HaveOccurred()) |
| 89 | + g.Expect(b).To(Equal(tt.expectedBashoRikishiID)) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + t.Run("unmarshal inside struct", func(t *testing.T) { |
| 95 | + g := NewWithT(t) |
| 96 | + type wrapper struct { |
| 97 | + Basho sumoapi.BashoRikishiID `json:"basho"` |
| 98 | + } |
| 99 | + jsonData := `{"basho":"202311-1"}` |
| 100 | + var w wrapper |
| 101 | + err := json.Unmarshal([]byte(jsonData), &w) |
| 102 | + g.Expect(err).ToNot(HaveOccurred()) |
| 103 | + g.Expect(w.Basho).To(Equal(sumoapi.BashoRikishiID{BashoID: sumoapi.BashoID{Year: 2023, Month: 11}, RikishiID: 1})) |
| 104 | + }) |
| 105 | +} |
0 commit comments