Skip to content

Commit 6d3e2ac

Browse files
committed
Add unit test LivelihoodBaselineFacetedSearchTestCase see HEA-651
1 parent c618501 commit 6d3e2ac

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

apps/baseline/tests/test_viewsets.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from baseline.models import LivelihoodZoneBaseline
1313
from common.fields import translation_fields
1414
from common.tests.factories import ClassifiedProductFactory, CountryFactory
15+
from metadata.tests.factories import (
16+
LivelihoodCategoryFactory,
17+
WealthCharacteristicFactory,
18+
)
1519

1620
from .factories import (
1721
BaselineLivelihoodActivityFactory,
@@ -536,6 +540,136 @@ def test_population_estimate_range_filter(self):
536540
self.assertEqual(response.status_code, 200)
537541
self.assertEqual(len(response.data), 0)
538542

543+
def test_filter_by_product(self):
544+
product = ClassifiedProductFactory(
545+
cpc="K0111",
546+
description_en="my product",
547+
common_name_en="common",
548+
kcals_per_unit=550,
549+
aliases=["test alias"],
550+
)
551+
ClassifiedProductFactory(cpc="K01111")
552+
baseline = LivelihoodZoneBaselineFactory()
553+
LivelihoodStrategyFactory(product=product, livelihood_zone_baseline=baseline)
554+
response = self.client.get(self.url, {"product": "K011"})
555+
self.assertEqual(response.status_code, 200)
556+
self.assertEqual(len(json.loads(response.content)), 1)
557+
# filter by cpc
558+
response = self.client.get(self.url, {"product": "K0111"})
559+
self.assertEqual(response.status_code, 200)
560+
self.assertEqual(len(json.loads(response.content)), 1)
561+
# filter by cpc startswith
562+
response = self.client.get(self.url, {"product": "K01111"})
563+
self.assertEqual(response.status_code, 200)
564+
self.assertEqual(len(json.loads(response.content)), 0)
565+
# filter by description icontains
566+
response = self.client.get(self.url, {"product": "my"})
567+
self.assertEqual(response.status_code, 200)
568+
self.assertEqual(len(json.loads(response.content.decode("utf-8"))), 1)
569+
# filter by description
570+
response = self.client.get(self.url, {"product": "my product"})
571+
self.assertEqual(response.status_code, 200)
572+
self.assertEqual(len(json.loads(response.content.decode("utf-8"))), 1)
573+
# filter by alias
574+
response = self.client.get(self.url, {"product": "test"})
575+
self.assertEqual(response.status_code, 200)
576+
self.assertEqual(len(json.loads(response.content.decode("utf-8"))), 1)
577+
578+
def test_filter_by_wealth_characteristic(self):
579+
baseline = LivelihoodZoneBaselineFactory()
580+
wealth_characteristic = WealthCharacteristicFactory()
581+
WealthGroupCharacteristicValueFactory(
582+
wealth_group__livelihood_zone_baseline=baseline, wealth_characteristic=wealth_characteristic
583+
)
584+
response = self.client.get(self.url, {"wealth_characteristic": wealth_characteristic.code})
585+
self.assertEqual(response.status_code, 200)
586+
self.assertEqual(len(json.loads(response.content.decode("utf-8"))), 1)
587+
response = self.client.get(self.url, {"wealth_characteristic": wealth_characteristic.name})
588+
self.assertEqual(response.status_code, 200)
589+
self.assertEqual(len(response.json()), 1)
590+
591+
592+
class LivelihoodBaselineFacetedSearchTestCase(APITestCase):
593+
def setUp(self):
594+
self.category1 = LivelihoodCategoryFactory()
595+
self.baseline1 = LivelihoodZoneBaselineFactory(main_livelihood_category=self.category1)
596+
self.baseline2 = LivelihoodZoneBaselineFactory(main_livelihood_category=self.category1)
597+
self.baseline3 = LivelihoodZoneBaselineFactory()
598+
self.product1 = ClassifiedProductFactory(
599+
cpc="K0111",
600+
description_en="my test",
601+
common_name_en="common",
602+
kcals_per_unit=550,
603+
aliases=["test alias"],
604+
)
605+
self.product2 = ClassifiedProductFactory(
606+
cpc="L0111",
607+
description_en="my mukera",
608+
common_name_en="common mukera",
609+
kcals_per_unit=550,
610+
)
611+
LivelihoodStrategyFactory(product=self.product1, livelihood_zone_baseline=self.baseline1)
612+
self.characteristic1 = WealthCharacteristicFactory(description_en="my test")
613+
self.characteristic2 = WealthCharacteristicFactory(description_en="my mukera", description_fr="my test")
614+
WealthGroupCharacteristicValueFactory(
615+
wealth_group__livelihood_zone_baseline=self.baseline1, wealth_characteristic=self.characteristic1
616+
)
617+
WealthGroupCharacteristicValueFactory(
618+
wealth_group__livelihood_zone_baseline=self.baseline2, wealth_characteristic=self.characteristic2
619+
)
620+
self.characteristic3 = WealthCharacteristicFactory()
621+
self.strategy = LivelihoodStrategyFactory(product=self.product1)
622+
self.baseline = LivelihoodZoneBaselineFactory(main_livelihood_category=self.category1)
623+
self.url = reverse("livelihood-baseline-faceted-search")
624+
625+
def test_search_with_product(self):
626+
# Test when search matches entries
627+
response = self.client.get(self.url, {"search": self.product1.description_en, "language": "en"})
628+
self.assertEqual(response.status_code, 200)
629+
data = response.data
630+
self.assertEqual(len(data["products"]), 1)
631+
self.assertEqual(data["products"][0]["count"], 2) # 2 zones have this proudct
632+
# Search by the second product
633+
response = self.client.get(
634+
self.url,
635+
{
636+
"search": self.product2.description_en,
637+
},
638+
)
639+
self.assertEqual(response.status_code, 200)
640+
data = response.data
641+
self.assertEqual(len(data["products"]), 0)
642+
643+
def test_search_with_wealth_characterstics(self):
644+
# Test when search matches entries
645+
response = self.client.get(self.url, {"search": self.characteristic1.description_en})
646+
self.assertEqual(response.status_code, 200)
647+
data = response.data
648+
self.assertEqual(len(data["items"]), 2)
649+
self.assertEqual(data["items"][0]["count"], 1) # 1 zone for this characteristic
650+
self.assertEqual(data["items"][1]["count"], 1) # 1 zone for this characteristic
651+
# Search by the second characteristic
652+
response = self.client.get(
653+
self.url,
654+
{
655+
"search": self.characteristic2.description_en,
656+
},
657+
)
658+
self.assertEqual(response.status_code, 200)
659+
data = response.data
660+
self.assertEqual(len(data["items"]), 1)
661+
self.assertEqual(data["items"][0]["count"], 1) # 1 zone for this characteristic
662+
# Search by the third characteristic
663+
response = self.client.get(
664+
self.url,
665+
{
666+
"search": self.characteristic3.description_en,
667+
},
668+
)
669+
data = response.data
670+
self.assertEqual(response.status_code, 200)
671+
self.assertEqual(len(data["items"]), 0)
672+
539673

540674
class LivelihoodProductCategoryViewSetTestCase(APITestCase):
541675
@classmethod

0 commit comments

Comments
 (0)