2121)
2222from common .fields import translation_fields
2323from common .tests .factories import ClassifiedProductFactory , CountryFactory
24- from metadata .models import LivelihoodActivityScenario
24+ from metadata .models import LivelihoodActivityScenario , LivelihoodStrategyType
2525from metadata .tests .factories import (
2626 CharacteristicGroupFactory ,
2727 LivelihoodCategoryFactory ,
@@ -762,6 +762,15 @@ def test_search_with_product(self):
762762 self .assertEqual (search_data ["products" ][0 ]["count" ], 2 ) # 2 zones have this product
763763 # confirm the product value is correct
764764 self .assertEqual (search_data ["products" ][0 ]["value" ], self .product1 .cpc )
765+ # confirm livelihood_zone_baselines are present with correct data
766+ baselines = search_data ["products" ][0 ]["livelihood_zone_baselines" ]
767+ self .assertEqual (len (baselines ), 2 )
768+ baseline_ids = {b ["id" ] for b in baselines }
769+ self .assertEqual (baseline_ids , {self .baseline1 .id , self .baseline3 .id })
770+ for b in baselines :
771+ self .assertIn ("name" , b )
772+ self .assertIn ("livelihood_zone__code" , b )
773+ self .assertIn ("reference_year_end_date" , b )
765774 # Apply the filters to the baseline
766775 baseline_url = reverse ("livelihoodzonebaseline-list" )
767776 response = self .client .get (
@@ -775,9 +784,11 @@ def test_search_with_product(self):
775784 self .assertTrue (any (d ["name" ] == self .baseline3 .name for d in data ))
776785 self .assertFalse (any (d ["name" ] == self .baseline2 .name for d in data ))
777786
787+ # Find the item matching characteristic1 (not relying on order)
788+ characteristic1_item = next (item for item in search_data ["items" ] if item ["value" ] == self .characteristic1 .pk )
778789 response = self .client .get (
779790 baseline_url ,
780- {search_data [ "items" ][ 0 ][ " filter" ]: search_data [ "items" ][ 0 ] ["value" ]},
791+ {characteristic1_item [ " filter" ]: characteristic1_item ["value" ]},
781792 )
782793 self .assertEqual (response .status_code , 200 )
783794 self .assertEqual (len (json .loads (response .content )), 1 )
@@ -804,6 +815,14 @@ def test_search_with_wealth_characterstics(self):
804815 self .assertEqual (len (data ["items" ]), 2 )
805816 self .assertEqual (data ["items" ][0 ]["count" ], 1 ) # 1 zone for this characteristic
806817 self .assertEqual (data ["items" ][1 ]["count" ], 1 ) # 1 zone for this characteristic
818+ # confirm livelihood_zone_baselines are present on each item
819+ for item in data ["items" ]:
820+ self .assertIn ("livelihood_zone_baselines" , item )
821+ self .assertEqual (len (item ["livelihood_zone_baselines" ]), 1 )
822+ self .assertIn ("id" , item ["livelihood_zone_baselines" ][0 ])
823+ self .assertIn ("name" , item ["livelihood_zone_baselines" ][0 ])
824+ self .assertIn ("livelihood_zone__code" , item ["livelihood_zone_baselines" ][0 ])
825+ self .assertIn ("reference_year_end_date" , item ["livelihood_zone_baselines" ][0 ])
807826 # Search by the second characteristic
808827 response = self .client .get (
809828 self .url ,
@@ -826,6 +845,86 @@ def test_search_with_wealth_characterstics(self):
826845 self .assertEqual (response .status_code , 200 )
827846 self .assertEqual (len (data ["items" ]), 0 )
828847
848+ def test_search_with_livelihood_strategy_type (self ):
849+ # Create products with "goat" in the name for partial match testing
850+ goat_product = ClassifiedProductFactory (
851+ cpc = "L09901AA" ,
852+ description_en = "Goats" ,
853+ common_name_en = "Goat" ,
854+ )
855+ goat_meat_product = ClassifiedProductFactory (
856+ cpc = "L09902AA" ,
857+ description_en = "Goat Meat" ,
858+ common_name_en = "Goat Meat" ,
859+ )
860+ goat_milk_product = ClassifiedProductFactory (
861+ cpc = "L09903AA" ,
862+ description_en = "Goat's Milk" ,
863+ common_name_en = "Goat Milk" ,
864+ )
865+ # Create strategies linking products to baselines with specific strategy types
866+ LivelihoodStrategyFactory (
867+ product = goat_milk_product ,
868+ livelihood_zone_baseline = self .baseline1 ,
869+ strategy_type = LivelihoodStrategyType .MILK_PRODUCTION ,
870+ )
871+ LivelihoodStrategyFactory (
872+ product = goat_meat_product ,
873+ livelihood_zone_baseline = self .baseline2 ,
874+ strategy_type = LivelihoodStrategyType .MEAT_PRODUCTION ,
875+ )
876+ LivelihoodStrategyFactory (
877+ product = goat_product ,
878+ livelihood_zone_baseline = self .baseline3 ,
879+ strategy_type = LivelihoodStrategyType .LIVESTOCK_SALE ,
880+ )
881+ # Test that search "Milk" returns MilkProduction in livelihood_strategy_types facet
882+ response = self .client .get (self .url , {"search" : "Milk" })
883+ self .assertEqual (response .status_code , 200 )
884+ data = response .data
885+ self .assertIn ("livelihood_strategy_types" , data )
886+ strategy_type_results = data ["livelihood_strategy_types" ]
887+ milk_results = [r for r in strategy_type_results if r ["value" ] == "MilkProduction" ]
888+ self .assertEqual (len (milk_results ), 1 )
889+ self .assertEqual (milk_results [0 ]["filter" ], "strategy_type" )
890+ self .assertEqual (milk_results [0 ]["value_label" ], "Milk Production" )
891+ self .assertEqual (milk_results [0 ]["count" ], 1 )
892+ # confirm livelihood_zone_baselines on strategy type result
893+ baselines = milk_results [0 ]["livelihood_zone_baselines" ]
894+ self .assertEqual (len (baselines ), 1 )
895+ self .assertEqual (baselines [0 ]["id" ], self .baseline1 .id )
896+ self .assertIn ("name" , baselines [0 ])
897+ self .assertIn ("livelihood_zone__code" , baselines [0 ])
898+ self .assertIn ("reference_year_end_date" , baselines [0 ])
899+
900+ # Test that search "lait" with language=fr returns MilkProduction via French translation
901+ response = self .client .get (self .url , {"search" : "lait" , "language" : "fr" })
902+ self .assertEqual (response .status_code , 200 )
903+ data = response .data
904+ strategy_type_results = data ["livelihood_strategy_types" ]
905+ milk_results = [r for r in strategy_type_results if r ["value" ] == "MilkProduction" ]
906+ self .assertEqual (len (milk_results ), 1 )
907+ self .assertEqual (milk_results [0 ]["value_label" ], "Production du lait" )
908+
909+ # Test that search "goat" returns multiple goat-related products
910+ response = self .client .get (self .url , {"search" : "goat" , "language" : "en" })
911+ self .assertEqual (response .status_code , 200 )
912+ data = response .data
913+ product_results = data ["products" ]
914+ product_cpcs = {r ["value" ] for r in product_results }
915+ self .assertIn (goat_product .cpc , product_cpcs )
916+ self .assertIn (goat_meat_product .cpc , product_cpcs )
917+ self .assertIn (goat_milk_product .cpc , product_cpcs )
918+ self .assertEqual (len (product_results ), 3 )
919+
920+ # test taht strategy_type filter to baseline list endpoint
921+ baseline_url = reverse ("livelihoodzonebaseline-list" )
922+ response = self .client .get (baseline_url , {"strategy_type" : "MilkProduction" })
923+ self .assertEqual (response .status_code , 200 )
924+ baseline_data = json .loads (response .content )
925+ self .assertEqual (len (baseline_data ), 1 )
926+ self .assertEqual (baseline_data [0 ]["name" ], self .baseline1 .name )
927+
829928
830929class LivelihoodProductCategoryViewSetTestCase (APITestCase ):
831930 @classmethod
0 commit comments