Skip to content

Commit

Permalink
chore: fixed action template search and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Jun 5, 2022
1 parent 56bf75d commit 380c2b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
14 changes: 10 additions & 4 deletions integration/action_template_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ func TestActionTemplateServiceAdd(t *testing.T) {
resource, err = client.ActionTemplates.Add(resource)
require.NoError(t, err)
require.NotNil(t, resource)

err = client.ActionTemplates.DeleteByID(resource.GetID())
assert.NoError(t, err)
defer client.ActionTemplates.DeleteByID(resource.GetID())
}

func TestActionTemplateServiceGetCategories(t *testing.T) {
Expand Down Expand Up @@ -97,7 +95,15 @@ func TestActionTemplateServiceSearch(t *testing.T) {
client := getOctopusClient()
require.NotNil(t, client)

resource, err := client.ActionTemplates.Search()
search := ""

resource, err := client.ActionTemplates.Search(search)
assert.NoError(t, err)
assert.NotEmpty(t, resource)

search = "Octopus.Script"

resource, err = client.ActionTemplates.Search(search)
assert.NoError(t, err)
assert.NotEmpty(t, resource)
}
28 changes: 21 additions & 7 deletions octopusdeploy/action_template_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package octopusdeploy

import (
"strings"

"github.com/OctopusDeploy/go-octopusdeploy/uritemplates"
"github.com/dghubble/sling"
"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -112,19 +115,30 @@ func (s actionTemplateService) GetByID(id string) (*ActionTemplate, error) {

// Search lists all available action templates including built-in, custom, and
// community-contributed step templates.
func (s actionTemplateService) Search() ([]ActionTemplateSearch, error) {
items := new([]ActionTemplateSearch)

func (s actionTemplateService) Search(searchQuery string) ([]ActionTemplateSearch, error) {
searchResults := []ActionTemplateSearch{}
err := validateInternalState(s)
if err != nil {
return *items, err
return searchResults, err
}

path := s.searchPath
template, err := uritemplates.Parse(s.searchPath)
if err != nil {
return searchResults, err
}

_, err = apiGet(s.getClient(), items, path)
path, err := template.Expand(map[string]interface{}{"type": searchQuery})
if err != nil {
return searchResults, err
}

return *items, err
if len(searchQuery) <= 0 {
path = strings.Split(path, "?")[0]
}

_, err = apiGet(s.getClient(), &searchResults, path)

return searchResults, err
}

// Update modifies an ActionTemplate based on the one provided as input.
Expand Down
33 changes: 1 addition & 32 deletions octopusdeploy/action_template_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ func createActionTemplate(t *testing.T) *ActionTemplate {
}

func createActionTemplateService(t *testing.T) *actionTemplateService {
categoriesPath := TestURIActionTemplatesCategories
logoPath := TestURIActionTemplatesLogo
searchPath := TestURIActionTemplatesSearch
versionedLogoPath := TestURIActionTemplateVersionedLogo

service := newActionTemplateService(nil, TestURIActionTemplates, categoriesPath, logoPath, searchPath, versionedLogoPath)
service := newActionTemplateService(nil, TestURIActionTemplates, TestURIActionTemplatesCategories, TestURIActionTemplatesLogo, TestURIActionTemplatesSearch, TestURIActionTemplateVersionedLogo)
testNewService(t, service, TestURIActionTemplates, ServiceActionTemplateService)
return service
}
Expand Down Expand Up @@ -70,13 +65,6 @@ func TestActionTemplateServiceAdd(t *testing.T) {

resource = createActionTemplate(t)
require.NotNil(t, resource)

resource, err = service.Add(resource)
require.NoError(t, err)
require.NotNil(t, resource)

err = service.DeleteByID(resource.GetID())
assert.NoError(t, err)
}

func TestActionTemplateServiceGetCategories(t *testing.T) {
Expand Down Expand Up @@ -104,16 +92,6 @@ func TestActionTemplateServiceGetByID(t *testing.T) {
resource, err = service.GetByID(id)
require.Error(t, err)
require.Nil(t, resource)

resources, err := service.GetAll()
require.NoError(t, err)
require.NotNil(t, resources)

for _, resource := range resources {
resourceToCompare, err := service.GetByID(resource.GetID())
require.NoError(t, err)
IsEqualActionTemplates(t, resource, resourceToCompare)
}
}

func TestActionTemplateServiceNew(t *testing.T) {
Expand Down Expand Up @@ -147,12 +125,3 @@ func TestActionTemplateServiceNew(t *testing.T) {
})
}
}

func TestActionTemplateServiceSearch(t *testing.T) {
service := createActionTemplateService(t)
require.NotNil(t, service)

resource, err := service.Search()
assert.NoError(t, err)
assert.NotEmpty(t, resource)
}

0 comments on commit 380c2b2

Please sign in to comment.