@@ -152,23 +152,24 @@ def mockshow(url, datadict):
152152 '{"success": false, "error": {"message": "TEST ERROR: Not show", "__type": "TEST ERROR: Not Show Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}' ,
153153 )
154154 result = json .dumps (resultdict )
155- if datadict ["id" ] == "74b74ae1-df0c-4716-829f-4f939a046811" :
155+ datadict_id = datadict ["id" ]
156+ if datadict_id == "74b74ae1-df0c-4716-829f-4f939a046811" :
156157 return MockResponse (
157158 200 ,
158159 '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}'
159160 % result ,
160161 )
161- if datadict [ "id" ] == "74b74ae1-df0c-4716-829f-4f939a046812" :
162+ if datadict_id == "74b74ae1-df0c-4716-829f-4f939a046812" :
162163 return MockResponse (
163164 404 ,
164165 '{"success": false, "error": {"message": "Not found", "__type": "Not Found Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}' ,
165166 )
166- if datadict [ "id" ] == "74b74ae1-df0c-4716-829f-4f939a046813" :
167+ if datadict_id == "74b74ae1-df0c-4716-829f-4f939a046813" :
167168 return MockResponse (
168169 200 ,
169170 '{"success": false, "error": {"message": "Not found", "__type": "Not Found Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}' ,
170171 )
171- if datadict [ "id" ] == "74b74ae1-df0c-4716-829f-4f939a046814" :
172+ if datadict_id == "74b74ae1-df0c-4716-829f-4f939a046814" :
172173 resdictcopy = copy .deepcopy (resultdict )
173174 resdictcopy ["url" ] = "lalalala"
174175 result = json .dumps (resdictcopy )
@@ -177,7 +178,7 @@ def mockshow(url, datadict):
177178 '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}'
178179 % result ,
179180 )
180- if datadict [ "id" ] == "74b74ae1-df0c-4716-829f-4f939a046815" :
181+ if datadict_id == "74b74ae1-df0c-4716-829f-4f939a046815" :
181182 resdictcopy = copy .deepcopy (resultdict )
182183 resdictcopy ["id" ] = "datastore_unknown_resource"
183184 result = json .dumps (resdictcopy )
@@ -186,6 +187,25 @@ def mockshow(url, datadict):
186187 '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}'
187188 % result ,
188189 )
190+ if datadict_id in (
191+ "74b74ae1-df0c-4716-829f-4f939a046816" ,
192+ "74b74ae1-df0c-4716-829f-4f939a046817" ,
193+ ):
194+ resdictcopy = copy .deepcopy (resultdict )
195+ resdictcopy ["url_type" ] = "upload"
196+ resdictcopy ["resource_type" ] = "file.upload"
197+ resdictcopy ["size" ] = 1548
198+ resdictcopy ["hash" ] = "3790da698479326339fa99a074cbc1f7"
199+ if datadict_id == "74b74ae1-df0c-4716-829f-4f939a046817" :
200+ resdictcopy ["last_modified" ] = "2023-05-08T13:09:42.209741"
201+ else :
202+ del resdictcopy ["last_modified" ]
203+ result = json .dumps (resdictcopy )
204+ return MockResponse (
205+ 200 ,
206+ '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}'
207+ % result ,
208+ )
189209 return MockResponse (
190210 404 ,
191211 '{"success": false, "error": {"message": "Not found", "__type": "Not Found Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}' ,
@@ -382,7 +402,7 @@ def post(url, data, headers, files, allow_redirects, auth=None):
382402 resultdictcopy ["url" ] = (
383403 f"http://test-data.humdata.org/dataset/6f36a41c-f126-4b18-aaaf-6c2ddfbc5d4d/resource/de6549d8-268b-4dfe-adaf-a4ae5c8510d5/download/{ filename } "
384404 )
385- resultdictcopy ["size" ] = datadict ["size" ]
405+ resultdictcopy ["size" ] = int ( datadict ["size" ])
386406 resultdictcopy ["hash" ] = datadict ["hash" ]
387407 resultdictcopy ["state" ] = datadict ["state" ]
388408
@@ -658,9 +678,12 @@ def test_read_from_hdx(self, configuration, read):
658678 with pytest .raises (HDXError ):
659679 Resource .read_from_hdx ("ABC" )
660680
661- def test_check_url_filetoupload (self , configuration ):
681+ def test_check_types_url_filetoupload (self , configuration ):
662682 resource_data_copy = copy .deepcopy (resource_data )
683+ del resource_data_copy ["url" ]
663684 resource = Resource (resource_data_copy )
685+ with pytest .raises (HDXError ):
686+ resource .check_neither_url_filetoupload ()
664687 resource .set_types ()
665688 resource .set_file_to_upload ("abc" )
666689 resource .set_types ()
@@ -735,7 +758,7 @@ def test_create_in_hdx(self, configuration, date_pattern, post_create, test_data
735758 resource ["url" ]
736759 == "http://test-data.humdata.org/dataset/6f36a41c-f126-4b18-aaaf-6c2ddfbc5d4d/resource/de6549d8-268b-4dfe-adaf-a4ae5c8510d5/download/test_data.csv"
737760 )
738- assert resource ["size" ] == " 1548"
761+ assert resource ["size" ] == 1548
739762 assert resource ["hash" ] == "3790da698479326339fa99a074cbc1f7"
740763 assert resource ["state" ] == "active"
741764 resource .set_file_to_upload (test_data )
@@ -745,7 +768,7 @@ def test_create_in_hdx(self, configuration, date_pattern, post_create, test_data
745768 resource ["url" ]
746769 == "http://test-data.humdata.org/dataset/6f36a41c-f126-4b18-aaaf-6c2ddfbc5d4d/resource/de6549d8-268b-4dfe-adaf-a4ae5c8510d5/download/test_data.csv"
747770 )
748- assert resource ["size" ] == " 1548"
771+ assert resource ["size" ] == 1548
749772 assert resource ["hash" ] == "3790da698479326339fa99a074cbc1f7"
750773 resource_data_copy ["name" ] = "MyResource2"
751774 resource = Resource (resource_data_copy )
@@ -804,7 +827,6 @@ def test_update_in_hdx(self, configuration, date_pattern, post_update, test_data
804827 assert resource ["size" ] == "1548"
805828 assert resource ["hash" ] == "3790da698479326339fa99a074cbc1f7"
806829 assert resource ["state" ] == "active"
807-
808830 resource ["id" ] = "NOTEXIST"
809831 with pytest .raises (HDXError ):
810832 resource .update_in_hdx ()
@@ -836,6 +858,18 @@ def test_update_in_hdx(self, configuration, date_pattern, post_update, test_data
836858 status = resource .update_in_hdx ()
837859 assert status == 1
838860 assert resource .get_format () == "geoservice"
861+
862+ # These resource ids are set up so that the test datasets read have the same
863+ # hash and size as the file being uploaded
864+ resource ["id" ] = "74b74ae1-df0c-4716-829f-4f939a046816"
865+ resource .set_file_to_upload (test_data , guess_format_from_suffix = True )
866+ status = resource .update_in_hdx ()
867+ assert status == 3
868+ resource ["id" ] = "74b74ae1-df0c-4716-829f-4f939a046817"
869+ resource .set_file_to_upload (test_data , guess_format_from_suffix = True )
870+ status = resource .update_in_hdx ()
871+ assert status == 4
872+
839873 resource ["format" ] = "NOTEXIST"
840874 with pytest .raises (HDXError ):
841875 resource .update_in_hdx ()
0 commit comments