Skip to content

Commit f706682

Browse files
committed
Update test for ingest_country_plan_file
1 parent b2745ea commit f706682

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

country_plan/tests/test_commands.py

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from api.factories.country import CountryFactory
66
from country_plan.factories import CountryPlanFactory
77
from country_plan.models import CountryPlan
8-
from country_plan.management.commands.ingest_country_plan_file import SOURCE
8+
from country_plan.management.commands.ingest_country_plan_file import PUBLIC_SOURCE, INTERNAL_SOURCE
99

1010
# NOTE: Only defined used fields
1111
FILE_BASE_DIRECTORY = 'https://example.org/Download.aspx?FileId='
12-
APPEAL_COUNTRY_PLAN_MOCK_RESPONSE = [
12+
PUBLIC_APPEAL_COUNTRY_PLAN_MOCK_RESPONSE = [
1313
{
1414
'BaseDirectory': FILE_BASE_DIRECTORY,
1515
'BaseFileName': '000000',
@@ -24,6 +24,7 @@
2424
'LocationCountryCode': 'CD',
2525
'LocationCountryName': 'Congo, The Democratic Republic Of The'
2626
},
27+
# Not included in INTERNAL
2728
{
2829
'BaseDirectory': FILE_BASE_DIRECTORY,
2930
'BaseFileName': '000002',
@@ -47,6 +48,31 @@
4748
}
4849
]
4950

51+
INTERNAL_APPEAL_COUNTRY_PLAN_MOCK_RESPONSE = [
52+
{
53+
'BaseDirectory': FILE_BASE_DIRECTORY,
54+
'BaseFileName': '000000',
55+
'Inserted': '2022-11-29T11:24:00',
56+
'LocationCountryCode': 'SY',
57+
'LocationCountryName': 'Syrian Arab Republic'
58+
},
59+
{
60+
'BaseDirectory': FILE_BASE_DIRECTORY,
61+
'BaseFileName': '000001',
62+
'Inserted': '2022-11-29T11:24:00',
63+
'LocationCountryCode': 'CD',
64+
'LocationCountryName': 'Congo, The Democratic Republic Of The'
65+
},
66+
# Not included in PUBLIC
67+
{
68+
'BaseDirectory': FILE_BASE_DIRECTORY,
69+
'BaseFileName': '000001',
70+
'Inserted': '2022-11-29T11:24:00',
71+
'LocationCountryCode': 'NP',
72+
'LocationCountryName': 'Nepal'
73+
},
74+
]
75+
5076

5177
class MockResponse():
5278
class FileStream():
@@ -75,8 +101,10 @@ def __exit__(self, *_):
75101

76102
class CountryPlanIngestTest(APITestCase):
77103
def mock_request(url, *_, **kwargs):
78-
if url == SOURCE:
79-
return MockResponse(json=APPEAL_COUNTRY_PLAN_MOCK_RESPONSE)
104+
if url == PUBLIC_SOURCE:
105+
return MockResponse(json=PUBLIC_APPEAL_COUNTRY_PLAN_MOCK_RESPONSE)
106+
if url == INTERNAL_SOURCE:
107+
return MockResponse(json=INTERNAL_APPEAL_COUNTRY_PLAN_MOCK_RESPONSE)
80108
if url.startswith(FILE_BASE_DIRECTORY):
81109
return MockResponse(stream=[b''])
82110

@@ -89,17 +117,25 @@ def test_country_plan_ingest(self, *_):
89117
iso='RC',
90118
),
91119
)
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
120+
EXISTING_CP = 1
121+
for country_iso in set([
122+
item['LocationCountryCode']
123+
for item in [
124+
*PUBLIC_APPEAL_COUNTRY_PLAN_MOCK_RESPONSE,
125+
*INTERNAL_APPEAL_COUNTRY_PLAN_MOCK_RESPONSE,
126+
]
127+
]):
128+
if country_iso == 'CD':
129+
# Not create country for this
130+
continue
131+
CountryFactory.create(iso=country_iso)
132+
# Before
133+
assert CountryPlan.objects.count() == EXISTING_CP
100134
assert CountryPlan.objects.filter(is_publish=True).count() == 0
101135
assert CountryPlan.objects.exclude(public_plan_file='').count() == 0
102136
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
137+
# After
138+
assert CountryPlan.objects.count() == EXISTING_CP + 5
139+
assert CountryPlan.objects.filter(is_publish=True).count() == 5
140+
assert CountryPlan.objects.exclude(public_plan_file='').count() == 4
141+
assert CountryPlan.objects.exclude(internal_plan_file='').count() == 2

0 commit comments

Comments
 (0)