|
| 1 | +package datastore_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/codeGROOVE-dev/ds9/pkg/datastore" |
| 9 | +) |
| 10 | + |
| 11 | +func TestBatchOperations(t *testing.T) { |
| 12 | + client, cleanup := datastore.NewMockClient(t) |
| 13 | + defer cleanup() |
| 14 | + |
| 15 | + ctx := context.Background() |
| 16 | + |
| 17 | + type Item struct { |
| 18 | + ID int |
| 19 | + } |
| 20 | + |
| 21 | + // Number of items > 1000 to test batching limits |
| 22 | + // Put limit: 500, Get limit: 1000 |
| 23 | + const count = 1200 |
| 24 | + keys := make([]*datastore.Key, count) |
| 25 | + items := make([]Item, count) |
| 26 | + |
| 27 | + for i := range count { |
| 28 | + keys[i] = datastore.NameKey("Item", fmt.Sprintf("item-%d", i), nil) |
| 29 | + items[i] = Item{ID: i} |
| 30 | + } |
| 31 | + |
| 32 | + // Test PutMulti |
| 33 | + if _, err := client.PutMulti(ctx, keys, items); err != nil { |
| 34 | + t.Fatalf("PutMulti failed: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + // Test GetMulti |
| 38 | + results := make([]Item, count) |
| 39 | + if err := client.GetMulti(ctx, keys, &results); err != nil { |
| 40 | + t.Fatalf("GetMulti failed: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + for i := range count { |
| 44 | + if results[i].ID != i { |
| 45 | + t.Errorf("Item %d mismatch: got %d, want %d", i, results[i].ID, i) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Test DeleteMulti |
| 50 | + if err := client.DeleteMulti(ctx, keys); err != nil { |
| 51 | + t.Fatalf("DeleteMulti failed: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + // Verify deletion |
| 55 | + err := client.GetMulti(ctx, keys, &results) |
| 56 | + // Should return MultiError with all ErrNoSuchEntity |
| 57 | + if err == nil { |
| 58 | + t.Fatal("Expected error after deletion, got nil") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestAllocateIDsBatch(t *testing.T) { |
| 63 | + client, cleanup := datastore.NewMockClient(t) |
| 64 | + defer cleanup() |
| 65 | + |
| 66 | + ctx := context.Background() |
| 67 | + |
| 68 | + // Test AllocateIDs > 500 |
| 69 | + const count = 600 |
| 70 | + keys := make([]*datastore.Key, count) |
| 71 | + for i := range count { |
| 72 | + keys[i] = datastore.IncompleteKey("Item", nil) |
| 73 | + } |
| 74 | + |
| 75 | + allocated, err := client.AllocateIDs(ctx, keys) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("AllocateIDs failed: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + if len(allocated) != count { |
| 81 | + t.Fatalf("Expected %d keys, got %d", count, len(allocated)) |
| 82 | + } |
| 83 | + |
| 84 | + seen := make(map[int64]bool) |
| 85 | + for i, k := range allocated { |
| 86 | + if k.Incomplete() { |
| 87 | + t.Errorf("Key %d is incomplete", i) |
| 88 | + } |
| 89 | + if seen[k.ID] { |
| 90 | + t.Errorf("Duplicate ID %d at index %d", k.ID, i) |
| 91 | + } |
| 92 | + seen[k.ID] = true |
| 93 | + } |
| 94 | +} |
0 commit comments