Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions linebot/messaging_api/tests/handwritten/model_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,69 @@ import (
"github.com/line/line-bot-sdk-go/v8/linebot/messaging_api"
)

func assertJSONFields(t *testing.T, jsonData []byte, expected map[string]any, absentKeys []string) {
var got map[string]any
if err := json.Unmarshal(jsonData, &got); err != nil {
t.Fatalf("Invalid JSON: %v", err)
}

for k, v := range expected {
val, ok := got[k]
if !ok {
t.Errorf("Expected field %q to be present, but it's missing", k)
continue
}

// If the deserialized value is float64, convert it to int before comparison
if f, ok := val.(float64); ok {
val = int(f)
}

if val != v {
t.Errorf("Expected %q=%v, got %v", k, v, val)
}
}

for _, k := range absentKeys {
if _, ok := got[k]; ok {
t.Errorf("Expected field %q to be absent, but it exists", k)
}
}
}

func TestLimitSerialization(t *testing.T) {
tests := []struct {
name string
limit messaging_api.Limit
expected string
name string
limit messaging_api.Limit
expected map[string]any
absentKeys []string
}{
{
name: "Max is set",
limit: messaging_api.Limit{Max: 10, UpToRemainingQuota: true},
expected: `{"max":10,"upToRemainingQuota":true}`,
expected: map[string]any{"max": 10, "upToRemainingQuota": true},
},
{
name: "Max is zero (omitempty)",
limit: messaging_api.Limit{Max: 0, UpToRemainingQuota: true},
expected: `{"upToRemainingQuota":true}`,
name: "Max is zero (omitempty)",
limit: messaging_api.Limit{Max: 0, UpToRemainingQuota: true},
expected: map[string]any{"upToRemainingQuota": true},
absentKeys: []string{"max"},
},
{
name: "Max is zero and UpToRemainingQuota is false",
limit: messaging_api.Limit{Max: 0, UpToRemainingQuota: false},
expected: `{"upToRemainingQuota":false}`,
name: "Max is zero and UpToRemainingQuota is false",
limit: messaging_api.Limit{Max: 0, UpToRemainingQuota: false},
expected: map[string]any{"upToRemainingQuota": false},
absentKeys: []string{"max"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.limit)
if err != nil {
t.Fatalf("Failed to marshal Limit: %v", err)
}
if string(data) != tt.expected {
t.Errorf("Expected JSON: %s, got: %s", tt.expected, string(data))
t.Fatalf("marshal error: %v", err)
}
assertJSONFields(t, data, tt.expected, tt.absentKeys)
})
}
}