Skip to content

Commit 026f7d9

Browse files
Improve test coverage
1 parent 04c82c2 commit 026f7d9

File tree

2 files changed

+120
-6
lines changed

2 files changed

+120
-6
lines changed

geonode/harvesting/tests/test_utils.py

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,33 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
#
1818
#########################################################################
19+
from django.conf import settings
1920
from lxml import etree
21+
from django.contrib.auth import get_user_model
22+
from django.utils import timezone
2023

21-
from geonode.tests.base import GeoNodeBaseSimpleTestSupport
24+
from geonode.base.populate_test_data import create_single_dataset
25+
from geonode.harvesting.models import HarvestableResource, Harvester
26+
from geonode.tests.base import GeoNodeBaseTestSupport
27+
from geonode.upload.handlers.remote.wms import create_harvestable_resource
2228
from geonode.utils import get_xpath_value
2329

2430

25-
class UtilsTestCase(GeoNodeBaseSimpleTestSupport):
31+
class UtilsTestCase(GeoNodeBaseTestSupport):
32+
33+
@classmethod
34+
def setUpClass(cls):
35+
super().setUpClass()
36+
cls.service_url = f"{settings.GEOSERVER_LOCATION}ows"
37+
cls.user = get_user_model().objects.get(username="admin")
38+
cls.harvester, _ = Harvester.objects.get_or_create(
39+
remote_url=cls.service_url,
40+
name="harvester1",
41+
default_owner=cls.user,
42+
harvester_type="geonode.harvesting.harvesters.wms.OgcWmsHarvester",
43+
)
44+
cls.dataset = create_single_dataset(name="example_harvestable_resource")
45+
2646
def test_get_xpath_value(self):
2747
fixtures = [
2848
(
@@ -48,3 +68,87 @@ def test_get_xpath_value(self):
4868
xml_el = etree.fromstring(element)
4969
result = get_xpath_value(xml_el, xpath_expr, nsmap=nsmap)
5070
self.assertEqual(result, expected)
71+
72+
def test_create_harvestable_resource(self):
73+
"""
74+
Given a geonode resource and a service url, should link the
75+
harvestable resource with the harvester
76+
"""
77+
78+
# be sure that the dataset was not linked to the harvester
79+
try:
80+
self.assertFalse(HarvestableResource.objects.filter(geonode_resource=self.dataset).exists())
81+
82+
result = create_harvestable_resource(self.dataset, self.service_url)
83+
# nothing is usually returned
84+
self.assertIsNone(result)
85+
86+
# evaluating that the harvestable resource has been created
87+
hresource = HarvestableResource.objects.filter(geonode_resource=self.dataset).first()
88+
self.assertIsNotNone(hresource)
89+
self.assertEqual(hresource.geonode_resource.pk, self.dataset.pk)
90+
finally:
91+
HarvestableResource.objects.filter(geonode_resource=self.dataset).delete()
92+
93+
def test_create_harvestable_resource_different_service_url(self):
94+
"""
95+
Should ignore if the service url provided does not exists in the DB
96+
"""
97+
with self.assertLogs(level="WARNING") as _log:
98+
create_harvestable_resource(self.dataset, "http://someurl.com")
99+
self.assertIn("The WMS layer does not belong to any known remote service", [x.message for x in _log.records])
100+
101+
def test_create_harvestable_resource_on_existing_harvestable_resource(self):
102+
"""
103+
If the harvestable resource already exists, it should link the newly created geonode resource
104+
with the harvestable resource, so the harvester will not harvest it again
105+
"""
106+
try:
107+
self.__create_harvestable_resource()
108+
109+
result = create_harvestable_resource(self.dataset, self.service_url)
110+
# nothing is usually returned
111+
self.assertIsNone(result)
112+
113+
# evaluating that the harvestable resource has been created
114+
hresource = HarvestableResource.objects.filter(geonode_resource=self.dataset).first()
115+
self.assertIsNotNone(hresource)
116+
self.assertEqual(hresource.geonode_resource.pk, self.dataset.pk)
117+
finally:
118+
HarvestableResource.objects.filter(geonode_resource=self.dataset).delete()
119+
120+
def test_create_harvestable_resource_different_geonode_resource(self):
121+
"""
122+
If the harvestable resource already have geonode resource aligned, it should
123+
just ignore and log the information
124+
"""
125+
try:
126+
self.__create_harvestable_resource(attach_resource=True)
127+
dataset2 = create_single_dataset("second dataset")
128+
dataset2.alternate = self.dataset.alternate
129+
dataset2.save()
130+
with self.assertLogs(level="WARNING") as _log:
131+
create_harvestable_resource(dataset2, self.service_url)
132+
self.assertIn(
133+
"The Resource assigned to the current HarvestableResource is different from the one provided, skipping...",
134+
[x.message for x in _log.records],
135+
)
136+
137+
finally:
138+
HarvestableResource.objects.filter(geonode_resource=self.dataset).delete()
139+
140+
def __create_harvestable_resource(self, attach_resource=False):
141+
HarvestableResource.objects.get_or_create(
142+
harvester=self.harvester,
143+
unique_identifier=self.dataset.alternate,
144+
geonode_resource=None if not attach_resource else self.dataset,
145+
title="Some title",
146+
defaults={
147+
"should_be_harvested": True,
148+
"remote_resource_type": self.dataset.resource_type,
149+
"last_updated": timezone.now(),
150+
"last_refreshed": timezone.now(),
151+
"last_harvested": timezone.now(),
152+
"last_harvesting_succeeded": True,
153+
},
154+
)

geonode/harvesting/utils.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from geonode.harvesting.models import HarvestableResource, Harvester
2222

2323

24-
logger = logging.getLogger("importer")
24+
logger = logging.getLogger(__name__)
2525

2626

2727
def create_harvestable_resource(geonode_resource, service_url):
@@ -34,10 +34,20 @@ def create_harvestable_resource(geonode_resource, service_url):
3434
logger.warning("The WMS layer does not belong to any known remote service")
3535
return
3636

37-
hresource = HarvestableResource.objects.filter(unique_identifier=geonode_resource.alternate)
38-
if hresource.exists():
39-
logger.info("Harvestable resource already exists")
37+
if hresource := HarvestableResource.objects.filter(unique_identifier=geonode_resource.alternate).first():
38+
logger.info("Harvestable resource already exists, linking geonode resource...")
39+
# if exists, we need to be sure that the resource from geonode is getting connected
40+
if not hresource.geonode_resource:
41+
hresource.geonode_resource = geonode_resource
42+
hresource.should_be_harvested = True
43+
hresource.save()
44+
elif hresource.geonode_resource.pk != geonode_resource.pk:
45+
logger.warning(
46+
"The Resource assigned to the current HarvestableResource is different from the one provided, skipping..."
47+
)
48+
return
4049
return
50+
4151
timestamp = timezone.now()
4252
_, created = HarvestableResource.objects.get_or_create(
4353
harvester=harvester,

0 commit comments

Comments
 (0)