|
| 1 | +from unittest import mock |
| 2 | +from django.core.management import call_command |
| 3 | + |
| 4 | +from main.test_case import APITestCase |
| 5 | +from api.factories.country import CountryFactory |
| 6 | +from country_plan.factories import CountryPlanFactory |
| 7 | +from country_plan.models import CountryPlan |
| 8 | +from country_plan.management.commands.ingest_country_plan_file import SOURCE |
| 9 | + |
| 10 | +# NOTE: Only defined used fields |
| 11 | +FILE_BASE_DIRECTORY = 'https://example.org/Download.aspx?FileId=' |
| 12 | +APPEAL_COUNTRY_PLAN_MOCK_RESPONSE = [ |
| 13 | + { |
| 14 | + 'BaseDirectory': FILE_BASE_DIRECTORY, |
| 15 | + 'BaseFileName': '000000', |
| 16 | + 'Inserted': '2022-11-29T11:24:00', |
| 17 | + 'LocationCountryCode': 'SY', |
| 18 | + 'LocationCountryName': 'Syrian Arab Republic' |
| 19 | + }, |
| 20 | + { |
| 21 | + 'BaseDirectory': FILE_BASE_DIRECTORY, |
| 22 | + 'BaseFileName': '000001', |
| 23 | + 'Inserted': '2022-11-29T11:24:00', |
| 24 | + 'LocationCountryCode': 'CD', |
| 25 | + 'LocationCountryName': 'Congo, The Democratic Republic Of The' |
| 26 | + }, |
| 27 | + { |
| 28 | + 'BaseDirectory': FILE_BASE_DIRECTORY, |
| 29 | + 'BaseFileName': '000002', |
| 30 | + 'Inserted': '2022-11-29T11:24:00', |
| 31 | + 'LocationCountryCode': 'MM', |
| 32 | + 'LocationCountryName': 'Myanmar' |
| 33 | + }, |
| 34 | + { |
| 35 | + 'BaseDirectory': FILE_BASE_DIRECTORY, |
| 36 | + 'BaseFileName': '000003', |
| 37 | + 'Inserted': '2022-11-29T11:24:00', |
| 38 | + 'LocationCountryCode': 'TM', |
| 39 | + 'LocationCountryName': 'Turkmenistan' |
| 40 | + }, |
| 41 | + { |
| 42 | + 'BaseDirectory': FILE_BASE_DIRECTORY, |
| 43 | + 'BaseFileName': '000004', |
| 44 | + 'Inserted': '2022-11-29T11:24:00', |
| 45 | + 'LocationCountryCode': 'GR', |
| 46 | + 'LocationCountryName': 'Greece' |
| 47 | + } |
| 48 | +] |
| 49 | + |
| 50 | + |
| 51 | +class MockResponse(): |
| 52 | + class FileStream(): |
| 53 | + def __init__(self, stream): |
| 54 | + self.stream = stream |
| 55 | + |
| 56 | + def raise_for_status(self): |
| 57 | + pass |
| 58 | + |
| 59 | + def iter_content(self, **_): |
| 60 | + return self.stream |
| 61 | + |
| 62 | + def __init__(self, json=None, stream=None): |
| 63 | + self._json = json |
| 64 | + self.stream = stream |
| 65 | + |
| 66 | + def json(self): |
| 67 | + return self._json |
| 68 | + |
| 69 | + def __enter__(self): |
| 70 | + return MockResponse.FileStream(self.stream) |
| 71 | + |
| 72 | + def __exit__(self, *_): |
| 73 | + pass |
| 74 | + |
| 75 | + |
| 76 | +class CountryPlanIngestTest(APITestCase): |
| 77 | + def mock_request(url, *_, **kwargs): |
| 78 | + if url == SOURCE: |
| 79 | + return MockResponse(json=APPEAL_COUNTRY_PLAN_MOCK_RESPONSE) |
| 80 | + if url.startswith(FILE_BASE_DIRECTORY): |
| 81 | + return MockResponse(stream=[b'']) |
| 82 | + |
| 83 | + @mock.patch('country_plan.management.commands.ingest_country_plan_file.requests.get', side_effect=mock_request) |
| 84 | + @mock.patch('main.utils.requests.get', side_effect=mock_request) |
| 85 | + def test_country_plan_ingest(self, *_): |
| 86 | + CountryPlanFactory.create( |
| 87 | + country=CountryFactory.create( |
| 88 | + name='Random Country', |
| 89 | + iso='RC', |
| 90 | + ), |
| 91 | + ) |
| 92 | + for country_plan_data in APPEAL_COUNTRY_PLAN_MOCK_RESPONSE[:3]: |
| 93 | + CountryPlanFactory.create( |
| 94 | + country=CountryFactory.create( |
| 95 | + name=country_plan_data['LocationCountryName'], |
| 96 | + iso=country_plan_data['LocationCountryCode'], |
| 97 | + ), |
| 98 | + ) |
| 99 | + assert CountryPlan.objects.count() == 4 |
| 100 | + assert CountryPlan.objects.filter(is_publish=True).count() == 0 |
| 101 | + assert CountryPlan.objects.exclude(public_plan_file='').count() == 0 |
| 102 | + call_command('ingest_country_plan_file') |
| 103 | + assert CountryPlan.objects.count() == 4 |
| 104 | + assert CountryPlan.objects.filter(is_publish=True).count() == 3 |
| 105 | + assert CountryPlan.objects.exclude(public_plan_file='').count() == 3 |
0 commit comments