2023-10-26 06:30:18 +00:00
|
|
|
package community
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupCommunityDataDBTest(t *testing.T) (*DataDB, func()) {
|
|
|
|
db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return NewDataDB(db), func() {
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestCommunityInfo(count int) map[string]thirdparty.CommunityInfo {
|
|
|
|
result := make(map[string]thirdparty.CommunityInfo)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
communityID := fmt.Sprintf("communityid-%d", i)
|
|
|
|
newCommunity := thirdparty.CommunityInfo{
|
|
|
|
CommunityName: fmt.Sprintf("communityname-%d", i),
|
|
|
|
CommunityColor: fmt.Sprintf("communitycolor-%d", i),
|
|
|
|
CommunityImage: fmt.Sprintf("communityimage-%d", i),
|
|
|
|
}
|
|
|
|
result[communityID] = newCommunity
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateCommunityInfo(t *testing.T) {
|
|
|
|
db, cleanup := setupCommunityDataDBTest(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
communityData := generateTestCommunityInfo(10)
|
2023-11-16 04:18:03 +00:00
|
|
|
extraCommunityID := "extra-community-id"
|
|
|
|
|
2023-10-26 06:30:18 +00:00
|
|
|
for communityID, communityInfo := range communityData {
|
2023-11-16 04:18:03 +00:00
|
|
|
communityInfo := communityInfo // Prevent lint warning G601: Implicit memory aliasing in for loop.
|
|
|
|
err := db.SetCommunityInfo(communityID, &communityInfo)
|
2023-10-26 06:30:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2023-11-16 04:18:03 +00:00
|
|
|
err := db.SetCommunityInfo(extraCommunityID, nil)
|
|
|
|
require.NoError(t, err)
|
2023-10-26 06:30:18 +00:00
|
|
|
|
|
|
|
for communityID, communityInfo := range communityData {
|
2023-11-16 04:18:03 +00:00
|
|
|
info, state, err := db.GetCommunityInfo(communityID)
|
2023-10-26 06:30:18 +00:00
|
|
|
require.NoError(t, err)
|
2023-11-16 04:18:03 +00:00
|
|
|
require.Equal(t, communityInfo, *info)
|
|
|
|
require.True(t, state.LastUpdateSuccesful)
|
2023-10-26 06:30:18 +00:00
|
|
|
}
|
2023-11-16 04:18:03 +00:00
|
|
|
info, state, err := db.GetCommunityInfo(extraCommunityID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, info)
|
|
|
|
require.False(t, state.LastUpdateSuccesful)
|
|
|
|
|
|
|
|
randomCommunityID := "random-community-id"
|
|
|
|
info, state, err = db.GetCommunityInfo(randomCommunityID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, info)
|
|
|
|
require.Empty(t, state)
|
2023-10-26 06:30:18 +00:00
|
|
|
}
|