Go · 844 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package db_test
4
5 import (
6 "context"
7 "testing"
8
9 "github.com/tenseleyFlow/shithub/internal/infra/db"
10 "github.com/tenseleyFlow/shithub/internal/testing/dbtest"
11 )
12
13 func TestQueryCounter_IncrementsOnEveryQuery(t *testing.T) {
14 t.Parallel()
15 pool := dbtest.NewTestDB(t)
16 ctx := db.WithCounter(context.Background())
17
18 // Three trivial queries against the live pool.
19 for i := 0; i < 3; i++ {
20 var n int
21 if err := pool.QueryRow(ctx, "SELECT 1").Scan(&n); err != nil {
22 t.Fatalf("SELECT 1: %v", err)
23 }
24 }
25 if got := db.FromContext(ctx); got != 3 {
26 t.Errorf("FromContext = %d; want 3", got)
27 }
28 }
29
30 func TestQueryCounter_NilCounterIsZeroAndSafe(t *testing.T) {
31 t.Parallel()
32 if got := db.FromContext(context.Background()); got != 0 {
33 t.Errorf("FromContext on bare ctx = %d; want 0", got)
34 }
35 }
36