Răsfoiți Sursa

test(storage): migrate the integration test database in TestMain

The Makefile built the binary only to run -migrate before the storage
tests, since the API, Google Reader and Fever tests already migrate
through servertest. Both storage test files now share the connection
setup.
Frédéric Guillot 4 ore în urmă
părinte
comite
259eead417

+ 1 - 3
Makefile

@@ -113,12 +113,10 @@ lint:
 	gofmt -d -e .
 	golangci-lint run
 
-integration-test: miniflux
+integration-test:
 	psql -U postgres -c 'drop database if exists miniflux_test;'
 	psql -U postgres -c 'create database miniflux_test;'
 
-	DATABASE_URL=$(DB_URL) ./$(APP) -migrate
-
 	TEST_DATABASE_URL=$(DB_URL) go test -v -count=1 ./internal/storage
 	TEST_DATABASE_URL=$(DB_URL) go test -v -count=1 ./internal/api
 	TEST_DATABASE_URL=$(DB_URL) go test -v -count=1 ./internal/googlereader

+ 2 - 13
internal/storage/entry_query_builder_integration_test.go

@@ -4,8 +4,6 @@
 package storage
 
 import (
-	"database/sql"
-	"os"
 	"slices"
 	"testing"
 	"time"
@@ -16,20 +14,11 @@ import (
 func newEntryQueryTestStorage(t *testing.T) *Storage {
 	t.Helper()
 
-	databaseURL := os.Getenv("TEST_DATABASE_URL")
-	if databaseURL == "" {
-		t.Skip("Set TEST_DATABASE_URL to a migrated PostgreSQL database to run storage integration tests")
-	}
-
-	db, err := sql.Open("postgres", databaseURL)
-	if err != nil {
-		t.Fatal(err)
-	}
-	t.Cleanup(func() { db.Close() })
+	db := openTestDatabase(t)
 
 	// Keep temporary tables on the same connection and leave the database unchanged.
 	db.SetMaxOpenConns(1)
-	_, err = db.Exec(`
+	_, err := db.Exec(`
 		SET TIME ZONE 'UTC';
 		CREATE TEMP TABLE users (LIKE users INCLUDING DEFAULTS);
 		CREATE TEMP TABLE categories (LIKE categories INCLUDING DEFAULTS);

+ 1 - 11
internal/storage/icon_integration_test.go

@@ -5,7 +5,6 @@ package storage
 
 import (
 	"database/sql"
-	"os"
 	"testing"
 	"time"
 
@@ -14,16 +13,7 @@ import (
 )
 
 func TestStoreFeedIconWhenAnotherTransactionCreatesTheSameIcon(t *testing.T) {
-	databaseURL := os.Getenv("TEST_DATABASE_URL")
-	if databaseURL == "" {
-		t.Skip("Set TEST_DATABASE_URL to a migrated PostgreSQL database to run storage integration tests")
-	}
-
-	db, err := sql.Open("postgres", databaseURL)
-	if err != nil {
-		t.Fatal(err)
-	}
-	t.Cleanup(func() { db.Close() })
+	db := openTestDatabase(t)
 	store := NewStorage(db)
 
 	suffix := crypto.GenerateRandomStringHex(10)

+ 56 - 0
internal/storage/integration_database_test.go

@@ -0,0 +1,56 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package storage
+
+import (
+	"database/sql"
+	"fmt"
+	"os"
+	"testing"
+
+	"miniflux.app/v2/internal/database"
+)
+
+const skipIntegrationTestsMessage = `Set TEST_DATABASE_URL to a PostgreSQL database to run the storage integration tests`
+
+// testDatabaseURL is the database migrated by TestMain, or empty when TEST_DATABASE_URL is not set.
+var testDatabaseURL string
+
+func TestMain(m *testing.M) {
+	if databaseURL := os.Getenv("TEST_DATABASE_URL"); databaseURL != "" {
+		if err := migrateDatabase(databaseURL); err != nil {
+			fmt.Fprintf(os.Stderr, "Unable to migrate the integration test database: %v\n", err)
+			os.Exit(1)
+		}
+		testDatabaseURL = databaseURL
+	}
+
+	os.Exit(m.Run())
+}
+
+func migrateDatabase(databaseURL string) error {
+	db, err := sql.Open("postgres", databaseURL)
+	if err != nil {
+		return err
+	}
+	defer db.Close()
+
+	return database.Migrate(db)
+}
+
+// openTestDatabase connects to the database migrated by TestMain, or skips the test when TEST_DATABASE_URL is not set.
+func openTestDatabase(t *testing.T) *sql.DB {
+	t.Helper()
+
+	if testDatabaseURL == "" {
+		t.Skip(skipIntegrationTestsMessage)
+	}
+
+	db, err := sql.Open("postgres", testDatabaseURL)
+	if err != nil {
+		t.Fatal(err)
+	}
+	t.Cleanup(func() { db.Close() })
+	return db
+}