Skip to content

Commit

Permalink
refactor: the create test run command
Browse files Browse the repository at this point in the history
Connect a new version of client.
Refactor the run service.
  • Loading branch information
gibiw committed Jul 15, 2024
1 parent 5512538 commit da3dc6f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 138 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
10 changes: 5 additions & 5 deletions internal/client/clientv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down
25 changes: 4 additions & 21 deletions internal/service/run/mocks/run.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 2 additions & 18 deletions internal/service/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
92 changes: 1 addition & 91 deletions internal/service/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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",
Expand All @@ -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,
).
Expand Down

0 comments on commit da3dc6f

Please sign in to comment.