|
3 | 3 |
|
4 | 4 | import pandas as pd |
5 | 5 | from django.test import TestCase |
6 | | -from pipelines.assets.livelihood_activity import get_label_attributes |
| 6 | +from pipelines.assets.livelihood_activity import ( |
| 7 | + get_label_attributes, |
| 8 | + get_livelihood_activity_label_map, |
| 9 | +) |
7 | 10 |
|
8 | 11 | from metadata.models import ActivityLabel |
9 | 12 |
|
@@ -44,3 +47,57 @@ def test_livelihood_activity_regexes(self): |
44 | 47 | any([bool(v) for k, v in unwanted_attributes.items()]), |
45 | 48 | msg=f"Extra attributes {({k: v for k, v in unwanted_attributes.items() if v})} found for '{label}'", |
46 | 49 | ) |
| 50 | + |
| 51 | + def test_activity_label_override(self): |
| 52 | + label = "riz - kg produits" |
| 53 | + expected_regex_attributes = { |
| 54 | + "activity_label": label, |
| 55 | + "is_start": True, |
| 56 | + "product_id": "riz", |
| 57 | + "unit_of_measure_id": "kg", |
| 58 | + "attribute": "quantity_produced", |
| 59 | + } |
| 60 | + # Test that the regular expression matches the label and returns the expected attributes |
| 61 | + regex_attributes = {k: v for k, v in get_label_attributes(label, LIVELIHOOD_ACTIVITY).items()} |
| 62 | + self.assertDictEqual( |
| 63 | + expected_regex_attributes, |
| 64 | + {k: v for k, v in regex_attributes.items() if k in expected_regex_attributes}, |
| 65 | + ) |
| 66 | + # Now create an ActivityLabel instance with status=OVERRIDE for the same label but different attributes |
| 67 | + expected_override_attributes = { |
| 68 | + "activity_label": label, |
| 69 | + "is_start": False, |
| 70 | + "product_id": "R01132", |
| 71 | + "season": "season 1", |
| 72 | + } |
| 73 | + ActivityLabel.objects.create( |
| 74 | + status=ActivityLabel.LabelStatus.OVERRIDE, |
| 75 | + activity_type=LIVELIHOOD_ACTIVITY, |
| 76 | + **expected_override_attributes, |
| 77 | + ) |
| 78 | + # Clear the cache of label attributes |
| 79 | + get_label_attributes.cache_clear() |
| 80 | + get_livelihood_activity_label_map.cache_clear() |
| 81 | + # Test that the override attributes are returned instead of the regex attributes |
| 82 | + override_attributes = {k: v for k, v in get_label_attributes(label, LIVELIHOOD_ACTIVITY).items()} |
| 83 | + self.assertDictEqual( |
| 84 | + expected_override_attributes, |
| 85 | + {k: v for k, v in override_attributes.items() if k in expected_override_attributes}, |
| 86 | + ) |
| 87 | + # Test that additional attributes set in the regex instance are ignored when using the override |
| 88 | + self.assertEqual(None, override_attributes["unit_of_measure_id"]) |
| 89 | + # Update the ActivityLabel instance to make it use the regex again |
| 90 | + ActivityLabel.objects.filter(activity_label=label, activity_type=LIVELIHOOD_ACTIVITY).update( |
| 91 | + status=ActivityLabel.LabelStatus.REGULAR_EXPRESSION |
| 92 | + ) |
| 93 | + # Clear the cache of label attributes |
| 94 | + get_label_attributes.cache_clear() |
| 95 | + get_livelihood_activity_label_map.cache_clear() |
| 96 | + # Test that the regex attributes are returned again |
| 97 | + regex_attributes = {k: v for k, v in get_label_attributes(label, LIVELIHOOD_ACTIVITY).items()} |
| 98 | + self.assertDictEqual( |
| 99 | + expected_regex_attributes, |
| 100 | + {k: v for k, v in regex_attributes.items() if k in expected_regex_attributes}, |
| 101 | + ) |
| 102 | + # Test that additional attributes set in the ActivityLabel instance are ignored when using the regex |
| 103 | + self.assertEqual("", regex_attributes["season"]) |
0 commit comments