test: 补齐 handler/repository/domain 层单元测试
This commit is contained in:
95
internal/repository/pagination_test.go
Normal file
95
internal/repository/pagination_test.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/user-management-system/internal/pkg/pagination"
|
||||
)
|
||||
|
||||
func TestPaginationResultFromTotal(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
total int64
|
||||
params pagination.PaginationParams
|
||||
wantPages int
|
||||
wantTotal int64
|
||||
wantPage int
|
||||
wantPageSize int
|
||||
}{
|
||||
{
|
||||
name: "exact division",
|
||||
total: 100,
|
||||
params: pagination.PaginationParams{Page: 1, PageSize: 20},
|
||||
wantPages: 5,
|
||||
wantTotal: 100,
|
||||
wantPage: 1,
|
||||
wantPageSize: 20,
|
||||
},
|
||||
{
|
||||
name: "with remainder",
|
||||
total: 105,
|
||||
params: pagination.PaginationParams{Page: 1, PageSize: 20},
|
||||
wantPages: 6,
|
||||
wantTotal: 105,
|
||||
wantPage: 1,
|
||||
wantPageSize: 20,
|
||||
},
|
||||
{
|
||||
name: "zero total",
|
||||
total: 0,
|
||||
params: pagination.PaginationParams{Page: 1, PageSize: 20},
|
||||
wantPages: 0,
|
||||
wantTotal: 0,
|
||||
wantPage: 1,
|
||||
wantPageSize: 20,
|
||||
},
|
||||
{
|
||||
name: "single page",
|
||||
total: 5,
|
||||
params: pagination.PaginationParams{Page: 1, PageSize: 20},
|
||||
wantPages: 1,
|
||||
wantTotal: 5,
|
||||
wantPage: 1,
|
||||
wantPageSize: 20,
|
||||
},
|
||||
{
|
||||
name: "page 2",
|
||||
total: 50,
|
||||
params: pagination.PaginationParams{Page: 2, PageSize: 20},
|
||||
wantPages: 3,
|
||||
wantTotal: 50,
|
||||
wantPage: 2,
|
||||
wantPageSize: 20,
|
||||
},
|
||||
{
|
||||
name: "small page size",
|
||||
total: 10,
|
||||
params: pagination.PaginationParams{Page: 1, PageSize: 3},
|
||||
wantPages: 4,
|
||||
wantTotal: 10,
|
||||
wantPage: 1,
|
||||
wantPageSize: 3,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := paginationResultFromTotal(tc.total, tc.params)
|
||||
if result == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
if result.Total != tc.wantTotal {
|
||||
t.Errorf("expected total %d, got %d", tc.wantTotal, result.Total)
|
||||
}
|
||||
if result.Page != tc.wantPage {
|
||||
t.Errorf("expected page %d, got %d", tc.wantPage, result.Page)
|
||||
}
|
||||
if result.PageSize != tc.wantPageSize {
|
||||
t.Errorf("expected page_size %d, got %d", tc.wantPageSize, result.PageSize)
|
||||
}
|
||||
if result.Pages != tc.wantPages {
|
||||
t.Errorf("expected pages %d, got %d", tc.wantPages, result.Pages)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user