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

     1  package cronjob
     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/builder"
    12  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    13  	"github.com/machinefi/w3bstream/pkg/errors/status"
    14  	"github.com/machinefi/w3bstream/pkg/models"
    15  	mock_sqlx "github.com/machinefi/w3bstream/pkg/test/mock_depends_kit_sqlx"
    16  	"github.com/machinefi/w3bstream/pkg/types"
    17  )
    18  
    19  func TestCronJob(t *testing.T) {
    20  	ctrl := gomock.NewController(t)
    21  	defer ctrl.Finish()
    22  
    23  	var (
    24  		db  = mock_sqlx.NewMockDBExecutor(ctrl)
    25  		prj = &models.Project{
    26  			RelProject: models.RelProject{ProjectID: 1},
    27  		}
    28  		ctx = contextx.WithContextCompose(
    29  			types.WithMgrDBExecutorContext(db),
    30  			confid.WithSFIDGeneratorContext(confid.MustNewSFIDGenerator()),
    31  			types.WithProjectContext(prj),
    32  		)(context.Background())
    33  	)
    34  
    35  	db.EXPECT().T(gomock.Any()).Return(&builder.Table{}).AnyTimes()
    36  
    37  	t.Run("Create", func(t *testing.T) {
    38  		req := CreateReq{
    39  			CronJobInfo: models.CronJobInfo{
    40  				CronExpressions: "* * * * *",
    41  			},
    42  		}
    43  
    44  		t.Run("#Success", func(t *testing.T) {
    45  			db.EXPECT().Exec(gomock.Any()).Return(nil, nil)
    46  
    47  			_, err := Create(ctx, &req)
    48  			NewWithT(t).Expect(err).To(BeNil())
    49  		})
    50  
    51  		t.Run("#InvalidCronExpressions", func(t *testing.T) {
    52  			req := req
    53  			req.CronExpressions = "*"
    54  			_, err := Create(ctx, &req)
    55  			NewWithT(t).Expect(err.Error()).To(ContainSubstring(status.InvalidCronExpressions.Error()))
    56  		})
    57  
    58  		t.Run("#CronJobConflict", func(t *testing.T) {
    59  			db.EXPECT().Exec(gomock.Any()).Return(nil, mock_sqlx.ErrConflict)
    60  
    61  			_, err := Create(ctx, &req)
    62  			NewWithT(t).Expect(err).To(Equal(status.CronJobConflict))
    63  		})
    64  	})
    65  
    66  	t.Run("Get", func(t *testing.T) {
    67  
    68  		t.Run("#Success", func(t *testing.T) {
    69  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil)
    70  
    71  			_, err := GetBySFID(ctx, 1)
    72  			NewWithT(t).Expect(err).To(BeNil())
    73  		})
    74  
    75  		t.Run("#CronJobNotFound", func(t *testing.T) {
    76  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound)
    77  
    78  			_, err := GetBySFID(ctx, 1)
    79  			NewWithT(t).Expect(err).To(Equal(status.CronJobNotFound))
    80  		})
    81  	})
    82  
    83  	t.Run("List", func(t *testing.T) {
    84  		req := ListReq{}
    85  
    86  		t.Run("#Success", func(t *testing.T) {
    87  			db.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(2)
    88  
    89  			_, err := List(ctx, &req)
    90  			NewWithT(t).Expect(err).To(BeNil())
    91  		})
    92  	})
    93  
    94  	t.Run("Remove", func(t *testing.T) {
    95  
    96  		t.Run("#Success", func(t *testing.T) {
    97  			db.EXPECT().Exec(gomock.Any()).Return(nil, nil)
    98  
    99  			err := RemoveBySFID(ctx, 1)
   100  			NewWithT(t).Expect(err).To(BeNil())
   101  		})
   102  	})
   103  }