2024-06-15 12:34:24 -07:00
|
|
|
package page
|
2021-12-18 15:08:04 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2021-12-18 15:08:04 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/tests"
|
2021-12-18 15:08:04 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPager(t *testing.T) {
|
2024-06-15 12:34:24 -07:00
|
|
|
e := echo.New()
|
|
|
|
ctx, _ := tests.NewContext(e, "/")
|
2021-12-18 15:08:04 -08:00
|
|
|
pgr := NewPager(ctx, 10)
|
|
|
|
assert.Equal(t, 10, pgr.ItemsPerPage)
|
|
|
|
assert.Equal(t, 1, pgr.Page)
|
|
|
|
assert.Equal(t, 0, pgr.Items)
|
|
|
|
assert.Equal(t, 0, pgr.Pages)
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
ctx, _ = tests.NewContext(e, fmt.Sprintf("/abc?%s=%d", PageQueryKey, 2))
|
2021-12-18 15:08:04 -08:00
|
|
|
pgr = NewPager(ctx, 10)
|
|
|
|
assert.Equal(t, 2, pgr.Page)
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
ctx, _ = tests.NewContext(e, fmt.Sprintf("/abc?%s=%d", PageQueryKey, -2))
|
2021-12-18 15:08:04 -08:00
|
|
|
pgr = NewPager(ctx, 10)
|
|
|
|
assert.Equal(t, 1, pgr.Page)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPager_SetItems(t *testing.T) {
|
2024-06-15 12:34:24 -07:00
|
|
|
ctx, _ := tests.NewContext(echo.New(), "/")
|
2021-12-18 15:08:04 -08:00
|
|
|
pgr := NewPager(ctx, 20)
|
|
|
|
pgr.SetItems(100)
|
|
|
|
assert.Equal(t, 100, pgr.Items)
|
|
|
|
assert.Equal(t, 5, pgr.Pages)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPager_IsBeginning(t *testing.T) {
|
2024-06-15 12:34:24 -07:00
|
|
|
ctx, _ := tests.NewContext(echo.New(), "/")
|
2021-12-18 15:08:04 -08:00
|
|
|
pgr := NewPager(ctx, 20)
|
|
|
|
pgr.Pages = 10
|
|
|
|
assert.True(t, pgr.IsBeginning())
|
|
|
|
pgr.Page = 2
|
|
|
|
assert.False(t, pgr.IsBeginning())
|
|
|
|
pgr.Page = 1
|
|
|
|
assert.True(t, pgr.IsBeginning())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPager_IsEnd(t *testing.T) {
|
2024-06-15 12:34:24 -07:00
|
|
|
ctx, _ := tests.NewContext(echo.New(), "/")
|
2021-12-18 15:08:04 -08:00
|
|
|
pgr := NewPager(ctx, 20)
|
|
|
|
pgr.Pages = 10
|
|
|
|
assert.False(t, pgr.IsEnd())
|
|
|
|
pgr.Page = 10
|
|
|
|
assert.True(t, pgr.IsEnd())
|
|
|
|
pgr.Page = 1
|
|
|
|
assert.False(t, pgr.IsEnd())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPager_GetOffset(t *testing.T) {
|
2024-06-15 12:34:24 -07:00
|
|
|
ctx, _ := tests.NewContext(echo.New(), "/")
|
2021-12-18 15:08:04 -08:00
|
|
|
pgr := NewPager(ctx, 20)
|
|
|
|
assert.Equal(t, 0, pgr.GetOffset())
|
|
|
|
pgr.Page = 2
|
|
|
|
assert.Equal(t, 20, pgr.GetOffset())
|
|
|
|
pgr.Page = 3
|
|
|
|
assert.Equal(t, 40, pgr.GetOffset())
|
|
|
|
}
|