Skip to content

Commit

Permalink
Added a migration test on a real state
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed May 8, 2020
1 parent 4e708cf commit 5f84b18
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion x/posts/legacy/v0.5.0/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Migrate(oldGenState v040posts.GenesisState) v040posts.GenesisState {
}

// GetReactionsToRegister takes the list of reactions that were registered in v0.4.0 and
//returns a new list without the registered Unicode emojis.
// returns a new list without the registered Unicode emojis.
func GetReactionsToRegister(oldRegisteredReactions []v040posts.Reaction) (reactionsToRegister []v040posts.Reaction) {
for _, reaction := range oldRegisteredReactions {
if _, err := emoji.LookupEmojiByCode(reaction.ShortCode); err != nil {
Expand Down
51 changes: 48 additions & 3 deletions x/posts/legacy/v0.5.0/migrate_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package v050_test

import (
"encoding/json"
"io/ioutil"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
emoji "github.com/desmos-labs/Go-Emoji-Utils"
v040posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.4.0"
v050 "github.com/desmos-labs/desmos/x/posts/legacy/v0.5.0"
v050posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.5.0"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -125,7 +128,7 @@ func TestMigrate(t *testing.T) {
},
}

migrated := v050.Migrate(v040GenState)
migrated := v050posts.Migrate(v040GenState)

// Check for posts
require.Len(t, migrated.Posts, len(expected.Posts))
Expand Down Expand Up @@ -182,7 +185,49 @@ func TestGetReactionsToRegister(t *testing.T) {
},
}

actual := v050.GetReactionsToRegister(registeredReactions)
actual := v050posts.GetReactionsToRegister(registeredReactions)

require.Equal(t, expected, actual)
}

func TestMigrate040(t *testing.T) {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount("desmos", "desmos"+sdk.PrefixPublic)
config.Seal()

content, err := ioutil.ReadFile("v040state.json")
require.NoError(t, err)

var v040state v040posts.GenesisState
err = json.Unmarshal(content, &v040state)
require.NoError(t, err)

v050state := v050posts.Migrate(v040state)
for _, reaction := range v050state.RegisteredReactions {
// Make sure each reaction shrotcode does not represent an emoji
_, err := emoji.LookupEmojiByCode(reaction.ShortCode)
require.Error(t, err)

// Make sure no reaction value is an emoji
_, err = emoji.LookupEmoji(reaction.Value)
require.Error(t, err)
}

// Make sure the posts are all the same
require.Equal(t, len(v050state.Posts), len(v040state.Posts))
for index, post := range v050state.Posts {
require.Equal(t, post, v040state.Posts[index])
}

// Make sure the reactions are all the same
require.Equal(t, len(v050state.PostReactions), len(v040state.PostReactions))
for index, postReaction := range v050state.PostReactions {
require.Equal(t, postReaction, v040state.PostReactions[index])
}

// Make sure the poll answers are all the same
require.Equal(t, len(v050state.UsersPollAnswers), len(v040state.UsersPollAnswers))
for index, answer := range v050state.UsersPollAnswers {
require.Equal(t, answer, v040state.UsersPollAnswers[index])
}
}
1 change: 1 addition & 0 deletions x/posts/legacy/v0.5.0/v040state.json

Large diffs are not rendered by default.

0 comments on commit 5f84b18

Please sign in to comment.