-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f5acec
commit c98bc26
Showing
6 changed files
with
193 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package fetchers | ||
|
||
import ( | ||
"errors" | ||
"main/pkg/types" | ||
) | ||
|
||
type TestFetcher struct { | ||
WithProposals bool | ||
WithProposalsError bool | ||
} | ||
|
||
func (f *TestFetcher) GetAllProposals( | ||
prevHeight int64, | ||
) ([]types.Proposal, int64, *types.QueryError) { | ||
if f.WithProposalsError { | ||
return []types.Proposal{}, 123, &types.QueryError{ | ||
QueryError: errors.New("error"), | ||
} | ||
} | ||
|
||
if f.WithProposals { | ||
return []types.Proposal{ | ||
{ | ||
ID: "1", | ||
}, | ||
}, 123, nil | ||
} | ||
|
||
return []types.Proposal{}, 123, nil | ||
} | ||
|
||
func (f *TestFetcher) GetVote( | ||
proposal, voter string, | ||
prevHeight int64, | ||
) (*types.Vote, int64, *types.QueryError) { | ||
return nil, 456, nil | ||
} | ||
|
||
func (f *TestFetcher) GetTallies() (types.ChainTallyInfos, error) { | ||
return types.ChainTallyInfos{}, nil | ||
} | ||
|
||
func (f *TestFetcher) GetChainParams() (*types.ChainWithVotingParams, []error) { | ||
return nil, []error{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package fs | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
type TestFS struct{} | ||
|
||
type TestFile struct { | ||
} | ||
|
||
func (f *TestFile) Write(p []byte) (int, error) { | ||
return 0, nil | ||
} | ||
|
||
func (f *TestFile) Close() error { | ||
return nil | ||
} | ||
|
||
func (fs *TestFS) ReadFile(name string) ([]byte, error) { | ||
return []byte{}, nil | ||
} | ||
|
||
func (fs *TestFS) WriteFile(name string, data []byte, perms os.FileMode) error { | ||
return nil | ||
} | ||
|
||
func (fs *TestFS) Create(path string) (File, error) { | ||
return &TestFile{}, nil // go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package state | ||
|
||
import ( | ||
"main/pkg/fetchers" | ||
"main/pkg/logger" | ||
"main/pkg/types" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReportGeneratorNew(t *testing.T) { | ||
t.Parallel() | ||
|
||
log := logger.GetNopLogger() | ||
chain := &types.Chain{Name: "chain", Type: "cosmos"} | ||
chains := types.Chains{chain} | ||
|
||
generator := NewStateGenerator(log, chains) | ||
assert.NotNil(t, generator) | ||
} | ||
|
||
func TestReportGeneratorProcessChain(t *testing.T) { | ||
t.Parallel() | ||
|
||
log := logger.GetNopLogger() | ||
chain := &types.Chain{Name: "chain", Type: "cosmos"} | ||
chains := types.Chains{chain} | ||
|
||
generator := Generator{ | ||
Logger: *log, | ||
Chains: chains, | ||
Fetchers: map[string]fetchers.Fetcher{ | ||
"chain": &fetchers.TestFetcher{}, | ||
}, | ||
} | ||
|
||
oldState := NewState() | ||
newState := generator.GetState(oldState) | ||
assert.Len(t, newState.ChainInfos, 1) | ||
} | ||
|
||
func TestReportGeneratorProcessProposalWithError(t *testing.T) { | ||
t.Parallel() | ||
|
||
log := logger.GetNopLogger() | ||
chain := &types.Chain{Name: "chain", Type: "cosmos"} | ||
chains := types.Chains{chain} | ||
fetcher := &fetchers.TestFetcher{WithProposalsError: true} | ||
|
||
generator := Generator{ | ||
Logger: *log, | ||
Chains: chains, | ||
Fetchers: map[string]fetchers.Fetcher{ | ||
"chain": fetcher, | ||
}, | ||
} | ||
|
||
oldVotes := map[string]WalletVotes{ | ||
"1": { | ||
Proposal: types.Proposal{ID: "1"}, | ||
}, | ||
} | ||
|
||
oldState := NewState() | ||
oldState.SetChainProposalsHeight(chain, 15) | ||
oldState.SetChainVotes(chain, oldVotes) | ||
|
||
newState := NewState() | ||
generator.ProcessChain(chain, newState, oldState, fetcher) | ||
assert.Len(t, newState.ChainInfos, 1) | ||
|
||
newVotes, ok := newState.ChainInfos["chain"] | ||
assert.True(t, ok) | ||
assert.NotNil(t, newVotes) | ||
assert.NotNil(t, newVotes.ProposalsError) | ||
assert.Equal(t, int64(15), newVotes.ProposalsHeight) | ||
|
||
proposal, ok := newVotes.ProposalVotes["1"] | ||
assert.True(t, ok) | ||
assert.Equal(t, "1", proposal.Proposal.ID) | ||
} |