Ivan Belyakov 2df9df10ab fix(tests): moved test db setup to a common place 't/helpers', created
interface for initializing db, which is implemented for appdatabase and
walletdatabase. TBD for multiaccounts DB.
Unified DB initializion for all tests using helpers and new interface.
Reduced sqlcipher kdf iterations for all tests to 1.
2023-08-18 09:00:56 +02:00

79 lines
1.8 KiB
Go

package browsers
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/t/helpers"
)
func setupTestDB(t *testing.T) (*Database, func()) {
db, cleanup, err := helpers.SetupTestSQLDB(appdatabase.DbInitializer{}, "browsers-tests")
require.NoError(t, err)
return NewDB(db), func() { require.NoError(t, cleanup()) }
}
func setupTestAPI(t *testing.T) (*API, func()) {
db, cancel := setupTestDB(t)
return &API{db: db}, cancel
}
func TestBookmarks(t *testing.T) {
api, cancel := setupTestAPI(t)
defer cancel()
bookmark := &Bookmark{
Name: "MyBookmark",
URL: "https://status.im",
ImageURL: "",
}
_, err := api.StoreBookmark(context.TODO(), *bookmark)
require.NoError(t, err)
rst, err := api.GetBookmarks(context.TODO())
require.NoError(t, err)
require.Len(t, rst, 1)
err = api.RemoveBookmark(context.TODO(), bookmark.URL)
require.NoError(t, err)
rst, err = api.GetBookmarks(context.TODO())
require.NoError(t, err)
require.Len(t, rst, 1)
require.Equal(t, rst[0].Removed, true)
require.NoError(t, api.DeleteBookmark(context.TODO(), bookmark.URL))
rst, err = api.GetBookmarks(context.TODO())
require.NoError(t, err)
require.Len(t, rst, 0)
}
func TestShouldSyncBookmark(t *testing.T) {
api, cancel := setupTestAPI(t)
defer cancel()
bookmark := &Bookmark{
Name: "MyBookmark",
URL: "https://status.im",
ImageURL: "",
Clock: 1,
}
_, err := api.StoreBookmark(context.TODO(), *bookmark)
require.NoError(t, err)
bookmark.Clock = 2
shouldSync, err := api.db.shouldSyncBookmark(bookmark, nil)
require.NoError(t, err)
require.True(t, shouldSync)
bookmark.Clock = 0
shouldSync, err = api.db.shouldSyncBookmark(bookmark, nil)
require.NoError(t, err)
require.False(t, shouldSync)
}