Skip to content

Commit ae336a7

Browse files
committed
Check that new or updated resources do not have both a url and a file to upload
1 parent 93ec4f7 commit ae336a7

2 files changed

Lines changed: 79 additions & 7 deletions

File tree

src/hdx/data/dataset.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,13 +537,7 @@ def _dataset_load_from_hdx(self, id_or_name: str) -> bool:
537537
return True
538538

539539
def check_resources_url_filetoupload(self) -> None:
540-
"""Check that metadata for dataset and its resources is complete. The parameter ignore_fields
541-
should be set if required to any fields that should be ignored for the particular operation.
542-
Prepend "resource:" for resource fields.
543-
544-
Args:
545-
ignore_fields (ListTuple[str]): Fields to ignore. Default is tuple().
546-
allow_no_resources (bool): Whether to allow no resources. Defaults to False.
540+
"""Check for error where both url or file to upload are provided for resources
547541
548542
Returns:
549543
None
@@ -1127,6 +1121,7 @@ def update_in_hdx(
11271121
Returns:
11281122
Dict: Status codes of resources
11291123
"""
1124+
self.check_resources_url_filetoupload()
11301125
loaded = False
11311126
if "id" in self.data:
11321127
self._check_existing_object("dataset", "id")
@@ -1193,6 +1188,7 @@ def create_in_hdx(
11931188
Returns:
11941189
Dict: Status codes of resources
11951190
"""
1191+
self.check_resources_url_filetoupload()
11961192
loadedid = self.data.get("id")
11971193
if loadedid is not None:
11981194
if not self._dataset_load_from_hdx(loadedid):

tests/hdx/data/test_update_dataset_resources.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,79 @@ def test_dataset_update_resources(
265265
"url_type": "upload",
266266
},
267267
]
268+
269+
def test_dataset_update_resources_position(
270+
self, fixture_path, configuration, test_data, test_xlsx
271+
):
272+
dataset = Dataset({"name": "test"})
273+
resource = Resource(
274+
{
275+
"name": "test1",
276+
"format": "csv",
277+
}
278+
)
279+
resource.set_file_to_upload(test_data)
280+
dataset.add_update_resource(resource)
281+
resource2 = Resource(
282+
{
283+
"name": "test2",
284+
"description": "test2",
285+
"format": "xlsx",
286+
}
287+
)
288+
resource2.set_file_to_upload(test_xlsx)
289+
dataset.add_update_resource(resource2)
290+
291+
dataset._old_data = dataset.data
292+
dataset._old_data["resources"] = dataset._copy_hdxobjects(
293+
dataset._resources,
294+
Resource,
295+
("_file_to_upload", "_data_updated", "_url_backup"),
296+
)
297+
298+
dataset._resources = []
299+
resource = Resource(
300+
{
301+
"name": "test1",
302+
"url": "https://data.humdata.org/dataset/resource/file1.csv",
303+
"format": "csv",
304+
"resource_type": "file.upload",
305+
"url_type": "upload",
306+
}
307+
)
308+
dataset.add_update_resource(resource)
309+
(
310+
resources_to_update,
311+
resources_to_delete,
312+
filestore_resources,
313+
new_resource_order,
314+
statuses,
315+
) = dataset._dataset_update_resources(True, False, True, True)
316+
assert resources_to_update == [
317+
{
318+
"format": "csv",
319+
"hash": "3790da698479326339fa99a074cbc1f7",
320+
"name": "test1",
321+
"resource_type": "file.upload",
322+
"size": 1548,
323+
"url": "updated_by_file_upload_step",
324+
"url_type": "upload",
325+
},
326+
{
327+
"description": "test2",
328+
"format": "xlsx",
329+
"hash": "6b8acf7e28d62685a1e829e7fa220d17",
330+
"name": "test2",
331+
"resource_type": "file.upload",
332+
"size": 23724,
333+
"url": "updated_by_file_upload_step",
334+
"url_type": "upload",
335+
},
336+
]
337+
assert resources_to_delete == []
338+
assert filestore_resources == {
339+
0: "tests/fixtures/test_data.csv",
340+
1: "tests/fixtures/size_hash/ACLED-All-Africa-File_20170101-to-20170708.xlsx",
341+
}
342+
assert new_resource_order == [("test1", "csv"), ("test2", "xlsx")]
343+
assert statuses == {"test1": 2, "test2": 2}

0 commit comments

Comments
 (0)