Skip to content

Commit

Permalink
adding tests for the get_time_info function and for the supports_time…
Browse files Browse the repository at this point in the history
… property
  • Loading branch information
Gpetrak committed Dec 9, 2024
1 parent dab92a5 commit 11457e0
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
75 changes: 75 additions & 0 deletions geonode/geoserver/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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)
38 changes: 37 additions & 1 deletion geonode/layers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit 11457e0

Please sign in to comment.