f80424d707
# Conflicts: # internal/db/db.go
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package db_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
"github.com/david/knox/internal/db"
|
|
"github.com/david/knox/internal/db/sqlcdb"
|
|
)
|
|
|
|
// TestSQLCPilotParity proves the sqlc-generated queries return exactly what the
|
|
// hand-written ones do, against a schema created by the real db.Open. It is the
|
|
// guard against the failure mode sqlc is meant to prevent: generated code that
|
|
// silently disagrees with the live schema.
|
|
func TestSQLCPilotParity(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "index.db")
|
|
kdb, err := db.Open(path)
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = kdb.Close() })
|
|
|
|
for _, fp := range []string{"aaaa1111", "bbbb2222"} {
|
|
_, _, err := kdb.RecordObservation(db.ObservationRecord{
|
|
Fingerprint: fp,
|
|
SourceID: "test",
|
|
Title: "entry " + fp,
|
|
Summary: "summary " + fp,
|
|
ContentType: "note",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("record %s: %v", fp, err)
|
|
}
|
|
}
|
|
|
|
conn, err := sql.Open("sqlite", path)
|
|
if err != nil {
|
|
t.Fatalf("open second connection: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = conn.Close() })
|
|
q := sqlcdb.New(conn)
|
|
|
|
t.Run("GetEntry", func(t *testing.T) {
|
|
want, err := kdb.FindEntry("aaaa1111")
|
|
if err != nil {
|
|
t.Fatalf("FindEntry: %v", err)
|
|
}
|
|
if want == nil {
|
|
t.Fatal("FindEntry returned nil for a recorded entry")
|
|
}
|
|
got, err := q.GetEntry(t.Context(), "aaaa1111")
|
|
if err != nil {
|
|
t.Fatalf("GetEntry: %v", err)
|
|
}
|
|
if got.Fingerprint != want.Fingerprint || got.Title != want.Title || got.SourceID != want.SourceID {
|
|
t.Errorf("mismatch: sqlc=%+v database/sql=%+v", got, want)
|
|
}
|
|
if got.CreatedAt != want.CreatedAt {
|
|
t.Errorf("created_at mismatch: sqlc=%q database/sql=%q", got.CreatedAt, want.CreatedAt)
|
|
}
|
|
})
|
|
|
|
t.Run("ListRecentEntries", func(t *testing.T) {
|
|
want, err := kdb.RecentEntries(10)
|
|
if err != nil {
|
|
t.Fatalf("RecentEntries: %v", err)
|
|
}
|
|
got, err := q.ListRecentEntries(t.Context(), 10)
|
|
if err != nil {
|
|
t.Fatalf("ListRecentEntries: %v", err)
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("count mismatch: sqlc=%d database/sql=%d", len(got), len(want))
|
|
}
|
|
for i := range got {
|
|
if got[i].Fingerprint != want[i].Fingerprint {
|
|
t.Errorf("order mismatch at %d: sqlc=%s database/sql=%s", i, got[i].Fingerprint, want[i].Fingerprint)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("Counts", func(t *testing.T) {
|
|
entries, err := q.CountEntries(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("CountEntries: %v", err)
|
|
}
|
|
if entries < 2 {
|
|
t.Errorf("CountEntries = %d, want >= 2", entries)
|
|
}
|
|
obs, err := q.CountObservations(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("CountObservations: %v", err)
|
|
}
|
|
if obs < 2 {
|
|
t.Errorf("CountObservations = %d, want >= 2", obs)
|
|
}
|
|
})
|
|
}
|