|
| 1 | +import pandas as pd |
| 2 | +from django.test import TestCase |
| 3 | + |
| 4 | +from common.tests.factories import CountryFactory |
| 5 | +from metadata.lookups import SeasonLookup, SeasonNameLookup |
| 6 | +from metadata.models import LivelihoodStrategyType |
| 7 | + |
| 8 | +from .factories import SeasonFactory |
| 9 | + |
| 10 | + |
| 11 | +class SeasonLookupTestCase(TestCase): |
| 12 | + def setUp(self): |
| 13 | + self.country = CountryFactory() |
| 14 | + |
| 15 | + # season with "Season 1" as an alias but no purpose |
| 16 | + self.season1_no_purpose = SeasonFactory( |
| 17 | + country=self.country, |
| 18 | + name_en="Generic First Season", |
| 19 | + aliases=["Season 1"], |
| 20 | + purpose=None, |
| 21 | + ) |
| 22 | + |
| 23 | + # MilkProduction purpose with "Season 1" as an alias |
| 24 | + self.season1_milk = SeasonFactory( |
| 25 | + country=self.country, |
| 26 | + name_en="Milk Production First Season", |
| 27 | + aliases=["Season 1"], |
| 28 | + purpose=LivelihoodStrategyType.MILK_PRODUCTION, |
| 29 | + ) |
| 30 | + |
| 31 | + # CropProduction purpose with "Season 1" as an alias |
| 32 | + self.season1_crop = SeasonFactory( |
| 33 | + country=self.country, |
| 34 | + name_en="Crop Production First Season", |
| 35 | + aliases=["Season 1"], |
| 36 | + purpose=LivelihoodStrategyType.CROP_PRODUCTION, |
| 37 | + ) |
| 38 | + |
| 39 | + def test_lookup_without_purpose(self): |
| 40 | + # when no purpose is provided, should match only seasons with null purpose |
| 41 | + df = pd.DataFrame( |
| 42 | + { |
| 43 | + "season": ["Season 1"], |
| 44 | + "country_id": [self.country.iso3166a2], |
| 45 | + } |
| 46 | + ) |
| 47 | + result_df = SeasonLookup().do_lookup(df, "season", "season_id") |
| 48 | + self.assertTrue("season_id" in result_df.columns) |
| 49 | + self.assertEqual(len(result_df), 1) |
| 50 | + self.assertEqual(result_df["season_id"][0], self.season1_no_purpose.id) |
| 51 | + |
| 52 | + def test_lookup_with_matching_purpose(self): |
| 53 | + # test when purpose is provided and matches a season's purpose |
| 54 | + df = pd.DataFrame( |
| 55 | + { |
| 56 | + "season": ["Season 1"], |
| 57 | + "country_id": [self.country.iso3166a2], |
| 58 | + "purpose": [LivelihoodStrategyType.MILK_PRODUCTION], |
| 59 | + } |
| 60 | + ) |
| 61 | + result_df = SeasonLookup().do_lookup(df, "season", "season_id") |
| 62 | + self.assertTrue("season_id" in result_df.columns) |
| 63 | + self.assertEqual(len(result_df), 1) |
| 64 | + self.assertEqual(result_df["season_id"][0], self.season1_milk.id) |
| 65 | + |
| 66 | + def test_lookup_with_strategy_type_alias(self): |
| 67 | + # test when strategy_type is provided and matches a season's purpose |
| 68 | + df = pd.DataFrame( |
| 69 | + { |
| 70 | + "season": ["Season 1"], |
| 71 | + "country_id": [self.country.iso3166a2], |
| 72 | + "strategy_type": [LivelihoodStrategyType.MILK_PRODUCTION], |
| 73 | + } |
| 74 | + ) |
| 75 | + result_df = SeasonLookup().do_lookup(df, "season", "season_id") |
| 76 | + self.assertTrue("season_id" in result_df.columns) |
| 77 | + self.assertEqual(len(result_df), 1) |
| 78 | + self.assertEqual(result_df["season_id"][0], self.season1_milk.id) |
| 79 | + |
| 80 | + def test_lookup_with_non_matching_purpose_falls_back(self): |
| 81 | + # test when purpose doesn't match any season's purpose, should fall back Seasons with null purpose |
| 82 | + df = pd.DataFrame( |
| 83 | + { |
| 84 | + "season": ["Season 1"], |
| 85 | + "country_id": [self.country.iso3166a2], |
| 86 | + "purpose": [LivelihoodStrategyType.LIVESTOCK_SALE], |
| 87 | + } |
| 88 | + ) |
| 89 | + result_df = SeasonLookup().do_lookup(df, "season", "season_id") |
| 90 | + self.assertTrue("season_id" in result_df.columns) |
| 91 | + self.assertEqual(len(result_df), 1) |
| 92 | + self.assertEqual(result_df["season_id"][0], self.season1_no_purpose.id) |
| 93 | + |
| 94 | + def test_season_name_lookup_with_purpose(self): |
| 95 | + df = pd.DataFrame( |
| 96 | + { |
| 97 | + "season": ["Season 1"], |
| 98 | + "country_id": [self.country.iso3166a2], |
| 99 | + "purpose": [LivelihoodStrategyType.MILK_PRODUCTION], |
| 100 | + } |
| 101 | + ) |
| 102 | + result_df = SeasonNameLookup().do_lookup(df, "season", "season_name") |
| 103 | + self.assertTrue("season_name" in result_df.columns) |
| 104 | + self.assertEqual(len(result_df), 1) |
| 105 | + self.assertEqual(result_df["season_name"][0], self.season1_milk.name_en) |
| 106 | + |
| 107 | + def test_lookup_raises_error_with_duplicate_null_purpose(self): |
| 108 | + # test when multiple seasons have null purpose and same alias, should raise error |
| 109 | + SeasonFactory( |
| 110 | + country=self.country, |
| 111 | + name_en="Another Generic First Season", |
| 112 | + aliases=["Season 1"], |
| 113 | + purpose=None, |
| 114 | + ) |
| 115 | + |
| 116 | + df = pd.DataFrame( |
| 117 | + { |
| 118 | + "season": ["Season 1"], |
| 119 | + "country_id": [self.country.iso3166a2], |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + with self.assertRaises(ValueError) as context: |
| 124 | + SeasonLookup().do_lookup(df, "season", "season_id") |
| 125 | + |
| 126 | + self.assertIn("found multiple Season matches", str(context.exception)) |
| 127 | + |
| 128 | + def test_lookup_raises_error_with_duplicate_same_purpose(self): |
| 129 | + # test when multiple seasons have same purpose and same alias, should raise error |
| 130 | + SeasonFactory( |
| 131 | + country=self.country, |
| 132 | + name_en="Another Milk Production Season", |
| 133 | + aliases=["Season 1"], |
| 134 | + purpose=LivelihoodStrategyType.MILK_PRODUCTION, |
| 135 | + ) |
| 136 | + |
| 137 | + df = pd.DataFrame( |
| 138 | + { |
| 139 | + "season": ["Season 1"], |
| 140 | + "country_id": [self.country.iso3166a2], |
| 141 | + "purpose": [LivelihoodStrategyType.MILK_PRODUCTION], |
| 142 | + } |
| 143 | + ) |
| 144 | + |
| 145 | + with self.assertRaises(ValueError) as context: |
| 146 | + SeasonLookup().do_lookup(df, "season", "season_id") |
| 147 | + |
| 148 | + self.assertIn("found multiple Season matches", str(context.exception)) |
0 commit comments