This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package pagination
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
func TestCreateFilterReturnsNonNil(t *testing.T) {
|
||||
f := CreateFilter(mongo.Pipeline{}, bson.D{})
|
||||
if f == nil {
|
||||
t.Fatal("expected non-nil MongoFilter")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateFilterPreservesPipeline(t *testing.T) {
|
||||
pipeline := mongo.Pipeline{
|
||||
bson.D{{Key: "$match", Value: bson.D{{Key: "active", Value: true}}}},
|
||||
bson.D{{Key: "$limit", Value: 10}},
|
||||
}
|
||||
f := CreateFilter(pipeline, bson.D{})
|
||||
|
||||
got := f.FilterQuery(context.Background())
|
||||
if len(got) != len(pipeline) {
|
||||
t.Fatalf("expected pipeline len %d, got %d", len(pipeline), len(got))
|
||||
}
|
||||
for i := range pipeline {
|
||||
if len(got[i]) != len(pipeline[i]) {
|
||||
t.Errorf("stage %d: expected len %d, got %d", i, len(pipeline[i]), len(got[i]))
|
||||
}
|
||||
for j, e := range pipeline[i] {
|
||||
if got[i][j].Key != e.Key {
|
||||
t.Errorf("stage %d field %d: expected key %q, got %q", i, j, e.Key, got[i][j].Key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateFilterPreservesSort(t *testing.T) {
|
||||
sort := bson.D{
|
||||
{Key: "createdAt", Value: -1},
|
||||
{Key: "_id", Value: 1},
|
||||
}
|
||||
f := CreateFilter(mongo.Pipeline{}, sort)
|
||||
|
||||
got := f.Sort(context.Background())
|
||||
if len(got) != len(sort) {
|
||||
t.Fatalf("expected sort len %d, got %d", len(sort), len(got))
|
||||
}
|
||||
for i, e := range sort {
|
||||
if got[i].Key != e.Key {
|
||||
t.Errorf("field %d: expected key %q, got %q", i, e.Key, got[i].Key)
|
||||
}
|
||||
if got[i].Value != e.Value {
|
||||
t.Errorf("field %d: expected value %v, got %v", i, e.Value, got[i].Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateFilterEmptyInputs(t *testing.T) {
|
||||
f := CreateFilter(mongo.Pipeline{}, bson.D{})
|
||||
|
||||
if got := f.FilterQuery(context.Background()); len(got) != 0 {
|
||||
t.Errorf("expected empty pipeline, got len %d", len(got))
|
||||
}
|
||||
if got := f.Sort(context.Background()); len(got) != 0 {
|
||||
t.Errorf("expected empty sort, got len %d", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateFilterNilInputs(t *testing.T) {
|
||||
f := CreateFilter(nil, nil)
|
||||
|
||||
if got := f.FilterQuery(context.Background()); got != nil {
|
||||
t.Errorf("expected nil pipeline, got %v", got)
|
||||
}
|
||||
if got := f.Sort(context.Background()); got != nil {
|
||||
t.Errorf("expected nil sort, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateFilterImplementsMongoFilter(t *testing.T) {
|
||||
var _ MongoFilter = CreateFilter(mongo.Pipeline{}, bson.D{})
|
||||
}
|
||||
|
||||
func TestCreateFilterIgnoresContext(t *testing.T) {
|
||||
pipeline := mongo.Pipeline{bson.D{{Key: "$count", Value: "n"}}}
|
||||
sort := bson.D{{Key: "x", Value: 1}}
|
||||
f := CreateFilter(pipeline, sort)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
if got := f.FilterQuery(ctx); len(got) != 1 {
|
||||
t.Errorf("expected pipeline len 1 even with cancelled ctx, got %d", len(got))
|
||||
}
|
||||
if got := f.Sort(ctx); len(got) != 1 {
|
||||
t.Errorf("expected sort len 1 even with cancelled ctx, got %d", len(got))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package pagination
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCalcPaginationTotalPagesZeroItems(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(0, 10); got != 0 {
|
||||
t.Errorf("expected 0, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesZeroLimit(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(100, 0); got != 0 {
|
||||
t.Errorf("expected 0, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesZeroBoth(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(0, 0); got != 0 {
|
||||
t.Errorf("expected 0, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesExactMultiple(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(100, 10); got != 10 {
|
||||
t.Errorf("expected 10, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesPartialLastPage(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(101, 10); got != 11 {
|
||||
t.Errorf("expected 11, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesSingleItem(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(1, 10); got != 1 {
|
||||
t.Errorf("expected 1, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesItemsLessThanLimit(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(5, 10); got != 1 {
|
||||
t.Errorf("expected 1, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesLimitOfOne(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(7, 1); got != 7 {
|
||||
t.Errorf("expected 7, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesItemsEqualLimit(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(10, 10); got != 1 {
|
||||
t.Errorf("expected 1, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesLargeNumbers(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(1_000_000, 250); got != 4000 {
|
||||
t.Errorf("expected 4000, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcPaginationTotalPagesOneMoreThanMultiple(t *testing.T) {
|
||||
if got := CalcPaginationTotalPages(11, 5); got != 3 {
|
||||
t.Errorf("expected 3, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaginationStructFields(t *testing.T) {
|
||||
p := Pagination{
|
||||
Page: 2,
|
||||
Limit: 25,
|
||||
TotalPages: 4,
|
||||
TotalItems: 100,
|
||||
CurrentPageCount: 25,
|
||||
}
|
||||
if p.Page != 2 || p.Limit != 25 || p.TotalPages != 4 || p.TotalItems != 100 || p.CurrentPageCount != 25 {
|
||||
t.Errorf("unexpected pagination struct values: %+v", p)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user