Skip to content

Commit 0ad9170

Browse files
committed
Ensure filestore resources are checked when creating datasets
Increase coverage
1 parent b5cf8f3 commit 0ad9170

4 files changed

Lines changed: 54 additions & 24 deletions

File tree

src/hdx/data/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,8 +1220,6 @@ def create_in_hdx(
12201220

12211221
if "ignore_check" not in kwargs: # allow ignoring of field checks
12221222
self.check_required_fields(allow_no_resources=allow_no_resources, **kwargs)
1223-
# No need to check again after revising dataset
1224-
kwargs["ignore_check"] = True
12251223
statuses = {}
12261224
filestore_resources = {}
12271225
if self._resources:
@@ -1230,6 +1228,8 @@ def create_in_hdx(
12301228
resource, filestore_resources, i, **kwargs
12311229
)
12321230
statuses[resource["name"]] = status
1231+
# No need to check again after revising dataset
1232+
kwargs["ignore_check"] = True
12331233
self.unseparate_resources()
12341234
self._prepare_hdx_call(self.data, kwargs)
12351235
kwargs["operation"] = "create"

src/hdx/data/resource.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,11 @@ def set_types(self, data: Optional[Dict] = None) -> None:
346346
data = self.data
347347
if self._file_to_upload is None:
348348
if "url" in data:
349-
if "resource_type" not in data:
350-
data["resource_type"] = "api"
351-
if "url_type" not in data:
352-
data["url_type"] = "api"
349+
data["resource_type"] = "api"
350+
data["url_type"] = "api"
353351
else:
354-
if "resource_type" not in data:
355-
data["resource_type"] = "file.upload"
356-
if "url_type" not in data:
357-
data["url_type"] = "upload"
352+
data["resource_type"] = "file.upload"
353+
data["url_type"] = "upload"
358354
if "tracking_summary" in data:
359355
del data["tracking_summary"]
360356
file_format = data.get("format")

tests/hdx/api/utilities/test_filestore_helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def test_dataset_update_filestore_resource(self, configuration, test_data):
4646
"hash": "3790da698479326339fa99a074cbc1f7",
4747
"size": 1548,
4848
"package_id": "6f36a41c-f126-4b18-aaaf-6c2ddfbc5d4d",
49-
"resource_type": "api",
50-
"url_type": "api",
49+
"resource_type": "file.upload",
50+
"url_type": "upload",
5151
"url": "updated_by_file_upload_step",
5252
}
5353
assert filestore_resources == {0: test_data}
@@ -63,8 +63,8 @@ def test_dataset_update_filestore_resource(self, configuration, test_data):
6363
"hash": "3790da698479326339fa99a074cbc1f7",
6464
"size": 1548,
6565
"package_id": "6f36a41c-f126-4b18-aaaf-6c2ddfbc5d4d",
66-
"resource_type": "api",
67-
"url_type": "api",
66+
"resource_type": "file.upload",
67+
"url_type": "upload",
6868
"url": "updated_by_file_upload_step",
6969
}
7070
assert filestore_resources == {}

tests/hdx/data/test_resource.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)