github.com/lino-network/lino@v0.6.11/x/post/model/storage_test.go (about)

     1  package model
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cosmos/cosmos-sdk/store"
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  	"github.com/stretchr/testify/suite"
     9  	abci "github.com/tendermint/tendermint/abci/types"
    10  	"github.com/tendermint/tendermint/libs/log"
    11  	dbm "github.com/tendermint/tm-db"
    12  
    13  	linotypes "github.com/lino-network/lino/types"
    14  )
    15  
    16  type postStoreTestSuite struct {
    17  	suite.Suite
    18  	ctx sdk.Context
    19  	ps  PostStorage
    20  }
    21  
    22  func TestPostStoreTestSuite(t *testing.T) {
    23  	suite.Run(t, &postStoreTestSuite{})
    24  }
    25  
    26  func (suite *postStoreTestSuite) SetupTest() {
    27  	TestKVStoreKey := sdk.NewKVStoreKey("post")
    28  	db := dbm.NewMemDB()
    29  	ms := store.NewCommitMultiStore(db)
    30  	ms.MountStoreWithDB(TestKVStoreKey, sdk.StoreTypeIAVL, db)
    31  	_ = ms.LoadLatestVersion()
    32  	suite.ctx = sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger())
    33  	suite.ps = NewPostStorage(TestKVStoreKey)
    34  }
    35  
    36  func (suite *postStoreTestSuite) TestPostGetSetHas() {
    37  	postInfo1 := &Post{
    38  		PostID:    "Test Post",
    39  		Title:     "Test Post",
    40  		Content:   "Test Post",
    41  		Author:    linotypes.AccountKey("author1"),
    42  		CreatedBy: linotypes.AccountKey("app"),
    43  		CreatedAt: 1,
    44  		UpdatedAt: 2,
    45  		IsDeleted: true,
    46  	}
    47  	postInfo2 := &Post{
    48  		PostID:    "Test Post",
    49  		Title:     "Test Post",
    50  		Content:   "Test Post",
    51  		Author:    linotypes.AccountKey("author"),
    52  		CreatedBy: linotypes.AccountKey("app"),
    53  		CreatedAt: 1,
    54  		UpdatedAt: 2,
    55  		IsDeleted: true,
    56  	}
    57  	permlink1 := linotypes.GetPermlink(postInfo1.Author, postInfo1.PostID)
    58  	permlink2 := linotypes.GetPermlink(postInfo2.Author, postInfo2.PostID)
    59  
    60  	// add post1
    61  	suite.False(suite.ps.HasPost(suite.ctx, permlink1))
    62  	suite.False(suite.ps.HasPost(suite.ctx, permlink2))
    63  	suite.ps.SetPost(suite.ctx, postInfo1)
    64  	suite.True(suite.ps.HasPost(suite.ctx, permlink1))
    65  	suite.False(suite.ps.HasPost(suite.ctx, permlink2))
    66  	rst1, err := suite.ps.GetPost(suite.ctx, permlink1)
    67  	suite.Nil(err)
    68  	suite.Equal(postInfo1, rst1)
    69  	_, err = suite.ps.GetPost(suite.ctx, permlink2)
    70  	suite.NotNil(err)
    71  
    72  	// add post2
    73  	suite.ps.SetPost(suite.ctx, postInfo2)
    74  	suite.True(suite.ps.HasPost(suite.ctx, permlink1))
    75  	suite.True(suite.ps.HasPost(suite.ctx, permlink2))
    76  	rst2, err := suite.ps.GetPost(suite.ctx, permlink2)
    77  	suite.Nil(err)
    78  	suite.Equal(postInfo2, rst2)
    79  	rst1, err = suite.ps.GetPost(suite.ctx, permlink1)
    80  	suite.Nil(err)
    81  	suite.Equal(postInfo1, rst1)
    82  }
    83  
    84  func (suite *postStoreTestSuite) TestConsumptionWIndowGetSet() {
    85  	suite.ps.SetConsumptionWindow(suite.ctx, linotypes.NewMiniDollar(1000))
    86  	suite.Equal(linotypes.NewMiniDollar(1000), suite.ps.GetConsumptionWindow(suite.ctx))
    87  
    88  	suite.ps.SetConsumptionWindow(suite.ctx, linotypes.NewMiniDollar(2000))
    89  	suite.Equal(linotypes.NewMiniDollar(2000), suite.ps.GetConsumptionWindow(suite.ctx))
    90  }