-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/tanlang/add unit test to models layer (#200)
* feat: add unit test which mapping manually
- Loading branch information
Showing
19 changed files
with
1,993 additions
and
105 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
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,59 @@ | ||
package badger | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/filecoin-project/go-fil-markets/piecestore" | ||
"github.com/filecoin-project/venus/venus-shared/testutil" | ||
"github.com/ipfs/go-cid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCidInfo(t *testing.T) { | ||
ctx := context.Background() | ||
repo := setup(t) | ||
r := repo.CidInfoRepo() | ||
|
||
cidInfoCases := make([]piecestore.CIDInfo, 10) | ||
testutil.Provide(t, &cidInfoCases) | ||
|
||
t.Run("AddPieceBlockLocations", func(t *testing.T) { | ||
|
||
pieceCid2cidInfo := make(map[cid.Cid][]piecestore.CIDInfo) | ||
for _, info := range cidInfoCases { | ||
for _, location := range info.PieceBlockLocations { | ||
if _, ok := pieceCid2cidInfo[location.PieceCID]; !ok { | ||
pieceCid2cidInfo[location.PieceCID] = make([]piecestore.CIDInfo, 0) | ||
} | ||
pieceCid2cidInfo[location.PieceCID] = append(pieceCid2cidInfo[location.PieceCID], info) | ||
} | ||
} | ||
|
||
for pieceCid, cidInfo := range pieceCid2cidInfo { | ||
playloadCid2location := make(map[cid.Cid]piecestore.BlockLocation) | ||
for _, info := range cidInfo { | ||
for _, location := range info.PieceBlockLocations { | ||
playloadCid2location[info.CID] = location.BlockLocation | ||
} | ||
} | ||
err := r.AddPieceBlockLocations(ctx, pieceCid, playloadCid2location) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
|
||
t.Run("GetCIDInfo", func(t *testing.T) { | ||
res, err := r.GetCIDInfo(ctx, cidInfoCases[0].CID) | ||
assert.NoError(t, err) | ||
assert.Equal(t, cidInfoCases[0], res) | ||
}) | ||
|
||
t.Run("ListCidInfoKeys", func(t *testing.T) { | ||
cidInfos, err := r.ListCidInfoKeys(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(cidInfoCases), len(cidInfos)) | ||
for _, info := range cidInfoCases { | ||
assert.Contains(t, cidInfos, info.CID) | ||
} | ||
}) | ||
} |
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,50 @@ | ||
package badger | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/filecoin-project/venus/venus-shared/testutil" | ||
types "github.com/filecoin-project/venus/venus-shared/types/market" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFund(t *testing.T) { | ||
ctx := context.Background() | ||
repo := setup(t) | ||
r := repo.FundRepo() | ||
|
||
fundedAddressStateCases := make([]types.FundedAddressState, 10) | ||
testutil.Provide(t, &fundedAddressStateCases) | ||
|
||
t.Run("SaveFundedAddressState", func(t *testing.T) { | ||
for _, state := range fundedAddressStateCases { | ||
err := r.SaveFundedAddressState(ctx, &state) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
|
||
t.Run("GetFundedAddressState", func(t *testing.T) { | ||
res, err := r.GetFundedAddressState(ctx, fundedAddressStateCases[0].Addr) | ||
assert.NoError(t, err) | ||
fundedAddressStateCases[0].UpdatedAt = res.UpdatedAt | ||
assert.Equal(t, fundedAddressStateCases[0], *res) | ||
}) | ||
|
||
// refresh the UpdatedAt field of test cases | ||
for i := 0; i < len(fundedAddressStateCases); i++ { | ||
res, err := r.GetFundedAddressState(ctx, fundedAddressStateCases[i].Addr) | ||
assert.NoError(t, err) | ||
fundedAddressStateCases[i].UpdatedAt = res.UpdatedAt | ||
} | ||
|
||
t.Run("ListFundedAddressState", func(t *testing.T) { | ||
res, err := r.ListFundedAddressState(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(fundedAddressStateCases), len(res)) | ||
|
||
for i := 0; i < len(res); i++ { | ||
assert.Contains(t, fundedAddressStateCases, *res[i]) | ||
} | ||
}) | ||
} |
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,145 @@ | ||
package badger | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/go-state-types/big" | ||
mrepo "github.com/filecoin-project/venus-market/v2/models/repo" | ||
"github.com/filecoin-project/venus/venus-shared/testutil" | ||
types "github.com/filecoin-project/venus/venus-shared/types/market" | ||
"github.com/ipfs/go-cid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPaych(t *testing.T) { | ||
ctx := context.Background() | ||
repo := setup(t) | ||
r := repo.PaychChannelInfoRepo() | ||
|
||
channelInfoCases := make([]types.ChannelInfo, 10) | ||
testutil.Provide(t, &channelInfoCases) | ||
channelInfoCases[0].Direction = types.DirOutbound | ||
|
||
t.Run("SaveChannel", func(t *testing.T) { | ||
for _, info := range channelInfoCases { | ||
err := r.SaveChannel(ctx, &info) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
|
||
t.Run("GetChannelByAddress", func(t *testing.T) { | ||
res, err := r.GetChannelByAddress(ctx, *channelInfoCases[0].Channel) | ||
assert.NoError(t, err) | ||
channelInfoCases[0].UpdatedAt = res.UpdatedAt | ||
assert.Equal(t, channelInfoCases[0], *res) | ||
}) | ||
|
||
t.Run("GetChannelByChannelID", func(t *testing.T) { | ||
res, err := r.GetChannelByChannelID(ctx, channelInfoCases[0].ChannelID) | ||
assert.NoError(t, err) | ||
channelInfoCases[0].UpdatedAt = res.UpdatedAt | ||
assert.Equal(t, channelInfoCases[0], *res) | ||
}) | ||
|
||
t.Run("WithPendingAddFunds", func(t *testing.T) { | ||
expect := make([]types.ChannelInfo, 0) | ||
for _, info := range channelInfoCases { | ||
if info.Direction == types.DirOutbound && (info.CreateMsg != nil || info.AddFundsMsg != nil) { | ||
expect = append(expect, info) | ||
} | ||
} | ||
|
||
res, err := r.WithPendingAddFunds(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(expect), len(res)) | ||
for i := 0; i < len(res); i++ { | ||
assert.Contains(t, expect, *res[i]) | ||
} | ||
}) | ||
|
||
// refresh the UpdatedAt field of test cases | ||
for i := 0; i < len(channelInfoCases); i++ { | ||
res, err := r.GetChannelByAddress(ctx, *channelInfoCases[i].Channel) | ||
assert.NoError(t, err) | ||
channelInfoCases[i].UpdatedAt = res.UpdatedAt | ||
} | ||
|
||
t.Run("ListChannel", func(t *testing.T) { | ||
res, err := r.ListChannel(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(channelInfoCases), len(res)) | ||
addrs := make([]address.Address, 0) | ||
for _, info := range channelInfoCases { | ||
addrs = append(addrs, *info.Channel) | ||
} | ||
for i := 0; i < len(res); i++ { | ||
assert.Contains(t, addrs, res[i]) | ||
} | ||
}) | ||
|
||
t.Run("CreateChannel and GetChannelByMessageCid", func(t *testing.T) { | ||
var paramsCase struct { | ||
From address.Address | ||
To address.Address | ||
CreateMsg cid.Cid | ||
Amt big.Int | ||
} | ||
|
||
testutil.Provide(t, ¶msCase) | ||
|
||
_, err := r.CreateChannel(ctx, paramsCase.From, paramsCase.To, paramsCase.CreateMsg, paramsCase.Amt) | ||
assert.NoError(t, err) | ||
|
||
_, err = r.GetChannelByMessageCid(ctx, paramsCase.CreateMsg) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("OutboundActiveByFromTo", func(t *testing.T) { | ||
res, err := r.OutboundActiveByFromTo(ctx, channelInfoCases[0].From(), channelInfoCases[0].To()) | ||
assert.NoError(t, err) | ||
assert.Equal(t, channelInfoCases[0], *res) | ||
}) | ||
|
||
t.Run("RemoveChannel", func(t *testing.T) { | ||
err := r.RemoveChannel(ctx, channelInfoCases[0].ChannelID) | ||
assert.NoError(t, err) | ||
_, err = r.GetChannelByAddress(ctx, *channelInfoCases[0].Channel) | ||
assert.True(t, errors.Is(err, mrepo.ErrNotFound)) | ||
}) | ||
} | ||
|
||
func TestMessage(t *testing.T) { | ||
ctx := context.Background() | ||
repo := setup(t) | ||
r := repo.PaychMsgInfoRepo() | ||
|
||
messageInfoCases := make([]types.MsgInfo, 10) | ||
testutil.Provide(t, &messageInfoCases) | ||
|
||
t.Run("SaveMessage", func(t *testing.T) { | ||
for _, info := range messageInfoCases { | ||
err := r.SaveMessage(ctx, &info) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
|
||
t.Run("GetMessage", func(t *testing.T) { | ||
res, err := r.GetMessage(ctx, messageInfoCases[0].MsgCid) | ||
assert.NoError(t, err) | ||
messageInfoCases[0].UpdatedAt = res.UpdatedAt | ||
assert.Equal(t, messageInfoCases[0], *res) | ||
}) | ||
|
||
t.Run("SaveMessageResult", func(t *testing.T) { | ||
err := r.SaveMessageResult(ctx, messageInfoCases[0].MsgCid, errors.New("test error")) | ||
assert.NoError(t, err) | ||
|
||
res, err := r.GetMessage(ctx, messageInfoCases[0].MsgCid) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, "test error", res.Err) | ||
}) | ||
} |
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,50 @@ | ||
package badger | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/filecoin-project/venus/venus-shared/testutil" | ||
types "github.com/filecoin-project/venus/venus-shared/types/market" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRetrievalAsk(t *testing.T) { | ||
ctx := context.Background() | ||
repo := setup(t) | ||
r := repo.RetrievalAskRepo() | ||
|
||
askCases := make([]types.RetrievalAsk, 10) | ||
testutil.Provide(t, &askCases) | ||
|
||
t.Run("SetAsk", func(t *testing.T) { | ||
for _, ask := range askCases { | ||
err := r.SetAsk(ctx, &ask) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
|
||
t.Run("GetAsk", func(t *testing.T) { | ||
res, err := r.GetAsk(ctx, askCases[0].Miner) | ||
assert.NoError(t, err) | ||
askCases[0].UpdatedAt = res.UpdatedAt | ||
assert.Equal(t, askCases[0], *res) | ||
}) | ||
|
||
// refresh UpdatedAt field | ||
|
||
for i := 0; i < len(askCases); i++ { | ||
res, err := r.GetAsk(ctx, askCases[i].Miner) | ||
assert.NoError(t, err) | ||
askCases[i].UpdatedAt = res.UpdatedAt | ||
} | ||
|
||
t.Run("ListAsk", func(t *testing.T) { | ||
res, err := r.ListAsk(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(askCases), len(res)) | ||
for _, ask := range res { | ||
assert.Contains(t, askCases, *ask) | ||
} | ||
}) | ||
} |
Oops, something went wrong.