KhaninArtur commented on a change in pull request #16891:
URL: https://github.com/apache/beam/pull/16891#discussion_r818769170



##########
File path: playground/backend/internal/code_processing/code_processing_test.go
##########
@@ -1004,3 +1190,354 @@ func syncMapLen(syncMap *sync.Map) int {
        })
        return length
 }
+
+func TestGetGraph(t *testing.T) {
+       ctx := context.Background()
+       pipelineId1 := uuid.New()
+       graph := "GRAPH"
+       err := cacheService.SetValue(ctx, pipelineId1, cache.Graph, graph)
+       if err != nil {
+               return
+       }
+       pipelineId2 := uuid.New()
+       err = cacheService.SetValue(ctx, pipelineId2, cache.Graph, 1)
+       if err != nil {
+               return
+       }
+       type args struct {
+               ctx          context.Context
+               cacheService cache.Cache
+               key          uuid.UUID
+               errorTitle   string
+       }
+       tests := []struct {
+               name    string
+               args    args
+               want    string
+               wantErr bool
+       }{
+               {
+                       name: "Get graph when key exist in cache",
+                       args: args{
+                               ctx:          context.Background(),
+                               cacheService: cacheService,
+                               key:          pipelineId1,
+                               errorTitle:   "error",
+                       },
+                       want:    graph,
+                       wantErr: false,
+               },
+               {
+                       name: "Get graph when key doesn't exist in cache",
+                       args: args{
+                               ctx:          context.Background(),
+                               cacheService: cacheService,
+                               key:          uuid.New(),
+                               errorTitle:   "error",
+                       },
+                       want:    "",
+                       wantErr: true,
+               },
+               {
+                       name: "Get graph when value from cache by key couldn't 
be converted to a string",
+                       args: args{
+                               ctx:          context.Background(),
+                               cacheService: cacheService,
+                               key:          pipelineId2,
+                               errorTitle:   "error",
+                       },
+                       want:    "",
+                       wantErr: true,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       got, err := GetGraph(tt.args.ctx, tt.args.cacheService, 
tt.args.key, tt.args.errorTitle)
+                       if (err != nil) != tt.wantErr {
+                               t.Errorf("GetGraph error = %v, wantErr %v", 
err, tt.wantErr)
+                               return
+                       }
+                       if got != tt.want {
+                               t.Errorf("GetGraph got = %v, want %v", got, 
tt.want)
+                       }
+               })
+       }
+}
+
+func Test_processSetupError(t *testing.T) {
+       client, mock := redismock.NewClientMock()
+       pipelineId := uuid.New()
+       errorMessage := "MOCK_ERROR"
+       type args struct {
+               err            error
+               pipelineId     uuid.UUID
+               cacheService   cache.Cache
+               ctxWithTimeout context.Context
+       }
+       tests := []struct {
+               name    string
+               mocks   func()
+               args    args
+               wantErr bool
+       }{
+               {
+                       name: "Error during HSet operation",
+                       mocks: func() {
+                               mock.ExpectHSet(pipelineId.String(), 
"MOCK_VALUE").SetErr(fmt.Errorf(errorMessage))
+                       },
+                       args: args{
+                               err:        fmt.Errorf(errorMessage),
+                               pipelineId: pipelineId,
+                               cacheService: &redis.Cache{
+                                       Client: client,
+                               },
+                               ctxWithTimeout: nil,
+                       },
+                       wantErr: true,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       tt.mocks()
+                       if err := processSetupError(tt.args.err, 
tt.args.pipelineId, tt.args.cacheService, tt.args.ctxWithTimeout); (err != nil) 
!= tt.wantErr {
+                               t.Errorf("processSetupError() error = %v, 
wantErr %v", err, tt.wantErr)
+                       }
+               })
+       }
+}
+
+func Test_processErrorWithSavingOutput(t *testing.T) {
+       client, mock := redismock.NewClientMock()
+       pipelineId := uuid.New()
+       errorMessage := "MOCK_ERROR"
+       subKey := cache.RunOutput
+       type args struct {
+               ctx          context.Context
+               err          error
+               errorOutput  []byte
+               pipelineId   uuid.UUID
+               subKey       cache.SubKey
+               cacheService cache.Cache
+               errorTitle   string
+               newStatus    pb.Status
+       }
+       tests := []struct {
+               name    string
+               mocks   func()
+               args    args
+               wantErr bool
+       }{
+               {
+                       name: "Error during HSet operation",
+                       mocks: func() {
+                               mock.ExpectHSet(pipelineId.String(), 
subKey).SetErr(fmt.Errorf(errorMessage))
+                       },
+                       args: args{
+                               ctx:          context.Background(),
+                               err:          fmt.Errorf(errorMessage),
+                               errorOutput:  nil,
+                               pipelineId:   pipelineId,
+                               subKey:       subKey,
+                               cacheService: &redis.Cache{Client: client},
+                               errorTitle:   "",
+                               newStatus:    0,
+                       },
+                       wantErr: true,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       tt.mocks()
+                       if err := processErrorWithSavingOutput(tt.args.ctx, 
tt.args.err, tt.args.errorOutput, tt.args.pipelineId, tt.args.subKey, 
tt.args.cacheService, tt.args.errorTitle, tt.args.newStatus); (err != nil) != 
tt.wantErr {
+                               t.Errorf("processErrorWithSavingOutput() error 
= %v, wantErr %v", err, tt.wantErr)
+                       }
+               })
+       }
+}
+
+func Test_processRunError(t *testing.T) {
+       client, mock := redismock.NewClientMock()
+       pipelineId := uuid.New()
+       errorMessage := "MOCK_ERROR"
+       subKey := cache.RunError
+       errorChannel := make(chan error, 1)
+       errorChannel <- fmt.Errorf(errorMessage)
+       type args struct {
+               ctx                   context.Context
+               errorChannel          chan error
+               errorOutput           []byte
+               pipelineId            uuid.UUID
+               cacheService          cache.Cache
+               stopReadLogsChannel   chan bool
+               finishReadLogsChannel chan bool
+       }
+       tests := []struct {
+               name    string
+               mocks   func()
+               args    args
+               wantErr bool
+       }{
+               {
+                       name: "Error during HSet operation",
+                       mocks: func() {
+                               mock.ExpectHSet(pipelineId.String(), 
subKey).SetErr(fmt.Errorf(errorMessage))
+                       },
+                       args: args{
+                               ctx:                   context.Background(),
+                               errorChannel:          errorChannel,
+                               errorOutput:           nil,
+                               pipelineId:            pipelineId,
+                               cacheService:          &redis.Cache{Client: 
client},
+                               stopReadLogsChannel:   nil,
+                               finishReadLogsChannel: nil,
+                       },
+                       wantErr: true,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       tt.mocks()
+                       if err := processRunError(tt.args.ctx, 
tt.args.errorChannel, tt.args.errorOutput, tt.args.pipelineId, 
tt.args.cacheService, tt.args.stopReadLogsChannel, 
tt.args.finishReadLogsChannel); (err != nil) != tt.wantErr {
+                               t.Errorf("processRunError() error = %v, wantErr 
%v", err, tt.wantErr)
+                       }
+               })
+       }
+}
+
+func Test_processCompileSuccess(t *testing.T) {
+       client, mock := redismock.NewClientMock()
+       pipelineId := uuid.New()
+       output := "output"
+       cacheMock := &redis.Cache{Client: client}
+       marshalLogs, _ := json.Marshal(cache.Logs)
+       marshalCompileOutput, _ := json.Marshal(cache.CompileOutput)
+       marshalRunOutput, _ := json.Marshal(cache.RunOutput)
+       marshalRunError, _ := json.Marshal(cache.RunError)
+       outputMarshal, _ := json.Marshal(output)
+       marshalEmptyString, _ := json.Marshal("")
+       type args struct {
+               ctx          context.Context
+               output       []byte
+               pipelineId   uuid.UUID
+               cacheService cache.Cache
+       }
+       tests := []struct {
+               name    string
+               mocks   func()
+               args    args
+               wantErr bool
+       }{
+               {
+                       name: "Error during set value to CompileOutput subKey",
+                       mocks: func() {
+                       },
+                       args: args{
+                               ctx:          context.Background(),
+                               output:       []byte(output),
+                               pipelineId:   pipelineId,
+                               cacheService: cacheMock,
+                       },
+                       wantErr: true,
+               },
+               {
+                       name: "Error during set value to RunOutput subKey",
+                       mocks: func() {
+                               mock.ExpectHSet(pipelineId.String(), 
marshalCompileOutput, outputMarshal).SetVal(1)
+                       },
+                       args: args{
+                               ctx:          context.Background(),
+                               output:       []byte(output),
+                               pipelineId:   pipelineId,
+                               cacheService: cacheMock,
+                       },
+                       wantErr: true,
+               },
+               {
+                       name: "Error during set value to RunError subKey",
+                       mocks: func() {
+                               mock.ExpectHSet(pipelineId.String(), 
marshalCompileOutput, outputMarshal).SetVal(1)
+                               mock.ExpectHSet(pipelineId.String(), 
marshalRunOutput, marshalEmptyString).SetVal(1)
+                       },
+                       args: args{
+                               ctx:          context.Background(),
+                               output:       []byte(output),
+                               pipelineId:   pipelineId,
+                               cacheService: cacheMock,
+                       },
+                       wantErr: true,
+               },
+               {
+                       name: "Error during set value to Logs subKey",
+                       mocks: func() {
+                               mock.ExpectHSet(pipelineId.String(), 
marshalCompileOutput, outputMarshal).SetVal(1)
+                               mock.ExpectHSet(pipelineId.String(), 
marshalRunOutput, marshalEmptyString).SetVal(1)
+                               mock.ExpectHSet(pipelineId.String(), 
marshalRunError, marshalEmptyString).SetVal(1)
+                       },
+                       args: args{
+                               ctx:          context.Background(),
+                               output:       []byte(output),
+                               pipelineId:   pipelineId,
+                               cacheService: cacheMock,
+                       },
+                       wantErr: true,
+               },
+               {
+                       name: "Error during set value to Graph subKey",
+                       mocks: func() {
+                               mock.ExpectHSet(pipelineId.String(), 
marshalCompileOutput, outputMarshal).SetVal(1)
+                               mock.ExpectHSet(pipelineId.String(), 
marshalRunOutput, marshalEmptyString).SetVal(1)
+                               mock.ExpectHSet(pipelineId.String(), 
marshalRunError, marshalEmptyString).SetVal(1)
+                               mock.ExpectHSet(pipelineId.String(), 
marshalLogs, marshalEmptyString).SetVal(1)
+                       },
+                       args: args{
+                               ctx:          context.Background(),
+                               output:       []byte(output),
+                               pipelineId:   pipelineId,
+                               cacheService: cacheMock,
+                       },
+                       wantErr: true,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       tt.mocks()
+                       if err := processCompileSuccess(tt.args.ctx, 
tt.args.output, tt.args.pipelineId, tt.args.cacheService); (err != nil) != 
tt.wantErr {
+                               t.Errorf("processCompileSuccess() error = %v, 
wantErr %v", err, tt.wantErr)
+                       }
+               })
+       }
+}
+
+func Test_readGraphFile(t *testing.T) {
+       pipelineLifeCycleCtx, _ := context.WithTimeout(context.Background(), 
1*time.Second)
+       type args struct {
+               pipelineLifeCycleCtx context.Context
+               backgroundCtx        context.Context
+               cacheService         cache.Cache
+               graphFilePath        string
+               pipelineId           uuid.UUID
+       }
+       tests := []struct {
+               name string
+               args args
+       }{
+               {
+                       name: "Successfully saving the prepared graph to the 
cache",
+                       args: args{
+                               pipelineLifeCycleCtx: pipelineLifeCycleCtx,
+                               backgroundCtx:        context.Background(),
+                               cacheService:         cacheService,
+                               graphFilePath:        graphFilePath,
+                               pipelineId:           uuid.New(),
+                       },

Review comment:
       Maybe, we can have this case as a negative one?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to