shiori/internal/database/database_test.go
Acelya ed5a3bcbb9
fix: failed to save bookmark: constraint failed: UNIQUE constraint failed: bookmark.url (#515)
After a first bookmark save, all subsequent ones fail because the update
query does not include an identifier and the update is done on all rows.

Introduced by 05fee53bd0
2022-10-13 19:18:40 +02:00

123 lines
3.1 KiB
Go

package database
import (
"context"
"testing"
"github.com/go-shiori/shiori/internal/model"
"github.com/stretchr/testify/assert"
)
type databaseTestCase func(t *testing.T, db DB)
type testDatabaseFactory func(ctx context.Context) (DB, error)
func testDatabase(t *testing.T, dbFactory testDatabaseFactory) {
tests := map[string]databaseTestCase{
"testCreateBookmark": testCreateBookmark,
"testCreateBookmarkTwice": testCreateBookmarkTwice,
"testCreateBookmarkWithTag": testCreateBookmarkWithTag,
"testCreateTwoDifferentBookmarks": testCreateTwoDifferentBookmarks,
"testUpdateBookmark": testUpdateBookmark,
}
for testName, testCase := range tests {
t.Run(testName, func(tInner *testing.T) {
ctx := context.TODO()
db, err := dbFactory(ctx)
assert.NoError(tInner, err, "Error recreating database")
testCase(tInner, db)
})
}
}
func testCreateBookmark(t *testing.T, db DB) {
ctx := context.TODO()
book := model.Bookmark{
URL: "https://github.com/go-shiori/obelisk",
Title: "shiori",
}
result, err := db.SaveBookmarks(ctx, true, book)
assert.NoError(t, err, "Save bookmarks must not fail")
assert.Equal(t, 1, result[0].ID, "Saved bookmark must have an ID set")
}
func testCreateBookmarkWithTag(t *testing.T, db DB) {
ctx := context.TODO()
book := model.Bookmark{
URL: "https://github.com/go-shiori/obelisk",
Title: "shiori",
Tags: []model.Tag{
{
Name: "test-tag",
},
},
}
result, err := db.SaveBookmarks(ctx, true, book)
assert.NoError(t, err, "Save bookmarks must not fail")
assert.Equal(t, book.URL, result[0].URL)
assert.Equal(t, book.Tags[0].Name, result[0].Tags[0].Name)
}
func testCreateBookmarkTwice(t *testing.T, db DB) {
ctx := context.TODO()
book := model.Bookmark{
URL: "https://github.com/go-shiori/shiori",
Title: "shiori",
}
result, err := db.SaveBookmarks(ctx, true, book)
assert.NoError(t, err, "Save bookmarks must not fail")
savedBookmark := result[0]
savedBookmark.Title = "modified"
_, err = db.SaveBookmarks(ctx, true, savedBookmark)
assert.Error(t, err, "Save bookmarks must fail")
}
func testCreateTwoDifferentBookmarks(t *testing.T, db DB) {
ctx := context.TODO()
book := model.Bookmark{
URL: "https://github.com/go-shiori/shiori",
Title: "shiori",
}
_, err := db.SaveBookmarks(ctx, true, book)
assert.NoError(t, err, "Save first bookmark must not fail")
book = model.Bookmark{
URL: "https://github.com/go-shiori/go-readability",
Title: "go-readability",
}
_, err = db.SaveBookmarks(ctx, true, book)
assert.NoError(t, err, "Save second bookmark must not fail")
}
func testUpdateBookmark(t *testing.T, db DB) {
ctx := context.TODO()
book := model.Bookmark{
URL: "https://github.com/go-shiori/shiori",
Title: "shiori",
}
result, err := db.SaveBookmarks(ctx, true, book)
assert.NoError(t, err, "Save bookmarks must not fail")
savedBookmark := result[0]
savedBookmark.Title = "modified"
result, err = db.SaveBookmarks(ctx, false, savedBookmark)
assert.NoError(t, err, "Save bookmarks must not fail")
assert.Equal(t, "modified", result[0].Title)
assert.Equal(t, savedBookmark.ID, result[0].ID)
}