Skip to content

Commit

Permalink
chore: added chain types
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Dec 21, 2023
1 parent 390380b commit 8c13ae8
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 50 deletions.
127 changes: 127 additions & 0 deletions pkg/types/chain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package types

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestWalletAddressOrAliasWithoutAlias(t *testing.T) {
t.Parallel()

wallet := Wallet{Address: "test"}
assert.Equal(t, "test", wallet.AddressOrAlias(), "Wrong value!")
}

func TestWalletAddressOrAliasWithAlias(t *testing.T) {
t.Parallel()

wallet := Wallet{Address: "test", Alias: "alias"}
assert.Equal(t, "alias", wallet.AddressOrAlias(), "Wrong value!")
}

func TestFindChainByNameIfPresent(t *testing.T) {
t.Parallel()

chains := Chains{
{Name: "chain1"},
{Name: "chain2"},
}

chain := chains.FindByName("chain2")
assert.NotNil(t, chain, "Chain should be presented!")
}

func TestFindChainByNameIfNotPresent(t *testing.T) {
t.Parallel()

chains := Chains{
{Name: "chain1"},
{Name: "chain2"},
}

chain := chains.FindByName("chain3")
assert.Nil(t, chain, "Chain should not be presented!")
}

func TestGetLinksEmpty(t *testing.T) {
t.Parallel()

chain := Chain{}
links := chain.GetExplorerProposalsLinks("test")

assert.Empty(t, links, "Expected 0 links")
}

func TestGetLinksPresent(t *testing.T) {
t.Parallel()

chain := Chain{
KeplrName: "chain",
Explorer: &Explorer{
ProposalLinkPattern: "example.com/proposal/%s",
},
}
links := chain.GetExplorerProposalsLinks("test")

assert.Len(t, links, 2, "Expected 2 links")
assert.Equal(t, "Keplr", links[0].Name, "Expected Keplr link")
assert.Equal(t, "https://wallet.keplr.app/#/chain/governance?detailId=test", links[0].Href, "Wrong Keplr link")
assert.Equal(t, "Explorer", links[1].Name, "Expected Explorer link")
assert.Equal(t, "example.com/proposal/test", links[1].Href, "Wrong explorer link")
}

func TestGetExplorerProposalLinkWithoutExplorer(t *testing.T) {
t.Parallel()

chain := Chain{}
proposal := Proposal{ID: "ID", Title: "Title"}
link := chain.GetProposalLink(proposal)

assert.Equal(t, "Title", link.Name, "Wrong value!")
assert.Equal(t, "", link.Href, "Wrong value!")
}

func TestGetExplorerProposalLinkWithExplorer(t *testing.T) {
t.Parallel()

chain := Chain{Explorer: &Explorer{ProposalLinkPattern: "example.com/%s"}}
proposal := Proposal{ID: "ID", Title: "Title"}
link := chain.GetProposalLink(proposal)

assert.Equal(t, "Title", link.Name, "Wrong value!")
assert.Equal(t, "example.com/ID", link.Href, "Wrong value!")
}

func TestGetWalletLinkWithoutExplorer(t *testing.T) {
t.Parallel()

chain := Chain{}
wallet := &Wallet{Address: "wallet"}
link := chain.GetWalletLink(wallet)

assert.Equal(t, "wallet", link.Name, "Wrong value!")
assert.Equal(t, "", link.Href, "Wrong value!")
}

func TestGetWalletLinkWithoutAlias(t *testing.T) {
t.Parallel()

chain := Chain{Explorer: &Explorer{WalletLinkPattern: "example.com/%s"}}
wallet := &Wallet{Address: "wallet"}
link := chain.GetWalletLink(wallet)

assert.Equal(t, "wallet", link.Name, "Wrong value!")
assert.Equal(t, "example.com/wallet", link.Href, "Wrong value!")
}

func TestGetWalletLinkWithAlias(t *testing.T) {
t.Parallel()

chain := Chain{Explorer: &Explorer{WalletLinkPattern: "example.com/%s"}}
wallet := &Wallet{Address: "wallet", Alias: "alias"}
link := chain.GetWalletLink(wallet)

assert.Equal(t, "alias", link.Name, "Wrong value!")
assert.Equal(t, "example.com/wallet", link.Href, "Wrong value!")
}
63 changes: 15 additions & 48 deletions pkg/types/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestValidateConfigInvalidTimezone(t *testing.T) {
require.Error(t, err, nil, "Error should be presented!")
}

func TestValidateConfigValidChain(t *testing.T) {
func TestValidateConfigInvalidWallet(t *testing.T) {
t.Parallel()

config := Config{
Expand All @@ -150,62 +150,29 @@ func TestValidateConfigValidChain(t *testing.T) {
{
Name: "chain",
LCDEndpoints: []string{"endpoint"},
Wallets: []*Wallet{{Address: "wallet"}},
Wallets: []*Wallet{{Address: ""}},
ProposalsType: "v1",
},
},
}
err := config.Validate()
require.NoError(t, err, "Error should not be presented!")
}

func TestFindChainByNameIfPresent(t *testing.T) {
t.Parallel()

chains := Chains{
{Name: "chain1"},
{Name: "chain2"},
}

chain := chains.FindByName("chain2")
assert.NotNil(t, chain, "Chain should be presented!")
}

func TestFindChainByNameIfNotPresent(t *testing.T) {
t.Parallel()

chains := Chains{
{Name: "chain1"},
{Name: "chain2"},
}

chain := chains.FindByName("chain3")
assert.Nil(t, chain, "Chain should not be presented!")
}

func TestGetLinksEmpty(t *testing.T) {
t.Parallel()

chain := Chain{}
links := chain.GetExplorerProposalsLinks("test")

assert.Empty(t, links, "Expected 0 links")
require.Error(t, err, nil, "Error should be presented!")
}

func TestGetLinksPresent(t *testing.T) {
func TestValidateConfigValidChain(t *testing.T) {
t.Parallel()

chain := Chain{
KeplrName: "chain",
Explorer: &Explorer{
ProposalLinkPattern: "example.com/proposal/%s",
config := Config{
Timezone: "Europe/Moscow",
Chains: []*Chain{
{
Name: "chain",
LCDEndpoints: []string{"endpoint"},
Wallets: []*Wallet{{Address: "wallet"}},
ProposalsType: "v1",
},
},
}
links := chain.GetExplorerProposalsLinks("test")

assert.Len(t, links, 2, "Expected 2 links")
assert.Equal(t, "Keplr", links[0].Name, "Expected Keplr link")
assert.Equal(t, "https://wallet.keplr.app/#/chain/governance?detailId=test", links[0].Href, "Wrong Keplr link")
assert.Equal(t, "Explorer", links[1].Name, "Expected Explorer link")
assert.Equal(t, "example.com/proposal/test", links[1].Href, "Wrong explorer link")
err := config.Validate()
require.NoError(t, err, "Error should not be presented!")
}
3 changes: 2 additions & 1 deletion pkg/types/proposal_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types

import (
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestProposalGetVotingTime(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/types/types_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package types

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSerializeLinkWithoutHref(t *testing.T) {
Expand Down

0 comments on commit 8c13ae8

Please sign in to comment.