@@ -0,0 +1,85 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package meta_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + |
| 9 | + metadb "github.com/tenseleyFlow/shithub/internal/meta/sqlc" |
| 10 | + "github.com/tenseleyFlow/shithub/internal/testing/dbtest" |
| 11 | +) |
| 12 | + |
| 13 | +// TestMetaRoundtrip exercises the dbtest harness end-to-end: clone-from-template, |
| 14 | +// migrations applied, sqlc-generated queries against the meta table. |
| 15 | +func TestMetaRoundtrip(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + pool := dbtest.NewTestDB(t) |
| 18 | + q := metadb.New() |
| 19 | + ctx := context.Background() |
| 20 | + |
| 21 | + // Migrations populate the schema_version + app rows. |
| 22 | + got, err := q.GetMeta(ctx, pool, "schema_version") |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("GetMeta(schema_version): %v", err) |
| 25 | + } |
| 26 | + if string(got.Value) != `"0001"` { |
| 27 | + t.Errorf("schema_version: got %s, want %q", got.Value, `"0001"`) |
| 28 | + } |
| 29 | + |
| 30 | + // Set a new key and read it back. |
| 31 | + if err := q.SetMeta(ctx, pool, metadb.SetMetaParams{Key: "test_key", Value: []byte(`"hello"`)}); err != nil { |
| 32 | + t.Fatalf("SetMeta: %v", err) |
| 33 | + } |
| 34 | + got2, err := q.GetMeta(ctx, pool, "test_key") |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("GetMeta(test_key): %v", err) |
| 37 | + } |
| 38 | + if string(got2.Value) != `"hello"` { |
| 39 | + t.Errorf("test_key: got %s, want %q", got2.Value, `"hello"`) |
| 40 | + } |
| 41 | + |
| 42 | + // updated_at trigger: re-set with a new value, updated_at advances. |
| 43 | + first := got2.UpdatedAt |
| 44 | + if err := q.SetMeta(ctx, pool, metadb.SetMetaParams{Key: "test_key", Value: []byte(`"world"`)}); err != nil { |
| 45 | + t.Fatalf("SetMeta upsert: %v", err) |
| 46 | + } |
| 47 | + got3, err := q.GetMeta(ctx, pool, "test_key") |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("GetMeta upsert: %v", err) |
| 50 | + } |
| 51 | + if !got3.UpdatedAt.Time.After(first.Time) { |
| 52 | + t.Errorf("updated_at trigger did not advance: first=%v, after=%v", first.Time, got3.UpdatedAt.Time) |
| 53 | + } |
| 54 | + |
| 55 | + // List + delete. |
| 56 | + rows, err := q.ListMeta(ctx, pool) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("ListMeta: %v", err) |
| 59 | + } |
| 60 | + if len(rows) < 3 { // schema_version, app, test_key |
| 61 | + t.Errorf("ListMeta: got %d rows, want >= 3", len(rows)) |
| 62 | + } |
| 63 | + if err := q.DeleteMeta(ctx, pool, "test_key"); err != nil { |
| 64 | + t.Fatalf("DeleteMeta: %v", err) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// TestMetaParallelism asserts dbtest gives each parallel test its own DB. |
| 69 | +// If two parallel tests shared a database, the writes here would race. |
| 70 | +func TestMetaParallelism(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + for i := 0; i < 4; i++ { |
| 73 | + i := i |
| 74 | + t.Run("worker", func(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + pool := dbtest.NewTestDB(t) |
| 77 | + q := metadb.New() |
| 78 | + key := "parallel_key" |
| 79 | + val := []byte(`"worker"`) |
| 80 | + if err := q.SetMeta(context.Background(), pool, metadb.SetMetaParams{Key: key, Value: val}); err != nil { |
| 81 | + t.Fatalf("SetMeta worker %d: %v", i, err) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |