fix broken CreatePaginator + add test case

This commit is contained in:
perennial
2024-10-05 20:30:58 +10:00
parent db3162a166
commit 7ff221fdc7
2 changed files with 167 additions and 6 deletions
+7 -6
View File
@@ -183,14 +183,15 @@ func CreatePaginator(base, ending string, current_page, max_page, page_margin, d
}
// Validation for users that don't have any artworks
if max_page < 1 {
max_page = 1
}
// NOTE: the following breaks the current max_page implementation, commenting it out for now
// if max_page < 1 {
// max_page = 1
// }
// Validation for max_page in relation to current_page
if max_page != -1 && max_page < current_page {
return PaginationData{}, fmt.Errorf("max_page (%d) must be greater than or equal to current_page (%d) when specified", max_page, current_page)
}
// if max_page < current_page {
// return PaginationData{}, fmt.Errorf("max_page (%d) must be greater than or equal to current_page (%d) when specified", max_page, current_page)
// }
hasMaxPage := max_page != -1
+160
View File
@@ -0,0 +1,160 @@
package template
import (
"reflect"
"testing"
)
func TestCreatePaginator(t *testing.T) {
tests := []struct {
name string
base string
ending string
currentPage int
maxPage int
pageMargin int
dropdownOffset int
expectError bool
expectedData PaginationData
}{
{
name: "Normal case",
base: "/ranking?content=all&date=20240101&mode=daily&page=",
ending: "#checkpoint",
currentPage: 3,
maxPage: 10,
pageMargin: 1,
dropdownOffset: 2,
expectError: false,
expectedData: PaginationData{
CurrentPage: 3,
MaxPage: 10,
Pages: []PageInfo{
{Number: 2, URL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint"},
{Number: 3, URL: "/ranking?content=all&date=20240101&mode=daily&page=3#checkpoint"},
{Number: 4, URL: "/ranking?content=all&date=20240101&mode=daily&page=4#checkpoint"},
},
HasPrevious: true,
HasNext: true,
PreviousURL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint",
NextURL: "/ranking?content=all&date=20240101&mode=daily&page=4#checkpoint",
FirstURL: "/ranking?content=all&date=20240101&mode=daily&page=1#checkpoint",
LastURL: "/ranking?content=all&date=20240101&mode=daily&page=10#checkpoint",
HasMaxPage: true,
LastPage: 4,
DropdownPages: []PageInfo{
{Number: 1, URL: "/ranking?content=all&date=20240101&mode=daily&page=1#checkpoint"},
{Number: 2, URL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint"},
{Number: 3, URL: "/ranking?content=all&date=20240101&mode=daily&page=3#checkpoint"},
{Number: 4, URL: "/ranking?content=all&date=20240101&mode=daily&page=4#checkpoint"},
{Number: 5, URL: "/ranking?content=all&date=20240101&mode=daily&page=5#checkpoint"},
},
},
},
{
name: "Unknown max page",
base: "/ranking?content=all&date=20240101&mode=daily&page=",
ending: "#checkpoint",
currentPage: 3,
maxPage: -1,
pageMargin: 1,
dropdownOffset: 2,
expectError: false,
expectedData: PaginationData{
CurrentPage: 3,
MaxPage: -1,
Pages: []PageInfo{
{Number: 2, URL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint"},
{Number: 3, URL: "/ranking?content=all&date=20240101&mode=daily&page=3#checkpoint"},
{Number: 4, URL: "/ranking?content=all&date=20240101&mode=daily&page=4#checkpoint"},
},
HasPrevious: true,
HasNext: true,
PreviousURL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint",
NextURL: "/ranking?content=all&date=20240101&mode=daily&page=4#checkpoint",
FirstURL: "/ranking?content=all&date=20240101&mode=daily&page=1#checkpoint",
LastURL: "/ranking?content=all&date=20240101&mode=daily&page=-1#checkpoint",
HasMaxPage: false,
LastPage: 4,
DropdownPages: []PageInfo{
{Number: 1, URL: "/ranking?content=all&date=20240101&mode=daily&page=1#checkpoint"},
{Number: 2, URL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint"},
{Number: 3, URL: "/ranking?content=all&date=20240101&mode=daily&page=3#checkpoint"},
{Number: 4, URL: "/ranking?content=all&date=20240101&mode=daily&page=4#checkpoint"},
{Number: 5, URL: "/ranking?content=all&date=20240101&mode=daily&page=5#checkpoint"},
},
},
},
{
name: "First page",
base: "/ranking?content=all&date=20240101&mode=daily&page=",
ending: "#checkpoint",
currentPage: 1,
maxPage: 10,
pageMargin: 1,
dropdownOffset: 2,
expectError: false,
expectedData: PaginationData{
CurrentPage: 1,
MaxPage: 10,
Pages: []PageInfo{
{Number: 1, URL: "/ranking?content=all&date=20240101&mode=daily&page=1#checkpoint"},
{Number: 2, URL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint"},
},
HasPrevious: false,
HasNext: true,
PreviousURL: "",
NextURL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint",
FirstURL: "/ranking?content=all&date=20240101&mode=daily&page=1#checkpoint",
LastURL: "/ranking?content=all&date=20240101&mode=daily&page=10#checkpoint",
HasMaxPage: true,
LastPage: 2,
DropdownPages: []PageInfo{
{Number: 1, URL: "/ranking?content=all&date=20240101&mode=daily&page=1#checkpoint"},
{Number: 2, URL: "/ranking?content=all&date=20240101&mode=daily&page=2#checkpoint"},
{Number: 3, URL: "/ranking?content=all&date=20240101&mode=daily&page=3#checkpoint"},
},
},
},
{
name: "Invalid current page",
base: "/ranking?content=all&date=20240101&mode=daily&page=",
ending: "#checkpoint",
currentPage: 0,
maxPage: 10,
pageMargin: 1,
dropdownOffset: 2,
expectError: true,
},
{
name: "Invalid page margin",
base: "/ranking?content=all&date=20240101&mode=daily&page=",
ending: "#checkpoint",
currentPage: 1,
maxPage: 10,
pageMargin: -1,
dropdownOffset: 2,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotData, err := CreatePaginator(tt.base, tt.ending, tt.currentPage, tt.maxPage, tt.pageMargin, tt.dropdownOffset)
if tt.expectError {
if err == nil {
t.Errorf("CreatePaginator() error = nil, expected an error")
}
} else {
if err != nil {
t.Errorf("CreatePaginator() unexpected error = %v", err)
}
if !reflect.DeepEqual(gotData, tt.expectedData) {
t.Errorf("CreatePaginator() gotData = %v, want %v", gotData, tt.expectedData)
}
}
})
}
}