Skip to content

Commit

Permalink
feat: test DataManager + fix errors (#88)
Browse files Browse the repository at this point in the history
* feat: test DataManager + fix errors

* chore: add test on not empty tallies
  • Loading branch information
freak12techno authored May 12, 2024
1 parent 696c541 commit 835d287
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 47 deletions.
38 changes: 23 additions & 15 deletions pkg/data/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ package data

import (
"fmt"
"main/pkg/fetchers"
fetchersPkg "main/pkg/fetchers"
"main/pkg/types"
"sync"

"github.com/rs/zerolog"
)

type Manager struct {
Logger zerolog.Logger
Chains types.Chains
Logger zerolog.Logger
Chains types.Chains
Fetchers []fetchersPkg.Fetcher
}

func NewManager(logger *zerolog.Logger, chains types.Chains) *Manager {
fetchers := make([]fetchersPkg.Fetcher, len(chains))

for index, chain := range chains {
fetchers[index] = fetchersPkg.GetFetcher(chain, logger)
}

return &Manager{
Logger: logger.With().Str("component", "data_manager").Logger(),
Chains: chains,
Logger: logger.With().Str("component", "data_manager").Logger(),
Chains: chains,
Fetchers: fetchers,
}
}

Expand All @@ -28,21 +36,21 @@ func (m *Manager) GetTallies() (map[string]types.ChainTallyInfos, error) {
errors := make([]error, 0)
tallies := make(map[string]types.ChainTallyInfos)

for _, chain := range m.Chains {
fetcher := fetchers.GetFetcher(chain, m.Logger)
for index, chain := range m.Chains {
fetcher := m.Fetchers[index]

wg.Add(1)
go func(c *types.Chain, fetcher fetchers.Fetcher) {
go func(c *types.Chain, fetcher fetchersPkg.Fetcher) {
defer wg.Done()

talliesForChain, err := fetcher.GetTallies()

mutex.Lock()

if err != nil {
m.Logger.Error().Err(err).Str("chain", c.Name).Msg("Error fetching staking pool")
m.Logger.Error().Err(err).Str("chain", c.Name).Msg("Error fetching tallies")
errors = append(errors, err)
} else {
} else if len(talliesForChain.TallyInfos) > 0 {
tallies[c.Name] = talliesForChain
}
mutex.Unlock()
Expand All @@ -66,14 +74,14 @@ func (m *Manager) GetParams() (map[string]types.ChainWithVotingParams, error) {
params := make(map[string]types.ChainWithVotingParams)
errors := make([]error, 0)

for _, chain := range m.Chains {
for index := range m.Chains {
fetcher := m.Fetchers[index]

wg.Add(1)

go func(chain *types.Chain) {
go func(fetcher fetchersPkg.Fetcher) {
defer wg.Done()

fetcher := fetchers.GetFetcher(chain, m.Logger)

chainParams, errs := fetcher.GetChainParams()
mutex.Lock()
defer mutex.Unlock()
Expand All @@ -84,7 +92,7 @@ func (m *Manager) GetParams() (map[string]types.ChainWithVotingParams, error) {
}

params[chainParams.Chain.Name] = *chainParams
}(chain)
}(fetcher)
}

wg.Wait()
Expand Down
98 changes: 98 additions & 0 deletions pkg/data/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package data

import (
fetchersPkg "main/pkg/fetchers"
"main/pkg/logger"
"main/pkg/types"
"testing"

"github.com/stretchr/testify/require"

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

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

log := logger.GetNopLogger()
dataManager := NewManager(log, types.Chains{
{Name: "chain"},
})

assert.NotNil(t, dataManager)
}

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

log := logger.GetNopLogger()
dataManager := &Manager{
Logger: *log,
Chains: types.Chains{{Name: "chain"}},
Fetchers: []fetchersPkg.Fetcher{&fetchersPkg.TestFetcher{WithTallyError: true}},
}

tallies, err := dataManager.GetTallies()
require.Error(t, err)
assert.Empty(t, tallies)
}

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

log := logger.GetNopLogger()
dataManager := &Manager{
Logger: *log,
Chains: types.Chains{{Name: "chain"}},
Fetchers: []fetchersPkg.Fetcher{&fetchersPkg.TestFetcher{}},
}

tallies, err := dataManager.GetTallies()
require.NoError(t, err)
assert.Empty(t, tallies)
}

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

log := logger.GetNopLogger()
dataManager := &Manager{
Logger: *log,
Chains: types.Chains{{Name: "chain"}},
Fetchers: []fetchersPkg.Fetcher{&fetchersPkg.TestFetcher{WithTallyNotEmpty: true}},
}

tallies, err := dataManager.GetTallies()
require.NoError(t, err)
assert.NotEmpty(t, tallies)
}

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

log := logger.GetNopLogger()
dataManager := &Manager{
Logger: *log,
Chains: types.Chains{{Name: "chain"}},
Fetchers: []fetchersPkg.Fetcher{&fetchersPkg.TestFetcher{WithParamsError: true}},
}

params, err := dataManager.GetParams()
require.Error(t, err)
assert.Empty(t, params)
}

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

log := logger.GetNopLogger()
dataManager := &Manager{
Logger: *log,
Chains: types.Chains{{Name: "chain"}},
Fetchers: []fetchersPkg.Fetcher{&fetchersPkg.TestFetcher{}},
}

params, err := dataManager.GetParams()
require.NoError(t, err)
assert.NotEmpty(t, params)
}
2 changes: 1 addition & 1 deletion pkg/fetchers/cosmos/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type RPC struct {
Logger zerolog.Logger
}

func NewRPC(chainConfig *types.Chain, logger zerolog.Logger) *RPC {
func NewRPC(chainConfig *types.Chain, logger *zerolog.Logger) *RPC {
return &RPC{
ChainConfig: chainConfig,
ProposalsType: chainConfig.ProposalsType,
Expand Down
6 changes: 5 additions & 1 deletion pkg/fetchers/cosmos/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"main/pkg/fetchers/cosmos/responses"
"main/pkg/types"
"main/pkg/utils"
"sync"

"cosmossdk.io/math"
Expand Down Expand Up @@ -61,7 +62,10 @@ func (rpc *RPC) GetTallies() (types.ChainTallyInfos, error) {
go func() {
defer wg.Done()

chainProposals, _, err := rpc.GetAllProposals(0)
chainProposalsAll, _, err := rpc.GetAllProposals(0)
chainProposals := utils.Filter(chainProposalsAll, func(p types.Proposal) bool {
return p.IsInVoting()
})

mutex.Lock()

Expand Down
2 changes: 1 addition & 1 deletion pkg/fetchers/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Fetcher interface {
GetChainParams() (*types.ChainWithVotingParams, []error)
}

func GetFetcher(chainConfig *types.Chain, logger zerolog.Logger) Fetcher {
func GetFetcher(chainConfig *types.Chain, logger *zerolog.Logger) Fetcher {
if chainConfig.Type == "neutron" {
return neutron.NewFetcher(chainConfig, logger)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fetchers/neutron/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Fetcher struct {
Client *http.Client
}

func NewFetcher(chainConfig *types.Chain, logger zerolog.Logger) *Fetcher {
func NewFetcher(chainConfig *types.Chain, logger *zerolog.Logger) *Fetcher {
return &Fetcher{
ChainConfig: chainConfig,
Logger: logger.With().Str("component", "neutron_fetcher").Logger(),
Expand Down
17 changes: 8 additions & 9 deletions pkg/fetchers/neutron/responses/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package responses

import (
"main/pkg/types"
"main/pkg/utils"
"strconv"
"time"

Expand Down Expand Up @@ -66,18 +65,18 @@ func (p ProposalWithID) ToProposal() (types.Proposal, error) {
}

func (p ProposalsResponse) ToTally() ([]types.TallyInfo, error) {
allProposals := utils.Filter(p.Data.Proposals, func(p ProposalWithID) bool {
return p.Proposal.Status == "open"
})
tallyInfos := make([]types.TallyInfo, 0)

tallyInfos := make([]types.TallyInfo, len(allProposals))

for index, proposal := range allProposals {
for _, proposal := range p.Data.Proposals {
proposalParsed, err := proposal.ToProposal()
if err != nil {
return []types.TallyInfo{}, err
}

if !proposalParsed.IsInVoting() {
continue
}

yesVotes, err := math.LegacyNewDecFromStr(proposal.Proposal.Votes.Yes)
if err != nil {
return []types.TallyInfo{}, err
Expand All @@ -98,15 +97,15 @@ func (p ProposalsResponse) ToTally() ([]types.TallyInfo, error) {
return []types.TallyInfo{}, err
}

tallyInfos[index] = types.TallyInfo{
tallyInfos = append(tallyInfos, types.TallyInfo{
Proposal: proposalParsed,
Tally: types.Tally{
{Option: "Yes", Voted: yesVotes},
{Option: "No", Voted: noVotes},
{Option: "Abstain", Voted: abstainVotes},
},
TotalVotingPower: totalVotes,
}
})
}

return tallyInfos, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/fetchers/neutron/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func (fetcher *Fetcher) GetTallies() (types.ChainTallyInfos, error) {
query := "{\"list_proposals\": {}}"
query := "{\"reverse_proposals\": {\"limit\": 1000}}"

var proposals responses.ProposalsResponse
if _, err := fetcher.GetSmartContractState(query, &proposals, 0); err != nil {
Expand Down
48 changes: 36 additions & 12 deletions pkg/fetchers/test_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
)

type TestFetcher struct {
WithProposals bool
WithPassedProposals bool
WithProposalsError bool
WithVote bool
WithVoteError bool
WithTallyError bool
WithTallyNotEmpty bool
WithParamsError bool
}

func (f *TestFetcher) GetAllProposals(
Expand All @@ -31,16 +33,12 @@ func (f *TestFetcher) GetAllProposals(
}, 123, nil
}

if f.WithProposals {
return []types.Proposal{
{
ID: "1",
Status: types.ProposalStatusVoting,
},
}, 123, nil
}

return []types.Proposal{}, 123, nil
return []types.Proposal{
{
ID: "1",
Status: types.ProposalStatusVoting,
},
}, 123, nil
}

func (f *TestFetcher) GetVote(
Expand All @@ -65,9 +63,35 @@ func (f *TestFetcher) GetVote(
}

func (f *TestFetcher) GetTallies() (types.ChainTallyInfos, error) {
if f.WithTallyError {
return types.ChainTallyInfos{}, &types.QueryError{
QueryError: errors.New("error"),
}
}

if f.WithTallyNotEmpty {
return types.ChainTallyInfos{
Chain: &types.Chain{Name: "test"},
TallyInfos: []types.TallyInfo{
{
Proposal: types.Proposal{ID: "id"},
Tally: types.Tally{},
},
},
}, nil
}

return types.ChainTallyInfos{}, nil
}

func (f *TestFetcher) GetChainParams() (*types.ChainWithVotingParams, []error) {
return nil, []error{}
if f.WithParamsError {
return &types.ChainWithVotingParams{}, []error{
errors.New("test"),
}
}

return &types.ChainWithVotingParams{
Chain: &types.Chain{Name: "test"},
}, []error{}
}
2 changes: 1 addition & 1 deletion pkg/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Client struct {
Logger zerolog.Logger
}

func NewClient(chainName string, hosts []string, logger zerolog.Logger) *Client {
func NewClient(chainName string, hosts []string, logger *zerolog.Logger) *Client {
return &Client{
Hosts: hosts,
Logger: logger.With().
Expand Down
2 changes: 1 addition & 1 deletion pkg/state/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewStateGenerator(logger *zerolog.Logger, chains types.Chains) *Generator {
fetchers := make(map[string]fetchersPkg.Fetcher, len(chains))

for _, chain := range chains {
fetchers[chain.Name] = fetchersPkg.GetFetcher(chain, *logger)
fetchers[chain.Name] = fetchersPkg.GetFetcher(chain, logger)
}

return &Generator{
Expand Down
Loading

0 comments on commit 835d287

Please sign in to comment.