Files
user-system/internal/pkg/pagination/pagination_test.go
Your Name 0b17ab42c2 test: improve pkg coverage - pagination and ip packages
- Add PaginationParams tests (internal/pkg/pagination): 100%
- Add IP utility function tests (internal/pkg/ip): 80%

Total project coverage: 55.0% (+0.6%)
2026-05-29 16:33:54 +08:00

71 lines
1.5 KiB
Go

package pagination
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestDefaultPagination(t *testing.T) {
p := DefaultPagination()
require.Equal(t, 1, p.Page)
require.Equal(t, 20, p.PageSize)
}
func TestPaginationParams_Offset(t *testing.T) {
tests := []struct {
name string
page int
pageSize int
want int
}{
{"page_1", 1, 20, 0},
{"page_2", 2, 20, 20},
{"page_10", 10, 20, 180},
{"zero_page", 0, 20, 0}, // < 1 defaults to 1
{"negative_page", -1, 20, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := PaginationParams{Page: tt.page, PageSize: tt.pageSize}
require.Equal(t, tt.want, p.Offset())
})
}
}
func TestPaginationParams_Limit(t *testing.T) {
tests := []struct {
name string
pageSize int
want int
}{
{"default_20", 20, 20},
{"valid_50", 50, 50},
{"max_100", 100, 100},
{"exceed_max_150", 150, 100}, // capped at 100
{"zero_size", 0, 20}, // < 1 defaults to 20
{"negative_size", -5, 20},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := PaginationParams{Page: 1, PageSize: tt.pageSize}
require.Equal(t, tt.want, p.Limit())
})
}
}
func TestPaginationResult_Fields(t *testing.T) {
result := PaginationResult{
Total: 100,
Page: 2,
PageSize: 20,
Pages: 5,
}
require.Equal(t, int64(100), result.Total)
require.Equal(t, 2, result.Page)
require.Equal(t, 20, result.PageSize)
require.Equal(t, 5, result.Pages)
}