|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from country_workspace.models.mixins import FlexFieldGroupingMixin |
| 6 | +from tests.extras.testutils.factories.smart_fields import ( |
| 7 | + DataCheckerFactory, |
| 8 | + DataCheckerFieldsetFactory, |
| 9 | + FieldsetFactory, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_checker(): |
| 15 | + return Mock() |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mixin_instance(mock_checker): |
| 20 | + instance = FlexFieldGroupingMixin() |
| 21 | + instance.checker = mock_checker |
| 22 | + return instance |
| 23 | + |
| 24 | + |
| 25 | +def test_get_grouping_info_empty_members(mixin_instance, mock_checker): |
| 26 | + mock_checker.members.select_related.return_value.all.return_value = [] |
| 27 | + result = mixin_instance.get_grouping_info() |
| 28 | + assert result == {} |
| 29 | + |
| 30 | + |
| 31 | +def test_get_grouping_info_with_group(mixin_instance, mock_checker): |
| 32 | + member1 = Mock(prefix="prefix1", group="group1", fieldset=Mock(group="fieldset_group1")) |
| 33 | + member2 = Mock(prefix="prefix2", group="group1", fieldset=Mock(group="fieldset_group2")) |
| 34 | + member3 = Mock(prefix="prefix3", group="group2", fieldset=Mock(group="fieldset_group3")) |
| 35 | + |
| 36 | + mock_checker.members.select_related.return_value.all.return_value = [member1, member2, member3] |
| 37 | + |
| 38 | + result = mixin_instance.get_grouping_info() |
| 39 | + |
| 40 | + expected = {"group1": ["prefix1", "prefix2"], "group2": ["prefix3"]} |
| 41 | + assert result == expected |
| 42 | + |
| 43 | + |
| 44 | +def test_get_grouping_info_with_fieldset_group(mixin_instance, mock_checker): |
| 45 | + member1 = Mock(prefix="prefix1", group=None, fieldset=Mock(group="fieldset_group1")) |
| 46 | + member2 = Mock(prefix="prefix2", group=None, fieldset=Mock(group="fieldset_group1")) |
| 47 | + |
| 48 | + mock_checker.members.select_related.return_value.all.return_value = [member1, member2] |
| 49 | + |
| 50 | + result = mixin_instance.get_grouping_info() |
| 51 | + |
| 52 | + expected = {"fieldset_group1": ["prefix1", "prefix2"]} |
| 53 | + assert result == expected |
| 54 | + |
| 55 | + |
| 56 | +def test_apply_grouping_no_grouping_info(mixin_instance): |
| 57 | + mixin_instance.flex_fields = {"field1": "value1", "field2": "value2"} |
| 58 | + |
| 59 | + with patch.object(mixin_instance, "get_grouping_info", return_value={}): |
| 60 | + result = mixin_instance.apply_grouping() |
| 61 | + |
| 62 | + assert result == {"field1": "value1", "field2": "value2"} |
| 63 | + |
| 64 | + |
| 65 | +def test_apply_grouping_single_group(mixin_instance): |
| 66 | + mixin_instance.flex_fields = { |
| 67 | + "prefix1_field1": "value1", |
| 68 | + "prefix1_field2": "value2", |
| 69 | + "prefix2_field1": "value3", |
| 70 | + "unprefixed_field": "value4", |
| 71 | + } |
| 72 | + |
| 73 | + grouping_info = {"group1": ["prefix1_", "prefix2_"]} |
| 74 | + |
| 75 | + with patch.object(mixin_instance, "get_grouping_info", return_value=grouping_info): |
| 76 | + result = mixin_instance.apply_grouping() |
| 77 | + |
| 78 | + expected = { |
| 79 | + "group1": [{"field1": "value1", "field2": "value2"}, {"field1": "value3"}], |
| 80 | + "unprefixed_field": "value4", |
| 81 | + } |
| 82 | + assert result == expected |
| 83 | + |
| 84 | + |
| 85 | +def test_apply_grouping_with_real_data_checker(db): |
| 86 | + checker = DataCheckerFactory(name="Test Checker") |
| 87 | + fieldset1 = FieldsetFactory(name="Household Fields", group="household") |
| 88 | + fieldset2 = FieldsetFactory(name="Individual Fields", group="individual") |
| 89 | + |
| 90 | + DataCheckerFieldsetFactory(checker=checker, fieldset=fieldset1, prefix="household_") |
| 91 | + DataCheckerFieldsetFactory(checker=checker, fieldset=fieldset2, prefix="individual_") |
| 92 | + |
| 93 | + instance = FlexFieldGroupingMixin() |
| 94 | + instance.checker = checker |
| 95 | + instance.flex_fields = { |
| 96 | + "household_name": "Test Family", |
| 97 | + "household_address": "Test Address", |
| 98 | + "individual_1_name": "John Doe", |
| 99 | + "individual_1_age": "25", |
| 100 | + "unprefixed_field": "value", |
| 101 | + } |
| 102 | + |
| 103 | + result = instance.apply_grouping() |
| 104 | + |
| 105 | + expected = { |
| 106 | + "household": [{"name": "Test Family", "address": "Test Address"}], |
| 107 | + "individual": [{"1_name": "John Doe", "1_age": "25"}], |
| 108 | + "unprefixed_field": "value", |
| 109 | + } |
| 110 | + assert result == expected |
0 commit comments