Skip to content

Commit d2935fe

Browse files
committed
Add BaselineWealthCharacteristicsValueViewSetTestCase see HEA-772
1 parent 1a03481 commit d2935fe

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

apps/baseline/tests/test_viewsets.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from metadata.tests.factories import (
1616
LivelihoodCategoryFactory,
1717
WealthCharacteristicFactory,
18+
WealthGroupCategoryFactory,
1819
)
1920

2021
from .factories import (
@@ -5760,3 +5761,145 @@ def test_html(self):
57605761
content = response.content
57615762
df = pd.read_html(content)[0].fillna("")
57625763
self.assertEqual(len(df), self.num_records + 1)
5764+
5765+
5766+
class BaselineWealthCharacteristicsValueViewSetTestCase(APITestCase):
5767+
5768+
@classmethod
5769+
def setUpTestData(cls):
5770+
cls.baseline = LivelihoodZoneBaselineFactory()
5771+
5772+
cls.very_poor_wg = WealthGroupCategoryFactory(code="VP", name_en="Very Poor")
5773+
cls.poor_wg = WealthGroupCategoryFactory(code="P", name_en="Poor")
5774+
5775+
cls.baseline_wg_vp = BaselineWealthGroupFactory(
5776+
livelihood_zone_baseline=cls.baseline,
5777+
wealth_group_category=cls.very_poor_wg,
5778+
)
5779+
cls.baseline_wg_p = BaselineWealthGroupFactory(
5780+
livelihood_zone_baseline=cls.baseline,
5781+
wealth_group_category=cls.poor_wg,
5782+
)
5783+
5784+
# Create community wealth groups (should be excluded)
5785+
cls.community = CommunityFactory(livelihood_zone_baseline=cls.baseline)
5786+
cls.community_wg = CommunityWealthGroupFactory(
5787+
livelihood_zone_baseline=cls.baseline,
5788+
wealth_group_category=cls.very_poor_wg,
5789+
community=cls.community,
5790+
)
5791+
# Create wealth characteristics
5792+
cls.char_with_group = WealthCharacteristicFactory(
5793+
code="household size",
5794+
name_en="Household size",
5795+
characteristic_group="Population",
5796+
)
5797+
cls.char_without_group = WealthCharacteristicFactory(
5798+
code="other",
5799+
name_en="Other characteristic",
5800+
characteristic_group=None,
5801+
)
5802+
# Create characteristic values for baseline wealth groups (with values)
5803+
cls.value_baseline_vp = WealthGroupCharacteristicValueFactory(
5804+
wealth_group=cls.baseline_wg_vp,
5805+
wealth_characteristic=cls.char_with_group,
5806+
value=5,
5807+
min_value=4,
5808+
max_value=6,
5809+
)
5810+
cls.value_baseline_p = WealthGroupCharacteristicValueFactory(
5811+
wealth_group=cls.baseline_wg_p,
5812+
wealth_characteristic=cls.char_with_group,
5813+
value=6,
5814+
min_value=5,
5815+
max_value=7,
5816+
)
5817+
5818+
# Create characteristic value for community wealth group (should be excluded)
5819+
cls.value_community = WealthGroupCharacteristicValueFactory(
5820+
wealth_group=cls.community_wg,
5821+
wealth_characteristic=cls.char_with_group,
5822+
value=3,
5823+
)
5824+
5825+
def setUp(self):
5826+
self.url = reverse("baselinewealthcharacteristics-list")
5827+
5828+
def test_only_baseline_wealth_groups_included(self):
5829+
response = self.client.get(self.url)
5830+
self.assertEqual(response.status_code, 200)
5831+
results = response.json()
5832+
baseline_ids = {self.value_baseline_vp.id, self.value_baseline_p.id}
5833+
returned_ids = {r["id"] for r in results}
5834+
5835+
# All baseline records should be present
5836+
self.assertTrue(baseline_ids.issubset(returned_ids))
5837+
# Community record should not be present
5838+
self.assertNotIn(self.value_community.id, returned_ids)
5839+
5840+
def test_filter_by_livelihood_zone_baseline(self):
5841+
# Create another baseline with its own data
5842+
other_baseline = LivelihoodZoneBaselineFactory()
5843+
other_wg = BaselineWealthGroupFactory(
5844+
livelihood_zone_baseline=other_baseline,
5845+
wealth_group_category=self.very_poor_wg,
5846+
)
5847+
WealthGroupCharacteristicValueFactory(
5848+
wealth_group=other_wg,
5849+
wealth_characteristic=self.char_with_group,
5850+
value=10,
5851+
)
5852+
# Filter by our original baseline
5853+
response = self.client.get(self.url, {"livelihood_zone_baseline": self.baseline.pk})
5854+
self.assertEqual(response.status_code, 200)
5855+
results = response.json()
5856+
# Should only have our 2 original baseline records
5857+
self.assertEqual(len([r for r in results if r["livelihood_zone_baseline"] == self.baseline.pk]), 2)
5858+
5859+
def test_filter_has_value_true(self):
5860+
# Create another baseline with its own data
5861+
other_baseline = LivelihoodZoneBaselineFactory()
5862+
other_wg = BaselineWealthGroupFactory(
5863+
livelihood_zone_baseline=other_baseline,
5864+
wealth_group_category=self.very_poor_wg,
5865+
)
5866+
WealthGroupCharacteristicValueFactory(
5867+
wealth_group=other_wg,
5868+
wealth_characteristic=self.char_with_group,
5869+
value=0,
5870+
min_value=None,
5871+
max_value=None,
5872+
)
5873+
response = self.client.get(self.url, {"has_value": "true"})
5874+
self.assertEqual(response.status_code, 200)
5875+
results = response.json()
5876+
# Should include records with values
5877+
returned_ids = {r["id"] for r in results}
5878+
self.assertIn(self.value_baseline_vp.id, returned_ids)
5879+
self.assertIn(self.value_baseline_p.id, returned_ids)
5880+
self.assertNotIn(other_baseline.id, returned_ids)
5881+
5882+
def test_response_fields(self):
5883+
response = self.client.get(self.url)
5884+
self.assertEqual(response.status_code, 200)
5885+
results = response.json()
5886+
if len(results) > 0:
5887+
expected_fields = {
5888+
"id",
5889+
"wealth_group",
5890+
"wealth_group_category",
5891+
"wealth_group_category_name",
5892+
"livelihood_zone_baseline",
5893+
"livelihood_zone_baseline_label",
5894+
"wealth_characteristic",
5895+
"wealth_characteristic_name",
5896+
"characteristic_group",
5897+
"product",
5898+
"product_name",
5899+
"unit_of_measure",
5900+
"unit_of_measure_name",
5901+
"value",
5902+
"min_value",
5903+
"max_value",
5904+
}
5905+
self.assertEqual(set(results[0].keys()), expected_fields)

0 commit comments

Comments
 (0)