Skip to content

Commit

Permalink
Feat/tanlang/add unit test to models layer (#200)
Browse files Browse the repository at this point in the history
* feat: add unit test which mapping manually
  • Loading branch information
LinZexiao authored Sep 20, 2022
1 parent 77893b4 commit 0b20156
Show file tree
Hide file tree
Showing 19 changed files with 1,993 additions and 105 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ require (
go.uber.org/zap v1.21.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gorm.io/driver/mysql v1.1.1
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.21.12
)

Expand Down Expand Up @@ -246,6 +247,7 @@ require (
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/miekg/dns v1.1.48 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,7 @@ github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/mattn/go-xmlrpc v0.0.3/go.mod h1:mqc2dz7tP5x5BKlCahN/n+hs7OSZKJkS9JsHNBRlrxA=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -2916,6 +2917,7 @@ gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.1.1 h1:yr1bpyqiwuSPJ4aGGUX9nu46RHXlF8RASQVb1QQNcvo=
gorm.io/driver/mysql v1.1.1/go.mod h1:KdrTanmfLPPyAOeYGyG+UpDys7/7eeWT1zCq+oekYnU=
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.9/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
Expand Down
59 changes: 59 additions & 0 deletions models/badger/cid_info_test.go
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)
}
})
}
50 changes: 50 additions & 0 deletions models/badger/fund_test.go
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])
}
})
}
145 changes: 145 additions & 0 deletions models/badger/paych_test.go
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, &paramsCase)

_, 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)
})
}
50 changes: 50 additions & 0 deletions models/badger/retrieval_ask_test.go
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)
}
})
}
Loading

0 comments on commit 0b20156

Please sign in to comment.