github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/strategy/strategy_test.go (about)

     1  package strategy
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	. "github.com/onsi/gomega"
     9  
    10  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
    11  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
    12  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    13  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/mock"
    14  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    15  	"github.com/machinefi/w3bstream/pkg/errors/status"
    16  	"github.com/machinefi/w3bstream/pkg/types"
    17  )
    18  
    19  func TestStrategy(t *testing.T) {
    20  	ctrl := gomock.NewController(t)
    21  	defer ctrl.Finish()
    22  
    23  	var (
    24  		db  = mock.NewMockDBExecutor(ctrl)
    25  		idg = confid.MustNewSFIDGenerator()
    26  		ctx = contextx.WithContextCompose(
    27  			types.WithMgrDBExecutorContext(db),
    28  			confid.WithSFIDGeneratorContext(idg),
    29  		)(context.Background())
    30  	)
    31  
    32  	t.Run("Get", func(t *testing.T) {
    33  		t.Run("#Success", func(t *testing.T) {
    34  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    35  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil)
    36  
    37  			_, err := GetBySFID(ctx, idg.MustGenSFID())
    38  			NewWithT(t).Expect(err).To(BeNil())
    39  		})
    40  
    41  		t.Run("#StrategyNotFound", func(t *testing.T) {
    42  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{})
    43  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(sqlx.NewSqlError(sqlx.SqlErrTypeNotFound, ""))
    44  
    45  			_, err := GetBySFID(ctx, 1)
    46  			NewWithT(t).Expect(err).To(Equal(status.StrategyNotFound))
    47  		})
    48  	})
    49  
    50  	t.Run("List", func(t *testing.T) {
    51  		t.Run("Success", func(t *testing.T) {
    52  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{}).Times(12)
    53  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(6)
    54  
    55  			// List
    56  			{
    57  				_, err := List(ctx, &ListReq{})
    58  				NewWithT(t).Expect(err).To(BeNil())
    59  			}
    60  
    61  			// ListByCond
    62  			{
    63  				_, err := ListByCond(ctx, &CondArgs{})
    64  				NewWithT(t).Expect(err).To(BeNil())
    65  			}
    66  
    67  			// ListDetailByCond
    68  			{
    69  				_, err := ListDetailByCond(ctx, &CondArgs{}, (&ListReq{}).Addition())
    70  				NewWithT(t).Expect(err).To(BeNil())
    71  			}
    72  
    73  			// ListDetail
    74  			{
    75  				_, err := ListDetail(ctx, &ListReq{})
    76  				NewWithT(t).Expect(err).To(BeNil())
    77  			}
    78  		})
    79  	})
    80  
    81  	t.Run("Remove", func(t *testing.T) {
    82  		t.Run("Success", func(t *testing.T) {
    83  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{}).Times(2)
    84  			db.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(2)
    85  
    86  			{
    87  				err := RemoveBySFID(ctx, 1)
    88  				NewWithT(t).Expect(err).To(BeNil())
    89  			}
    90  
    91  			// Remove
    92  			{
    93  				err := Remove(ctx, &CondArgs{})
    94  				NewWithT(t).Expect(err).To(BeNil())
    95  			}
    96  		})
    97  	})
    98  
    99  	t.Run("Filter", func(t *testing.T) {
   100  		t.Run("Success", func(t *testing.T) {
   101  			db.EXPECT().T(gomock.Any()).Return(&builder.Table{}).AnyTimes()
   102  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   103  
   104  			_, err := FilterByProjectAndEvent(ctx, 1, "DEFAULT")
   105  			NewWithT(t).Expect(err).To(BeNil())
   106  		})
   107  	})
   108  }