From 11457e0271c22bbd63b60b8916702c1171ec8632 Mon Sep 17 00:00:00 2001 From: gpetrak Date: Mon, 9 Dec 2024 17:51:14 +0200 Subject: [PATCH] adding tests for the get_time_info function and for the supports_time property --- geonode/geoserver/tests/test_helpers.py | 75 +++++++++++++++++++++++++ geonode/layers/tests.py | 38 ++++++++++++- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/geonode/geoserver/tests/test_helpers.py b/geonode/geoserver/tests/test_helpers.py index 2fb1b3c6754..f9a260ed9c6 100644 --- a/geonode/geoserver/tests/test_helpers.py +++ b/geonode/geoserver/tests/test_helpers.py @@ -38,8 +38,10 @@ extract_name_from_sld, get_dataset_capabilities_url, get_layer_ows_url, + get_time_info, ) from geonode.geoserver.ows import _wcs_link, _wfs_link, _wms_link +from unittest.mock import patch, Mock, MagicMock logger = logging.getLogger(__name__) @@ -275,3 +277,76 @@ def test_layer_ows_url(self): expected_url = f"{ows_url}geonode/CA/ows" capabilities_url = get_layer_ows_url(dataset) self.assertEqual(capabilities_url, expected_url, capabilities_url) + + # Tests for geonode.geoserver.helpers.get_time_info + @patch("geonode.geoserver.helpers.gs_catalog") + def test_get_time_info_valid_layer(self, mock_gs_catalog): + + mock_layer = Mock() + mock_layer.name = "test_layer" + mock_attribute = Mock(pk=1) + mock_end_attribute = Mock(pk=2) + + mock_query_set = MagicMock() + + # Mock filter to return the mock QuerySet + def mock_filter(attribute=None): + if attribute == "begin": + mock_query_set.first.return_value = mock_attribute + elif attribute == "end": + mock_query_set.first.return_value = mock_end_attribute + return mock_query_set + + mock_layer.attributes.filter = mock_filter + + # Build mock GeoServer's time info + mock_gs_time_info = Mock() + mock_gs_time_info.enabled = True + mock_gs_time_info.attribute = "begin" + mock_gs_time_info.end_attribute = "end" + mock_gs_time_info.presentation = "continuous" + mock_gs_time_info.resolution = 5000 + mock_gs_time_info._lookup = [("seconds", 1), ("minutes", 60)] + + mock_gs_layer = Mock() + mock_gs_layer.resource.metadata.get.return_value = mock_gs_time_info + mock_gs_catalog.get_layer.return_value = mock_gs_layer + + result = get_time_info(mock_layer) + + self.assertEqual(result["attribute"], 1) + self.assertEqual(result["end_attribute"], 2) + self.assertEqual(result["presentation"], "DISCRETE_INTERVAL") + self.assertEqual(result["precision_value"], 5) + self.assertEqual(result["precision_step"], "seconds") + + @patch("geonode.geoserver.helpers.gs_catalog") + def test_get_time_info_with_time_disabled(self, mock_gs_catalog): + mock_layer = Mock() + mock_layer.name = "test_layer" + + mock_gs_time_info = Mock() + mock_gs_time_info.enabled = False + mock_gs_time_info.attribute = "begin" + mock_gs_time_info.end_attribute = "end" + mock_gs_time_info.presentation = "DISCRETE_INTERVAL" + mock_gs_time_info.resolution = 10000 + mock_gs_time_info._lookup = [("seconds", 1), ("minutes", 60)] + + mock_gs_layer = Mock() + mock_gs_layer.resource.metadata.get.return_value = mock_gs_time_info + mock_gs_catalog.get_layer.return_value = mock_gs_layer + + result = get_time_info(mock_layer) + self.assertEqual(result, {}) + + @patch("geonode.geoserver.helpers.gs_catalog") + def test_get_time_info_no_layer(self, mock_gs_catalog): + + mock_gs_catalog.get_layer.return_value = None + + mock_layer = Mock() + mock_layer.name = "nonexistent_layer" + + result = get_time_info(mock_layer) + self.assertIsNone(result) diff --git a/geonode/layers/tests.py b/geonode/layers/tests.py index d11ad69ec5f..ba94558e5b4 100644 --- a/geonode/layers/tests.py +++ b/geonode/layers/tests.py @@ -23,7 +23,7 @@ import logging from uuid import uuid4 -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, patch, PropertyMock from collections import namedtuple from django.urls import reverse @@ -937,6 +937,42 @@ def test_dataset_download_call_the_catalog_work_for_vector(self, pathed_template ({"alternate": layer.alternate, "download_format": "application/zip"},), pathed_template.mock_calls[1].args ) + @patch.object(Dataset, "get_choices", new_callable=PropertyMock) + def test_supports_time_with_vector_time_subtype(self, mock_get_choices): + + # set valid attributes + mock_get_choices.return_value = [(4, "timestamp"), (5, "begin"), (6, "end")] + + mock_dataset = Dataset(subtype="vector") + self.assertTrue(mock_dataset.supports_time) + + @patch.object(Dataset, "get_choices", new_callable=PropertyMock) + def test_supports_time_with_non_vector_subtype(self, mock_get_choices): + + # set valid attributes + mock_get_choices.return_value = [(4, "timestamp"), (5, "begin"), (6, "end")] + + mock_dataset = Dataset(subtype="raster") + self.assertFalse(mock_dataset.supports_time) + + @patch.object(Dataset, "get_choices", new_callable=PropertyMock) + def test_supports_time_with_vector_subtype_and_invalid_attributes(self, mock_get_choices): + + # Get an empty list from get_choices method due to invalid attributes + mock_get_choices.return_value = [] + + mock_dataset = Dataset(subtype="vector") + self.assertFalse(mock_dataset.supports_time) + + @patch.object(Dataset, "get_choices", new_callable=PropertyMock) + def test_supports_time_with_raster_subtype_and_invalid_attributes(self, mock_get_choices): + + # Get an empty list from get_choices method due to invalid attributes + mock_get_choices.return_value = [] + + mock_dataset = Dataset(subtype="raster") + self.assertFalse(mock_dataset.supports_time) + class TestLayerDetailMapViewRights(GeoNodeBaseTestSupport): fixtures = ["initial_data.json", "group_test_data.json", "default_oauth_apps.json"]