Consolidate SQLite and Postgres store implementations into a single commonStore with database-specific SQL queries passed via configuration. This eliminates ~100 lines of duplicate code while maintaining full functionality for both backends. Key changes: - Move all store methods to commonStore in store.go - Remove sqliteStore and postgresStore wrapper structs - Refactor SQLite to use QueryRow() pattern like Postgres - Pass database-specific queries via storeQueries struct - Make store types unexported, only expose Store interface All tests pass for both SQLite and PostgreSQL backends.
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package webpush_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"heckel.io/ntfy/v2/util"
|
|
"heckel.io/ntfy/v2/webpush"
|
|
)
|
|
|
|
func newTestPostgresStore(t *testing.T) webpush.Store {
|
|
dsn := os.Getenv("NTFY_TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
t.Skip("NTFY_TEST_DATABASE_URL not set, skipping PostgreSQL tests")
|
|
}
|
|
// Create a unique schema for this test
|
|
schema := fmt.Sprintf("test_%s", util.RandomString(10))
|
|
setupDB, err := sql.Open("pgx", dsn)
|
|
require.Nil(t, err)
|
|
_, err = setupDB.Exec(fmt.Sprintf("CREATE SCHEMA %s", schema))
|
|
require.Nil(t, err)
|
|
require.Nil(t, setupDB.Close())
|
|
// Open store with search_path set to the new schema
|
|
u, err := url.Parse(dsn)
|
|
require.Nil(t, err)
|
|
q := u.Query()
|
|
q.Set("search_path", schema)
|
|
u.RawQuery = q.Encode()
|
|
store, err := webpush.NewPostgresStore(u.String())
|
|
require.Nil(t, err)
|
|
t.Cleanup(func() {
|
|
store.Close()
|
|
cleanDB, err := sql.Open("pgx", dsn)
|
|
if err == nil {
|
|
cleanDB.Exec(fmt.Sprintf("DROP SCHEMA %s CASCADE", schema))
|
|
cleanDB.Close()
|
|
}
|
|
})
|
|
return store
|
|
}
|
|
|
|
func TestPostgresStoreUpsertSubscriptionSubscriptionsForTopic(t *testing.T) {
|
|
testStoreUpsertSubscriptionSubscriptionsForTopic(t, newTestPostgresStore(t))
|
|
}
|
|
|
|
func TestPostgresStoreUpsertSubscriptionSubscriberIPLimitReached(t *testing.T) {
|
|
testStoreUpsertSubscriptionSubscriberIPLimitReached(t, newTestPostgresStore(t))
|
|
}
|
|
|
|
func TestPostgresStoreUpsertSubscriptionUpdateTopics(t *testing.T) {
|
|
testStoreUpsertSubscriptionUpdateTopics(t, newTestPostgresStore(t))
|
|
}
|
|
|
|
func TestPostgresStoreUpsertSubscriptionUpdateFields(t *testing.T) {
|
|
testStoreUpsertSubscriptionUpdateFields(t, newTestPostgresStore(t))
|
|
}
|
|
|
|
func TestPostgresStoreRemoveByUserIDMultiple(t *testing.T) {
|
|
testStoreRemoveByUserIDMultiple(t, newTestPostgresStore(t))
|
|
}
|
|
|
|
func TestPostgresStoreRemoveByEndpoint(t *testing.T) {
|
|
testStoreRemoveByEndpoint(t, newTestPostgresStore(t))
|
|
}
|
|
|
|
func TestPostgresStoreRemoveByUserID(t *testing.T) {
|
|
testStoreRemoveByUserID(t, newTestPostgresStore(t))
|
|
}
|
|
|
|
func TestPostgresStoreRemoveByUserIDEmpty(t *testing.T) {
|
|
testStoreRemoveByUserIDEmpty(t, newTestPostgresStore(t))
|
|
}
|
|
|
|
func TestPostgresStoreExpiryWarningSent(t *testing.T) {
|
|
store := newTestPostgresStore(t)
|
|
testStoreExpiryWarningSent(t, store, store.SetSubscriptionUpdatedAt)
|
|
}
|
|
|
|
func TestPostgresStoreExpiring(t *testing.T) {
|
|
store := newTestPostgresStore(t)
|
|
testStoreExpiring(t, store, store.SetSubscriptionUpdatedAt)
|
|
}
|
|
|
|
func TestPostgresStoreRemoveExpired(t *testing.T) {
|
|
store := newTestPostgresStore(t)
|
|
testStoreRemoveExpired(t, store, store.SetSubscriptionUpdatedAt)
|
|
}
|