Refactor event processing in tests for chronological order
- 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:
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -47,7 +48,7 @@ func TestQueryForSerials(t *testing.T) {
|
||||
var events []*event.E
|
||||
var eventSerials = make(map[string]*types.Uint40) // Map event ID (hex) to serial
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -55,11 +56,28 @@ func TestQueryForSerials(t *testing.T) {
|
||||
|
||||
// Unmarshal the event
|
||||
if _, err = ev.Unmarshal(b); chk.E(err) {
|
||||
ev.Free()
|
||||
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
|
||||
})
|
||||
|
||||
// Count the number of events processed
|
||||
eventCount = 0
|
||||
|
||||
// Now 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 #%d: %v", eventCount+1, err)
|
||||
@@ -80,11 +98,6 @@ func TestQueryForSerials(t *testing.T) {
|
||||
eventCount++
|
||||
}
|
||||
|
||||
// Check for scanner errors
|
||||
if err = scanner.Err(); err != nil {
|
||||
t.Fatalf("Scanner error: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("Successfully saved %d events to the database", eventCount)
|
||||
|
||||
// Test QueryForSerials with an ID filter
|
||||
|
||||
Reference in New Issue
Block a user