From a9f1611c482df2f2d9c51bacade0977e9d97a856 Mon Sep 17 00:00:00 2001 From: Mazyar Date: Wed, 3 Jun 2026 12:43:12 +0330 Subject: [PATCH] Added curso test --- pkg/mongo/cursor_test.go | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 pkg/mongo/cursor_test.go diff --git a/pkg/mongo/cursor_test.go b/pkg/mongo/cursor_test.go new file mode 100644 index 0000000..fad37c4 --- /dev/null +++ b/pkg/mongo/cursor_test.go @@ -0,0 +1,55 @@ +package mongo + +import ( + "strings" + "testing" + + "go.mongodb.org/mongo-driver/v2/bson" +) + +func TestEncodeDecodePageCursor(t *testing.T) { + encoded, err := encodePageCursor("created_at", -1, int64(1700000000), "507f1f77bcf86cd799439011", "abc123", 10) + if err != nil { + t.Fatalf("encode: %v", err) + } + if strings.Contains(encoded, "+") || strings.Contains(encoded, "/") { + t.Fatalf("expected URL-safe cursor, got %q", encoded) + } + + decoded, err := decodePageCursor(encoded) + if err != nil { + t.Fatalf("decode: %v", err) + } + + if decoded.SortField != "created_at" || decoded.SortOrder != -1 { + t.Fatalf("unexpected sort: %+v", decoded) + } + if decoded.FiltersHash != "abc123" || decoded.PageOffset != 10 { + t.Fatalf("unexpected metadata: %+v", decoded) + } +} + +func TestBuildListPaginationCursorAndOffsetConflict(t *testing.T) { + _, err := BuildListPagination(10, 5, "cursor-token", "created_at", "desc", bson.M{}, ListPaginationOptions{}) + if err == nil { + t.Fatal("expected error when cursor and offset are both set") + } +} + +func TestBuildListPaginationOffsetMode(t *testing.T) { + p, err := BuildListPagination(10, 20, "", "created_at", "desc", bson.M{"status": "active"}, ListPaginationOptions{}) + if err != nil { + t.Fatalf("build: %v", err) + } + if p.Skip != 20 || p.Limit != 10 || p.Cursor != "" { + t.Fatalf("unexpected pagination: %+v", p) + } +} + +func TestHashFilterStable(t *testing.T) { + a := HashFilter(bson.M{"status": "active"}) + b := HashFilter(bson.M{"status": "active"}) + if a == "" || a != b { + t.Fatalf("expected stable non-empty hash, got %q and %q", a, b) + } +}