From d990487044a27f31c279ac8ad0f8f4a903de47e7 Mon Sep 17 00:00:00 2001 From: Jan Galek Date: Tue, 17 Dec 2024 01:47:46 +0100 Subject: [PATCH 1/3] [Test] cover mode by tests --- tests/paginator_test.go | 347 ++++++++++++++++++++++++++++------------ 1 file changed, 241 insertions(+), 106 deletions(-) diff --git a/tests/paginator_test.go b/tests/paginator_test.go index 20bc48c..c920697 100644 --- a/tests/paginator_test.go +++ b/tests/paginator_test.go @@ -1,142 +1,277 @@ package tests import ( - paginator2 "github.com/gouef/paginator" + "github.com/gouef/paginator" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "testing" ) +func TestNewPaginator_DefaultValues(t *testing.T) { + p := paginator.NewPaginator() + + assert.Equal(t, 1, p.GetPage()) + assert.Equal(t, 1, p.GetBase()) + assert.Equal(t, 1, p.GetItemsPerPage()) + assert.Nil(t, p.GetItemCount()) +} + +func TestPaginator_SetPage(t *testing.T) { + p := paginator.NewPaginator() + + p.SetPage(5) + assert.Equal(t, 5, p.GetPage()) +} + +func TestPaginator_SetBase(t *testing.T) { + p := paginator.NewPaginator() + + p.SetBase(0) + assert.Equal(t, 0, p.GetBase()) +} + +func TestPaginator_SetItemsPerPage(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemsPerPage(10) + assert.Equal(t, 10, p.GetItemsPerPage()) + + p.SetItemsPerPage(0) + assert.Equal(t, 1, p.GetItemsPerPage(), "itemsPerPage nesmí být menší než 1") +} + +func TestPaginator_SetItemCount(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemCount(100) + assert.Equal(t, 100, *p.GetItemCount()) + + p.SetItemCount(-10) + assert.Equal(t, 0, *p.GetItemCount(), "itemCount nesmí být záporné") +} + +func TestPaginator_GetPageCount(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemsPerPage(10).SetItemCount(95) + pageCount := p.GetPageCount() + require.NotNil(t, pageCount) + assert.Equal(t, 10, *pageCount) // 95 / 10 = 10 (zaokrouhleno nahoru) + + p.SetItemsPerPage(20).SetItemCount(100) + assert.Equal(t, 5, *p.GetPageCount()) +} + +func TestPaginator_GetLastPage(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemsPerPage(10).SetItemCount(95) + lastPage := p.GetLastPage() + require.NotNil(t, lastPage) + assert.Equal(t, 10, *lastPage) + + p.SetItemsPerPage(20).SetItemCount(100) + assert.Equal(t, 5, *p.GetLastPage()) +} + +func TestPaginator_IsFirst(t *testing.T) { + p := paginator.NewPaginator() + + p.SetPage(1) + assert.True(t, p.IsFirst()) + + p.SetPage(2) + assert.False(t, p.IsFirst()) +} + +func TestPaginator_IsLast(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemsPerPage(10).SetItemCount(30) + p.SetPage(3) + assert.True(t, p.IsLast()) + + p.SetPage(2) + assert.False(t, p.IsLast()) +} + +func TestPaginator_GetOffset(t *testing.T) { + p := paginator.NewPaginator() + + p.SetPage(3).SetItemsPerPage(10) + assert.Equal(t, 20, p.GetOffset()) +} + +func TestPaginator_GetCountdownOffset(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemsPerPage(10).SetItemCount(30).SetPage(2) + countdown := p.GetCountdownOffset() + require.NotNil(t, countdown) + assert.Equal(t, 20, *countdown) +} + +func TestPaginator_GetLength(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemsPerPage(10).SetItemCount(25).SetPage(3) + assert.Equal(t, 5, p.GetLength()) // 25 - (2 * 10) = 5 + + p.SetPage(2) + assert.Equal(t, 10, p.GetLength()) +} + +func TestPaginator_GetFirstItemOnPage(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemsPerPage(10).SetItemCount(100).SetPage(3) + assert.Equal(t, 21, p.GetFirstItemOnPage()) // offset + 1 = 20 + 1 + + p.SetPage(1) + assert.Equal(t, 1, p.GetFirstItemOnPage()) +} + +func TestPaginator_GetLastItemOnPage(t *testing.T) { + p := paginator.NewPaginator() + + p.SetItemsPerPage(10).SetItemCount(100).SetPage(3) + assert.Equal(t, 30, p.GetLastItemOnPage()) // offset + length = 20 + 10 + + p.SetPage(1) + assert.Equal(t, 10, p.GetLastItemOnPage()) +} + func test1(t *testing.T) { - paginator := paginator2.NewPaginator() - paginator.SetItemCount(7) - paginator.SetItemsPerPage(6) - paginator.SetBase(0) - paginator.SetPage(3) - assert.Same(t, 1, paginator.GetPage()) - assert.Same(t, 2, paginator.GetPageCount()) - assert.Same(t, 0, paginator.GetFirstPage()) - assert.Same(t, 1, paginator.GetLastPage()) - assert.Same(t, 7, paginator.GetFirstItemOnPage()) - assert.Same(t, 7, paginator.GetLastItemOnPage()) - assert.Same(t, 6, paginator.GetOffset()) - assert.Same(t, 0, paginator.GetCountdownOffset()) - assert.Same(t, 1, paginator.GetLength()) + p := paginator.NewPaginator() + p.SetItemCount(7) + p.SetItemsPerPage(6) + p.SetBase(0) + p.SetPage(3) + assert.Same(t, 1, p.GetPage()) + assert.Same(t, 2, p.GetPageCount()) + assert.Same(t, 0, p.GetFirstPage()) + assert.Same(t, 1, p.GetLastPage()) + assert.Same(t, 7, p.GetFirstItemOnPage()) + assert.Same(t, 7, p.GetLastItemOnPage()) + assert.Same(t, 6, p.GetOffset()) + assert.Same(t, 0, p.GetCountdownOffset()) + assert.Same(t, 1, p.GetLength()) } func test2(t *testing.T) { - paginator := paginator2.NewPaginator() - paginator.SetItemCount(7) - paginator.SetItemsPerPage(6) - paginator.SetBase(0) - paginator.SetPage(-1) + p := paginator.NewPaginator() + p.SetItemCount(7) + p.SetItemsPerPage(6) + p.SetBase(0) + p.SetPage(-1) - assert.Same(t, 0, paginator.GetPage()) - assert.Same(t, 1, paginator.GetFirstItemOnPage()) - assert.Same(t, 6, paginator.GetLastItemOnPage()) - assert.Same(t, 0, paginator.GetOffset()) - assert.Same(t, 1, paginator.GetCountdownOffset()) - assert.Same(t, 6, paginator.GetLength()) + assert.Same(t, 0, p.GetPage()) + assert.Same(t, 1, p.GetFirstItemOnPage()) + assert.Same(t, 6, p.GetLastItemOnPage()) + assert.Same(t, 0, p.GetOffset()) + assert.Same(t, 1, p.GetCountdownOffset()) + assert.Same(t, 6, p.GetLength()) } func test3(t *testing.T) { - paginator := paginator2.NewPaginator() - paginator.SetItemCount(7) - paginator.SetItemsPerPage(7) - paginator.SetBase(0) - paginator.SetPage(-1) - - assert.Same(t, 0, paginator.GetPage()) - assert.Same(t, 1, paginator.GetPageCount()) - assert.Same(t, 0, paginator.GetFirstPage()) - assert.Same(t, 0, paginator.GetLastPage()) - assert.Same(t, 1, paginator.GetFirstItemOnPage()) - assert.Same(t, 7, paginator.GetLastItemOnPage()) - assert.Same(t, 0, paginator.GetOffset()) - assert.Same(t, 0, paginator.GetCountdownOffset()) - assert.Same(t, 7, paginator.GetLength()) + p := paginator.NewPaginator() + p.SetItemCount(7) + p.SetItemsPerPage(7) + p.SetBase(0) + p.SetPage(-1) + + assert.Same(t, 0, p.GetPage()) + assert.Same(t, 1, p.GetPageCount()) + assert.Same(t, 0, p.GetFirstPage()) + assert.Same(t, 0, p.GetLastPage()) + assert.Same(t, 1, p.GetFirstItemOnPage()) + assert.Same(t, 7, p.GetLastItemOnPage()) + assert.Same(t, 0, p.GetOffset()) + assert.Same(t, 0, p.GetCountdownOffset()) + assert.Same(t, 7, p.GetLength()) } func test4(t *testing.T) { - paginator := paginator2.NewPaginator() - paginator.SetItemCount(-1) - paginator.SetItemsPerPage(7) - paginator.SetBase(0) - paginator.SetPage(-1) - - assert.Same(t, 0, paginator.GetPage()) - assert.Same(t, 0, paginator.GetPageCount()) - assert.Same(t, 0, paginator.GetFirstPage()) - assert.Same(t, 0, paginator.GetLastPage()) - assert.Same(t, 0, paginator.GetFirstItemOnPage()) - assert.Same(t, 0, paginator.GetLastItemOnPage()) - assert.Same(t, 0, paginator.GetOffset()) - assert.Same(t, 0, paginator.GetCountdownOffset()) - assert.Same(t, 0, paginator.GetLength()) + p := paginator.NewPaginator() + p.SetItemCount(-1) + p.SetItemsPerPage(7) + p.SetBase(0) + p.SetPage(-1) + + assert.Same(t, 0, p.GetPage()) + assert.Same(t, 0, p.GetPageCount()) + assert.Same(t, 0, p.GetFirstPage()) + assert.Same(t, 0, p.GetLastPage()) + assert.Same(t, 0, p.GetFirstItemOnPage()) + assert.Same(t, 0, p.GetLastItemOnPage()) + assert.Same(t, 0, p.GetOffset()) + assert.Same(t, 0, p.GetCountdownOffset()) + assert.Same(t, 0, p.GetLength()) } func test5(t *testing.T) { - paginator := paginator2.NewPaginator() - paginator.SetItemCount(7) - paginator.SetItemsPerPage(6) - paginator.SetBase(1) - paginator.SetPage(3) - - assert.Same(t, 2, paginator.GetPage()) - assert.Same(t, 2, paginator.GetPageCount()) - assert.Same(t, 1, paginator.GetFirstPage()) - assert.Same(t, 2, paginator.GetLastPage()) - assert.Same(t, 7, paginator.GetFirstItemOnPage()) - assert.Same(t, 7, paginator.GetLastItemOnPage()) - assert.Same(t, 6, paginator.GetOffset()) - assert.Same(t, 0, paginator.GetCountdownOffset()) - assert.Same(t, 1, paginator.GetLength()) + p := paginator.NewPaginator() + p.SetItemCount(7) + p.SetItemsPerPage(6) + p.SetBase(1) + p.SetPage(3) + + assert.Same(t, 2, p.GetPage()) + assert.Same(t, 2, p.GetPageCount()) + assert.Same(t, 1, p.GetFirstPage()) + assert.Same(t, 2, p.GetLastPage()) + assert.Same(t, 7, p.GetFirstItemOnPage()) + assert.Same(t, 7, p.GetLastItemOnPage()) + assert.Same(t, 6, p.GetOffset()) + assert.Same(t, 0, p.GetCountdownOffset()) + assert.Same(t, 1, p.GetLength()) } func test6(t *testing.T) { - paginator := paginator2.NewPaginator() + p := paginator.NewPaginator() // ItemCount: 0 - paginator.SetItemCount(0) - assert.True(t, paginator.IsFirst()) - assert.True(t, paginator.IsLast()) - assert.Same(t, 0, paginator.GetFirstItemOnPage()) - assert.Same(t, 0, paginator.GetLastItemOnPage()) + p.SetItemCount(0) + assert.True(t, p.IsFirst()) + assert.True(t, p.IsLast()) + assert.Same(t, 0, p.GetFirstItemOnPage()) + assert.Same(t, 0, p.GetLastItemOnPage()) // ItemCount: 1 - paginator.SetItemCount(1) - assert.True(t, paginator.IsFirst()) - assert.True(t, paginator.IsLast()) - assert.Same(t, 1, paginator.GetFirstItemOnPage()) - assert.Same(t, 1, paginator.GetLastItemOnPage()) + p.SetItemCount(1) + assert.True(t, p.IsFirst()) + assert.True(t, p.IsLast()) + assert.Same(t, 1, p.GetFirstItemOnPage()) + assert.Same(t, 1, p.GetLastItemOnPage()) // ItemCount: 2 - paginator.SetItemCount(2) - assert.True(t, paginator.IsFirst()) - assert.False(t, paginator.IsLast()) - assert.Same(t, 1, paginator.GetFirstItemOnPage()) - assert.Same(t, 1, paginator.GetLastItemOnPage()) + p.SetItemCount(2) + assert.True(t, p.IsFirst()) + assert.False(t, p.IsLast()) + assert.Same(t, 1, p.GetFirstItemOnPage()) + assert.Same(t, 1, p.GetLastItemOnPage()) // Page 2 - paginator.SetPage(2) - assert.False(t, paginator.IsFirst()) - assert.True(t, paginator.IsLast()) - assert.Same(t, 2, paginator.GetFirstItemOnPage()) - assert.Same(t, 2, paginator.GetLastItemOnPage()) + p.SetPage(2) + assert.False(t, p.IsFirst()) + assert.True(t, p.IsLast()) + assert.Same(t, 2, p.GetFirstItemOnPage()) + assert.Same(t, 2, p.GetLastItemOnPage()) } func test7(t *testing.T) { - paginator := paginator2.NewPaginator() - paginator.SetItemsPerPage(6) - paginator.SetBase(0) - paginator.SetPage(3) - - assert.Same(t, 3, paginator.GetPage()) - assert.Nil(t, paginator.GetPageCount()) - assert.Same(t, 0, paginator.GetFirstPage()) - assert.Nil(t, paginator.GetLastPage()) - assert.Same(t, 19, paginator.GetFirstItemOnPage()) - assert.Same(t, 24, paginator.GetLastItemOnPage()) - assert.Same(t, 18, paginator.GetOffset()) - assert.Nil(t, paginator.GetCountdownOffset()) - assert.Same(t, 6, paginator.GetLength()) + p := paginator.NewPaginator() + p.SetItemsPerPage(6) + p.SetBase(0) + p.SetPage(3) + + assert.Same(t, 3, p.GetPage()) + assert.Nil(t, p.GetPageCount()) + assert.Same(t, 0, p.GetFirstPage()) + assert.Nil(t, p.GetLastPage()) + assert.Same(t, 19, p.GetFirstItemOnPage()) + assert.Same(t, 24, p.GetLastItemOnPage()) + assert.Same(t, 18, p.GetOffset()) + assert.Nil(t, p.GetCountdownOffset()) + assert.Same(t, 6, p.GetLength()) } From e005d508bb9c1bb90728e3f861ba4d07b5ab1480 Mon Sep 17 00:00:00 2001 From: Jan Galek Date: Tue, 17 Dec 2024 01:53:35 +0100 Subject: [PATCH 2/3] [Test] cover mode by tests --- paginator.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/paginator.go b/paginator.go index 9b9071c..c9de3e9 100644 --- a/paginator.go +++ b/paginator.go @@ -51,16 +51,23 @@ func (p *Paginator) GetLastPage() *int { } func (p *Paginator) GetFirstItemOnPage() int { - if *p.GetItemCount() != 0 { - i := p.offset + 1 - return i + if p.GetItemCount() == nil || *p.GetItemCount() == 0 { + return 0 } - return 0 + offset := p.GetOffset() + return offset + 1 } func (p *Paginator) GetLastItemOnPage() int { - return p.offset + p.length + if p.GetItemCount() == nil || *p.GetItemCount() == 0 { + return 0 + } + + offset := p.GetOffset() + length := p.GetLength() + + return offset + length } func (p *Paginator) SetBase(base int) *Paginator { @@ -134,7 +141,8 @@ func (p *Paginator) GetCountdownOffset() *int { if p.GetItemCount() == nil { return nil } - val := max(0, *p.GetItemCount()-(p.GetPageIndex()-1)*p.GetItemsPerPage()) + + val := max(0, *p.GetItemCount()-p.GetPageIndex()*p.GetItemsPerPage()) return &val } func (p *Paginator) GetLength() int { From 978dea83eb34adebc72c0d8cd6bef807651e5eea Mon Sep 17 00:00:00 2001 From: Jan Galek Date: Tue, 17 Dec 2024 02:01:42 +0100 Subject: [PATCH 3/3] [Test] cover mode by tests --- tests/paginator_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/paginator_test.go b/tests/paginator_test.go index c920697..3c5d4ec 100644 --- a/tests/paginator_test.go +++ b/tests/paginator_test.go @@ -62,6 +62,12 @@ func TestPaginator_GetPageCount(t *testing.T) { assert.Equal(t, 5, *p.GetPageCount()) } +func TestPaginator_GetPageCount_ItemCountNil(t *testing.T) { + p := paginator.NewPaginator() + + assert.Nil(t, p.GetPageCount(), "PageCount should be nil when itemCount is nil") +} + func TestPaginator_GetLastPage(t *testing.T) { p := paginator.NewPaginator() @@ -74,6 +80,12 @@ func TestPaginator_GetLastPage(t *testing.T) { assert.Equal(t, 5, *p.GetLastPage()) } +func TestPaginator_GetLastPage_ItemCountNil(t *testing.T) { + p := paginator.NewPaginator() + + assert.Nil(t, p.GetLastPage(), "LastPage should return nil when itemCount is nil") +} + func TestPaginator_IsFirst(t *testing.T) { p := paginator.NewPaginator() @@ -111,6 +123,12 @@ func TestPaginator_GetCountdownOffset(t *testing.T) { assert.Equal(t, 20, *countdown) } +func TestPaginator_GetCountdownOffset_ItemCountNil(t *testing.T) { + p := paginator.NewPaginator() + + assert.Nil(t, p.GetCountdownOffset(), "CountdownOffset should be nil when itemCount is nil") +} + func TestPaginator_GetLength(t *testing.T) { p := paginator.NewPaginator() @@ -121,6 +139,13 @@ func TestPaginator_GetLength(t *testing.T) { assert.Equal(t, 10, p.GetLength()) } +func TestPaginator_GetLength_ItemCountNil(t *testing.T) { + p := paginator.NewPaginator() + p.SetItemsPerPage(10) + + assert.Equal(t, 10, p.GetLength(), "Length should return itemsPerPage when itemCount is nil") +} + func TestPaginator_GetFirstItemOnPage(t *testing.T) { p := paginator.NewPaginator() @@ -131,6 +156,13 @@ func TestPaginator_GetFirstItemOnPage(t *testing.T) { assert.Equal(t, 1, p.GetFirstItemOnPage()) } +func TestPaginator_GetFirstItemOnPage_ItemCountNil(t *testing.T) { + p := paginator.NewPaginator() + p.SetPage(2) + + assert.Equal(t, 0, p.GetFirstItemOnPage(), "FirstItemOnPage should return 0 when itemCount is nil") +} + func TestPaginator_GetLastItemOnPage(t *testing.T) { p := paginator.NewPaginator() @@ -141,6 +173,13 @@ func TestPaginator_GetLastItemOnPage(t *testing.T) { assert.Equal(t, 10, p.GetLastItemOnPage()) } +func TestPaginator_GetLastItemOnPage_ItemCountNil(t *testing.T) { + p := paginator.NewPaginator() + p.SetPage(2) + + assert.Equal(t, 0, p.GetLastItemOnPage(), "LastItemOnPage should return 0 when itemCount is nil") +} + func test1(t *testing.T) { p := paginator.NewPaginator() p.SetItemCount(7)