55from api .factories .country import CountryFactory
66from country_plan .factories import CountryPlanFactory
77from 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
1111FILE_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' ,
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' ,
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
5177class MockResponse ():
5278 class FileStream ():
@@ -75,8 +101,10 @@ def __exit__(self, *_):
75101
76102class 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