|
12 | 12 | from baseline.models import LivelihoodZoneBaseline |
13 | 13 | from common.fields import translation_fields |
14 | 14 | from common.tests.factories import ClassifiedProductFactory, CountryFactory |
| 15 | +from metadata.tests.factories import ( |
| 16 | + LivelihoodCategoryFactory, |
| 17 | + WealthCharacteristicFactory, |
| 18 | +) |
15 | 19 |
|
16 | 20 | from .factories import ( |
17 | 21 | BaselineLivelihoodActivityFactory, |
@@ -536,6 +540,157 @@ def test_population_estimate_range_filter(self): |
536 | 540 | self.assertEqual(response.status_code, 200) |
537 | 541 | self.assertEqual(len(response.data), 0) |
538 | 542 |
|
| 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 LivelihoodZoneBaselineFacetedSearchViewTestCase(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, livelihood_zone_baseline=self.baseline3) |
| 622 | + self.baseline = LivelihoodZoneBaselineFactory(main_livelihood_category=self.category1) |
| 623 | + self.url = reverse("livelihood-zone-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 | + search_data = response.data |
| 630 | + self.assertEqual(len(search_data["products"]), 1) |
| 631 | + self.assertEqual(search_data["products"][0]["count"], 2) # 2 zones have this product |
| 632 | + # confirm the product value is correct |
| 633 | + self.assertEqual(search_data["products"][0]["value"], self.product1.cpc) |
| 634 | + # Apply the filters to the baseline |
| 635 | + baseline_url = reverse("livelihoodzonebaseline-list") |
| 636 | + response = self.client.get( |
| 637 | + baseline_url, {search_data["products"][0]["filter"]: search_data["products"][0]["value"]} |
| 638 | + ) |
| 639 | + self.assertEqual(response.status_code, 200) |
| 640 | + self.assertEqual(len(json.loads(response.content)), 2) |
| 641 | + data = json.loads(response.content) |
| 642 | + self.assertTrue(any(d["name"] == self.baseline1.name for d in data)) |
| 643 | + self.assertTrue(any(d["name"] == self.baseline3.name for d in data)) |
| 644 | + self.assertFalse(any(d["name"] == self.baseline2.name for d in data)) |
| 645 | + |
| 646 | + response = self.client.get(baseline_url, {search_data["items"][0]["filter"]: search_data["items"][0]["value"]}) |
| 647 | + self.assertEqual(response.status_code, 200) |
| 648 | + self.assertEqual(len(json.loads(response.content)), 1) |
| 649 | + data = json.loads(response.content) |
| 650 | + self.assertTrue(any(d["name"] == self.baseline1.name for d in data)) |
| 651 | + self.assertFalse(any(d["name"] == self.baseline2.name for d in data)) |
| 652 | + self.assertFalse(any(d["name"] == self.baseline3.name for d in data)) |
| 653 | + # Search by the second product |
| 654 | + response = self.client.get( |
| 655 | + self.url, |
| 656 | + { |
| 657 | + "search": self.product2.description_en, |
| 658 | + }, |
| 659 | + ) |
| 660 | + self.assertEqual(response.status_code, 200) |
| 661 | + search_data = response.data |
| 662 | + self.assertEqual(len(search_data["products"]), 0) |
| 663 | + |
| 664 | + def test_search_with_wealth_characterstics(self): |
| 665 | + # Test when search matches entries |
| 666 | + response = self.client.get(self.url, {"search": self.characteristic1.description_en}) |
| 667 | + self.assertEqual(response.status_code, 200) |
| 668 | + data = response.data |
| 669 | + self.assertEqual(len(data["items"]), 2) |
| 670 | + self.assertEqual(data["items"][0]["count"], 1) # 1 zone for this characteristic |
| 671 | + self.assertEqual(data["items"][1]["count"], 1) # 1 zone for this characteristic |
| 672 | + # Search by the second characteristic |
| 673 | + response = self.client.get( |
| 674 | + self.url, |
| 675 | + { |
| 676 | + "search": self.characteristic2.description_en, |
| 677 | + }, |
| 678 | + ) |
| 679 | + self.assertEqual(response.status_code, 200) |
| 680 | + data = response.data |
| 681 | + self.assertEqual(len(data["items"]), 1) |
| 682 | + self.assertEqual(data["items"][0]["count"], 1) # 1 zone for this characteristic |
| 683 | + # Search by the third characteristic |
| 684 | + response = self.client.get( |
| 685 | + self.url, |
| 686 | + { |
| 687 | + "search": self.characteristic3.description_en, |
| 688 | + }, |
| 689 | + ) |
| 690 | + data = response.data |
| 691 | + self.assertEqual(response.status_code, 200) |
| 692 | + self.assertEqual(len(data["items"]), 0) |
| 693 | + |
539 | 694 |
|
540 | 695 | class LivelihoodProductCategoryViewSetTestCase(APITestCase): |
541 | 696 | @classmethod |
|
0 commit comments