Skip to content

Commit

Permalink
feat: divide unit test for models layer (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZexiao authored Oct 26, 2022
1 parent 21bad2d commit 4a24cee
Show file tree
Hide file tree
Showing 21 changed files with 1,171 additions and 686 deletions.
100 changes: 67 additions & 33 deletions models/badger/cid_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,88 @@ import (
"testing"

"github.com/filecoin-project/go-fil-markets/piecestore"
"github.com/filecoin-project/venus-market/v2/models/repo"
"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()
func TestAddPieceBlockLocations(t *testing.T) {
ctx, r, cidInfoCases := prepareCidInfoTest(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)
}
}

func TestGetCIDInfo(t *testing.T) {
ctx, r, cidInfoCases := prepareCidInfoTest(t)

inSertCidInfo(ctx, t, r, cidInfoCases[0])
res, err := r.GetCIDInfo(ctx, cidInfoCases[0].CID)
assert.NoError(t, err)
assert.Equal(t, cidInfoCases[0], res)
}

func TestListCidInfoKeys(t *testing.T) {
ctx, r, cidInfoCases := prepareCidInfoTest(t)

inSertCidInfo(ctx, t, r, cidInfoCases...)

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)
}
}

func prepareCidInfoTest(t *testing.T) (context.Context, repo.ICidInfoRepo, []piecestore.CIDInfo) {
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)
return context.Background(), r, cidInfoCases
}

func inSertCidInfo(ctx context.Context, t *testing.T, r repo.ICidInfoRepo, cidInfoCases ...piecestore.CIDInfo) {
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
}
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)
err := r.AddPieceBlockLocations(ctx, pieceCid, playloadCid2location)
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)
}
})
}
}
55 changes: 37 additions & 18 deletions models/badger/fund_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,43 @@ import (
"context"
"testing"

"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/stretchr/testify/assert"
)

func TestFund(t *testing.T) {
ctx := context.Background()
repo := setup(t)
r := repo.FundRepo()
func TestSaveFundedAddressState(t *testing.T) {
ctx, r, fundedAddressStateCases := prepareFundTest(t)

fundedAddressStateCases := make([]types.FundedAddressState, 10)
testutil.Provide(t, &fundedAddressStateCases)
for _, state := range fundedAddressStateCases {
err := r.SaveFundedAddressState(ctx, &state)
assert.NoError(t, err)
}
}

t.Run("SaveFundedAddressState", func(t *testing.T) {
for _, state := range fundedAddressStateCases {
err := r.SaveFundedAddressState(ctx, &state)
assert.NoError(t, err)
}
})
func TestGetFundedAddressState(t *testing.T) {
ctx, r, fundedAddressStateCases := prepareFundTest(t)

t.Run("GetFundedAddressState", func(t *testing.T) {
res, err := r.GetFundedAddressState(ctx, fundedAddressStateCases[0].Addr)
for _, state := range fundedAddressStateCases {
err := r.SaveFundedAddressState(ctx, &state)
assert.NoError(t, err)
fundedAddressStateCases[0].UpdatedAt = res.UpdatedAt
assert.Equal(t, fundedAddressStateCases[0], *res)
})
}

res, err := r.GetFundedAddressState(ctx, fundedAddressStateCases[0].Addr)
assert.NoError(t, err)
fundedAddressStateCases[0].UpdatedAt = res.UpdatedAt
assert.Equal(t, fundedAddressStateCases[0], *res)
}

func TestListFundedAddressState(t *testing.T) {
ctx, r, fundedAddressStateCases := prepareFundTest(t)

for _, state := range fundedAddressStateCases {
err := r.SaveFundedAddressState(ctx, &state)
assert.NoError(t, err)
}

// 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)
Expand All @@ -48,3 +57,13 @@ func TestFund(t *testing.T) {
}
})
}

func prepareFundTest(t *testing.T) (context.Context, repo.FundRepo, []types.FundedAddressState) {
ctx := context.Background()
repo := setup(t)
r := repo.FundRepo()

fundedAddressStateCases := make([]types.FundedAddressState, 10)
testutil.Provide(t, &fundedAddressStateCases)
return ctx, r, fundedAddressStateCases
}
Loading

0 comments on commit 4a24cee

Please sign in to comment.