Skip to content

Commit 03aeb8d

Browse files
author
Mike
committed
Fix marking resource data updated when using create_in_hdx and update_in_hdx of dataset
1 parent c1eff09 commit 03aeb8d

7 files changed

Lines changed: 30 additions & 13 deletions

File tree

src/hdx/data/dataset.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ def add_update_resource(
290290
updated_resource.set_file_to_upload(
291291
resource.get_file_to_upload()
292292
)
293+
if resource.is_data_updated():
294+
updated_resource.mark_data_updated()
293295

294296
def add_update_resources(
295297
self,
@@ -332,6 +334,8 @@ def add_update_resources(
332334
updated_resource.set_file_to_upload(
333335
resource.get_file_to_upload()
334336
)
337+
if resource.is_data_updated():
338+
updated_resource.mark_data_updated()
335339
for resource_index in updated_resource_no_matches:
336340
resource = resource_objects[resource_index]
337341
resource.check_url_filetoupload()
@@ -470,7 +474,9 @@ def _dataset_create_resources(self) -> None:
470474

471475
if "resources" in self.data:
472476
self.old_data["resources"] = self._copy_hdxobjects(
473-
self.resources, res_module.Resource, "file_to_upload"
477+
self.resources,
478+
res_module.Resource,
479+
("file_to_upload", "data_updated"),
474480
)
475481
self.init_resources()
476482
self.separate_resources()

src/hdx/data/filestore_helper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Helper to the Dataset class for handling resources with filestores.
22
"""
3+
from datetime import datetime
34
from typing import TYPE_CHECKING, Any, Dict
45

56
from hdx.utilities.dictandlist import merge_two_dictionaries
@@ -91,9 +92,13 @@ def dataset_merge_filestore_resource(
9192
if file_to_upload:
9293
resource.set_file_to_upload(file_to_upload)
9394
filestore_resources[resource_index] = file_to_upload
95+
data_updated = updated_resource.is_data_updated()
9496
merge_two_dictionaries(resource, updated_resource)
9597
cls.resource_check_required_fields(
9698
resource, check_upload=True, **kwargs
9799
)
98100
if resource.get_file_to_upload():
99101
resource["url"] = cls.temporary_url
102+
if data_updated:
103+
resource["last_modified"] = datetime.utcnow().isoformat()
104+
resource.data_updated = False

src/hdx/data/hdxobject.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,14 +621,14 @@ def _copy_hdxobjects(
621621
self,
622622
hdxobjects: ListTuple["HDXObject"],
623623
hdxobjectclass: type,
624-
attribute_to_copy: Optional[str] = None,
624+
attributes_to_copy: ListTuple[str] = (),
625625
) -> List["HDXObject"]:
626626
"""Helper function to make a deep copy of a supplied list of HDX objects
627627
628628
Args:
629629
hdxobjects (ListTuple[HDXObject]): list of HDX objects to copy
630630
hdxobjectclass (type): Type of the HDX Objects to be copied
631-
attribute_to_copy (Optional[str]): An attribute to copy over from the HDX object. Defaults to None.
631+
attributes_to_copy (ListTuple[str]): Attributes to copy over from the HDX object. Defaults to ().
632632
633633
Returns:
634634
List[HDXObject]: Deep copy of list of HDX objects
@@ -639,9 +639,9 @@ def _copy_hdxobjects(
639639
newhdxobject = hdxobjectclass(
640640
newhdxobjectdata, configuration=self.configuration
641641
)
642-
if attribute_to_copy:
643-
value = getattr(hdxobject, attribute_to_copy)
644-
setattr(newhdxobject, attribute_to_copy, value)
642+
for attribute in attributes_to_copy:
643+
value = getattr(hdxobject, attribute)
644+
setattr(newhdxobject, attribute, value)
645645
newhdxobjects.append(newhdxobject)
646646
return newhdxobjects
647647

tests/hdx/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Global fixtures"""
2+
import re
23
import smtplib
34
from os.path import join
45

@@ -1548,3 +1549,8 @@ class MockSMTP(MockSMTPBase):
15481549
monkeypatch.setattr(smtplib, "SMTP_SSL", MockSMTPSSL)
15491550
monkeypatch.setattr(smtplib, "LMTP", MockLMTP)
15501551
monkeypatch.setattr(smtplib, "SMTP", MockSMTP)
1552+
1553+
1554+
@pytest.fixture(scope="session")
1555+
def date_pattern():
1556+
return re.compile(r"[12]\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\d\d\d\d")

tests/hdx/data/test_dataset_core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def test_create_in_hdx(self, configuration, post_create):
632632
assert len(dataset.resources) == 3
633633
# Dataset creates that end up updating are in the test below
634634

635-
def test_update_in_hdx(self, configuration, post_update):
635+
def test_update_in_hdx(self, configuration, post_update, date_pattern):
636636
dataset = Dataset()
637637
dataset["id"] = "NOTEXIST"
638638
with pytest.raises(HDXError):
@@ -707,10 +707,15 @@ def test_update_in_hdx(self, configuration, post_update):
707707
dataset = Dataset(datasetdata)
708708
resourcesdata = copy.deepcopy(resources_data)
709709
resource = Resource(resourcesdata[0])
710+
resource.mark_data_updated()
710711
dataset.add_update_resources([resource, resource])
711712
dataset.create_in_hdx()
712713
assert dataset["state"] == "active"
713714
assert len(dataset.resources) == 3
715+
resource = dataset.get_resource()
716+
assert resource.is_data_updated() is False
717+
match = date_pattern.search(resource["last_modified"])
718+
assert match
714719

715720
dataset = Dataset(datasetdata)
716721
resourcesdata = copy.deepcopy(resources_data)

tests/hdx/data/test_resource.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import copy
33
import json
44
import os
5-
import re
65
from datetime import datetime, timezone
76
from os import remove
87
from os.path import basename, join
@@ -329,10 +328,6 @@ class TestResource:
329328

330329
datastore = None
331330

332-
@pytest.fixture(scope="class")
333-
def date_pattern(self):
334-
return re.compile(r"[12]\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\d\d\d\d")
335-
336331
@pytest.fixture(scope="class")
337332
def static_yaml(self):
338333
return join("tests", "fixtures", "config", "hdx_resource_static.yaml")

tests/hdx/data/test_update_dataset_resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_dataset_update_resources(
8585
):
8686
dataset.old_data = new_dataset.data
8787
dataset.old_data["resources"] = new_dataset._copy_hdxobjects(
88-
new_dataset.resources, Resource, "file_to_upload"
88+
new_dataset.resources, Resource, ("file_to_upload", "data_updated")
8989
)
9090
(
9191
resources_to_delete,

0 commit comments

Comments
 (0)