2022-10-12 05:47:38 +08:00
|
|
|
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{
|
2022-10-14 01:18:40 +08:00
|
|
|
"testCreateBookmark": testCreateBookmark,
|
|
|
|
"testCreateBookmarkTwice": testCreateBookmarkTwice,
|
|
|
|
"testCreateBookmarkWithTag": testCreateBookmarkWithTag,
|
|
|
|
"testUpdateBookmark": testUpdateBookmark,
|
2022-10-15 17:33:29 +08:00
|
|
|
"testGetBookmarks": testGetBookmarks,
|
|
|
|
"testGetBookmarksCount": testGetBookmarksCount,
|
|
|
|
"testCreateTwoDifferentBookmarks": testCreateTwoDifferentBookmarks,
|
2022-10-12 05:47:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-10-14 01:18:40 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-10-12 05:47:38 +08:00
|
|
|
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)
|
|
|
|
}
|
2022-10-15 17:33:29 +08:00
|
|
|
|
|
|
|
func testGetBookmarks(t *testing.T, db DB) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
book := model.Bookmark{
|
|
|
|
URL: "https://github.com/go-shiori/shiori",
|
|
|
|
Title: "shiori",
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmarks, err := db.SaveBookmarks(ctx, true, book)
|
|
|
|
assert.NoError(t, err, "Save bookmarks must not fail")
|
|
|
|
|
|
|
|
savedBookmark := bookmarks[0]
|
|
|
|
|
|
|
|
results, err := db.GetBookmarks(ctx, GetBookmarksOptions{
|
|
|
|
Keyword: "go-shiori",
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err, "Get bookmarks should not fail")
|
|
|
|
assert.Len(t, results, 1, "results should contain one item")
|
|
|
|
assert.Equal(t, savedBookmark.ID, results[0].ID, "bookmark should be the one saved")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetBookmarksCount(t *testing.T, db DB) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
expectedCount := 1
|
|
|
|
book := model.Bookmark{
|
|
|
|
URL: "https://github.com/go-shiori/shiori",
|
|
|
|
Title: "shiori",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := db.SaveBookmarks(ctx, true, book)
|
|
|
|
assert.NoError(t, err, "Save bookmarks must not fail")
|
|
|
|
|
|
|
|
count, err := db.GetBookmarksCount(ctx, GetBookmarksOptions{
|
|
|
|
Keyword: "go-shiori",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err, "Get bookmarks count should not fail")
|
|
|
|
assert.Equal(t, count, expectedCount, "count should be %d", expectedCount)
|
|
|
|
}
|