storage: test postgres via docker

This commit is contained in:
nkanaev
2026-06-23 13:44:10 +01:00
parent 14b06dcbaf
commit 21c7f9a4a4
3 changed files with 87 additions and 60 deletions

4
go.mod
View File

@@ -1,8 +1,6 @@
module github.com/nkanaev/yarr module github.com/nkanaev/yarr
go 1.23.0 go 1.25.0
toolchain go1.23.5
require ( require (
fyne.io/systray v1.12.0 fyne.io/systray v1.12.0

View File

@@ -291,14 +291,12 @@ func TestListItemsPaginated(t *testing.T) {
}) })
} }
func TestMarkItemsRead(t *testing.T) { func TestMarkAllItemsRead(t *testing.T) {
// NOTE: starred items must not be marked as read
var read model.ItemStatus = model.READ var read model.ItemStatus = model.READ
dbtest(t, func(t *testing.T, db storage.Storage) {
dbtest(t, func(t *testing.T, db1 storage.Storage) { testItemsSetup(db)
testItemsSetup(db1) db.MarkItemsRead(model.MarkFilter{})
db1.MarkItemsRead(model.MarkFilter{}) have := getItemGuids(db.ListItems(model.ItemFilter{Status: &read}, 10, false, false))
have := getItemGuids(db1.ListItems(model.ItemFilter{Status: &read}, 10, false, false))
want := []string{ want := []string{
"item111", "item112", "item121", "item122", "item111", "item112", "item121", "item122",
"item211", "item011", "item012", "item211", "item011", "item012",
@@ -309,11 +307,14 @@ func TestMarkItemsRead(t *testing.T) {
t.Fail() t.Fail()
} }
}) })
}
dbtest(t, func(t *testing.T, db2 storage.Storage) { func TestMarkItemsReadByFolder(t *testing.T) {
scope2 := testItemsSetup(db2) var read model.ItemStatus = model.READ
db2.MarkItemsRead(model.MarkFilter{FolderID: &scope2.folder1.Id}) dbtest(t, func(t *testing.T, db storage.Storage) {
have := getItemGuids(db2.ListItems(model.ItemFilter{Status: &read}, 10, false, false)) scope := testItemsSetup(db)
db.MarkItemsRead(model.MarkFilter{FolderID: &scope.folder1.Id})
have := getItemGuids(db.ListItems(model.ItemFilter{Status: &read}, 10, false, false))
want := []string{ want := []string{
"item111", "item112", "item121", "item122", "item111", "item112", "item121", "item122",
"item211", "item012", "item211", "item012",
@@ -324,11 +325,14 @@ func TestMarkItemsRead(t *testing.T) {
t.Fail() t.Fail()
} }
}) })
}
dbtest(t, func(t *testing.T, db3 storage.Storage) { func TestMarkItemsReadByFeed(t *testing.T) {
scope3 := testItemsSetup(db3) var read model.ItemStatus = model.READ
db3.MarkItemsRead(model.MarkFilter{FeedID: &scope3.feed11.Id}) dbtest(t, func(t *testing.T, db storage.Storage) {
have := getItemGuids(db3.ListItems(model.ItemFilter{Status: &read}, 10, false, false)) scope := testItemsSetup(db)
db.MarkItemsRead(model.MarkFilter{FeedID: &scope.feed11.Id})
have := getItemGuids(db.ListItems(model.ItemFilter{Status: &read}, 10, false, false))
want := []string{ want := []string{
"item111", "item112", "item122", "item111", "item112", "item122",
"item211", "item012", "item211", "item012",

View File

@@ -1,30 +1,32 @@
package tests package tests
import ( import (
"crypto/rand" "context"
"crypto/sha256"
"database/sql" "database/sql"
"encoding/hex"
"fmt" "fmt"
"net/url"
"os" "os"
"os/exec"
"strings"
"testing" "testing"
"time"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/nkanaev/yarr/src/storage" "github.com/nkanaev/yarr/src/storage"
) )
func dbtest(t *testing.T, testcase func(t *testing.T, db storage.Storage)) { func dbtest(t *testing.T, testcase func(t *testing.T, db storage.Storage)) {
t.Parallel()
testurls := map[string]string{ testurls := map[string]string{
"sqlite": ":memory:", "sqlite": ":memory:",
} }
if pgUrl := os.Getenv("YARR_POSTGRES_TEST_URL"); pgUrl != "" { if pgImage := os.Getenv("YARR_POSTGRES_TEST_IMAGE"); pgImage != "" {
dburl, cleanup, err := createPostgresDB(pgUrl) dburl, cleanup := startPostgresContainer(t, pgImage)
if err != nil {
t.Fatalf("failed to create postgres test database: %v", err)
}
t.Cleanup(cleanup) t.Cleanup(cleanup)
testurls["postgres"] = dburl testurls["postgres"] = dburl
} else if !testing.Short() {
t.Fatalf("YARR_POSTGRES_TEST_IMAGE not set; use -short to skip docker tests")
} }
for testname, url := range testurls { for testname, url := range testurls {
@@ -38,49 +40,72 @@ func dbtest(t *testing.T, testcase func(t *testing.T, db storage.Storage)) {
} }
} }
func createPostgresDB(pgUrl string) (string, func(), error) { func startPostgresContainer(t *testing.T, image string) (string, func()) {
u, err := url.Parse(pgUrl) // database credentials
dbUser := "testuser"
dbPass := "password"
dbName := "yarrtest"
// generate unique container name
testHash := sha256.Sum256([]byte(t.Name()))
containerName := fmt.Sprintf("yarr-test-pg-%x-%d", testHash[:8], time.Now().UnixNano())
cmd := exec.Command(
"docker", "run", "-d", "--rm",
"--name", containerName,
"-p", "0:5432",
"-e", "POSTGRES_USER="+dbUser,
"-e", "POSTGRES_PASSWORD="+dbPass,
"-e", "POSTGRES_DB="+dbName,
image,
)
out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return "", nil, err t.Fatalf("failed to start postgres container: %v\n%s", err, string(out))
} }
u.Path = "/postgres" // retrieve the host port assigned by docker
adminConnStr := u.String() portCmd := exec.Command("docker", "port", containerName, "5432/tcp")
portOut, err := portCmd.Output()
adminDB, err := sql.Open("postgres", adminConnStr)
if err != nil { if err != nil {
return "", nil, fmt.Errorf("admin connect: %w", err) t.Fatalf("failed to get container port: %v", err)
} }
parts := strings.Split(strings.TrimSpace(string(portOut)), ":")
dbPort := parts[len(parts)-1]
b := make([]byte, 4) // build connection string
if _, err := rand.Read(b); err != nil { pgUrl := fmt.Sprintf(
adminDB.Close() "postgres://%s:%s@localhost:%s/%s?sslmode=disable",
return "", nil, fmt.Errorf("generate suffix: %w", err) dbUser,
} dbPass,
dbPort,
dbName,
)
testDBName := "yarr_test_" + hex.EncodeToString(b) // wait up to 15 seconds for the container to accept connections
deadline := time.Now().Add(15 * time.Second)
if _, err := adminDB.Exec(fmt.Sprintf(`CREATE DATABASE "%s"`, testDBName)); err != nil { for time.Now().Before(deadline) {
adminDB.Close() db, err := sql.Open("postgres", pgUrl)
return "", nil, fmt.Errorf("create database: %w", err)
}
adminDB.Close()
u.Path = "/" + testDBName
testURL := u.String()
cleanup := func() {
dropDB, err := sql.Open("postgres", adminConnStr)
if err != nil { if err != nil {
return continue
} }
defer dropDB.Close() ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
dropDB.Exec(fmt.Sprintf( err = db.PingContext(ctx)
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '%s' AND pid <> pg_backend_pid()`, cancel()
testDBName, db.Close()
)) if err == nil {
dropDB.Exec(fmt.Sprintf(`DROP DATABASE IF EXISTS "%s"`, testDBName)) goto ready
}
time.Sleep(200 * time.Millisecond)
} }
t.Fatalf("timed out waiting for postgres container to be ready")
return testURL, cleanup, nil ready:
// return connection url and a cleanup function that stops the container
return pgUrl, func() {
stop := exec.Command("docker", "stop", containerName)
if err := stop.Run(); err != nil {
t.Logf("failed to stop container %s: %v", containerName, err)
}
}
} }