Skip to content

Commit

Permalink
fix: return error code if uploading fails
Browse files Browse the repository at this point in the history
- The `upload` function now returns an error if an issue occurs during the upload process.
- Updated unit tests to reflect the new error-handling behavior.

#58
  • Loading branch information
gibiw committed Dec 12, 2024
1 parent 0aac299 commit 0f93da6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
5 changes: 4 additions & 1 deletion cmd/testops/result/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ func Command() *cobra.Command {
Suite: suite,
}

s.Upload(cmd.Context(), param)
err := s.Upload(cmd.Context(), param)
if err != nil {
return err
}

logger.Info("Results uploaded successfully")

Expand Down
1 change: 1 addition & 0 deletions internal/parsers/junit/junit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package junit
21 changes: 11 additions & 10 deletions internal/service/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package result

import (
"context"
"fmt"
models "github.com/qase-tms/qasectl/internal/models/result"
"log/slog"
)
Expand Down Expand Up @@ -35,29 +36,27 @@ func NewService(client client, parser Parser, rs runService) *Service {
}

// Upload imports the data
func (s *Service) Upload(ctx context.Context, p UploadParams) {
func (s *Service) Upload(ctx context.Context, p UploadParams) error {
const op = "result.parser.import"
logger := slog.With("op", op)

results, err := s.parser.Parse()
if err != nil {
logger.Error("failed to parse results", "error", err)
return
return fmt.Errorf("failed to parse results: %w", err)
}

logger.Info("number of results found", "count", len(results))

if len(results) == 0 {
logger.Info("no results to upload")
return
return fmt.Errorf("no results to upload")
}

runID := p.RunID
isTestRunCreated := false
if runID == 0 {
ID, err := s.rs.CreateRun(ctx, p.Project, p.Title, p.Description, "", 0, 0)
if err != nil {
logger.Error("failed to create run", "error", err)
return
return err
}
runID = ID
isTestRunCreated = true
Expand All @@ -78,7 +77,7 @@ func (s *Service) Upload(ctx context.Context, p UploadParams) {
if int64(len(results)) < p.Batch {
err := s.client.UploadData(ctx, p.Project, runID, results)
if err != nil {
logger.Error("failed to upload results", "error", err)
return fmt.Errorf("failed to upload results: %w", err)
}
} else {
for i := int64(0); i < int64(len(results)); i += p.Batch {
Expand All @@ -90,15 +89,17 @@ func (s *Service) Upload(ctx context.Context, p UploadParams) {

err := s.client.UploadData(ctx, p.Project, runID, results[i:end])
if err != nil {
logger.Error("failed to upload results", "error", err)
return fmt.Errorf("failed to upload results: %w", err)
}
}
}

if isTestRunCreated {
err := s.rs.CompleteRun(ctx, p.Project, runID)
if err != nil {
logger.Error("failed to complete run", "error", err)
return err
}
}

return nil
}
44 changes: 23 additions & 21 deletions internal/service/result/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func TestService_Upload(t *testing.T) {
err: nil,
isUsed: false,
},
wantErr: false,
errMessage: "",
wantErr: true,
errMessage: "failed to parse results: failed parser",
},
{
name: "empty results",
Expand Down Expand Up @@ -201,8 +201,8 @@ func TestService_Upload(t *testing.T) {
err: nil,
isUsed: false,
},
wantErr: false,
errMessage: "",
wantErr: true,
errMessage: "no results to upload",
},
{
name: "failed create test run",
Expand Down Expand Up @@ -234,8 +234,8 @@ func TestService_Upload(t *testing.T) {
err: nil,
isUsed: false,
},
wantErr: false,
errMessage: "",
wantErr: true,
errMessage: "failed create test run",
},
{
name: "use batch",
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestService_Upload(t *testing.T) {
},
err: errors.New("failed upload data"),
isUsed: true,
count: 2,
count: 1,
runID: 1,
},
pArgs: pArgs{
Expand All @@ -298,10 +298,10 @@ func TestService_Upload(t *testing.T) {
},
cArgs: cArgs{
err: nil,
isUsed: true,
isUsed: false,
},
wantErr: false,
errMessage: "",
wantErr: true,
errMessage: "failed to upload results: failed upload data",
},
{
name: "failed upload data",
Expand Down Expand Up @@ -331,10 +331,10 @@ func TestService_Upload(t *testing.T) {
},
cArgs: cArgs{
err: nil,
isUsed: true,
isUsed: false,
},
wantErr: false,
errMessage: "",
wantErr: true,
errMessage: "failed to upload results: failed upload data",
},
{
name: "failed complete run",
Expand Down Expand Up @@ -366,8 +366,8 @@ func TestService_Upload(t *testing.T) {
err: errors.New("failed complete run"),
isUsed: true,
},
wantErr: false,
errMessage: "",
wantErr: true,
errMessage: "failed complete run",
},
}
for _, tt := range tests {
Expand All @@ -387,15 +387,17 @@ func TestService_Upload(t *testing.T) {
}

if tt.args.isUsed {
if tt.args.count != 1 {
f.client.EXPECT().UploadData(gomock.Any(), tt.args.p.Project, tt.args.runID, gomock.Any()).Return(tt.args.err).Times(tt.args.count)
} else {
f.client.EXPECT().UploadData(gomock.Any(), tt.args.p.Project, tt.args.runID, tt.pArgs.models).Return(tt.args.err).Times(tt.args.count)
}
f.client.EXPECT().UploadData(gomock.Any(), tt.args.p.Project, tt.args.runID, gomock.Any()).Return(tt.args.err).Times(tt.args.count)
}
s := NewService(f.client, f.parser, f.rs)

s.Upload(context.Background(), tt.args.p)
err := s.Upload(context.Background(), tt.args.p)
if (err != nil) != tt.wantErr {
t.Errorf("Service.Upload() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil && err.Error() != tt.errMessage {
t.Errorf("Service.Upload() error = %v, wantErr %v", err, tt.errMessage)
}
})
}
}

0 comments on commit 0f93da6

Please sign in to comment.