github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/blockchain/api_height_test.go (about) 1 package blockchain 2 3 /* 4 import ( 5 "context" 6 "testing" 7 8 "github.com/golang/mock/gomock" 9 . "github.com/onsi/gomega" 10 11 confid "github.com/machinefi/w3bstream/pkg/depends/conf/id" 12 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx" 13 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 14 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/mock" 15 "github.com/machinefi/w3bstream/pkg/depends/x/contextx" 16 "github.com/machinefi/w3bstream/pkg/enums" 17 "github.com/machinefi/w3bstream/pkg/errors/status" 18 "github.com/machinefi/w3bstream/pkg/models" 19 "github.com/machinefi/w3bstream/pkg/types" 20 ) 21 22 func TestChainHeight(t *testing.T) { 23 ctrl := gomock.NewController(t) 24 defer ctrl.Finish() 25 26 var ( 27 db = mock.NewMockDBExecutor(ctrl) 28 chain = &types.Chain{ 29 ChainID: 4690, 30 Endpoint: "https://babel-api.testnet.iotex.io", 31 Name: enums.IOTEX_TESTNET, 32 } 33 chainConf = &types.ChainConfig{ 34 Chains: map[enums.ChainName]*types.Chain{chain.Name: chain}, 35 ChainIDs: map[uint64]*types.Chain{4690: chain}, 36 } 37 ethClients = &types.ETHClientConfig{ 38 Clients: map[uint32]string{4690: "https://babel-api.testnet.iotex.io"}, 39 } 40 ctx = contextx.WithContextCompose( 41 types.WithMonitorDBExecutorContext(db), 42 confid.WithSFIDGeneratorContext(confid.MustNewSFIDGenerator()), 43 types.WithETHClientConfigContext(ethClients), 44 types.WithChainConfigContext(chainConf), 45 )(context.Background()) 46 req = CreateChainHeightReq{ 47 ProjectName: "test_project", 48 ChainHeightInfo: models.ChainHeightInfo{ 49 ChainID: 4690, 50 }, 51 } 52 ) 53 54 t.Run("Create", func(t *testing.T) { 55 56 t.Run("#Success", func(t *testing.T) { 57 db.EXPECT().T(gomock.Any()).Return(&builder.Table{}) 58 db.EXPECT().Exec(gomock.Any()).Return(nil, nil) 59 60 _, err := CreateChainHeight(ctx, &req) 61 NewWithT(t).Expect(err).To(BeNil()) 62 }) 63 64 t.Run("#ChainIDNotExist", func(t *testing.T) { 65 req := req 66 req.ChainID = 1 67 _, err := CreateChainHeight(ctx, &req) 68 NewWithT(t).Expect(err).To(Equal(status.BlockchainNotFound)) 69 }) 70 71 t.Run("#ChainHeightConflict", func(t *testing.T) { 72 db.EXPECT().T(gomock.Any()).Return(&builder.Table{}) 73 db.EXPECT().Exec(gomock.Any()).Return(nil, sqlx.NewSqlError(sqlx.SqlErrTypeConflict, "")) 74 75 _, err := CreateChainHeight(ctx, &req) 76 NewWithT(t).Expect(err).To(Equal(status.ChainHeightConflict)) 77 }) 78 }) 79 80 t.Run("Get", func(t *testing.T) { 81 82 t.Run("#Success", func(t *testing.T) { 83 db.EXPECT().T(gomock.Any()).Return(&builder.Table{}) 84 db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil) 85 86 _, err := GetChainHeightBySFID(ctx, 1) 87 NewWithT(t).Expect(err).To(BeNil()) 88 }) 89 90 t.Run("#ChainHeightNotFound", func(t *testing.T) { 91 db.EXPECT().T(gomock.Any()).Return(&builder.Table{}) 92 db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(sqlx.NewSqlError(sqlx.SqlErrTypeNotFound, "")) 93 94 _, err := GetChainHeightBySFID(ctx, 1) 95 NewWithT(t).Expect(err).To(Equal(status.ChainHeightNotFound)) 96 }) 97 }) 98 99 t.Run("Remove", func(t *testing.T) { 100 101 t.Run("#Success", func(t *testing.T) { 102 db.EXPECT().T(gomock.Any()).Return(&builder.Table{}) 103 db.EXPECT().Exec(gomock.Any()).Return(nil, nil) 104 105 err := RemoveChainHeightBySFID(ctx, 1) 106 NewWithT(t).Expect(err).To(BeNil()) 107 }) 108 }) 109 } 110 */