status-go/services/connector/database/persistence_test.go

96 lines
2.1 KiB
Go

package persistence
import (
"testing"
"database/sql"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/walletdatabase"
"github.com/stretchr/testify/require"
)
var testDApp = DApp{
Name: "Test DApp",
URL: "https://test-dapp-url.com",
IconURL: "https://test-dapp-icon-url.com",
SharedAccount: types.HexToAddress("0x1234567890"),
ChainID: 0x1,
}
func setupTestDB(t *testing.T) (db *sql.DB, close func()) {
db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
require.NoError(t, err)
return db, func() {
require.NoError(t, db.Close())
}
}
func TestInsertAndSelectDApp(t *testing.T) {
db, close := setupTestDB(t)
defer close()
err := UpsertDApp(db, &testDApp)
require.NoError(t, err)
dAppBack, err := SelectDAppByUrl(db, testDApp.URL)
require.NoError(t, err)
require.Equal(t, &testDApp, dAppBack)
}
func TestInsertAndUpdateDApp(t *testing.T) {
db, close := setupTestDB(t)
defer close()
err := UpsertDApp(db, &testDApp)
require.NoError(t, err)
updatedDApp := DApp{
Name: "Updated Test DApp",
URL: testDApp.URL,
IconURL: "https://updated-test-dapp-icon-url.com",
}
err = UpsertDApp(db, &updatedDApp)
require.NoError(t, err)
dAppBack, err := SelectDAppByUrl(db, testDApp.URL)
require.NoError(t, err)
require.Equal(t, &updatedDApp, dAppBack)
require.NotEqual(t, &testDApp, dAppBack)
}
func TestInsertAndRemoveDApp(t *testing.T) {
db, close := setupTestDB(t)
defer close()
err := UpsertDApp(db, &testDApp)
require.NoError(t, err)
dAppBack, err := SelectDAppByUrl(db, testDApp.URL)
require.NoError(t, err)
require.Equal(t, &testDApp, dAppBack)
err = DeleteDApp(db, testDApp.URL)
require.NoError(t, err)
dAppBack, err = SelectDAppByUrl(db, testDApp.URL)
require.NoError(t, err)
require.Empty(t, dAppBack)
}
func TestSelectAllDApps(t *testing.T) {
db, close := setupTestDB(t)
defer close()
err := UpsertDApp(db, &testDApp)
require.NoError(t, err)
dApps, err := SelectAllDApps(db)
require.NoError(t, err)
require.Len(t, dApps, 1)
require.Equal(t, testDApp, dApps[0])
}