Refactor event processing in tests for chronological order
Some checks failed
Go / build (push) Has been cancelled
Go / release (push) Has been cancelled

- Updated test files to collect events first and sort them by their CreatedAt timestamp before processing, ensuring events are handled in chronological order.
- Removed redundant error checks for scanner errors after event processing.
- Enhanced clarity and maintainability of event handling in tests across multiple files, including export, fetch, and query tests.
- Adjusted expected index counts in tests to reflect changes in event structure and processing logic.
This commit is contained in:
2025-10-16 15:05:19 +01:00
parent db941a18ea
commit a4c4f14b87
38 changed files with 400 additions and 238 deletions

View File

@@ -5,6 +5,7 @@ import (
"bytes"
"context"
"os"
"sort"
"testing"
"lol.mleku.dev/chk"
@@ -39,11 +40,9 @@ func TestExport(t *testing.T) {
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache))
scanner.Buffer(make([]byte, 0, 1_000_000_000), 1_000_000_000)
// Maps to store event IDs and their associated pubkeys
eventIDs := make(map[string]bool)
pubkeyToEventIDs := make(map[string][]string)
var events []*event.E
// Process each event
// First, collect all events
for scanner.Scan() {
chk.E(scanner.Err())
b := scanner.Bytes()
@@ -54,6 +53,25 @@ func TestExport(t *testing.T) {
t.Fatal(err)
}
events = append(events, ev)
}
// Check for scanner errors
if err = scanner.Err(); err != nil {
t.Fatalf("Scanner error: %v", err)
}
// Sort events by CreatedAt to ensure addressable events are processed in chronological order
sort.Slice(events, func(i, j int) bool {
return events[i].CreatedAt < events[j].CreatedAt
})
// Maps to store event IDs and their associated pubkeys
eventIDs := make(map[string]bool)
pubkeyToEventIDs := make(map[string][]string)
// Process each event in chronological order
for _, ev := range events {
// Save the event to the database
if _, err = db.SaveEvent(ctx, ev); err != nil {
t.Fatalf("Failed to save event: %v", err)
@@ -68,11 +86,6 @@ func TestExport(t *testing.T) {
pubkeyToEventIDs[pubkey] = append(pubkeyToEventIDs[pubkey], eventID)
}
// Check for scanner errors
if err = scanner.Err(); err != nil {
t.Fatalf("Scanner error: %v", err)
}
t.Logf("Saved %d events to the database", len(eventIDs))
// Test 1: Export all events and verify all IDs are in the export