Skip to content

Commit

Permalink
Added UT for creating a template with same name after deleting one
Browse files Browse the repository at this point in the history
Signed-off-by: parauliya <[email protected]>
  • Loading branch information
parauliya committed Dec 7, 2020
1 parent dee0a3f commit 0f7e7de
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
21 changes: 15 additions & 6 deletions db/mock/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

type Template struct {
ID uuid.UUID
Data string
ID uuid.UUID
Data string
Deleted bool
}

// CreateTemplate creates a new workflow template
Expand All @@ -20,12 +21,20 @@ func (d DB) CreateTemplate(ctx context.Context, name string, data string, id uui
}

if _, ok := d.TemplateDB[name]; ok {
return errors.New("Template name already exist in the database")
tmpl := d.TemplateDB[name]
switch stmpl := tmpl.(type) {
case Template:
if !stmpl.Deleted {
return errors.New("Template name already exist in the database")
}
default:
return errors.New("Not a Template type in the database")
}
}

d.TemplateDB[name] = Template{
ID: id,
Data: data,
ID: id,
Data: data,
Deleted: false,
}

return nil
Expand Down
29 changes: 27 additions & 2 deletions grpc-server/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func TestCreateTemplate(t *testing.T) {
args: args{
db: mock.DB{
TemplateDB: map[string]interface{}{
"template_1": template1,
"template_1": mock.Template{
Data: template1,
Deleted: false,
},
},
},
name: "template_2",
Expand All @@ -98,7 +101,10 @@ func TestCreateTemplate(t *testing.T) {
args: args{
db: mock.DB{
TemplateDB: map[string]interface{}{
"template_1": template1,
"template_1": mock.Template{
Data: template1,
Deleted: false,
},
},
},
name: "template_1",
Expand All @@ -108,6 +114,24 @@ func TestCreateTemplate(t *testing.T) {
expectedError: true,
},
},

"SuccessfulTemplateCreationAfterDeletingWithSameName": {
args: args{
db: mock.DB{
TemplateDB: map[string]interface{}{
"template_1": mock.Template{
Data: template1,
Deleted: true,
},
},
},
name: "template_1",
template: template2,
},
want: want{
expectedError: false,
},
},
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand All @@ -124,6 +148,7 @@ func TestCreateTemplate(t *testing.T) {
assert.Nil(t, err)
assert.NotEmpty(t, res)
}
tc.args.db.ClearTemplateDB()
})
}
}
Expand Down

0 comments on commit 0f7e7de

Please sign in to comment.