Skip to content

Commit 0405260

Browse files
committed
Adds person_data_reader and tests
1 parent c6e4717 commit 0405260

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Collection, Mapping
4+
from typing import Any
5+
6+
from wireup import service
7+
8+
Row = Collection[Mapping[str, Any]]
9+
10+
11+
@service
12+
class PersonDataReader:
13+
"""Handles extracting and interpreting person data."""
14+
15+
def get_person_cohorts(self, person_data: Row) -> set[str]:
16+
cohorts_row: Mapping[str, dict[str, dict[str, dict[str, Any]]]] = next(
17+
(row for row in person_data if row.get("ATTRIBUTE_TYPE") == "COHORTS"),
18+
{},
19+
)
20+
return set(cohorts_row.get("COHORT_MAP", {}).get("cohorts", {}).get("M", {}).keys())
21+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import pytest
2+
from hamcrest import assert_that, is_
3+
4+
from eligibility_signposting_api.services.person_data_reader import PersonDataReader
5+
6+
7+
@pytest.fixture
8+
def person_data_reader():
9+
return PersonDataReader()
10+
11+
12+
def test_get_person_cohorts_empty_data(person_data_reader):
13+
result = person_data_reader.get_person_cohorts([])
14+
assert_that(result, is_(set()))
15+
16+
17+
def test_get_person_cohorts_no_cohorts_attribute_type(person_data_reader):
18+
no_cohorts_type = [
19+
{"ATTRIBUTE_TYPE": "NAME", "VALUE": "John Doe"},
20+
{"ATTRIBUTE_TYPE": "AGE", "VALUE": 30},
21+
]
22+
result = person_data_reader.get_person_cohorts(no_cohorts_type)
23+
assert_that(result, is_(set()))
24+
25+
26+
def test_get_person_cohorts_no_cohort_map_key(person_data_reader):
27+
no_cohorts_map = [
28+
{"ATTRIBUTE_TYPE": "COHORTS", "OTHER_FIELD": "value"},
29+
]
30+
result = person_data_reader.get_person_cohorts(no_cohorts_map)
31+
assert_that(result, is_(set()))
32+
33+
34+
def test_get_person_cohorts_no_cohorts_list_key(person_data_reader):
35+
no_cohorts_list = [
36+
{"ATTRIBUTE_TYPE": "COHORTS", "COHORT_MAP": {"another_key": {}}},
37+
]
38+
result = person_data_reader.get_person_cohorts(no_cohorts_list)
39+
assert_that(result, is_(set()))
40+
41+
42+
def test_get_person_cohorts_no_m_key(person_data_reader):
43+
no_m_key = [
44+
{"ATTRIBUTE_TYPE": "COHORTS", "COHORT_MAP": {"cohorts": {"X": {}}}},
45+
]
46+
result = person_data_reader.get_person_cohorts(no_m_key)
47+
assert_that(result, is_(set()))
48+
49+
50+
def test_get_person_cohorts_single_cohort(person_data_reader):
51+
single_cohorts = [
52+
{"ATTRIBUTE_TYPE": "COHORTS", "COHORT_MAP": {"cohorts": {"M": {"COHORT_A": {}}}}},
53+
{"ATTRIBUTE_TYPE": "NAME", "VALUE": "Jane Smith"},
54+
]
55+
result = person_data_reader.get_person_cohorts(single_cohorts)
56+
assert_that(result, is_({"COHORT_A"}))
57+
58+
59+
def test_get_person_cohorts_multiple_cohorts(person_data_reader):
60+
multiple_cohorts = [
61+
{"ATTRIBUTE_TYPE": "COHORTS", "COHORT_MAP": {"cohorts": {"M": {"COHORT_B": {}, "COHORT_C": {}}}}},
62+
{"ATTRIBUTE_TYPE": "AGE", "VALUE": 45},
63+
]
64+
result = person_data_reader.get_person_cohorts(multiple_cohorts)
65+
assert_that(result, is_({"COHORT_B", "COHORT_C"}))
66+
67+
68+
def test_get_person_cohorts_mixed_data(person_data_reader):
69+
mixed_data = [
70+
{"ATTRIBUTE_TYPE": "NAME", "VALUE": "Alice"},
71+
{"ATTRIBUTE_TYPE": "COHORTS", "COHORT_MAP": {"cohorts": {"M": {"COHORT_D": {}, "COHORT_E": {}}}}},
72+
{"ATTRIBUTE_TYPE": "ADDRESS", "VALUE": "123 Main St"},
73+
]
74+
result = person_data_reader.get_person_cohorts(mixed_data)
75+
assert_that(result, is_({"COHORT_D", "COHORT_E"}))
76+
77+
78+
def test_get_person_cohorts_with_other_attribute_types_present(person_data_reader):
79+
data = [
80+
{"ATTRIBUTE_TYPE": "NAME", "VALUE": "Charlie"},
81+
{"ATTRIBUTE_TYPE": "COHORTS", "COHORT_MAP": {"cohorts": {"M": {"COHORT_F": {}}}}},
82+
{"ATTRIBUTE_TYPE": "AGE", "VALUE": 25},
83+
]
84+
result = person_data_reader.get_person_cohorts(data)
85+
assert_that(result, is_({"COHORT_F"}))

0 commit comments

Comments
 (0)