diff options
Diffstat (limited to 'tests/managers/thl/test_profiling')
| -rw-r--r-- | tests/managers/thl/test_profiling/__init__.py | 0 | ||||
| -rw-r--r-- | tests/managers/thl/test_profiling/test_question.py | 49 | ||||
| -rw-r--r-- | tests/managers/thl/test_profiling/test_schema.py | 44 | ||||
| -rw-r--r-- | tests/managers/thl/test_profiling/test_uqa.py | 1 | ||||
| -rw-r--r-- | tests/managers/thl/test_profiling/test_user_upk.py | 59 |
5 files changed, 153 insertions, 0 deletions
diff --git a/tests/managers/thl/test_profiling/__init__.py b/tests/managers/thl/test_profiling/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/managers/thl/test_profiling/__init__.py diff --git a/tests/managers/thl/test_profiling/test_question.py b/tests/managers/thl/test_profiling/test_question.py new file mode 100644 index 0000000..998466e --- /dev/null +++ b/tests/managers/thl/test_profiling/test_question.py @@ -0,0 +1,49 @@ +from uuid import uuid4 + +from generalresearch.managers.thl.profiling.question import QuestionManager +from generalresearch.models import Source + + +class TestQuestionManager: + + def test_get_multi_upk(self, question_manager: QuestionManager, upk_data): + qs = question_manager.get_multi_upk( + question_ids=[ + "8a22de34f985476aac85e15547100db8", + "0565f87d4bf044298ba169de1339ff7e", + "b2b32d68403647e3a87e778a6348d34c", + uuid4().hex, + ] + ) + assert len(qs) == 3 + + def test_get_questions_ranked(self, question_manager: QuestionManager, upk_data): + qs = question_manager.get_questions_ranked(country_iso="mx", language_iso="spa") + assert len(qs) >= 40 + assert qs[0].importance.task_score > qs[40].importance.task_score + assert all(q.country_iso == "mx" and q.language_iso == "spa" for q in qs) + + def test_lookup_by_property(self, question_manager: QuestionManager, upk_data): + q = question_manager.lookup_by_property( + property_code="i:industry", country_iso="us", language_iso="eng" + ) + assert q.source == Source.INNOVATE + + q.explanation_template = "You work in the {answer} industry." + q.explanation_fragment_template = "you work in the {answer} industry" + question_manager.update_question_explanation(q) + + q = question_manager.lookup_by_property( + property_code="i:industry", country_iso="us", language_iso="eng" + ) + assert q.explanation_template + + def test_filter_by_property(self, question_manager: QuestionManager, upk_data): + lookup = [ + ("i:industry", "us", "eng"), + ("i:industry", "mx", "eng"), + ("m:age", "us", "eng"), + (f"m:{uuid4().hex}", "us", "eng"), + ] + qs = question_manager.filter_by_property(lookup) + assert len(qs) == 3 diff --git a/tests/managers/thl/test_profiling/test_schema.py b/tests/managers/thl/test_profiling/test_schema.py new file mode 100644 index 0000000..ae61527 --- /dev/null +++ b/tests/managers/thl/test_profiling/test_schema.py @@ -0,0 +1,44 @@ +from generalresearch.models.thl.profiling.upk_property import PropertyType + + +class TestUpkSchemaManager: + + def test_get_props_info(self, upk_schema_manager, upk_data): + props = upk_schema_manager.get_props_info() + assert ( + len(props) == 16955 + ) # ~ 70 properties x each country they are available in + + gender = [ + x + for x in props + if x.country_iso == "us" + and x.property_id == "73175402104741549f21de2071556cd7" + ] + assert len(gender) == 1 + gender = gender[0] + assert len(gender.allowed_items) == 3 + assert gender.allowed_items[0].label == "female" + assert gender.allowed_items[1].label == "male" + assert gender.prop_type == PropertyType.UPK_ITEM + assert gender.categories[0].label == "Demographic" + + age = [ + x + for x in props + if x.country_iso == "us" + and x.property_id == "94f7379437874076b345d76642d4ce6d" + ] + assert len(age) == 1 + age = age[0] + assert age.allowed_items is None + assert age.prop_type == PropertyType.UPK_NUMERICAL + assert age.gold_standard + + cars = [ + x + for x in props + if x.country_iso == "us" and x.property_label == "household_auto_type" + ][0] + assert not cars.gold_standard + assert cars.categories[0].label == "Autos & Vehicles" diff --git a/tests/managers/thl/test_profiling/test_uqa.py b/tests/managers/thl/test_profiling/test_uqa.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/managers/thl/test_profiling/test_uqa.py @@ -0,0 +1 @@ + diff --git a/tests/managers/thl/test_profiling/test_user_upk.py b/tests/managers/thl/test_profiling/test_user_upk.py new file mode 100644 index 0000000..53bb8fe --- /dev/null +++ b/tests/managers/thl/test_profiling/test_user_upk.py @@ -0,0 +1,59 @@ +from datetime import datetime, timezone + +from generalresearch.managers.thl.profiling.user_upk import UserUpkManager + +now = datetime.now(tz=timezone.utc) +base = { + "country_iso": "us", + "language_iso": "eng", + "timestamp": now, +} +upk_ans_dict = [ + {"pred": "gr:gender", "obj": "gr:male"}, + {"pred": "gr:age_in_years", "obj": "43"}, + {"pred": "gr:home_postal_code", "obj": "33143"}, + {"pred": "gr:ethnic_group", "obj": "gr:caucasians"}, + {"pred": "gr:ethnic_group", "obj": "gr:asian"}, +] +for a in upk_ans_dict: + a.update(base) + + +class TestUserUpkManager: + + def test_user_upk_empty(self, user_upk_manager: UserUpkManager, upk_data, user): + res = user_upk_manager.get_user_upk_mysql(user_id=user.user_id) + assert len(res) == 0 + + def test_user_upk(self, user_upk_manager: UserUpkManager, upk_data, user): + for x in upk_ans_dict: + x["user_id"] = user.user_id + user_upk = user_upk_manager.populate_user_upk_from_dict(upk_ans_dict) + user_upk_manager.set_user_upk(upk_ans=user_upk) + + d = user_upk_manager.get_user_upk_simple(user_id=user.user_id) + assert d["gender"] == "male" + assert d["age_in_years"] == 43 + assert d["home_postal_code"] == "33143" + assert d["ethnic_group"] == {"caucasians", "asian"} + + # Change my answers. age 43->44, gender male->female, + # ethnic->remove asian, add black_or_african_american + for x in upk_ans_dict: + if x["pred"] == "age_in_years": + x["obj"] = "44" + if x["pred"] == "gender": + x["obj"] = "female" + upk_ans_dict[-1]["obj"] = "black_or_african_american" + user_upk = user_upk_manager.populate_user_upk_from_dict(upk_ans_dict) + user_upk_manager.set_user_upk(upk_ans=user_upk) + + d = user_upk_manager.get_user_upk_simple(user_id=user.user_id) + assert d["gender"] == "female" + assert d["age_in_years"] == 44 + assert d["home_postal_code"] == "33143" + assert d["ethnic_group"] == {"caucasians", "black_or_african_american"} + + age, gender = user_upk_manager.get_age_gender(user_id=user.user_id) + assert age == 44 + assert gender == "female" |
