Skip to content

Commit

Permalink
test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Dec 27, 2023
1 parent 11c39d4 commit 91f84a9
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 1 deletion.
Empty file.
49 changes: 49 additions & 0 deletions metadata_catalogue/datasets/libs/tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -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 = """
<OGRVRTDataSource>
<OGRVRTLayer name="my_table">
<SrcDataSource>IGNORE</SrcDataSource>
<SrcLayer>IGNORE</SrcLayer>
<GeometryType>wkbPoint</GeometryType>
<FieldDefn name="id" type="Integer" />
<FieldDefn name="name" type="String" />
<FieldDefn name="value" type="Real" />
<Feature>
<Field name="id">1</Field>
<Field name="name">John</Field>
<Field name="value">42.5</Field>
<Geometry>
<Point>
<Coordinates>1.0,2.0</Coordinates>
</Point>
</Geometry>
</Feature>
<Feature>
<Field name="id">2</Field>
<Field name="name">Jane</Field>
<Field name="value">63.2</Field>
<Geometry>
<Point>
<Coordinates>3.0,4.0</Coordinates>
</Point>
</Geometry>
</Feature>
<!-- Add more Feature blocks for additional rows -->
</OGRVRTLayer>
</OGRVRTDataSource>
"""
108 changes: 108 additions & 0 deletions metadata_catalogue/datasets/libs/tests/test_ipt.py
Original file line number Diff line number Diff line change
@@ -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 = """
<rss version="2.0" xmlns:ipt="http://ipt.gbif.org/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>NINA IPT service</title>
<link>https://ipt.nina.no</link>
<atom:link href="https://ipt.nina.no/rss.do" rel="self" type="application/rss+xml"/>
<description>Resource metadata of NINA IPT service</description>
<language>en-us</language>
<!-- RFC-822 date-time / Wed, 02 Oct 2010 13:00:00 GMT -->
<pubDate>Tue, 05 May 2015 09:54:05 +0000</pubDate>
<lastBuildDate>Mon, 25 Dec 2023 12:00:03 +0000</lastBuildDate>
<!-- UUID of the IPT making RSS feed available -->
<ipt:identifier>e312f477-a9ba-485e-8c1c-700868a0bfb7</ipt:identifier>
<generator>GBIF IPT 2.7.6-rd4b666d</generator>
<webMaster>[email protected] (Roald Vang)</webMaster>
<docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
<ttl>15</ttl>
<geo:Point>
<geo:lat>63.413916</geo:lat>
<geo:long>10.406317</geo:long>
</geo:Point>
<item>
<title>NINA Artskart data - Version 1.336</title>
<link>https://ipt.nina.no/resource?r=nina_artskart</link>
<!-- shows what changed in this version, or shows the resource description if change summary was empty -->
<description>[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.]</description>
<author>[email protected] (Roald Vang)</author>
<ipt:eml>https://ipt.nina.no/eml.do?r=nina_artskart</ipt:eml>
<ipt:dwca>https://ipt.nina.no/archive.do?r=nina_artskart</ipt:dwca>
<pubDate>Mon, 25 Dec 2023 12:00:14 +0000</pubDate>
<guid isPermaLink="false">4a832966-48ad-4d83-858c-044705f74cac/v1.336</guid>
</item>
</channel>
</rss>
"""


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 = """
<rss version="2.0" xmlns:ipt="http://ipt.gbif.org/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>NINA IPT service</title>
<link>https://ipt.nina.no</link>
<atom:link href="https://ipt.nina.no/rss.do" rel="self" type="application/rss+xml"/>
<description>Resource metadata of NINA IPT service</description>
<language>en-us</language>
<!-- RFC-822 date-time / Wed, 02 Oct 2010 13:00:00 GMT -->
<pubDate>Tue, 05 May 2015 09:54:05 +0000</pubDate>
<lastBuildDate>Mon, 25 Dec 2023 12:00:03 +0000</lastBuildDate>
<!-- UUID of the IPT making RSS feed available -->
<ipt:identifier>e312f477-a9ba-485e-8c1c-700868a0bfb7</ipt:identifier>
<generator>GBIF IPT 2.7.6-rd4b666d</generator>
<webMaster>[email protected] (Roald Vang)</webMaster>
<docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
<ttl>15</ttl>
<geo:Point>
<geo:lat>63.413916</geo:lat>
<geo:long>10.406317</geo:long>
</geo:Point>
<item>
<title>NINA Artskart data - Version 1.336</title>
<link>https://ipt.nina.no/resource?r=nina_artskart</link>
<!-- shows what changed in this version, or shows the resource description if change summary was empty -->
<description>[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.]</description>
<author>[email protected] (Roald Vang)</author>
<ipt:eml>https://ipt.nina.no/eml.do?r=nina_artskart</ipt:eml>
<pubDate>Mon, 25 Dec 2023 12:00:14 +0000</pubDate>
<guid isPermaLink="false">4a832966-48ad-4d83-858c-044705f74cac/v1.336</guid>
</item>
</channel>
</rss>
"""


@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
21 changes: 21 additions & 0 deletions metadata_catalogue/datasets/libs/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion metadata_catalogue/datasets/libs/utils.py
Original file line number Diff line number Diff line change
@@ -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 ""
Expand Down

0 comments on commit 91f84a9

Please sign in to comment.