Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getting template by name not returning the associated id #362

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type hardware interface {

type template interface {
CreateTemplate(ctx context.Context, name string, data string, id uuid.UUID) error
GetTemplate(ctx context.Context, fields map[string]string) (string, string, error)
GetTemplate(ctx context.Context, fields map[string]string) (string, string, string, error)
DeleteTemplate(ctx context.Context, name string) error
ListTemplates(in string, fn func(id, n string, in, del *timestamp.Timestamp) error) error
UpdateTemplate(ctx context.Context, name string, data string, id uuid.UUID) error
Expand Down
2 changes: 1 addition & 1 deletion db/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ type DB struct {
InsertIntoWorkflowEventTableFunc func(ctx context.Context, wfEvent *pb.WorkflowActionStatus, time time.Time) error
// template
TemplateDB map[string]interface{}
GetTemplateFunc func(ctx context.Context, fields map[string]string) (string, string, error)
GetTemplateFunc func(ctx context.Context, fields map[string]string) (string, string, string, error)
}
2 changes: 1 addition & 1 deletion db/mock/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (d DB) CreateTemplate(ctx context.Context, name string, data string, id uui
}

// GetTemplate returns a workflow template
func (d DB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, error) {
func (d DB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, string, error) {
return d.GetTemplateFunc(ctx, fields)
}

Expand Down
13 changes: 7 additions & 6 deletions db/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,32 @@ func (d TinkDB) CreateTemplate(ctx context.Context, name string, data string, id
}

// GetTemplate returns a workflow template
func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, error) {
func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, string, error) {
getCondition, err := buildGetCondition(fields)
if err != nil {
return "", "", errors.Wrap(err, "failed to build get condition")
return "", "", "", errors.Wrap(err, "failed to build get condition")
}

query := `
SELECT name, data
SELECT id, name, data
FROM template
WHERE
` + getCondition + `
deleted_at IS NULL
`
row := d.instance.QueryRowContext(ctx, query)
id := []byte{}
name := []byte{}
data := []byte{}
err = row.Scan(&name, &data)
err = row.Scan(&id, &name, &data)
if err == nil {
return string(name), string(data), nil
return string(id), string(name), string(data), nil
}
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
}
return "", "", err
return "", "", "", err
}

// DeleteTemplate deletes a workflow template
Expand Down
4 changes: 2 additions & 2 deletions grpc-server/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *server) GetTemplate(ctx context.Context, in *template.GetRequest) (*tem
"id": in.GetId(),
"name": in.GetName(),
}
n, d, err := s.db.GetTemplate(ctx, fields)
id, n, d, err := s.db.GetTemplate(ctx, fields)
logger.Info("done " + msg)
if err != nil {
metrics.CacheErrors.With(labels).Inc()
Expand All @@ -72,7 +72,7 @@ func (s *server) GetTemplate(ctx context.Context, in *template.GetRequest) (*tem
}
l.Error(err)
}
return &template.WorkflowTemplate{Id: in.GetId(), Name: n, Data: d}, err
return &template.WorkflowTemplate{Id: id, Name: n, Data: d}, err
}

// DeleteTemplate implements template.DeleteTemplate
Expand Down
48 changes: 24 additions & 24 deletions grpc-server/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ func TestGetTemplate(t *testing.T) {
TemplateDB: map[string]interface{}{
templateName1: template1,
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {
t.Log("in get template func")

if fields["id"] == templateID1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
if fields["name"] == templateName1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
return "", "", errors.New("failed to get template")
return "", "", "", errors.New("failed to get template")
},
},
getRequest: &pb.GetRequest{GetBy: &pb.GetRequest_Name{Name: templateName1}},
Expand All @@ -155,16 +155,16 @@ func TestGetTemplate(t *testing.T) {
TemplateDB: map[string]interface{}{
templateName1: template1,
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {
t.Log("in get template func")

if fields["id"] == templateID1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
if fields["name"] == templateName1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
return "", "", errors.New("failed to get template")
return "", "", "", errors.New("failed to get template")
},
},
getRequest: &pb.GetRequest{GetBy: &pb.GetRequest_Name{Name: templateName2}},
Expand All @@ -178,16 +178,16 @@ func TestGetTemplate(t *testing.T) {
TemplateDB: map[string]interface{}{
templateName1: template1,
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {
t.Log("in get template func")

if fields["id"] == templateID1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
if fields["name"] == templateName1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
return "", "", errors.New("failed to get template")
return "", "", "", errors.New("failed to get template")
},
},
getRequest: &pb.GetRequest{GetBy: &pb.GetRequest_Id{Id: templateID1}},
Expand All @@ -201,16 +201,16 @@ func TestGetTemplate(t *testing.T) {
TemplateDB: map[string]interface{}{
templateName1: template1,
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {
t.Log("in get template func")

if fields["id"] == templateID1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
if fields["name"] == templateName1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
return "", "", errors.New("failed to get template")
return "", "", "", errors.New("failed to get template")
},
},
getRequest: &pb.GetRequest{GetBy: &pb.GetRequest_Id{Id: templateID2}},
Expand All @@ -224,16 +224,16 @@ func TestGetTemplate(t *testing.T) {
TemplateDB: map[string]interface{}{
templateName1: template1,
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {
t.Log("in get template func")

if fields["id"] == templateID1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
if fields["name"] == templateName1 {
return "", template1, nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 if statements can be combined as one.

return templateID1, templateName1, template1, nil
}
return "", "", errors.New("failed to get template")
return "", "", "", errors.New("failed to get template")
},
},
getRequest: &pb.GetRequest{},
Expand All @@ -247,16 +247,16 @@ func TestGetTemplate(t *testing.T) {
TemplateDB: map[string]interface{}{
templateName1: template1,
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {
t.Log("in get template func")

if fields["id"] == templateID1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
if fields["name"] == templateName1 {
return "", template1, nil
return templateID1, templateName1, template1, nil
}
return "", "", errors.New("failed to get template")
return "", "", "", errors.New("failed to get template")
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion grpc-server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func createYaml(ctx context.Context, db db.Database, templateID string, devices
fields := map[string]string{
"id": templateID,
}
_, templateData, err := db.GetTemplate(ctx, fields)
_, _, templateData, err := db.GetTemplate(ctx, fields)
if err != nil {
return "", errors.Wrapf(err, errFailedToGetTemplate, templateID)
}
Expand Down
14 changes: 7 additions & 7 deletions grpc-server/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func TestCreateWorkflow(t *testing.T) {
args args
want want
}{
"FailedToGetTempalte": {
"FailedToGetTemplate": {
args: args{
db: mock.DB{
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
return "", "", errors.New("failed to get template")
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {
return "", "", "", errors.New("failed to get template")
},
},
wfTemplate: templateID,
Expand All @@ -58,8 +58,8 @@ func TestCreateWorkflow(t *testing.T) {
"FailedCreatingWorkflow": {
args: args{
db: mock.DB{
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
return "", templateData, nil
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of returning an empty string, maybe we can return a pre-defined name for mock (e.g., MockedTemplate?).

return "", "", templateData, nil
},
CreateWorkflowFunc: func(ctx context.Context, wf db.Workflow, data string, id uuid.UUID) error {
return errors.New("failed to create a workfow")
Expand All @@ -75,8 +75,8 @@ func TestCreateWorkflow(t *testing.T) {
"SuccessCreatingWorkflow": {
args: args{
db: mock.DB{
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, error) {
return "", templateData, nil
GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) {
return "", "", templateData, nil
},
CreateWorkflowFunc: func(ctx context.Context, wf db.Workflow, data string, id uuid.UUID) error {
return nil
Expand Down