|
| 1 | +import io |
| 2 | +import json |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from eligibility_signposting_api.repos.campaign_repo import BucketName, CampaignRepo, campaign_config_cache |
| 8 | +from tests.fixtures.builders.model.rule import CampaignConfigFactory |
| 9 | + |
| 10 | + |
| 11 | +def make_s3_body(payload: dict): |
| 12 | + return {"Body": io.BytesIO(json.dumps(payload).encode("utf-8"))} |
| 13 | + |
| 14 | + |
| 15 | +class TestCampaignRepo: |
| 16 | + @pytest.fixture(autouse=True) |
| 17 | + def clear_cache(self): |
| 18 | + campaign_config_cache.clear() |
| 19 | + |
| 20 | + @pytest.fixture |
| 21 | + def mock_s3_client(self): |
| 22 | + return MagicMock() |
| 23 | + |
| 24 | + @pytest.fixture |
| 25 | + def repo(self, mock_s3_client): |
| 26 | + return CampaignRepo( |
| 27 | + s3_client=mock_s3_client, |
| 28 | + bucket_name=BucketName("test-bucket"), |
| 29 | + ) |
| 30 | + |
| 31 | + @pytest.fixture |
| 32 | + def rules_payload(self): |
| 33 | + campaign_config = CampaignConfigFactory.build() |
| 34 | + return {"campaign_config": campaign_config.model_dump(mode="json")} |
| 35 | + |
| 36 | + def test_get_campaign_configs_loads_from_s3(self, repo, mock_s3_client, rules_payload): |
| 37 | + mock_s3_client.list_objects.return_value = {"Contents": [{"Key": "rsv.json"}]} |
| 38 | + mock_s3_client.get_object.return_value = make_s3_body(rules_payload) |
| 39 | + |
| 40 | + result = list(repo.get_campaign_configs("consumer_id")) |
| 41 | + |
| 42 | + assert len(result) == 1 |
| 43 | + assert result[0].id == rules_payload["campaign_config"]["id"] |
| 44 | + |
| 45 | + mock_s3_client.list_objects.assert_called_once_with(Bucket="test-bucket") |
| 46 | + mock_s3_client.get_object.assert_called_once_with( |
| 47 | + Bucket="test-bucket", |
| 48 | + Key="rsv.json", |
| 49 | + ) |
| 50 | + |
| 51 | + def test_get_campaign_configs_uses_cache_within_ttl( |
| 52 | + self, |
| 53 | + repo, |
| 54 | + mock_s3_client, |
| 55 | + ): |
| 56 | + first_config = CampaignConfigFactory.build(version=1) |
| 57 | + |
| 58 | + mock_s3_client.list_objects.return_value = {"Contents": [{"Key": "rsv.json"}]} |
| 59 | + mock_s3_client.get_object.return_value = make_s3_body({"campaign_config": first_config.model_dump(mode="json")}) |
| 60 | + |
| 61 | + first = list(repo.get_campaign_configs("consumer_id")) |
| 62 | + second = list(repo.get_campaign_configs("consumer_id")) |
| 63 | + |
| 64 | + assert first[0].version == 1 |
| 65 | + assert second[0].version == 1 |
| 66 | + assert mock_s3_client.list_objects.call_count == 1 |
| 67 | + assert mock_s3_client.get_object.call_count == 1 |
| 68 | + |
| 69 | + def test_get_campaign_configs_refreshes_after_cache_is_cleared( |
| 70 | + self, |
| 71 | + repo, |
| 72 | + mock_s3_client, |
| 73 | + ): |
| 74 | + first_config = CampaignConfigFactory.build(version=1) |
| 75 | + second_config = CampaignConfigFactory.build(version=2) |
| 76 | + |
| 77 | + mock_s3_client.list_objects.return_value = {"Contents": [{"Key": "rsv.json"}]} |
| 78 | + mock_s3_client.get_object.side_effect = [ |
| 79 | + make_s3_body({"campaign_config": first_config.model_dump(mode="json")}), |
| 80 | + make_s3_body({"campaign_config": second_config.model_dump(mode="json")}), |
| 81 | + ] |
| 82 | + |
| 83 | + first = list(repo.get_campaign_configs("consumer_id")) |
| 84 | + second = list(repo.get_campaign_configs("consumer_id")) |
| 85 | + campaign_config_cache.clear() |
| 86 | + third = list(repo.get_campaign_configs("test-consumer-1")) |
| 87 | + |
| 88 | + expected_call_count = 2 |
| 89 | + |
| 90 | + assert first[0].version == first_config.version |
| 91 | + assert second[0].version == first_config.version |
| 92 | + assert third[0].version == second_config.version |
| 93 | + assert mock_s3_client.list_objects.call_count == expected_call_count |
| 94 | + assert mock_s3_client.get_object.call_count == expected_call_count |
0 commit comments