mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-15 04:30:55 +08:00
A `defer d.Close()` runs when the test function returns, before its t.Cleanup functions. The cleanups that delete the test's rows then ran on a closed pool, the error was ignored, and the rows stayed in the database for the next run. PR #220 fixed three files; this applies the same pattern to every remaining db test that combines `defer d.Close()` with row cleanups. - Close the pool with t.Cleanup, registered before the row cleanups so it runs last (LIFO). - Report cleanup errors with t.Errorf through small helpers in db/cleanup_helpers_test.go. A profile the test already deleted is not an error. Test files only. No assertion or production change.
35 lines
940 B
Go
35 lines
940 B
Go
package db
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
// These helpers report a failed test cleanup instead of swallowing it: a silent
|
|
// failure leaves the test's rows behind for the next run on the same database.
|
|
|
|
// cleanupErr reports err from the cleanup step described by what.
|
|
func cleanupErr(t *testing.T, what string, err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Errorf("cleanup %s: %v", what, err)
|
|
}
|
|
}
|
|
|
|
// cleanupExec runs one cleanup statement. Matching no row is not an error.
|
|
func cleanupExec(t *testing.T, d *DB, query string, args ...any) {
|
|
t.Helper()
|
|
_, err := d.Exec(query, args...)
|
|
cleanupErr(t, query, err)
|
|
}
|
|
|
|
// cleanupProfile deletes a profile through DeleteProfile. A profile the test
|
|
// already deleted is not an error.
|
|
func cleanupProfile(t *testing.T, d *DB, id int64) {
|
|
t.Helper()
|
|
if err := d.DeleteProfile(id); !errors.Is(err, ErrLLMProfileNotFound) {
|
|
cleanupErr(t, fmt.Sprintf("profile %d", id), err)
|
|
}
|
|
}
|