ntfy/server/cache_sqlite_test.go

96 lines
2.3 KiB
Go
Raw Normal View History

2021-12-07 19:45:15 +03:00
package server
import (
2021-12-09 07:24:09 +03:00
"database/sql"
"fmt"
"github.com/stretchr/testify/assert"
2021-12-07 19:45:15 +03:00
"path/filepath"
"testing"
2021-12-09 07:24:09 +03:00
"time"
2021-12-07 19:45:15 +03:00
)
func TestSqliteCache_Messages(t *testing.T) {
2021-12-07 19:45:15 +03:00
testCacheMessages(t, newSqliteTestCache(t))
}
func TestSqliteCache_MessagesScheduled(t *testing.T) {
testCacheMessagesScheduled(t, newSqliteTestCache(t))
}
2021-12-09 06:57:31 +03:00
func TestSqliteCache_Topics(t *testing.T) {
testCacheTopics(t, newSqliteTestCache(t))
}
2021-12-07 19:45:15 +03:00
func TestSqliteCache_MessagesTagsPrioAndTitle(t *testing.T) {
testCacheMessagesTagsPrioAndTitle(t, newSqliteTestCache(t))
}
2021-12-09 06:57:31 +03:00
func TestSqliteCache_Prune(t *testing.T) {
testCachePrune(t, newSqliteTestCache(t))
}
func TestSqliteCache_Migration_From0(t *testing.T) {
2021-12-09 07:24:09 +03:00
filename := newSqliteTestCacheFile(t)
db, err := sql.Open("sqlite3", filename)
assert.Nil(t, err)
// Create "version 0" schema
_, err = db.Exec(`
BEGIN;
CREATE TABLE IF NOT EXISTS messages (
id VARCHAR(20) PRIMARY KEY,
time INT NOT NULL,
topic VARCHAR(64) NOT NULL,
message VARCHAR(1024) NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic);
COMMIT;
`)
assert.Nil(t, err)
// Insert a bunch of messages
for i := 0; i < 10; i++ {
_, err = db.Exec(`INSERT INTO messages (id, time, topic, message) VALUES (?, ?, ?, ?)`,
fmt.Sprintf("abcd%d", i), time.Now().Unix(), "mytopic", fmt.Sprintf("some message %d", i))
assert.Nil(t, err)
}
// Create cache to trigger migration
c := newSqliteTestCacheFromFile(t, filename)
messages, err := c.Messages("mytopic", sinceAllMessages, false)
2021-12-09 07:24:09 +03:00
assert.Nil(t, err)
assert.Equal(t, 10, len(messages))
assert.Equal(t, "some message 5", messages[5].Message)
assert.Equal(t, "", messages[5].Title)
assert.Nil(t, messages[5].Tags)
assert.Equal(t, 0, messages[5].Priority)
rows, err := c.db.Query(`SELECT version FROM schemaVersion`)
assert.Nil(t, err)
assert.True(t, rows.Next())
var schemaVersion int
assert.Nil(t, rows.Scan(&schemaVersion))
assert.Equal(t, 2, schemaVersion)
2021-12-09 07:24:09 +03:00
}
func newSqliteTestCache(t *testing.T) *sqliteCache {
c, err := newSqliteCache(newSqliteTestCacheFile(t))
if err != nil {
t.Fatal(err)
}
return c
}
func newSqliteTestCacheFile(t *testing.T) string {
return filepath.Join(t.TempDir(), "cache.db")
}
func newSqliteTestCacheFromFile(t *testing.T, filename string) *sqliteCache {
2021-12-07 19:45:15 +03:00
c, err := newSqliteCache(filename)
if err != nil {
t.Fatal(err)
}
return c
}