@@ -4900,6 +4900,8 @@ class LivelihoodActivitySummaryViewSetTestCase(APITestCase):
49004900 PRODUCT_DEFINITIONS = (
49014901 ("R01122" , "Maize (corn), other" , "Maize/corn grain" ),
49024902 ("R01142" , "Sorghum, other" , "Sorghum grain" ),
4903+ ("R01520" , "Cassava" , "Cassava" ),
4904+ ("L02111" , "Cattle" , "Cattle" ),
49034905 ("S86119" , "Other support services to crop production" , "Other cropping inputs" ),
49044906 ("S86121" , "Farm animal husbandry services on inputs owned by others" , "Livestock care" ),
49054907 ("S88537" , "Stone cutting, shaping and finishing services" , "Stone cutting" ),
@@ -4971,13 +4973,20 @@ def _create_livelihood_activities(cls, wealth_group, product):
49714973 LivelihoodActivityScenario .values if not wealth_group .community else [LivelihoodActivityScenario .BASELINE ]
49724974 )
49734975 for scenario in scenarios :
4974- if product .cpc in ["R01122" , "R01142" ]:
4976+ if product .cpc in ["R01122" , "R01142" , "R01520" ]:
49754977 CropProductionFactory (
49764978 livelihood_zone_baseline = wealth_group .livelihood_zone_baseline ,
49774979 wealth_group = wealth_group ,
49784980 livelihood_strategy__product = product ,
49794981 scenario = scenario ,
49804982 )
4983+ elif product .cpc in ["L02111" ]:
4984+ LivestockSaleFactory (
4985+ livelihood_zone_baseline = wealth_group .livelihood_zone_baseline ,
4986+ wealth_group = wealth_group ,
4987+ livelihood_strategy__product = product ,
4988+ scenario = scenario ,
4989+ )
49814990 elif product .cpc in ["S86119" , "S86121" ]:
49824991 OtherPurchaseFactory (
49834992 livelihood_zone_baseline = wealth_group .livelihood_zone_baseline ,
@@ -5116,6 +5125,31 @@ def test_summary_supports_product_slices(self):
51165125 with self .subTest (row = row ):
51175126 self .check_row_against_expected_slices (row , fields , expected , expected_slice )
51185127
5128+ def test_summary_supports_multiple_product_slices (self ):
5129+ fields = ["livelihood_zone_baseline" , "scenario" ]
5130+ expected = self .activity_df .groupby (fields ).agg (
5131+ kcals_consumed = ("kcals_consumed" , "sum" ),
5132+ income = ("income" , "sum" ),
5133+ expenditure = ("expenditure" , "sum" ),
5134+ percentage_kcals = ("percentage_kcals" , "sum" ),
5135+ )
5136+ expected_slice = (
5137+ self .activity_df [self .activity_df ["product" ].isin (["R01122" , "R01520" ])]
5138+ .groupby (fields )
5139+ .agg (
5140+ kcals_consumed = ("kcals_consumed" , "sum" ),
5141+ income = ("income" , "sum" ),
5142+ expenditure = ("expenditure" , "sum" ),
5143+ percentage_kcals = ("percentage_kcals" , "sum" ),
5144+ )
5145+ )
5146+ response = self .client .get (self .url , {"fields" : "," .join (fields ), "slice_by_product" : ["R01122" , "R01520" ]})
5147+ self .assertEqual (response .status_code , 200 )
5148+ self .assertEqual (len (response .json ()), len (expected ))
5149+ for row in response .json ():
5150+ with self .subTest (row = row ):
5151+ self .check_row_against_expected_slices (row , fields , expected , expected_slice )
5152+
51195153 def test_summary_supports_strategy_type_slices (self ):
51205154 fields = ["livelihood_zone_baseline" , "scenario" ]
51215155 expected = self .activity_df .groupby (fields ).agg (
@@ -5141,6 +5175,33 @@ def test_summary_supports_strategy_type_slices(self):
51415175 with self .subTest (row = row ):
51425176 self .check_row_against_expected_slices (row , fields , expected , expected_slice )
51435177
5178+ def test_summary_supports_multiple_strategy_type_slices (self ):
5179+ fields = ["livelihood_zone_baseline" , "scenario" ]
5180+ expected = self .activity_df .groupby (fields ).agg (
5181+ kcals_consumed = ("kcals_consumed" , "sum" ),
5182+ income = ("income" , "sum" ),
5183+ expenditure = ("expenditure" , "sum" ),
5184+ percentage_kcals = ("percentage_kcals" , "sum" ),
5185+ )
5186+ expected_slice = (
5187+ self .activity_df [self .activity_df ["strategy_type" ].isin (["CropProduction" , "OtherCashIncome" ])]
5188+ .groupby (fields )
5189+ .agg (
5190+ kcals_consumed = ("kcals_consumed" , "sum" ),
5191+ income = ("income" , "sum" ),
5192+ expenditure = ("expenditure" , "sum" ),
5193+ percentage_kcals = ("percentage_kcals" , "sum" ),
5194+ )
5195+ )
5196+ response = self .client .get (
5197+ self .url , {"fields" : "," .join (fields ), "slice_by_strategy_type" : ["CropProduction" , "OtherCashIncome" ]}
5198+ )
5199+ self .assertEqual (response .status_code , 200 )
5200+ self .assertEqual (len (response .json ()), len (expected ))
5201+ for row in response .json ():
5202+ with self .subTest (row = row ):
5203+ self .check_row_against_expected_slices (row , fields , expected , expected_slice )
5204+
51445205 def test_ordering (self ):
51455206 fields = ["wealth_group_category_ordering" , "strategy_type" , "reference_year_end_date" ]
51465207 expected = (
@@ -5194,6 +5255,41 @@ def test_summary_supports_combined_product_and_strategy_type_slices(self):
51945255 with self .subTest (row = row ):
51955256 self .check_row_against_expected_slices (row , fields , expected , expected_slice )
51965257
5258+ def test_summary_supports_multiple_combined_product_and_strategy_type_slices (self ):
5259+ fields = ["livelihood_zone_baseline" , "scenario" ]
5260+ expected = self .activity_df .groupby (fields ).agg (
5261+ kcals_consumed = ("kcals_consumed" , "sum" ),
5262+ income = ("income" , "sum" ),
5263+ expenditure = ("expenditure" , "sum" ),
5264+ percentage_kcals = ("percentage_kcals" , "sum" ),
5265+ )
5266+ expected_slice = (
5267+ self .activity_df [
5268+ (self .activity_df ["product" ].isin (["R01122" , "L02111" ]))
5269+ & (self .activity_df ["strategy_type" ].isin (["CropProduction" , "LivestockSale" ]))
5270+ ]
5271+ .groupby (fields )
5272+ .agg (
5273+ kcals_consumed = ("kcals_consumed" , "sum" ),
5274+ income = ("income" , "sum" ),
5275+ expenditure = ("expenditure" , "sum" ),
5276+ percentage_kcals = ("percentage_kcals" , "sum" ),
5277+ )
5278+ )
5279+ response = self .client .get (
5280+ self .url ,
5281+ {
5282+ "fields" : "," .join (fields ),
5283+ "slice_by_product" : ["R01122" , "L02111" ],
5284+ "slice_by_strategy_type" : ["CropProduction" , "LivestockSale" ],
5285+ },
5286+ )
5287+ self .assertEqual (response .status_code , 200 )
5288+ self .assertEqual (len (response .json ()), len (expected ))
5289+ for row in response .json ():
5290+ with self .subTest (row = row ):
5291+ self .check_row_against_expected_slices (row , fields , expected , expected_slice )
5292+
51975293 def test_min_max_row_filter (self ):
51985294 fields = ["livelihood_zone_baseline" , "scenario" , "wealth_group_category" ]
51995295 expected = self .activity_df .groupby (fields ).agg (
0 commit comments