From 7ff221fdc7494aa564f9e272ffe9569afe7193d2 Mon Sep 17 00:00:00 2001 From: perennial Date: Sat, 5 Oct 2024 20:30:58 +1000 Subject: [PATCH] fix broken CreatePaginator + add test case --- server/template/templateFunctions.go | 13 +- server/template/templateFunctions_test.go | 160 ++++++++++++++++++++++ 2 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 server/template/templateFunctions_test.go diff --git a/server/template/templateFunctions.go b/server/template/templateFunctions.go index 8c3fdd7..ffcc4d2 100644 --- a/server/template/templateFunctions.go +++ b/server/template/templateFunctions.go @@ -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 diff --git a/server/template/templateFunctions_test.go b/server/template/templateFunctions_test.go new file mode 100644 index 0000000..aa849f2 --- /dev/null +++ b/server/template/templateFunctions_test.go @@ -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) + } + } + }) + } +}