From 2b1bef54cea94d7f784bf875bbeaedfac057e73e Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Thu, 3 May 2018 00:41:24 -0400 Subject: [PATCH 1/2] add failing unit tests --- .../smart-search/queryset.service-test.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/awx/ui/test/spec/smart-search/queryset.service-test.js b/awx/ui/test/spec/smart-search/queryset.service-test.js index bd1f5ae833..eb8563c37d 100644 --- a/awx/ui/test/spec/smart-search/queryset.service-test.js +++ b/awx/ui/test/spec/smart-search/queryset.service-test.js @@ -58,6 +58,32 @@ describe('Service: QuerySet', () => { }); }); + describe('getSearchInputQueryset', () => { + it('creates the expected queryset', () =>{ + spyOn(QuerySet, 'encodeParam').and.callThrough(); + + const term = 'name:foo'; + const isFilterableBaseField = () => true; + const isRelatedField = () => false; + + expect(QuerySet.getSearchInputQueryset(term, isFilterableBaseField, isRelatedField)).toEqual({ name__icontains_DEFAULT: 'foo' }); + expect(QuerySet.encodeParam).toHaveBeenCalledWith({ term: "name:foo", searchTerm: true, singleSearchParam: null }); + }); + }); + + describe('removeTermsFromQueryset', () => { + it('creates the expected queryset', () =>{ + spyOn(QuerySet, 'encodeParam').and.callThrough(); + + const queryset = { page_size: "20", order_by: "name", project__search_DEFAULT: "foo" }; + const term = 'project:foo'; + const isFilterableBaseField = () => false; + const isRelatedField = () => true; + + expect(QuerySet.removeTermsFromQueryset(queryset, term, isFilterableBaseField, isRelatedField)).toEqual({ page_size: "20", order_by: "name" }); + expect(QuerySet.encodeParam).toHaveBeenCalledWith({ term: 'project:foo', relatedSearchTerm: true, singleSearchParam: null }); + }); + }); describe('fn search', () => { let pattern = /\/api\/v2\/inventories\/(.+)\/groups\/*/, From 1ed9f609fcb17951d193a89f4934d747029b0f89 Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Thu, 3 May 2018 00:42:36 -0400 Subject: [PATCH 2/2] use correct param names when calling encodeParams --- .../client/src/shared/smart-search/queryset.service.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/awx/ui/client/src/shared/smart-search/queryset.service.js b/awx/ui/client/src/shared/smart-search/queryset.service.js index 7d34a40a4e..ba8aad5dc5 100644 --- a/awx/ui/client/src/shared/smart-search/queryset.service.js +++ b/awx/ui/client/src/shared/smart-search/queryset.service.js @@ -104,6 +104,7 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc } let paramString = exclude ? "not__" : ""; let valueString = paramParts[1]; + if(keySplit.length === 1) { if(searchTerm && !lessThanGreaterThan) { if(singleSearchParam) { @@ -407,9 +408,9 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc if (termParts.length === 1) { termParams = searchWithoutKey(term, singleSearchParam); } else if ((isAnsibleFactField && isAnsibleFactField(termParts)) || (isFilterableBaseField && isFilterableBaseField(termParts))) { - termParams = this.encodeParam({ term, singleSearchParam }); + termParams = this.encodeParam({ term, singleSearchParam, searchTerm: true }); } else if (isRelatedField && isRelatedField(termParts)) { - termParams = this.encodeParam({ term, singleSearchParam, related: true }); + termParams = this.encodeParam({ term, singleSearchParam, relatedSearchTerm: true }); } else { termParams = searchWithoutKey(term, singleSearchParam); } @@ -458,9 +459,9 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc if (termParts.length === 1) { removed = searchWithoutKey(term, singleSearchParam); } else if ((isAnsibleFactField && isAnsibleFactField(termParts)) || (isFilterableBaseField && isFilterableBaseField(termParts))) { - removed = this.encodeParam({ term, singleSearchParam }); + removed = this.encodeParam({ term, singleSearchParam, searchTerm: true }); } else if (isRelatedField && isRelatedField(termParts)) { - removed = this.encodeParam({ term, singleSearchParam, related: true }); + removed = this.encodeParam({ term, singleSearchParam, relatedSearchTerm: true }); } else { removed = searchWithoutKey(term, singleSearchParam); }