fix: ensure test libraries are cleaned up after each test completes
Problem: When running `make test-integration`, the last test to run would leave its "test" libraries in the database. This happened because: 1. setupTestServer() cleaned up old "test" libraries at the START 2. Tests created their own libraries 3. When tests finished, t.Cleanup() called setup.Close() which only closed connections but did NOT delete libraries 4. The LAST test's libraries persisted because no subsequent test cleaned them For example, "Job Status Test Library" from TestWorker_JobStatusTracking would remain in the database after all tests completed, visible when logging into the UI. Root Cause: The cleanup logic only ran at the START of each test (in setupTestServer), not at the END. This worked for intermediate tests (each test cleaned up the previous test's libraries), but the final test had no cleanup. Solution: Added library cleanup to the t.Cleanup() function in setupTestServer(). Now each test deletes its own "test" libraries when it completes, ensuring: - Clean state after `make test-integration` finishes - No residual test data in the database - Safe for tests with subtests (cleanup runs after all subtests finish) Note on Test Structure: Tests like TestOPDSEndpoints and TestCollectionSearchLibraryFilter create libraries once and share them across all subtests. The t.Cleanup() function runs AFTER all subtests complete, so this change is safe and doesn't interfere with subtest resource sharing. Files changed: - cmd/server/tests/test_helpers_test.go: Added library cleanup to t.Cleanup()
This commit is contained in:
@@ -616,6 +616,17 @@ func setupTestServer(t *testing.T) *TestServerSetup {
|
||||
|
||||
// Register cleanup function to run automatically when test completes
|
||||
t.Cleanup(func() {
|
||||
// Clean up test libraries created by this test
|
||||
ctx := context.Background()
|
||||
allLibs, err := queries.ListLibraries(ctx)
|
||||
if err == nil {
|
||||
for _, lib := range allLibs {
|
||||
if strings.Contains(strings.ToLower(lib.Name), "test") {
|
||||
queries.DeleteLibrary(ctx, lib.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := setup.Close(); err != nil {
|
||||
t.Errorf("Failed to cleanup test server: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user