From da98caba4917bf22e45487d271748d607ec4a04e Mon Sep 17 00:00:00 2001 From: jmeridth Date: Thu, 21 Jan 2021 10:48:57 -0600 Subject: [PATCH 1/5] Add Search Results Count to sdk - [x] add Count func to Search interface - [x] implement Count func - [x] add test - [x] add profile.cov to .gitignore Co-authored-by: Jason Meridth Co-authored-by: Matt Lopez " --- .gitignore | 3 ++- fixture/GET/search_count_ticket.json | 3 +++ zendesk/search.go | 35 ++++++++++++++++++++++++++++ zendesk/search_test.go | 16 +++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 fixture/GET/search_count_ticket.json diff --git a/.gitignore b/.gitignore index 1c55b44e..4837de48 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,8 @@ vendor/ # coverage by go test coverage.txt +profile.cov .idea *.iml -.pre-commit-config.yaml \ No newline at end of file +.pre-commit-config.yaml diff --git a/fixture/GET/search_count_ticket.json b/fixture/GET/search_count_ticket.json new file mode 100644 index 00000000..3205fc91 --- /dev/null +++ b/fixture/GET/search_count_ticket.json @@ -0,0 +1,3 @@ +{ + "count": 10 +} diff --git a/zendesk/search.go b/zendesk/search.go index 0cff9889..3dfb70ee 100644 --- a/zendesk/search.go +++ b/zendesk/search.go @@ -16,8 +16,13 @@ type SearchOptions struct { SortOrder string `url:"sort_order,omitempty"` } +type CountOptions struct { + Query string `url:"query"` +} + type SearchAPI interface { Search(ctx context.Context, opts *SearchOptions) (SearchResults, Page, error) + Count(ctx context.Context, opts *CountOptions) (int, error) } type SearchResults struct { @@ -140,3 +145,33 @@ func (z *Client) Search(ctx context.Context, opts *SearchOptions) (SearchResults return data.Results, data.Page, nil } + +// Count allows users to get count of results of a query of zendesk's unified search api. +// +// ref: https://developer.zendesk.com/rest_api/docs/support/search#show-results-count +func (z *Client) Count(ctx context.Context, opts *CountOptions) (int, error) { + var data struct { + Count int `json:"count"` + } + + if opts == nil { + return 0, &OptionsError{opts} + } + + u, err := addOptions("/search/count.json", opts) + if err != nil { + return 0, err + } + + body, err := z.get(ctx, u) + if err != nil { + return 0, err + } + + err = json.Unmarshal(body, &data) + if err != nil { + return 0, err + } + + return data.Count, nil +} diff --git a/zendesk/search_test.go b/zendesk/search_test.go index 4d3668f7..bd6ef341 100644 --- a/zendesk/search_test.go +++ b/zendesk/search_test.go @@ -33,6 +33,22 @@ func TestSearchTickets(t *testing.T) { } } +func TestCountTickets(t *testing.T) { + mockAPI := newMockAPI(http.MethodGet, "search_count_ticket.json") + client := newTestClient(mockAPI) + defer mockAPI.Close() + + count, err := client.Count(ctx, &CountOptions{}) + if err != nil { + t.Fatalf("Failed to get count: %s", err) + } + + expected := 10 + if count != expected { + t.Fatalf("expected count of tickets is %d, but got %d", expected, count) + } +} + func BenchmarkUnmarshalSearchResults(b *testing.B) { file := readFixture("ticket_result.json") for i := 0; i < b.N; i++ { From 33f0a363f20f79d929bfd39f661324673ad9dd06 Mon Sep 17 00:00:00 2001 From: jmeridth Date: Thu, 21 Jan 2021 11:09:25 -0600 Subject: [PATCH 2/5] Add comment to CountOptions type to fix linting - [x] change func Count to SearchCount for clarity off client --- zendesk/search.go | 7 +++++-- zendesk/search_test.go | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/zendesk/search.go b/zendesk/search.go index 3dfb70ee..1ceaa31c 100644 --- a/zendesk/search.go +++ b/zendesk/search.go @@ -16,13 +16,16 @@ type SearchOptions struct { SortOrder string `url:"sort_order,omitempty"` } +// CountOptions are the options that can be provided to the search results count API +// +// ref: https://developer.zendesk.com/rest_api/docs/support/search#show-results-count type CountOptions struct { Query string `url:"query"` } type SearchAPI interface { Search(ctx context.Context, opts *SearchOptions) (SearchResults, Page, error) - Count(ctx context.Context, opts *CountOptions) (int, error) + SearchCount(ctx context.Context, opts *CountOptions) (int, error) } type SearchResults struct { @@ -149,7 +152,7 @@ func (z *Client) Search(ctx context.Context, opts *SearchOptions) (SearchResults // Count allows users to get count of results of a query of zendesk's unified search api. // // ref: https://developer.zendesk.com/rest_api/docs/support/search#show-results-count -func (z *Client) Count(ctx context.Context, opts *CountOptions) (int, error) { +func (z *Client) SearchCount(ctx context.Context, opts *CountOptions) (int, error) { var data struct { Count int `json:"count"` } diff --git a/zendesk/search_test.go b/zendesk/search_test.go index bd6ef341..29422ba9 100644 --- a/zendesk/search_test.go +++ b/zendesk/search_test.go @@ -38,7 +38,7 @@ func TestCountTickets(t *testing.T) { client := newTestClient(mockAPI) defer mockAPI.Close() - count, err := client.Count(ctx, &CountOptions{}) + count, err := client.SearchCount(ctx, &CountOptions{}) if err != nil { t.Fatalf("Failed to get count: %s", err) } From 6906cd8f4f5793cce230f97b6c70ed9f3208b181 Mon Sep 17 00:00:00 2001 From: jmeridth Date: Thu, 21 Jan 2021 11:47:42 -0600 Subject: [PATCH 3/5] Add generated mocks to client.go for SearchCount Co-authored-by: Jason Meridth Co-authored-by: Matt Lopez --- zendesk/mock/client.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go index caea67f1..665d6fb0 100644 --- a/zendesk/mock/client.go +++ b/zendesk/mock/client.go @@ -953,6 +953,22 @@ func (mr *ClientMockRecorder) Search(arg0, arg1 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Search", reflect.TypeOf((*Client)(nil).Search), arg0, arg1) } +// SearchCount mocks base method +func (m *Client) SearchCount(ctx context.Context, opts *zendesk.CountOptions) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SearchCount", ctx, opts) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SearchCount indicates an expected call of SearchCount +func (mr *ClientMockRecorder) SearchCount(ctx, opts interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SearchCount", reflect.TypeOf((*Client)(nil).SearchCount), ctx, + opts) +} + // UpdateAutomation mocks base method. func (m *Client) UpdateAutomation(arg0 context.Context, arg1 int64, arg2 zendesk.Automation) (zendesk.Automation, error) { m.ctrl.T.Helper() From 3a5af13c882d9bdc01b83df43df475d8b5d1da00 Mon Sep 17 00:00:00 2001 From: jmeridth Date: Thu, 21 Jan 2021 11:50:20 -0600 Subject: [PATCH 4/5] Fix linting of comment Co-authored-by: Jason Meridth Co-authored-by: Matt Lopez --- zendesk/search.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zendesk/search.go b/zendesk/search.go index 1ceaa31c..576316de 100644 --- a/zendesk/search.go +++ b/zendesk/search.go @@ -149,7 +149,7 @@ func (z *Client) Search(ctx context.Context, opts *SearchOptions) (SearchResults return data.Results, data.Page, nil } -// Count allows users to get count of results of a query of zendesk's unified search api. +// SearchCount allows users to get count of results of a query of zendesk's unified search api. // // ref: https://developer.zendesk.com/rest_api/docs/support/search#show-results-count func (z *Client) SearchCount(ctx context.Context, opts *CountOptions) (int, error) { From a906b5bb9a137d56316974afe532df584a9ae54f Mon Sep 17 00:00:00 2001 From: jmeridth Date: Thu, 21 Jan 2021 19:43:52 -0600 Subject: [PATCH 5/5] add note to README on how to regenerate mock client --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index d8049d49..004fc1ff 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ func main() { go-zendesk has a [mock package](https://pkg.go.dev/github.com/nukosuke/go-zendesk/zendesk/mock) generated by [golang/mock](https://github.com/golang/mock). You can simulate the response from Zendesk API with it. +## To regenerate the mock client + +`go generate ./...` + ## Authors - [nukosuke](https://github.com/nukosuke) - [tamccall](https://github.com/tamccall)