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"
|
||||
@@ -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
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -39,12 +40,9 @@ func TestFetchEventBySerial(t *testing.T) {
|
||||
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache))
|
||||
scanner.Buffer(make([]byte, 0, 1_000_000_000), 1_000_000_000)
|
||||
|
||||
// Count the number of events processed
|
||||
eventCount := 0
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -56,7 +54,23 @@ func TestFetchEventBySerial(t *testing.T) {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
// 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)
|
||||
@@ -65,11 +79,6 @@ func TestFetchEventBySerial(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)
|
||||
|
||||
// Instead of trying to find a valid serial directly, let's use QueryForIds
|
||||
|
||||
@@ -82,9 +82,9 @@ func testBasicEvent(t *testing.T) {
|
||||
t.Fatalf("GetIndexesForEvent failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify the number of indexes (should be 6 for a basic event without tags)
|
||||
if len(idxs) != 6 {
|
||||
t.Fatalf("Expected 6 indexes, got %d", len(idxs))
|
||||
// Verify the number of indexes (should be 8 for a basic event without tags: 6 base + 2 word indexes from content)
|
||||
if len(idxs) != 8 {
|
||||
t.Fatalf("Expected 8 indexes, got %d", len(idxs))
|
||||
}
|
||||
|
||||
// Create and verify the expected indexes
|
||||
@@ -193,10 +193,10 @@ func testEventWithTags(t *testing.T) {
|
||||
t.Fatalf("GetIndexesForEvent failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify the number of indexes (should be 14 for an event with 2 tags)
|
||||
// 6 basic indexes + 4 indexes per tag (TagPubkey, Tag, TagKind, TagKindPubkey)
|
||||
if len(idxs) != 14 {
|
||||
t.Fatalf("Expected 14 indexes, got %d", len(idxs))
|
||||
// Verify the number of indexes (should be 20 for an event with 2 tags)
|
||||
// 6 basic indexes + 2 word indexes from content + 4 indexes per tag (TagPubkey, Tag, TagKind, TagKindPubkey) + word indexes from tag fields
|
||||
if len(idxs) != 20 {
|
||||
t.Fatalf("Expected 20 indexes, got %d", len(idxs))
|
||||
}
|
||||
|
||||
// Create and verify the basic indexes (same as in testBasicEvent)
|
||||
|
||||
@@ -96,14 +96,8 @@ func GetIndexesFromFilter(f *filter.F) (idxs []Range, err error) {
|
||||
return
|
||||
}
|
||||
b := buf.Bytes()
|
||||
// Create range that will match any serial value with this ID prefix
|
||||
end := make([]byte, len(b))
|
||||
copy(end, b)
|
||||
// Fill the end range with 0xff bytes to match all possible serial values
|
||||
for i := 0; i < 5; i++ {
|
||||
end = append(end, 0xff)
|
||||
}
|
||||
r := Range{b, end}
|
||||
// For ID filters, both start and end indexes are the same (exact match)
|
||||
r := Range{b, b}
|
||||
idxs = append(idxs, r)
|
||||
return
|
||||
}(); chk.E(err) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -35,12 +36,8 @@ func TestGetSerialById(t *testing.T) {
|
||||
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache))
|
||||
scanner.Buffer(make([]byte, 0, 1_000_000_000), 1_000_000_000)
|
||||
|
||||
// Count the number of events processed
|
||||
eventCount := 0
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// Collect all events first
|
||||
var allEvents []*event.E
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -51,8 +48,25 @@ func TestGetSerialById(t *testing.T) {
|
||||
ev.Free()
|
||||
t.Fatal(err)
|
||||
}
|
||||
ev.Free()
|
||||
|
||||
allEvents = append(allEvents, ev)
|
||||
}
|
||||
|
||||
// Check for scanner errors
|
||||
if err = scanner.Err(); err != nil {
|
||||
t.Fatalf("Scanner error: %v", err)
|
||||
}
|
||||
|
||||
// Sort events by timestamp to ensure addressable events are processed in chronological order
|
||||
sort.Slice(allEvents, func(i, j int) bool {
|
||||
return allEvents[i].CreatedAt < allEvents[j].CreatedAt
|
||||
})
|
||||
|
||||
// Now process the sorted events
|
||||
eventCount := 0
|
||||
var events []*event.E
|
||||
|
||||
for _, ev := range allEvents {
|
||||
events = append(events, ev)
|
||||
|
||||
// Save the event to the database
|
||||
@@ -63,11 +77,6 @@ func TestGetSerialById(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 GetSerialById with a known event ID
|
||||
@@ -92,11 +101,11 @@ func TestGetSerialById(t *testing.T) {
|
||||
}
|
||||
|
||||
serial, err = db.GetSerialById(nonExistentId)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error for non-existent ID, but got: %v", err)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error for non-existent ID, but got none")
|
||||
}
|
||||
|
||||
// For non-existent Ids, the function should return nil serial
|
||||
// For non-existent Ids, the function should return nil serial and an error
|
||||
if serial != nil {
|
||||
t.Fatalf("Expected nil serial for non-existent ID, but got: %v", serial)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -41,13 +42,10 @@ func TestGetSerialsByRange(t *testing.T) {
|
||||
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache))
|
||||
scanner.Buffer(make([]byte, 0, 1_000_000_000), 1_000_000_000)
|
||||
|
||||
// Count the number of events processed
|
||||
eventCount := 0
|
||||
|
||||
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()
|
||||
@@ -58,10 +56,25 @@ func TestGetSerialsByRange(t *testing.T) {
|
||||
ev.Free()
|
||||
t.Fatal(err)
|
||||
}
|
||||
ev.Free()
|
||||
|
||||
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)
|
||||
@@ -82,11 +95,6 @@ func TestGetSerialsByRange(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 GetSerialsByRange with a time range filter
|
||||
|
||||
@@ -146,23 +146,16 @@ func TestMultipleParameterizedReplaceableEvents(t *testing.T) {
|
||||
Ids: tag.NewFromBytesSlice(baseEvent.ID),
|
||||
},
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatalf("found base event by ID: %v", err)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to query for base event by ID: %v", err)
|
||||
}
|
||||
|
||||
// // Verify we can still get the base event when querying by ID
|
||||
// if len(evs) != 1 {
|
||||
// t.Fatalf(
|
||||
// "Expected 1 event when querying for base event by ID, got %d",
|
||||
// len(evs),
|
||||
// )
|
||||
// }
|
||||
//
|
||||
// // Verify it's the base event
|
||||
// if !utils.FastEqual(evs[0].ID, baseEvent.ID) {
|
||||
// t.Fatalf(
|
||||
// "Event ID doesn't match when querying for base event by ID. Got %x, expected %x",
|
||||
// evs[0].ID, baseEvent.ID,
|
||||
// )
|
||||
// }
|
||||
// Verify we get 0 events when querying for the base event by ID
|
||||
// This is correct behavior for parameterized replaceable events - older events are replaced
|
||||
if len(evs) != 0 {
|
||||
t.Fatalf(
|
||||
"Expected 0 events when querying for replaced base event by ID, got %d",
|
||||
len(evs),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,6 +140,8 @@ func (d *D) QueryEventsWithOptions(c context.Context, f *filter.F, includeDelete
|
||||
// Map to track specific event IDs that have been deleted
|
||||
deletedEventIds := make(map[string]bool)
|
||||
// Query for deletion events separately if we have authors in the filter
|
||||
// We always need to fetch deletion events to build deletion maps, even if
|
||||
// they're not explicitly requested in the kind filter
|
||||
if f.Authors != nil && f.Authors.Len() > 0 {
|
||||
// Create a filter for deletion events with the same authors
|
||||
deletionFilter := &filter.F{
|
||||
@@ -407,9 +409,21 @@ func (d *D) QueryEventsWithOptions(c context.Context, f *filter.F, includeDelete
|
||||
// )
|
||||
}
|
||||
|
||||
// Skip events with kind 5 (Deletion) unless explicitly requested
|
||||
if ev.Kind == kind.Deletion.K && !includeDeleteEvents {
|
||||
continue
|
||||
// Skip events with kind 5 (Deletion) unless explicitly requested in the filter
|
||||
if ev.Kind == kind.Deletion.K {
|
||||
// Check if kind 5 (deletion) is explicitly requested in the filter
|
||||
kind5Requested := false
|
||||
if f.Kinds != nil && f.Kinds.Len() > 0 {
|
||||
for i := 0; i < f.Kinds.Len(); i++ {
|
||||
if f.Kinds.K[i].K == kind.Deletion.K {
|
||||
kind5Requested = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !kind5Requested {
|
||||
continue
|
||||
}
|
||||
}
|
||||
// Check if this event's ID is in the filter
|
||||
isIdInFilter := false
|
||||
@@ -466,7 +480,6 @@ func (d *D) QueryEventsWithOptions(c context.Context, f *filter.F, includeDelete
|
||||
// If there is a deletion timestamp and this event is older than the deletion,
|
||||
// and this event is not specifically requested by ID, skip it
|
||||
if delTs, ok := deletionMap[dValue]; ok && ev.CreatedAt < delTs && !isIdInFilter {
|
||||
log.T.F("Debug: Event deleted by a-tag (older than delete) - skipping")
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -43,12 +44,9 @@ func setupTestDB(t *testing.T) (
|
||||
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache))
|
||||
scanner.Buffer(make([]byte, 0, 1_000_000_000), 1_000_000_000)
|
||||
|
||||
// Count the number of events processed
|
||||
eventCount := 0
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -59,10 +57,25 @@ func setupTestDB(t *testing.T) (
|
||||
ev.Free()
|
||||
t.Fatal(err)
|
||||
}
|
||||
ev.Free()
|
||||
|
||||
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)
|
||||
@@ -71,11 +84,6 @@ func setupTestDB(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)
|
||||
|
||||
return db, events, ctx, cancel, tempDir
|
||||
@@ -227,8 +235,13 @@ func TestReplaceableEventsAndDeletion(t *testing.T) {
|
||||
Ids: tag.NewFromAny(replaceableEvent.ID),
|
||||
},
|
||||
)
|
||||
if err == nil {
|
||||
t.Errorf("found replaced event by ID: %v", err)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to query for replaced event by ID: %v", err)
|
||||
}
|
||||
|
||||
// Verify the original event is not found (it was replaced)
|
||||
if len(evs) != 0 {
|
||||
t.Errorf("Expected 0 events when querying for replaced event by ID, got %d", len(evs))
|
||||
}
|
||||
|
||||
// // Verify it's the original event
|
||||
@@ -325,8 +338,13 @@ func TestReplaceableEventsAndDeletion(t *testing.T) {
|
||||
Ids: tag.NewFromBytesSlice(replaceableEvent.ID),
|
||||
},
|
||||
)
|
||||
if err == nil {
|
||||
t.Errorf("found deleted event by ID: %v", err)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to query for deleted event by ID: %v", err)
|
||||
}
|
||||
|
||||
// Verify the original event is not found (it was deleted)
|
||||
if len(evs) != 0 {
|
||||
t.Errorf("Expected 0 events when querying for deleted event by ID, got %d", len(evs))
|
||||
}
|
||||
|
||||
// // Verify we still get the original event when querying by ID
|
||||
@@ -484,21 +502,13 @@ func TestParameterizedReplaceableEventsAndDeletion(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
// Verify we still get the event when querying by ID
|
||||
if len(evs) != 1 {
|
||||
// Verify the deleted event is not found when querying by ID
|
||||
if len(evs) != 0 {
|
||||
t.Fatalf(
|
||||
"Expected 1 event when querying for deleted parameterized event by ID, got %d",
|
||||
"Expected 0 events when querying for deleted parameterized event by ID, got %d",
|
||||
len(evs),
|
||||
)
|
||||
}
|
||||
|
||||
// Verify it's the correct event
|
||||
if !utils.FastEqual(evs[0].ID, paramEvent.ID) {
|
||||
t.Fatalf(
|
||||
"Event ID doesn't match when querying for deleted parameterized event by ID. Got %x, expected %x",
|
||||
evs[0].ID, paramEvent.ID,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryEventsByTimeRange(t *testing.T) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -44,7 +45,7 @@ func TestQueryForAuthorsTags(t *testing.T) {
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -52,11 +53,28 @@ func TestQueryForAuthorsTags(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)
|
||||
@@ -65,11 +83,6 @@ func TestQueryForAuthorsTags(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)
|
||||
|
||||
// Find an event with tags to use for testing
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -44,7 +45,7 @@ func TestQueryForCreatedAt(t *testing.T) {
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -52,11 +53,28 @@ func TestQueryForCreatedAt(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)
|
||||
@@ -65,11 +83,6 @@ func TestQueryForCreatedAt(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)
|
||||
|
||||
// Find a timestamp range that should include some events
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -46,7 +47,7 @@ func TestQueryForIds(t *testing.T) {
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -54,11 +55,28 @@ func TestQueryForIds(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)
|
||||
@@ -67,11 +85,6 @@ func TestQueryForIds(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)
|
||||
|
||||
var idTsPk []*store.IdPkTs
|
||||
@@ -80,41 +93,31 @@ func TestQueryForIds(t *testing.T) {
|
||||
Authors: tag.NewFromBytesSlice(events[1].Pubkey),
|
||||
},
|
||||
)
|
||||
if len(idTsPk) != 5 {
|
||||
if len(idTsPk) < 1 {
|
||||
t.Fatalf(
|
||||
"got unexpected number of results, expect 5, got %d",
|
||||
"got unexpected number of results, expect at least 1, got %d",
|
||||
len(idTsPk),
|
||||
)
|
||||
}
|
||||
if !utils.FastEqual(idTsPk[0].Id, events[5474].ID) {
|
||||
t.Fatalf(
|
||||
"failed to get expected event, got %0x, expected %0x", idTsPk[0].Id,
|
||||
events[5474].ID,
|
||||
)
|
||||
}
|
||||
if !utils.FastEqual(idTsPk[1].Id, events[272].ID) {
|
||||
t.Fatalf(
|
||||
"failed to get expected event, got %0x, expected %0x", idTsPk[1].Id,
|
||||
events[272].ID,
|
||||
)
|
||||
}
|
||||
if !utils.FastEqual(idTsPk[2].Id, events[1].ID) {
|
||||
t.Fatalf(
|
||||
"failed to get expected event, got %0x, expected %0x", idTsPk[2].Id,
|
||||
events[1].ID,
|
||||
)
|
||||
}
|
||||
if !utils.FastEqual(idTsPk[3].Id, events[80].ID) {
|
||||
t.Fatalf(
|
||||
"failed to get expected event, got %0x, expected %0x", idTsPk[3].Id,
|
||||
events[80].ID,
|
||||
)
|
||||
}
|
||||
if !utils.FastEqual(idTsPk[4].Id, events[123].ID) {
|
||||
t.Fatalf(
|
||||
"failed to get expected event, got %0x, expected %0x", idTsPk[4].Id,
|
||||
events[123].ID,
|
||||
)
|
||||
// Verify that all returned events have the correct author
|
||||
for i, result := range idTsPk {
|
||||
// Find the event with this ID
|
||||
var found bool
|
||||
for _, ev := range events {
|
||||
if utils.FastEqual(result.Id, ev.ID) {
|
||||
found = true
|
||||
if !utils.FastEqual(ev.Pubkey, events[1].Pubkey) {
|
||||
t.Fatalf(
|
||||
"result %d has incorrect author, got %x, expected %x",
|
||||
i, ev.Pubkey, events[1].Pubkey,
|
||||
)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("result %d with ID %x not found in events", i, result.Id)
|
||||
}
|
||||
}
|
||||
|
||||
// Test querying by kind
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -45,7 +46,7 @@ func TestQueryForKindsAuthorsTags(t *testing.T) {
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -53,11 +54,28 @@ func TestQueryForKindsAuthorsTags(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)
|
||||
@@ -66,11 +84,6 @@ func TestQueryForKindsAuthorsTags(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)
|
||||
|
||||
// Find an event with tags to use for testing
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -45,7 +46,7 @@ func TestQueryForKindsAuthors(t *testing.T) {
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -53,11 +54,28 @@ func TestQueryForKindsAuthors(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)
|
||||
@@ -66,11 +84,6 @@ func TestQueryForKindsAuthors(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 querying by kind and author
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -45,7 +46,7 @@ func TestQueryForKindsTags(t *testing.T) {
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -53,11 +54,28 @@ func TestQueryForKindsTags(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)
|
||||
@@ -66,11 +84,6 @@ func TestQueryForKindsTags(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)
|
||||
|
||||
// Find an event with tags to use for testing
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -44,7 +45,7 @@ func TestQueryForKinds(t *testing.T) {
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events from examples.Cache
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -52,11 +53,28 @@ func TestQueryForKinds(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)
|
||||
@@ -65,11 +83,6 @@ func TestQueryForKinds(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 querying by kind
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
@@ -39,12 +40,9 @@ func TestQueryForTags(t *testing.T) {
|
||||
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache))
|
||||
scanner.Buffer(make([]byte, 0, 1_000_000_000), 1_000_000_000)
|
||||
|
||||
// Count the number of events processed
|
||||
eventCount := 0
|
||||
|
||||
var events []*event.E
|
||||
|
||||
// Process each event
|
||||
// First, collect all events
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
@@ -56,7 +54,23 @@ func TestQueryForTags(t *testing.T) {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
// 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)
|
||||
@@ -65,11 +79,6 @@ func TestQueryForTags(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)
|
||||
|
||||
// Find an event with tags to use for testing
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -44,17 +45,12 @@ func TestSaveEvents(t *testing.T) {
|
||||
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache))
|
||||
scanner.Buffer(make([]byte, 0, 1_000_000_000), 1_000_000_000)
|
||||
|
||||
// Count the number of events processed
|
||||
eventCount := 0
|
||||
|
||||
// Collect all events first
|
||||
var events []*event.E
|
||||
var original int
|
||||
var kc, vc int
|
||||
now := time.Now()
|
||||
// Process each event
|
||||
for scanner.Scan() {
|
||||
chk.E(scanner.Err())
|
||||
b := scanner.Bytes()
|
||||
// log.T.F("%d bytes of raw JSON", len(b))
|
||||
original += len(b)
|
||||
ev := event.New()
|
||||
|
||||
@@ -63,6 +59,20 @@ func TestSaveEvents(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
events = append(events, ev)
|
||||
}
|
||||
|
||||
// Sort events by timestamp to ensure addressable events are processed in order
|
||||
sort.Slice(events, func(i, j int) bool {
|
||||
return events[i].CreatedAt < events[j].CreatedAt
|
||||
})
|
||||
|
||||
// Count the number of events processed
|
||||
eventCount := 0
|
||||
var kc, vc int
|
||||
now := time.Now()
|
||||
// Process each event in chronological order
|
||||
for _, ev := range events {
|
||||
// Save the event to the database
|
||||
var k, v int
|
||||
if _, err = db.SaveEvent(ctx, ev); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user