From 50c3cd5c4b7c3fa71dfe6f0fa8b4da9cdff36543 Mon Sep 17 00:00:00 2001 From: LavMatt Date: Tue, 6 Aug 2024 09:07:10 +0100 Subject: [PATCH] add unit tests for new details functions --- home/service/details.py | 2 +- tests/home/service/test_details.py | 61 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/home/service/details.py b/home/service/details.py index 4881938d5..ea9036e68 100644 --- a/home/service/details.py +++ b/home/service/details.py @@ -9,7 +9,7 @@ from .base import GenericService -def _parse_parent(relationships) -> EntityRef | None: +def _parse_parent(relationships: dict) -> EntityRef | None: """ returns the EntityRef of the first parent if one exists """ diff --git a/tests/home/service/test_details.py b/tests/home/service/test_details.py index b22d07451..440a60a21 100644 --- a/tests/home/service/test_details.py +++ b/tests/home/service/test_details.py @@ -1,3 +1,4 @@ +import pytest from data_platform_catalogue.entities import ( AccessInformation, Chart, @@ -18,6 +19,8 @@ DashboardDetailsService, DatabaseDetailsService, DatasetDetailsService, + _parse_parent, + is_access_requirements_a_url, ) from tests.conftest import ( generate_dashboard_metadata, @@ -26,6 +29,63 @@ ) +@pytest.mark.parametrize( + "input, expected_output", + [ + ( + { + RelationshipType.PARENT: [ + EntitySummary( + entity_ref=EntityRef(urn="urn:li:db", display_name="db"), + description="test", + entity_type="database", + tags=[], + ) + ] + }, + EntityRef(urn="urn:li:db", display_name="db"), + ), + ({}, None), + ( + { + RelationshipType.DATA_LINEAGE: [ + EntitySummary( + entity_ref=EntityRef(urn="urn:li:db", display_name="db"), + description="test", + entity_type="database", + tags=[], + ) + ] + }, + None, + ), + ], +) +def test_parse_parent(input, expected_output): + result = _parse_parent(input) + assert result == expected_output + + +@pytest.mark.parametrize( + "input, expected_output", + [ + ("122", False), + ("https://test.gov.uk", True), + ("https://test.gov.uk/data/#readme", True), + ("http://test.co.uk", True), + ("ftp.example.com/how-to-access.txt", False), + ("Just some instructions", False), + ("", False), + (123, False), + (None, False), + (["https://test.gov.uk"], False), + ], +) +def test_is_access_requirements_a_url(input, expected_output): + result = is_access_requirements_a_url(input) + assert result == expected_output + + class TestDatasetDetailsService: def test_get_context_contains_table_metadata(self, dataset_with_parent): service = DatasetDetailsService(dataset_with_parent["urn"]) @@ -156,6 +216,7 @@ def test_get_context(self, mock_catalogue): "parent_entity": None, "parent_type": "dashboard", "h1_value": "test", + "is_access_requirements_url": False, } assert context == expected