Skip to content

Commit

Permalink
[Fixes #11273] Faceting: /facets loses some filters along the way
Browse files Browse the repository at this point in the history
  • Loading branch information
etj committed Jul 17, 2023
1 parent 9104553 commit a772d50
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
56 changes: 53 additions & 3 deletions geonode/facets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from geonode.base.models import Thesaurus, ThesaurusLabel, ThesaurusKeyword, ThesaurusKeywordLabel, ResourceBase, Region
from geonode.facets.models import facet_registry
from geonode.facets.providers.baseinfo import FeaturedFacetProvider

Check warning on line 32 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L32

Added line #L32 was not covered by tests
from geonode.facets.providers.region import RegionFacetProvider
from geonode.tests.base import GeoNodeBaseTestSupport
import geonode.facets.views as views
Expand Down Expand Up @@ -136,13 +137,13 @@ def _create_resources(self):
# RB19 -> T0K0 T0K1 FEAT

if x % 2 == 1:
print(f"ADDING KEYWORDS {self.thesauri_k['0_0']} to RB {d}")
logger.debug(f"ADDING KEYWORDS {self.thesauri_k['0_0']} to RB {d}")

Check warning on line 140 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L140

Added line #L140 was not covered by tests
d.tkeywords.add(self.thesauri_k["0_0"])
if x % 2 == 1 and x > 10:
print(f"ADDING KEYWORDS {self.thesauri_k['0_1']} to RB {d}")
logger.debug(f"ADDING KEYWORDS {self.thesauri_k['0_1']} to RB {d}")

Check warning on line 143 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L143

Added line #L143 was not covered by tests
d.tkeywords.add(self.thesauri_k["0_1"])
if x < 10:
print(f"ADDING KEYWORDS {self.thesauri_k['1_0']} to RB {d}")
logger.debug(f"ADDING KEYWORDS {self.thesauri_k['1_0']} to RB {d}")

Check warning on line 146 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L146

Added line #L146 was not covered by tests
d.tkeywords.add(self.thesauri_k["1_0"])
if 7 < x < 13:
d.tkeywords.add(self.thesauri_k["1_1"])
Expand Down Expand Up @@ -335,6 +336,55 @@ def test_prefiltering(self):
self.assertEqual(totals, obj["topics"]["total"], f"Bad totals for facet '{facet} and filter {filters}")
self.assertEqual(count0, obj["topics"]["items"][0]["count"], f"Bad count0 for facet '{facet}")

def test_prefiltering_tkeywords(self):
regname = RegionFacetProvider().name
featname = FeaturedFacetProvider().name
t1filter = facet_registry.get_provider("t_1").get_info()["filter"]
tkey_1_1 = self.thesauri_k["1_1"].id

Check warning on line 343 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L339-L343

Added lines #L339 - L343 were not covered by tests

expected_region = {"R1": 1}
expected_feat = {True: 2, False: 3}

Check warning on line 346 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L345-L346

Added lines #L345 - L346 were not covered by tests

# Run the single requests
for facet, params, items in (
(regname, {t1filter: tkey_1_1}, expected_region),
(featname, {t1filter: tkey_1_1}, expected_feat),
):
req = self.rf.get(reverse("get_facet", args=[facet]), data=params)
res: JsonResponse = views.get_facet(req, facet)
obj = json.loads(res.content)

Check warning on line 355 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L353-L355

Added lines #L353 - L355 were not covered by tests

self.assertEqual(

Check warning on line 357 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L357

Added line #L357 was not covered by tests
len(items),
len(obj["topics"]["items"]),
f"Bad count for items '{facet} \n PARAMS: {params} \n RESULT: {obj} \n EXPECTED: {items}",
)
# search item
for item in items.keys():
found = next((i for i in obj["topics"]["items"] if i["key"] == item), None)
self.assertIsNotNone(found, f"Topic '{item}' not found in facet {facet} -- {obj}")
self.assertEqual(items[item], found.get("count", None), f"Bad count for facet '{facet}:{item}")

Check warning on line 366 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L365-L366

Added lines #L365 - L366 were not covered by tests

# Run the single request
req = self.rf.get(reverse("list_facets"), data={"include_topics": 1, t1filter: tkey_1_1})
res: JsonResponse = views.list_facets(req)
obj = json.loads(res.content)

Check warning on line 371 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L369-L371

Added lines #L369 - L371 were not covered by tests

facets_list = obj["facets"]
fmap = self._facets_to_map(facets_list)

Check warning on line 374 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L373-L374

Added lines #L373 - L374 were not covered by tests

for name, items in (
(regname, expected_region),
(featname, expected_feat),
):
self.assertIn(name, fmap)
facet = fmap[name]

Check warning on line 381 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L380-L381

Added lines #L380 - L381 were not covered by tests

for item in items.keys():
found = next((i for i in facet["topics"]["items"] if i["key"] == item), None)
self.assertIsNotNone(found, f"Topic '{item}' not found in facet {facet} -- {facet}")
self.assertEqual(items[item], found.get("count", None), f"Bad count for facet '{facet}:{item}")

Check warning on line 386 in geonode/facets/tests.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/tests.py#L385-L386

Added lines #L385 - L386 were not covered by tests

def test_config(self):
for facet, type, order in (
("resourcetype", None, None),
Expand Down
4 changes: 3 additions & 1 deletion geonode/facets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def list_facets(request, **kwargs):
include_config = _resolve_boolean(request, PARAM_INCLUDE_CONFIG, False)

facets = []
prefiltered = None

Check warning on line 56 in geonode/facets/views.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/views.py#L56

Added line #L56 was not covered by tests

for provider in facet_registry.get_providers():
logger.debug("Fetching data from provider %r", provider)
Expand All @@ -68,7 +69,8 @@ def list_facets(request, **kwargs):
info["link"] = f"{reverse('get_facet', args=[info['name']])}?{urlencode(link_args)}"

if include_topics:
info["topics"] = _get_topics(provider, queryset=_prefilter_topics(request), lang=lang)
prefiltered = prefiltered or _prefilter_topics(request)
info["topics"] = _get_topics(provider, queryset=prefiltered, lang=lang)

Check warning on line 73 in geonode/facets/views.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/views.py#L72-L73

Added lines #L72 - L73 were not covered by tests

facets.append(info)

Expand Down

0 comments on commit a772d50

Please sign in to comment.