From 6a50c911bc9c32b5f14be9ce31fdbaaffef15840 Mon Sep 17 00:00:00 2001 From: Nick dos Remedios Date: Mon, 13 Jan 2025 15:13:17 +1100 Subject: [PATCH] #935 Added unit tests --- .../biocache/util/QueryFormatUtilsSpec.groovy | 117 +++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/src/test/java/au/org/ala/biocache/util/QueryFormatUtilsSpec.groovy b/src/test/java/au/org/ala/biocache/util/QueryFormatUtilsSpec.groovy index 5763aaabd..50968d741 100644 --- a/src/test/java/au/org/ala/biocache/util/QueryFormatUtilsSpec.groovy +++ b/src/test/java/au/org/ala/biocache/util/QueryFormatUtilsSpec.groovy @@ -1,6 +1,13 @@ package au.org.ala.biocache.util +import au.org.ala.biocache.dao.QidCacheDAO +import au.org.ala.biocache.dto.Qid +import au.org.ala.biocache.dto.SpatialSearchRequestDTO +import au.org.ala.biocache.service.AuthService +import au.org.ala.biocache.service.DataQualityService +import au.org.ala.biocache.service.LayersService import au.org.ala.biocache.service.ListsService +import au.org.ala.biocache.util.solr.FieldMappingUtil import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.ObjectMapper import com.google.common.io.Resources @@ -14,11 +21,22 @@ class QueryFormatUtilsSpec extends Specification { def listsService = Stub(ListsService) def searchUtils = Stub(SearchUtils) + def layersService = Stub(LayersService) + def qidCacheDao = Stub(QidCacheDAO) + def dataQualityService = Stub(DataQualityService) + def authService = Stub(AuthService) + def fieldMappingUtil = Stub(FieldMappingUtil) def setup() { - queryFormatUtils.listsService = listsService queryFormatUtils.searchUtils = searchUtils + queryFormatUtils.layersService = layersService + queryFormatUtils.qidCacheDao = qidCacheDao + queryFormatUtils.dataQualityService = dataQualityService + queryFormatUtils.fieldMappingUtil = Mock(FieldMappingUtil) { + translateQueryFields(_) >> { String query -> return query } + } + queryFormatUtils.authService = authService } @@ -69,6 +87,103 @@ class QueryFormatUtilsSpec extends Specification { 'before species_list:dr123 between species_list:dr456 after' | 'field:before species_list:dr123 field:between species_list:dr456 field:after' || 'before dr123 (FAILED) between dr456 (FAILED) after' | 'field:before (NOT *:*) field:between (NOT *:*) field:after' } + def "test formatSearchQuery with empty query"() { + given: + SpatialSearchRequestDTO searchParams = new SpatialSearchRequestDTO() + searchParams.setQ("") + searchParams.setFq(new String[0]) + + when: + def result = queryFormatUtils.formatSearchQuery(searchParams, false) + + then: + result[0].isEmpty() + result[1].isEmpty() + searchParams.getFormattedQuery() == "" + searchParams.getDisplayString() == "" + } + + def "test formatSearchQuery with simple query"() { + given: + SpatialSearchRequestDTO searchParams = new SpatialSearchRequestDTO() + searchParams.setQ("taxon_name:Test") + searchParams.setFq(new String[0]) + + when: + def result = queryFormatUtils.formatSearchQuery(searchParams, false) + + then: + result[0].isEmpty() + result[1].isEmpty() + searchParams.getFormattedQuery() == "taxon_name:Test" + searchParams.getDisplayString() == "taxon_name:Test" + } + + def "test formatSearchQuery with qid"() { + given: + SpatialSearchRequestDTO searchParams = new SpatialSearchRequestDTO() + searchParams.setQ("qid:123") + searchParams.setFq(new String[0]) + qidCacheDao.get(_) >> new Qid(q: "taxon_name:Test", fqs: new String[0]) + + when: + def result = queryFormatUtils.formatSearchQuery(searchParams, false) + + then: + result[0].isEmpty() + result[1].isEmpty() + searchParams.getFormattedQuery() == "taxon_name:Test" + searchParams.getDisplayString() == "taxon_name:Test" + } + + def "test formatSearchQuery with facets"() { + given: + SpatialSearchRequestDTO searchParams = new SpatialSearchRequestDTO() + searchParams.setQ("taxon_name:Test") + searchParams.setFacet(true) + searchParams.setIncludeUnfilteredFacetValues(false) + searchParams.setFq(new String[]{"month:1", "year:2020"}) + searchParams.setFacets(new String[]{"month", "year", "eventDate"}) + + when: + def result = queryFormatUtils.formatSearchQuery(searchParams, false) + + then: + result[0].size() == 2 + result[1].size() == 2 + searchParams.getFormattedQuery() == "taxon_name:Test" + searchParams.getFormattedFq() == new String[]{"month:1", "year:2020"} + searchParams.getFq() == new String[]{"month:1", "year:2020"} + searchParams.getDisplayString() == "taxon_name:Test" + searchParams.getFacets() == new String[]{"month", "year", "eventDate"} + searchParams.getPivotFacets() == new String[]{} + } + + + + def "test formatSearchQuery with tagging and excluded facets"() { + given: + SpatialSearchRequestDTO searchParams = new SpatialSearchRequestDTO() + searchParams.setQ("taxon_name:Test") + searchParams.setFacet(true) + searchParams.setIncludeUnfilteredFacetValues(true) + searchParams.setFq(new String[]{"month:1", "year:2020"}) + searchParams.setFacets(new String[]{"month", "year", "eventDate"}) + + when: + def result = queryFormatUtils.formatSearchQuery(searchParams, false) + + then: + result[0].size() == 2 + result[1].size() == 2 + searchParams.getFormattedQuery() == "taxon_name:Test" + searchParams.getDisplayString() == "taxon_name:Test" + searchParams.getFormattedFq() == new String[]{"{!tag=month}month:1", "{!tag=year}year:2020"} + searchParams.getFq() == new String[]{"month:1", "year:2020"} + searchParams.getFacets() == new String[]{"eventDate"} + searchParams.getPivotFacets() == new String[]{"{!ex=month,year}month", "{!ex=month,year}year"} + } + private static ObjectMapper om = new ObjectMapper() private static String getResultQuery(String uid) {