|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "callit/internal/model" |
| 10 | +) |
| 11 | + |
| 12 | +func TestWorkerLogInsertUsesAutoIncrementID(t *testing.T) { |
| 13 | + store, err := Open(filepath.Join(t.TempDir(), "app.db")) |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("open store failed: %v", err) |
| 16 | + } |
| 17 | + t.Cleanup(func() { |
| 18 | + if closeErr := store.Close(); closeErr != nil { |
| 19 | + t.Fatalf("close store failed: %v", closeErr) |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + ctx := context.Background() |
| 24 | + first := model.WorkerLog{ |
| 25 | + WorkerID: "worker-a", |
| 26 | + RequestID: "req-1", |
| 27 | + Status: 200, |
| 28 | + DurationMS: 10, |
| 29 | + CreatedAt: time.Date(2026, 3, 18, 1, 0, 0, 0, time.UTC), |
| 30 | + } |
| 31 | + second := model.WorkerLog{ |
| 32 | + WorkerID: "worker-a", |
| 33 | + RequestID: "req-2", |
| 34 | + Status: 500, |
| 35 | + DurationMS: 20, |
| 36 | + CreatedAt: time.Date(2026, 3, 18, 1, 0, 1, 0, time.UTC), |
| 37 | + } |
| 38 | + |
| 39 | + if err := store.WorkerLog.Insert(ctx, first); err != nil { |
| 40 | + t.Fatalf("insert first log failed: %v", err) |
| 41 | + } |
| 42 | + if err := store.WorkerLog.Insert(ctx, second); err != nil { |
| 43 | + t.Fatalf("insert second log failed: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + logs, total, err := store.WorkerLog.ListPaged(ctx, "worker-a", 1, 10) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("list logs failed: %v", err) |
| 49 | + } |
| 50 | + if total != 2 { |
| 51 | + t.Fatalf("unexpected total: got=%d want=2", total) |
| 52 | + } |
| 53 | + if len(logs) != 2 { |
| 54 | + t.Fatalf("unexpected log count: got=%d want=2", len(logs)) |
| 55 | + } |
| 56 | + if logs[0].ID <= 0 { |
| 57 | + t.Fatalf("expected auto-increment id, got=%d", logs[0].ID) |
| 58 | + } |
| 59 | + if logs[1].ID <= 0 { |
| 60 | + t.Fatalf("expected auto-increment id, got=%d", logs[1].ID) |
| 61 | + } |
| 62 | + if logs[0].ID <= logs[1].ID { |
| 63 | + t.Fatalf("expected newer log id to be larger, got first=%d second=%d", logs[0].ID, logs[1].ID) |
| 64 | + } |
| 65 | +} |
0 commit comments