github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/blockchain/api_contract_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/errors/status"
    17  	"github.com/machinefi/w3bstream/pkg/models"
    18  	"github.com/machinefi/w3bstream/pkg/types"
    19  )
    20  
    21  func TestContractLog(t *testing.T) {
    22  	ctrl := gomock.NewController(t)
    23  	defer ctrl.Finish()
    24  
    25  	var (
    26  		db         = mock.NewMockDBExecutor(ctrl)
    27  		ethClients = &types.ETHClientConfig{
    28  			Clients: map[uint32]string{4690: "https://babel-api.testnet.iotex.io"},
    29  		}
    30  		ctx = contextx.WithContextCompose(
    31  			types.WithMonitorDBExecutorContext(db),
    32  			confid.WithSFIDGeneratorContext(confid.MustNewSFIDGenerator()),
    33  			types.WithETHClientConfigContext(ethClients),
    34  		)(context.Background())
    35  		req = CreateContractLogReq{
    36  			ProjectName: "test_project_for_blockchain_unit_test",
    37  			ContractLogInfo: models.ContractLogInfo{
    38  				ChainID: 4690,
    39  			},
    40  		}
    41  	)
    42  
    43  	t.Run("Create", func(t *testing.T) {
    44  
    45  		t.Run("#Success", func(t *testing.T) {
    46  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    47  			db.EXPECT().Exec(gomock.Any()).Return(nil, nil)
    48  
    49  			_, err := CreateContractLog(ctx, &req)
    50  			NewWithT(t).Expect(err).To(BeNil())
    51  		})
    52  
    53  		t.Run("#ChainIDNotExist", func(t *testing.T) {
    54  			req := req
    55  			req.ChainID = 1
    56  			_, err := CreateContractLog(ctx, &req)
    57  			NewWithT(t).Expect(err).To(Equal(status.BlockchainNotFound))
    58  		})
    59  
    60  		t.Run("#ContractLogConflict", func(t *testing.T) {
    61  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    62  			db.EXPECT().Exec(gomock.Any()).Return(nil, sqlx.NewSqlError(sqlx.SqlErrTypeConflict, ""))
    63  
    64  			_, err := CreateContractLog(ctx, &req)
    65  			NewWithT(t).Expect(err).To(Equal(status.ContractLogConflict))
    66  		})
    67  	})
    68  
    69  	t.Run("Get", func(t *testing.T) {
    70  
    71  		t.Run("#Success", func(t *testing.T) {
    72  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    73  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil)
    74  
    75  			_, err := GetContractLogBySFID(ctx, 1)
    76  			NewWithT(t).Expect(err).To(BeNil())
    77  		})
    78  
    79  		t.Run("#ContractLogNotFound", func(t *testing.T) {
    80  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    81  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(sqlx.NewSqlError(sqlx.SqlErrTypeNotFound, ""))
    82  
    83  			_, err := GetContractLogBySFID(ctx, 1)
    84  			NewWithT(t).Expect(err).To(Equal(status.ContractLogNotFound))
    85  		})
    86  	})
    87  
    88  	t.Run("Remove", func(t *testing.T) {
    89  
    90  		t.Run("#Success", func(t *testing.T) {
    91  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    92  			db.EXPECT().Exec(gomock.Any()).Return(nil, nil)
    93  
    94  			err := RemoveContractLogBySFID(ctx, 1)
    95  			NewWithT(t).Expect(err).To(BeNil())
    96  		})
    97  	})
    98  }
    99  */