Skip to content

Commit

Permalink
[Fixes #11620] Facets: refact view as a class (#11625)
Browse files Browse the repository at this point in the history
Co-authored-by: mattiagiupponi <[email protected]>
  • Loading branch information
etj and mattiagiupponi authored Oct 23, 2023
1 parent c437a0a commit da856b5
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 220 deletions.
52 changes: 20 additions & 32 deletions geonode/facets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
from geonode.facets.providers.category import CategoryFacetProvider
from geonode.facets.providers.keyword import KeywordFacetProvider
from geonode.facets.providers.region import RegionFacetProvider
from geonode.facets.views import ListFacetsView, GetFacetView
from geonode.tests.base import GeoNodeBaseTestSupport
import geonode.facets.views as views


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -85,7 +85,7 @@ def _create_thesauri(cls):
for tn in range(2):
t = Thesaurus.objects.create(identifier=f"t_{tn}", title=f"Thesaurus {tn}", order=100 + tn * 10)
cls.thesauri[tn] = t
for tl in (
for tl in ( # fmt: skip
"en",
"it",
):
Expand All @@ -94,7 +94,7 @@ def _create_thesauri(cls):
for tkn in range(10):
tk = ThesaurusKeyword.objects.create(thesaurus=t, alt_label=f"T{tn}_K{tkn}_ALT")
cls.thesauri_k[f"{tn}_{tkn}"] = tk
for tkl in (
for tkl in ( # fmt: skip
"en",
"it",
):
Expand All @@ -104,7 +104,7 @@ def _create_thesauri(cls):
def _create_regions(cls):
cls.regions = {}

for code, name in (
for code, name in ( # fmt: skip
("R0", "Region0"),
("R1", "Region1"),
("R2", "Region2"),
Expand All @@ -115,7 +115,7 @@ def _create_regions(cls):
def _create_categories(cls):
cls.cats = {}

for code, name in (
for code, name in ( # fmt: skip
("C0", "Cat0"),
("C1", "Cat1"),
("C2", "Cat2"),
Expand All @@ -126,7 +126,7 @@ def _create_categories(cls):
def _create_keywords(cls):
cls.kw = {}

for code, name in (
for code, name in ( # fmt: skip
("K0", "Keyword0"),
("K1", "Keyword1"),
("K2", "Keyword2"),
Expand Down Expand Up @@ -186,22 +186,22 @@ def _create_resources(self):
if (x % 6) in (0, 1, 2):
d.featured = True

for reg, idx in (
for reg, idx in ( # fmt: skip
("R0", (0, 1)),
("R1", (0, 2, 8, 13)),
):
if x in idx:
d.regions.add(self.regions[reg])

for kw, idx in (
for kw, idx in ( # fmt: skip
("K0", (0, 3, 4, 5)),
("K1", [1, 4]),
("K2", [2, 5]),
):
if x in idx:
d.keywords.add(self.kw[kw])

for cat, idx in (
for cat, idx in ( # fmt: skip
("C0", [0, 2, 4]),
("C1", [5, 15, 16]),
("C2", [18, 19]),
Expand All @@ -218,7 +218,7 @@ def _facets_to_map(facets):

def test_facets_base(self):
req = self.rf.get(reverse("list_facets"), data={"lang": "en"})
res: JsonResponse = views.list_facets(req)
res: JsonResponse = ListFacetsView.as_view()(req)
obj = json.loads(res.content)
self.assertIn("facets", obj)
facets_list = obj["facets"]
Expand All @@ -238,13 +238,13 @@ def test_facets_rich(self):

# run the request
req = self.rf.get(reverse("list_facets"), data={"include_topics": 1, "lang": "en"})
res: JsonResponse = views.list_facets(req)
res: JsonResponse = ListFacetsView.as_view()(req)
obj = json.loads(res.content)

facets_list = obj["facets"]
self.assertEqual(8, len(facets_list))
fmap = self._facets_to_map(facets_list)
for expected in (
for expected in ( # fmt: skip
{
"name": "category",
"topics": {
Expand Down Expand Up @@ -356,7 +356,7 @@ def test_bad_lang(self):

# run the request with a valid language
req = self.rf.get(reverse("get_facet", args=["t_0"]), data={"lang": "en"})
res: JsonResponse = views.get_facet(req, "t_0")
res: JsonResponse = GetFacetView.as_view()(req, "t_0")
obj = json.loads(res.content)

self.assertEqual(2, obj["topics"]["total"])
Expand All @@ -366,26 +366,14 @@ def test_bad_lang(self):

# run the request with an INVALID language
req = self.rf.get(reverse("get_facet", args=["t_0"]), data={"lang": "ZZ"})
res: JsonResponse = views.get_facet(req, "t_0")
res: JsonResponse = GetFacetView.as_view()(req, "t_0")
obj = json.loads(res.content)

self.assertEqual(2, obj["topics"]["total"])
self.assertEqual(10, obj["topics"]["items"][0]["count"]) # make sure the count is still there
self.assertEqual("T0_K0_ALT", obj["topics"]["items"][0]["label"]) # check for the alternate label
self.assertFalse(obj["topics"]["items"][0]["is_localized"]) # check for the localization flag

def test_topics(self):
for facet, keys, exp in (
("t_0", [self.thesauri_k["0_0"].id, self.thesauri_k["0_1"].id, -999], 2),
("category", ["C1", "C2", "nomatch"], 2),
("owner", [self.user.id, -100], 1),
("region", ["R0", "R1", "nomatch"], 2),
):
req = self.rf.get(reverse("get_facet_topics", args=[facet]), data={"lang": "en", "key": keys})
res: JsonResponse = views.get_facet_topics(req, facet)
obj = json.loads(res.content)
self.assertEqual(exp, len(obj["topics"]["items"]), f"Unexpected topic count {exp} for facet {facet}")

def test_prefiltering(self):
reginfo = RegionFacetProvider().get_info()
regfilter = reginfo["filter"]
Expand All @@ -403,7 +391,7 @@ def test_prefiltering(self):
(reginfo["name"], {t1filter: self.thesauri_k["1_0"].id}, 2, 3),
):
req = self.rf.get(reverse("get_facet", args=[facet]), data=filters)
res: JsonResponse = views.get_facet(req, facet)
res: JsonResponse = GetFacetView.as_view()(req, facet)
obj = json.loads(res.content)
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}")
Expand All @@ -423,7 +411,7 @@ def test_prefiltering_tkeywords(self):
(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)
res: JsonResponse = GetFacetView.as_view()(req, facet)
obj = json.loads(res.content)

self.assertEqual(
Expand All @@ -439,13 +427,13 @@ def test_prefiltering_tkeywords(self):

# 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)
res: JsonResponse = ListFacetsView.as_view()(req)
obj = json.loads(res.content)

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

for name, items in (
for name, items in ( # fmt: skip
(regname, expected_region),
(featname, expected_feat),
):
Expand All @@ -466,7 +454,7 @@ def test_config(self):
("owner", "select", 8),
):
req = self.rf.get(reverse("get_facet", args=[facet]), data={"include_config": True})
res: JsonResponse = views.get_facet(req, facet)
res: JsonResponse = GetFacetView.as_view()(req, facet)
obj = json.loads(res.content)
self.assertIn("config", obj, "Config info not found in payload")
conf = obj["config"]
Expand Down Expand Up @@ -525,7 +513,7 @@ def t(tk):
(kwname, {t0filter: t("0_0"), regfilter: "R0", "key": ["K0"]}, {"K0": None}),
):
req = self.rf.get(reverse("get_facet", args=[facet]), data=params)
res: JsonResponse = views.get_facet(req, facet)
res: JsonResponse = GetFacetView.as_view()(req, facet)
obj = json.loads(res.content)
# self.assertEqual(totals, obj["topics"]["total"], f"Bad totals for facet '{facet} and params {params}")

Expand Down
7 changes: 3 additions & 4 deletions geonode/facets/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
#########################################################################

from django.urls import path
from . import views
from .views import ListFacetsView, GetFacetView

urlpatterns = [
path("facets", views.list_facets, name="list_facets"),
path("facets/<facet>", views.get_facet, name="get_facet"),
path("facets/<facet>/topics", views.get_facet_topics, name="get_facet_topics"),
path("facets", ListFacetsView.as_view(), name="list_facets"),
path("facets/<facet>", GetFacetView.as_view(), name="get_facet"),
]
Loading

0 comments on commit da856b5

Please sign in to comment.