From da3dc6f95db700f01882df72a550c9eb2962405d Mon Sep 17 00:00:00 2001 From: Dmitrii Gridnev Date: Mon, 15 Jul 2024 12:55:33 +0200 Subject: [PATCH] refactor: the create test run command Connect a new version of client. Refactor the run service. --- go.mod | 2 +- go.sum | 4 +- internal/client/clientv1.go | 10 ++-- internal/service/run/mocks/run.go | 25 ++------- internal/service/run/run.go | 20 +------ internal/service/run/run_test.go | 92 +------------------------------ 6 files changed, 15 insertions(+), 138 deletions(-) diff --git a/go.mod b/go.mod index a06cd04..83c7a44 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( github.com/google/uuid v1.4.0 github.com/magiconair/properties v1.8.7 - github.com/qase-tms/qase-go/qase-api-client v1.0.0 + github.com/qase-tms/qase-go/qase-api-client v1.0.1 github.com/spf13/cobra v1.8.0 github.com/spf13/viper v1.18.2 go.uber.org/mock v0.4.0 diff --git a/go.sum b/go.sum index c2ce781..82cc94c 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,8 @@ github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/qase-tms/qase-go/qase-api-client v1.0.0 h1:bAzLbD1dncvHPLqVnJqeQO1ZgmFV+CvujAEUHpeYEt4= -github.com/qase-tms/qase-go/qase-api-client v1.0.0/go.mod h1:5w7jTtz8r3ccc8axnK0Sq6Dx5JKJtJnaz8AuysTw7SA= +github.com/qase-tms/qase-go/qase-api-client v1.0.1 h1:FTkG4y5im/B2C+RC398FokB/3AqL4i3LaY/ul/WymSU= +github.com/qase-tms/qase-go/qase-api-client v1.0.1/go.mod h1:5w7jTtz8r3ccc8axnK0Sq6Dx5JKJtJnaz8AuysTw7SA= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/internal/client/clientv1.go b/internal/client/clientv1.go index 0ca7fb4..c324941 100644 --- a/internal/client/clientv1.go +++ b/internal/client/clientv1.go @@ -235,7 +235,7 @@ func (c *ClientV1) GetPlans(ctx context.Context, projectCode string) ([]run.Plan } // CreateRun creates a new run -func (c *ClientV1) CreateRun(ctx context.Context, projectCode, title string, description string, envID, mileID, planID int64) (int64, error) { +func (c *ClientV1) CreateRun(ctx context.Context, projectCode, title string, description, envSlug string, mileID, planID int64) (int64, error) { const op = "client.clientv1.createrun" logger := slog.With("op", op) @@ -251,8 +251,8 @@ func (c *ClientV1) CreateRun(ctx context.Context, projectCode, title string, des m.SetDescription(description) } - if envID != 0 { - m.SetEnvironmentId(envID) + if envSlug != "" { + m.SetEnvironmentSlug(envSlug) } if mileID != 0 { @@ -272,7 +272,7 @@ func (c *ClientV1) CreateRun(ctx context.Context, projectCode, title string, des if err != nil { logger.Debug("failed to create run", "response", r) - return 0, fmt.Errorf("failed to create run: %w", err) + return 0, fmt.Errorf("failed to create run: %w. %s", err, r.Body) } logger.Info("created run", "runID", resp.Result.GetId(), "title", title, "description", description) @@ -295,7 +295,7 @@ func (c *ClientV1) CompleteRun(ctx context.Context, projectCode string, runId in if err != nil { logger.Debug("failed to complete run", "response", r) - return fmt.Errorf("failed to complete run: %w", err) + return fmt.Errorf("failed to complete run: %w. %s", err, r.Body) } logger.Info("completed run", "runId", runId) diff --git a/internal/service/run/mocks/run.go b/internal/service/run/mocks/run.go index c698c1c..8296ca2 100644 --- a/internal/service/run/mocks/run.go +++ b/internal/service/run/mocks/run.go @@ -5,7 +5,6 @@ // // mockgen -source=run.go -destination=/Users/gda/Documents/github/qase-tms/qasectl/internal/service/run/mocks/run.go -package=mocks // - // Package mocks is a generated GoMock package. package mocks @@ -13,7 +12,6 @@ import ( context "context" reflect "reflect" - run "github.com/qase-tms/qasectl/internal/models/run" gomock "go.uber.org/mock/gomock" ) @@ -55,31 +53,16 @@ func (mr *MockclientMockRecorder) CompleteRun(ctx, projectCode, runId any) *gomo } // CreateRun mocks base method. -func (m *Mockclient) CreateRun(ctx context.Context, projectCode, title, description string, envID, mileID, planID int64) (int64, error) { +func (m *Mockclient) CreateRun(ctx context.Context, projectCode, title, description, envSlug string, mileID, planID int64) (int64, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateRun", ctx, projectCode, title, description, envID, mileID, planID) + ret := m.ctrl.Call(m, "CreateRun", ctx, projectCode, title, description, envSlug, mileID, planID) ret0, _ := ret[0].(int64) ret1, _ := ret[1].(error) return ret0, ret1 } // CreateRun indicates an expected call of CreateRun. -func (mr *MockclientMockRecorder) CreateRun(ctx, projectCode, title, description, envID, mileID, planID any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateRun", reflect.TypeOf((*Mockclient)(nil).CreateRun), ctx, projectCode, title, description, envID, mileID, planID) -} - -// GetEnvironments mocks base method. -func (m *Mockclient) GetEnvironments(ctx context.Context, projectCode string) ([]run.Environment, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetEnvironments", ctx, projectCode) - ret0, _ := ret[0].([]run.Environment) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetEnvironments indicates an expected call of GetEnvironments. -func (mr *MockclientMockRecorder) GetEnvironments(ctx, projectCode any) *gomock.Call { +func (mr *MockclientMockRecorder) CreateRun(ctx, projectCode, title, description, envSlug, mileID, planID any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetEnvironments", reflect.TypeOf((*Mockclient)(nil).GetEnvironments), ctx, projectCode) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateRun", reflect.TypeOf((*Mockclient)(nil).CreateRun), ctx, projectCode, title, description, envSlug, mileID, planID) } diff --git a/internal/service/run/run.go b/internal/service/run/run.go index 4c1c49c..1919056 100644 --- a/internal/service/run/run.go +++ b/internal/service/run/run.go @@ -2,16 +2,13 @@ package run import ( "context" - "fmt" - "github.com/qase-tms/qasectl/internal/models/run" ) // client is a client for run // //go:generate mockgen -source=$GOFILE -destination=$PWD/mocks/${GOFILE} -package=mocks type client interface { - GetEnvironments(ctx context.Context, projectCode string) ([]run.Environment, error) - CreateRun(ctx context.Context, projectCode, title, description string, envID, mileID, planID int64) (int64, error) + CreateRun(ctx context.Context, projectCode, title string, description, envSlug string, mileID, planID int64) (int64, error) CompleteRun(ctx context.Context, projectCode string, runId int64) error } @@ -27,20 +24,7 @@ func NewService(client client) *Service { // CreateRun creates a new run func (s *Service) CreateRun(ctx context.Context, pc, t, d, e string, m, plan int64) (int64, error) { - var envID int64 = 0 - if e != "" { - es, err := s.client.GetEnvironments(ctx, pc) - if err != nil { - return 0, fmt.Errorf("failed to get environments: %w", err) - } - for _, env := range es { - if env.Slug == e { - envID = env.ID - } - } - } - - return s.client.CreateRun(ctx, pc, t, d, envID, m, plan) + return s.client.CreateRun(ctx, pc, t, d, e, m, plan) } // CompleteRun completes a run diff --git a/internal/service/run/run_test.go b/internal/service/run/run_test.go index 2897921..4302a2d 100644 --- a/internal/service/run/run_test.go +++ b/internal/service/run/run_test.go @@ -4,7 +4,6 @@ import ( "context" "errors" "github.com/magiconair/properties/assert" - "github.com/qase-tms/qasectl/internal/models/run" "go.uber.org/mock/gomock" "testing" ) @@ -73,14 +72,9 @@ func TestService_CreateRun(t *testing.T) { plan int64 args baseArgs } - type eArgs struct { - models []run.Environment - args baseArgs - } tests := []struct { name string args args - eArgs eArgs want int64 wantErr bool errMessage string @@ -99,79 +93,10 @@ func TestService_CreateRun(t *testing.T) { isUsed: true, }, }, - eArgs: eArgs{ - models: []run.Environment{ - { - ID: 1, - Slug: "test", - }}, - args: baseArgs{ - err: nil, - isUsed: true, - }, - }, - want: 1, - wantErr: false, - errMessage: "", - }, - { - name: "environment not found", - args: args{ - pc: "test", - t: "test", - d: "test", - e: "test", - m: 0, - plan: 0, - args: baseArgs{ - err: nil, - isUsed: true, - }, - }, - eArgs: eArgs{ - models: []run.Environment{ - { - ID: 0, - Slug: "test1", - }}, - args: baseArgs{ - err: nil, - isUsed: true, - }, - }, want: 1, wantErr: false, errMessage: "", }, - { - name: "failed to get environments", - args: args{ - pc: "test", - t: "test", - d: "test", - e: "test", - m: 0, - plan: 0, - args: baseArgs{ - err: nil, - isUsed: false, - }, - }, - eArgs: eArgs{ - models: []run.Environment{ - { - ID: 0, - Slug: "test", - }}, - args: baseArgs{ - err: errors.New("error"), - isUsed: true, - }, - }, - want: 0, - wantErr: true, - errMessage: "failed to get environments: error", - }, { name: "failed to create run", args: args{ @@ -186,17 +111,6 @@ func TestService_CreateRun(t *testing.T) { isUsed: true, }, }, - eArgs: eArgs{ - models: []run.Environment{ - { - ID: 0, - Slug: "test", - }}, - args: baseArgs{ - err: nil, - isUsed: true, - }, - }, want: 0, wantErr: true, errMessage: "error", @@ -206,16 +120,12 @@ func TestService_CreateRun(t *testing.T) { t.Run(tt.name, func(t *testing.T) { f := newFixture(t) - if tt.eArgs.args.isUsed { - f.client.EXPECT().GetEnvironments(gomock.Any(), tt.args.pc).Return(tt.eArgs.models, tt.eArgs.args.err) - } - if tt.args.args.isUsed { f.client.EXPECT().CreateRun(gomock.Any(), tt.args.pc, tt.args.t, tt.args.d, - tt.eArgs.models[0].ID, + tt.args.e, tt.args.m, tt.args.plan, ).