status-go/multiaccounts/settings_social_links/database_test.go

109 lines
2.6 KiB
Go
Raw Normal View History

2022-08-02 12:56:26 +00:00
package sociallinkssettings
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/identity"
)
func openTestDB(t *testing.T) (*SocialLinksSettings, func()) {
db, stop, err := appdatabase.SetupTestSQLDB("settings-social-links-tests-")
if err != nil {
require.NoError(t, stop())
}
require.NoError(t, err)
socialLinkSettings := NewSocialLinksSettings(db)
if err != nil {
require.NoError(t, stop())
}
require.NoError(t, err)
return socialLinkSettings, func() {
require.NoError(t, stop())
}
}
func socialLinksWithDefaults(twitter, personalSite, github, youtube, discord, telegram string) identity.SocialLinks {
return identity.SocialLinks{
{
Text: identity.TwitterID,
URL: twitter,
},
{
Text: identity.PersonalSiteID,
URL: personalSite,
},
{
Text: identity.GithubID,
URL: github,
},
{
Text: identity.YoutubeID,
URL: youtube,
},
{
Text: identity.DiscordID,
URL: discord,
},
{
Text: identity.TelegramID,
URL: telegram,
},
}
}
func TestDatabase(t *testing.T) {
socialLinkSettings, stop := openTestDB(t)
defer stop()
// fresh database should have default rows
links, err := socialLinkSettings.GetSocialLinks()
require.NoError(t, err)
require.True(t, links.Equals(socialLinksWithDefaults("", "", "", "", "", "")))
// cleaning db should not remove default rows
links = identity.SocialLinks{}
err = socialLinkSettings.SetSocialLinks(&links)
require.NoError(t, err)
links, err = socialLinkSettings.GetSocialLinks()
require.NoError(t, err)
require.True(t, links.Equals(socialLinksWithDefaults("", "", "", "", "", "")))
// custom links
links = identity.SocialLinks{
{
Text: identity.TwitterID,
URL: "Status_ico",
},
{
Text: "customLink",
URL: "customLink.com",
},
{
Text: identity.TelegramID,
URL: "dummy.telegram",
},
}
err = socialLinkSettings.SetSocialLinks(&links)
require.NoError(t, err)
expected := socialLinksWithDefaults("Status_ico", "", "", "", "", "dummy.telegram")
expected = append(expected, identity.SocialLink{Text: "customLink", URL: "customLink.com"})
links, err = socialLinkSettings.GetSocialLinks()
require.NoError(t, err)
require.True(t, links.Equals(expected))
// cleaning database with defaults should remove custom links
links = socialLinksWithDefaults("", "", "", "", "", "")
err = socialLinkSettings.SetSocialLinks(&links)
require.NoError(t, err)
links, err = socialLinkSettings.GetSocialLinks()
require.NoError(t, err)
require.True(t, links.Equals(socialLinksWithDefaults("", "", "", "", "", "")))
}