add pagination engine to render
This commit is contained in:
+210
-8
@@ -2,6 +2,7 @@ package ron
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"reflect"
|
||||
@@ -13,8 +14,9 @@ func Test_DefaultHTMLRender(t *testing.T) {
|
||||
expected := &Render{
|
||||
EnableCache: false,
|
||||
TemplatesPath: "templates",
|
||||
Functions: make(template.FuncMap),
|
||||
templateCache: make(templateCache),
|
||||
TemplateData: TemplateData{},
|
||||
Functions: template.FuncMap{},
|
||||
templateCache: templateCache{},
|
||||
}
|
||||
|
||||
actual := defaultHTMLRender()
|
||||
@@ -27,8 +29,9 @@ func Test_HTMLRender(t *testing.T) {
|
||||
expected := &Render{
|
||||
EnableCache: false,
|
||||
TemplatesPath: "templates",
|
||||
Functions: make(template.FuncMap),
|
||||
templateCache: make(templateCache),
|
||||
TemplateData: TemplateData{},
|
||||
Functions: template.FuncMap{},
|
||||
templateCache: templateCache{},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
@@ -58,15 +61,17 @@ func Test_applyRenderConfig(t *testing.T) {
|
||||
{"Empty OptionFunc", &Render{
|
||||
EnableCache: false,
|
||||
TemplatesPath: "templates",
|
||||
Functions: make(template.FuncMap),
|
||||
templateCache: make(templateCache),
|
||||
TemplateData: TemplateData{},
|
||||
Functions: template.FuncMap{},
|
||||
templateCache: templateCache{},
|
||||
}, defaultHTMLRender()},
|
||||
{
|
||||
name: "Two OptionFunc", expected: &Render{
|
||||
EnableCache: true,
|
||||
TemplatesPath: "foobar",
|
||||
Functions: make(template.FuncMap),
|
||||
templateCache: make(templateCache),
|
||||
TemplateData: TemplateData{},
|
||||
Functions: template.FuncMap{},
|
||||
templateCache: templateCache{},
|
||||
},
|
||||
actual: NewHTMLRender(func(r *Render) {
|
||||
r.EnableCache = true
|
||||
@@ -193,3 +198,200 @@ func Test_TemplateDefault(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type SomethingElements struct {
|
||||
Name string
|
||||
Description string
|
||||
}
|
||||
|
||||
func createDummyElements() []SomethingElements {
|
||||
return []SomethingElements{
|
||||
{"element 1", "description 1"},
|
||||
{"element 2", "description 2"},
|
||||
{"element 3", "description 3"},
|
||||
{"element 4", "description 4"},
|
||||
{"element 5", "description 5"},
|
||||
{"element 6", "description 6"},
|
||||
{"element 7", "description 7"},
|
||||
{"element 8", "description 8"},
|
||||
{"element 9", "description 9"},
|
||||
{"element 10", "description 10"},
|
||||
{"element 11", "description 11"},
|
||||
{"element 12", "description 12"},
|
||||
{"element 13", "description 13"},
|
||||
{"element 14", "description 14"},
|
||||
{"element 15", "description 15"},
|
||||
{"element 16", "description 16"},
|
||||
{"element 17", "description 17"},
|
||||
{"element 18", "description 18"},
|
||||
{"element 19", "description 19"},
|
||||
{"element 20", "description 20"},
|
||||
{"element 21", "description 21"},
|
||||
{"element 22", "description 22"},
|
||||
{"element 23", "description 23"},
|
||||
{"element 24", "description 24"},
|
||||
{"element 25", "description 25"},
|
||||
{"element 26", "description 26"},
|
||||
{"element 27", "description 27"},
|
||||
{"element 28", "description 28"},
|
||||
{"element 29", "description 29"},
|
||||
{"element 30", "description 30"},
|
||||
{"element 31", "description 31"},
|
||||
{"element 32", "description 32"},
|
||||
{"element 33", "description 33"},
|
||||
{"element 34", "description 34"},
|
||||
{"element 35", "description 35"},
|
||||
{"element 36", "description 36"},
|
||||
{"element 37", "description 37"},
|
||||
{"element 38", "description 38"},
|
||||
{"element 39", "description 39"},
|
||||
{"element 40", "description 40"},
|
||||
{"element 41", "description 41"},
|
||||
{"element 42", "description 42"},
|
||||
{"element 43", "description 43"},
|
||||
{"element 44", "description 44"},
|
||||
{"element 45", "description 45"},
|
||||
{"element 46", "description 46"},
|
||||
{"element 47", "description 47"},
|
||||
{"element 48", "description 48"},
|
||||
{"element 49", "description 49"},
|
||||
{"element 50", "description 50"},
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PaginationParams(t *testing.T) {
|
||||
elements := createDummyElements()
|
||||
tests := []struct {
|
||||
name string
|
||||
req *http.Request
|
||||
given *Pages
|
||||
expected *Pages
|
||||
}{
|
||||
{
|
||||
name: "All defaults",
|
||||
req: httptest.NewRequest("GET", "/", nil),
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
},
|
||||
expected: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 20,
|
||||
ActualPage: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Without params",
|
||||
req: httptest.NewRequest("GET", "/", nil),
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 10,
|
||||
ActualPage: 3,
|
||||
},
|
||||
expected: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 10,
|
||||
ActualPage: 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "With page param",
|
||||
req: httptest.NewRequest("GET", "/?page=2", nil),
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
},
|
||||
expected: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 20,
|
||||
ActualPage: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "With limit param",
|
||||
req: httptest.NewRequest("GET", "/?limit=10", nil),
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
},
|
||||
expected: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 10,
|
||||
ActualPage: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "With page and limit param",
|
||||
req: httptest.NewRequest("GET", "/?page=2&limit=10", nil),
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
},
|
||||
expected: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 10,
|
||||
ActualPage: 2,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.given.PaginationParams(tt.req)
|
||||
if !reflect.DeepEqual(tt.expected, tt.given) {
|
||||
t.Errorf("Expected: %v, Actual: %v", tt.expected, tt.given)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PaginateArray(t *testing.T) {
|
||||
elements := createDummyElements()
|
||||
tests := []struct {
|
||||
name string
|
||||
given *Pages
|
||||
expected []SomethingElements
|
||||
}{
|
||||
{
|
||||
name: "First page",
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 10,
|
||||
ActualPage: 1,
|
||||
},
|
||||
expected: elements[:10],
|
||||
},
|
||||
{
|
||||
name: "Second page",
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 10,
|
||||
ActualPage: 2,
|
||||
},
|
||||
expected: elements[10:20],
|
||||
},
|
||||
{
|
||||
name: "Out of range superior",
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 10,
|
||||
ActualPage: 999,
|
||||
},
|
||||
expected: elements[40:50],
|
||||
},
|
||||
{
|
||||
name: "Out of range inferior",
|
||||
given: &Pages{
|
||||
TotalElements: len(elements),
|
||||
ElementsPerPage: 10,
|
||||
ActualPage: -1,
|
||||
},
|
||||
expected: elements[:10],
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual := tt.given.PaginateArray(elements)
|
||||
if !reflect.DeepEqual(tt.expected, actual) {
|
||||
t.Errorf("Expected: %v, Actual: %v", tt.expected, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user