diff --git a/metadata_catalogue/datasets/libs/tests/__init__.py b/metadata_catalogue/datasets/libs/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/metadata_catalogue/datasets/libs/tests/test_checks.py b/metadata_catalogue/datasets/libs/tests/test_checks.py new file mode 100644 index 0000000..dcb34ac --- /dev/null +++ b/metadata_catalogue/datasets/libs/tests/test_checks.py @@ -0,0 +1,49 @@ +import pytest + +from ...models import Dataset +from ..checks import vrt + + +@pytest.mark.django_db(transaction=True) +def test_vrt_validation(): + d = Dataset.objects.create(name="test") + c = d.get_content() + c.gdal_vrt_definition = """ + + + IGNORE + IGNORE + wkbPoint + + + + + + + 1 + John + 42.5 + + + 1.0,2.0 + + + + + + 2 + Jane + 63.2 + + + 3.0,4.0 + + + + + + + + + + """ diff --git a/metadata_catalogue/datasets/libs/tests/test_ipt.py b/metadata_catalogue/datasets/libs/tests/test_ipt.py new file mode 100644 index 0000000..18f5f57 --- /dev/null +++ b/metadata_catalogue/datasets/libs/tests/test_ipt.py @@ -0,0 +1,108 @@ +import logging +from unittest.mock import patch + +import pytest + +from ...models import Dataset +from ..ipt import rss_to_datasets + +RSS_TEXT = """ + + + NINA IPT service + https://ipt.nina.no + + Resource metadata of NINA IPT service + en-us + + Tue, 05 May 2015 09:54:05 +0000 + Mon, 25 Dec 2023 12:00:03 +0000 + + e312f477-a9ba-485e-8c1c-700868a0bfb7 + GBIF IPT 2.7.6-rd4b666d + rv@nina.no (Roald Vang) + http://cyber.law.harvard.edu/rss/rss.html + 15 + + 63.413916 + 10.406317 + + + NINA Artskart data - Version 1.336 + https://ipt.nina.no/resource?r=nina_artskart + + [Several smaller datasets with biodiversity data delivered from NINA to The Norwegian Biodiversity Information Centres Species Map Service, https://artskart.artsdatabanken.no. New datasets are added regularly.] + rv@nina.no (Roald Vang) + https://ipt.nina.no/eml.do?r=nina_artskart + https://ipt.nina.no/archive.do?r=nina_artskart + Mon, 25 Dec 2023 12:00:14 +0000 + 4a832966-48ad-4d83-858c-044705f74cac/v1.336 + + + +""" + + +def mocked_getLogger(name): + # You can customize the behavior of the mock logger as needed + mock_logger = logging.getLogger(name) + mock_logger.setLevel(logging.DEBUG) + return mock_logger + + +@pytest.mark.django_db(transaction=True) +@patch("metadata_catalogue.datasets.libs.ipt.async_task") +@patch("metadata_catalogue.datasets.libs.ipt.logger.warn") +def test_rss_to_datasets(logger_mock, task_mock): + rss_to_datasets(RSS_TEXT) + assert task_mock.called + assert not logger_mock.called + + assert Dataset.objects.count() == 1 + + +RSS_TEXT_NO_DWCA = """ + + + NINA IPT service + https://ipt.nina.no + + Resource metadata of NINA IPT service + en-us + + Tue, 05 May 2015 09:54:05 +0000 + Mon, 25 Dec 2023 12:00:03 +0000 + + e312f477-a9ba-485e-8c1c-700868a0bfb7 + GBIF IPT 2.7.6-rd4b666d + rv@nina.no (Roald Vang) + http://cyber.law.harvard.edu/rss/rss.html + 15 + + 63.413916 + 10.406317 + + + NINA Artskart data - Version 1.336 + https://ipt.nina.no/resource?r=nina_artskart + + [Several smaller datasets with biodiversity data delivered from NINA to The Norwegian Biodiversity Information Centres Species Map Service, https://artskart.artsdatabanken.no. New datasets are added regularly.] + rv@nina.no (Roald Vang) + https://ipt.nina.no/eml.do?r=nina_artskart + Mon, 25 Dec 2023 12:00:14 +0000 + 4a832966-48ad-4d83-858c-044705f74cac/v1.336 + + + +""" + + +@pytest.mark.django_db(transaction=True) +@patch("metadata_catalogue.datasets.libs.ipt.async_task") +@patch("metadata_catalogue.datasets.libs.ipt.logger.warn") +def test_rss_to_datasets_no_dwca(logger_mock, task_mock): + rss_to_datasets(RSS_TEXT_NO_DWCA) + assert not task_mock.called + assert logger_mock.called + + assert Dataset.objects.count() == 0 diff --git a/metadata_catalogue/datasets/libs/tests/test_utils.py b/metadata_catalogue/datasets/libs/tests/test_utils.py new file mode 100644 index 0000000..be716ae --- /dev/null +++ b/metadata_catalogue/datasets/libs/tests/test_utils.py @@ -0,0 +1,21 @@ +from django.test.client import RequestFactory + +from ..utils import req_to_base, safe_get + + +class TestClass: + def __init__(self, value=None) -> None: + self.test = value + + +def test_safe_get(): + assert safe_get(None, "test") == "" + assert safe_get(TestClass(), "test") == "" + assert safe_get(TestClass(), "test2") == "" + assert safe_get(TestClass("test"), "test") == "test" + + +def test_req_to_base(): + factory = RequestFactory() + assert req_to_base(factory.get("/")) == "http://testserver" + assert req_to_base(factory.get("/api/", secure=True)) == "https://testserver" diff --git a/metadata_catalogue/datasets/libs/utils.py b/metadata_catalogue/datasets/libs/utils.py index df6a5b1..7a5a15a 100644 --- a/metadata_catalogue/datasets/libs/utils.py +++ b/metadata_catalogue/datasets/libs/utils.py @@ -1,5 +1,5 @@ def safe_get(element, attribute): - if element: + if element and hasattr(element, attribute): if (v := element.__getattribute__(attribute)) is not None: return v return ""