Files
ntfy/message/cache_sqlite_internal_test.go
T
binwiederhier ee6db15589 Move the query plan tests next to the queries they test
cache_postgres_internal_test.go and cache_sqlite_internal_test.go, the internal companions of
cache_postgres_test.go and cache_sqlite_test.go: the query constants are unexported.
2026-10-16 15:31:45 +00:00

38 lines
1.4 KiB
Go

package message
import (
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// See TestPostgresQueries_SinceIDUsesOnlyIndexConditions
func TestSQLiteQueries_SinceIDUsesOnlyIndexConditions(t *testing.T) {
c, err := NewSQLiteStore(filepath.Join(t.TempDir(), "cache.db"), "", 0, 0, 0, false)
require.Nil(t, err)
defer c.Close()
for name, query := range map[string]string{
"published": sqliteQueries.selectMessagesSinceID,
"include scheduled": sqliteQueries.selectMessagesSinceIDScheduled,
} {
rows, err := c.db.Primary().Query("EXPLAIN QUERY PLAN "+query, "mytopic", 1000, 1792160000)
require.Nil(t, err)
plan := make([]string, 0)
for rows.Next() {
var id, parent, notUsed int
var detail string
require.Nil(t, rows.Scan(&id, &parent, &notUsed, &detail))
plan = append(plan, detail)
}
require.Nil(t, rows.Close())
for _, step := range plan {
// "(topic=?)" alone reads every row of the topic, and "rowid<?" every row written before the marker
require.False(t, strings.HasPrefix(step, "SCAN messages"), "%s: full table scan:\n%s", name, strings.Join(plan, "\n"))
require.False(t, strings.HasSuffix(step, "(topic=?)"), "%s: reads the whole topic:\n%s", name, strings.Join(plan, "\n"))
require.False(t, strings.HasSuffix(step, "(topic=? AND rowid<?)"), "%s: reads the topic's history:\n%s", name, strings.Join(plan, "\n"))
}
}
}