72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
package page
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type testOrderField string
|
|
|
|
func (f testOrderField) Column() string { return string(f) }
|
|
func (f testOrderField) String() string { return string(f) }
|
|
|
|
func TestNewCursor(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
orderBy := OrderBy[testOrderField]{
|
|
Field: testOrderField("created_at"),
|
|
Direction: OrderDirectionAsc,
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
size int
|
|
expectedSize int
|
|
}{
|
|
{
|
|
name: "negative size defaults to DefaultCursorSize",
|
|
size: -10,
|
|
expectedSize: DefaultCursorSize,
|
|
},
|
|
{
|
|
name: "zero size defaults to DefaultCursorSize",
|
|
size: 0,
|
|
expectedSize: DefaultCursorSize,
|
|
},
|
|
{
|
|
name: "valid size is kept as-is",
|
|
size: 50,
|
|
expectedSize: 50,
|
|
},
|
|
{
|
|
name: "oversized value is clamped to MaxCursorSize",
|
|
size: 500,
|
|
expectedSize: MaxCursorSize,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cursor := NewCursor[testOrderField](tt.size, nil, Head, orderBy)
|
|
assert.Equal(t, tt.expectedSize, cursor.Size)
|
|
})
|
|
}
|
|
}
|