|
21 | 21 | ) |
22 | 22 | from common.fields import translation_fields |
23 | 23 | from common.tests.factories import ClassifiedProductFactory, CountryFactory |
24 | | -from metadata.models import LivelihoodActivityScenario |
| 24 | +from metadata.models import LivelihoodActivityScenario, LivelihoodStrategyType |
25 | 25 | from metadata.tests.factories import ( |
26 | 26 | CharacteristicGroupFactory, |
27 | 27 | LivelihoodCategoryFactory, |
@@ -775,9 +775,11 @@ def test_search_with_product(self): |
775 | 775 | self.assertTrue(any(d["name"] == self.baseline3.name for d in data)) |
776 | 776 | self.assertFalse(any(d["name"] == self.baseline2.name for d in data)) |
777 | 777 |
|
| 778 | + # Find the item matching characteristic1 (not relying on order) |
| 779 | + characteristic1_item = next(item for item in search_data["items"] if item["value"] == self.characteristic1.pk) |
778 | 780 | response = self.client.get( |
779 | 781 | baseline_url, |
780 | | - {search_data["items"][0]["filter"]: search_data["items"][0]["value"]}, |
| 782 | + {characteristic1_item["filter"]: characteristic1_item["value"]}, |
781 | 783 | ) |
782 | 784 | self.assertEqual(response.status_code, 200) |
783 | 785 | self.assertEqual(len(json.loads(response.content)), 1) |
@@ -826,6 +828,79 @@ def test_search_with_wealth_characterstics(self): |
826 | 828 | self.assertEqual(response.status_code, 200) |
827 | 829 | self.assertEqual(len(data["items"]), 0) |
828 | 830 |
|
| 831 | + def test_search_with_livelihood_strategy_type(self): |
| 832 | + # Create products with "goat" in the name for partial match testing |
| 833 | + goat_product = ClassifiedProductFactory( |
| 834 | + cpc="L09901AA", |
| 835 | + description_en="Goats", |
| 836 | + common_name_en="Goat", |
| 837 | + ) |
| 838 | + goat_meat_product = ClassifiedProductFactory( |
| 839 | + cpc="L09902AA", |
| 840 | + description_en="Goat Meat", |
| 841 | + common_name_en="Goat Meat", |
| 842 | + ) |
| 843 | + goat_milk_product = ClassifiedProductFactory( |
| 844 | + cpc="L09903AA", |
| 845 | + description_en="Goat's Milk", |
| 846 | + common_name_en="Goat Milk", |
| 847 | + ) |
| 848 | + # Create strategies linking products to baselines with specific strategy types |
| 849 | + LivelihoodStrategyFactory( |
| 850 | + product=goat_milk_product, |
| 851 | + livelihood_zone_baseline=self.baseline1, |
| 852 | + strategy_type=LivelihoodStrategyType.MILK_PRODUCTION, |
| 853 | + ) |
| 854 | + LivelihoodStrategyFactory( |
| 855 | + product=goat_meat_product, |
| 856 | + livelihood_zone_baseline=self.baseline2, |
| 857 | + strategy_type=LivelihoodStrategyType.MEAT_PRODUCTION, |
| 858 | + ) |
| 859 | + LivelihoodStrategyFactory( |
| 860 | + product=goat_product, |
| 861 | + livelihood_zone_baseline=self.baseline3, |
| 862 | + strategy_type=LivelihoodStrategyType.LIVESTOCK_SALE, |
| 863 | + ) |
| 864 | + # Test that search "Milk" returns MilkProduction in livelihood_strategy_types facet |
| 865 | + response = self.client.get(self.url, {"search": "Milk"}) |
| 866 | + self.assertEqual(response.status_code, 200) |
| 867 | + data = response.data |
| 868 | + self.assertIn("livelihood_strategy_types", data) |
| 869 | + strategy_type_results = data["livelihood_strategy_types"] |
| 870 | + milk_results = [r for r in strategy_type_results if r["value"] == "MilkProduction"] |
| 871 | + self.assertEqual(len(milk_results), 1) |
| 872 | + self.assertEqual(milk_results[0]["filter"], "strategy_type") |
| 873 | + self.assertEqual(milk_results[0]["value_label"], "Milk Production") |
| 874 | + self.assertEqual(milk_results[0]["count"], 1) |
| 875 | + |
| 876 | + # Test that search "lait" with language=fr returns MilkProduction via French translation |
| 877 | + response = self.client.get(self.url, {"search": "lait", "language": "fr"}) |
| 878 | + self.assertEqual(response.status_code, 200) |
| 879 | + data = response.data |
| 880 | + strategy_type_results = data["livelihood_strategy_types"] |
| 881 | + milk_results = [r for r in strategy_type_results if r["value"] == "MilkProduction"] |
| 882 | + self.assertEqual(len(milk_results), 1) |
| 883 | + self.assertEqual(milk_results[0]["value_label"], "Production du lait") |
| 884 | + |
| 885 | + # Test that search "goat" returns multiple goat-related products |
| 886 | + response = self.client.get(self.url, {"search": "goat", "language": "en"}) |
| 887 | + self.assertEqual(response.status_code, 200) |
| 888 | + data = response.data |
| 889 | + product_results = data["products"] |
| 890 | + product_cpcs = {r["value"] for r in product_results} |
| 891 | + self.assertIn(goat_product.cpc, product_cpcs) |
| 892 | + self.assertIn(goat_meat_product.cpc, product_cpcs) |
| 893 | + self.assertIn(goat_milk_product.cpc, product_cpcs) |
| 894 | + self.assertEqual(len(product_results), 3) |
| 895 | + |
| 896 | + # test taht strategy_type filter to baseline list endpoint |
| 897 | + baseline_url = reverse("livelihoodzonebaseline-list") |
| 898 | + response = self.client.get(baseline_url, {"strategy_type": "MilkProduction"}) |
| 899 | + self.assertEqual(response.status_code, 200) |
| 900 | + baseline_data = json.loads(response.content) |
| 901 | + self.assertEqual(len(baseline_data), 1) |
| 902 | + self.assertEqual(baseline_data[0]["name"], self.baseline1.name) |
| 903 | + |
829 | 904 |
|
830 | 905 | class LivelihoodProductCategoryViewSetTestCase(APITestCase): |
831 | 906 | @classmethod |
|
0 commit comments