From bfc5fcc8c6b1c24f1a61c6430d81b83017e914a9 Mon Sep 17 00:00:00 2001 From: Ahmed Hossam Date: Fri, 20 Sep 2024 23:02:21 +0300 Subject: [PATCH 1/3] Fixed duplicate timestamps causing pagination test failure --- db_test.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/db_test.go b/db_test.go index 4c68964..6aeadf5 100644 --- a/db_test.go +++ b/db_test.go @@ -1494,6 +1494,40 @@ func TestIndexQueryWithSort(t *testing.T) { }) } +func checkUniqueTimestamps(db *c.DB) { + // Retrieve all documents from the collection + allDocs, err := db.FindAll(q.NewQuery("test")) + if err != nil { + fmt.Println("Error retrieving documents:", err) + return + } + + // Use a map to track timestamps + timestamps := make(map[time.Time]bool) + + // Check for duplicate timestamps + hasDuplicate := false + for _, doc := range allDocs { + // Get the timestamp from the document + timestamp := doc.Get("timestamp").(time.Time) + + // Check if timestamp is already in the map + if timestamps[timestamp] { + hasDuplicate = true + } else { + // Add timestamp to the map if it's unique + timestamps[timestamp] = true + } + } + + if hasDuplicate { + fmt.Println("\n**There are duplicate timestamps!**") + } else { + fmt.Println("\n**All timestamps are unique.**") + } + fmt.Println() +} + func TestPagedQueryUsingIndex(t *testing.T) { runCloverTest(t, func(t *testing.T, db *c.DB) { err := db.CreateCollection("test") @@ -1507,7 +1541,11 @@ func TestPagedQueryUsingIndex(t *testing.T) { n := 10003 for i := 0; i < n; i++ { doc := d.NewDocument() - doc.Set("timestamp", time.Now()) + // Old code that caused duplicates: + // doc.Set("timestamp", time.Now()) + + // New code to ensure unique timestamps: + doc.Set("timestamp", time.Now().Add(time.Duration(i)*time.Nanosecond)) if len(docs) == 1024 { err := db.Insert("test", docs...) @@ -1527,6 +1565,7 @@ func TestPagedQueryUsingIndex(t *testing.T) { require.NoError(t, err) } + checkUniqueTimestamps(db) sortOpt := q.SortOption{Field: "timestamp", Direction: -1} count := 0 From 0c2df3728c4c0ee43cc6c6a7a82a1283d51d25ac Mon Sep 17 00:00:00 2001 From: Ahmed Hossam Date: Fri, 20 Sep 2024 23:58:29 +0300 Subject: [PATCH 2/3] Removed unnecessary comments and checkUniqueTimestamps function as per review feedback. --- db_test.go | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/db_test.go b/db_test.go index 6aeadf5..ccc42a6 100644 --- a/db_test.go +++ b/db_test.go @@ -1494,40 +1494,6 @@ func TestIndexQueryWithSort(t *testing.T) { }) } -func checkUniqueTimestamps(db *c.DB) { - // Retrieve all documents from the collection - allDocs, err := db.FindAll(q.NewQuery("test")) - if err != nil { - fmt.Println("Error retrieving documents:", err) - return - } - - // Use a map to track timestamps - timestamps := make(map[time.Time]bool) - - // Check for duplicate timestamps - hasDuplicate := false - for _, doc := range allDocs { - // Get the timestamp from the document - timestamp := doc.Get("timestamp").(time.Time) - - // Check if timestamp is already in the map - if timestamps[timestamp] { - hasDuplicate = true - } else { - // Add timestamp to the map if it's unique - timestamps[timestamp] = true - } - } - - if hasDuplicate { - fmt.Println("\n**There are duplicate timestamps!**") - } else { - fmt.Println("\n**All timestamps are unique.**") - } - fmt.Println() -} - func TestPagedQueryUsingIndex(t *testing.T) { runCloverTest(t, func(t *testing.T, db *c.DB) { err := db.CreateCollection("test") @@ -1541,7 +1507,7 @@ func TestPagedQueryUsingIndex(t *testing.T) { n := 10003 for i := 0; i < n; i++ { doc := d.NewDocument() - // Old code that caused duplicates: + // doc.Set("timestamp", time.Now()) // New code to ensure unique timestamps: @@ -1565,7 +1531,6 @@ func TestPagedQueryUsingIndex(t *testing.T) { require.NoError(t, err) } - checkUniqueTimestamps(db) sortOpt := q.SortOption{Field: "timestamp", Direction: -1} count := 0 From 3d189e9b4f3aaea766b036bdab3f08f865115bd4 Mon Sep 17 00:00:00 2001 From: Ahmed Hossam Date: Sat, 21 Sep 2024 14:20:05 +0300 Subject: [PATCH 3/3] updated comments --- db_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/db_test.go b/db_test.go index ccc42a6..308cd8a 100644 --- a/db_test.go +++ b/db_test.go @@ -1508,9 +1508,7 @@ func TestPagedQueryUsingIndex(t *testing.T) { for i := 0; i < n; i++ { doc := d.NewDocument() - // doc.Set("timestamp", time.Now()) - - // New code to ensure unique timestamps: + // Ensure unique timestamps by adding a small time offset (in nanoseconds) doc.Set("timestamp", time.Now().Add(time.Duration(i)*time.Nanosecond)) if len(docs) == 1024 {