Skip to content

Commit

Permalink
test: add tests to confirm plugin CKAN 2.9/2.10 differences work ok
Browse files Browse the repository at this point in the history
In CKAN 2.9 the package controller hook functions are called (for example) after_create but in CKAN 2.10 they've changed to (for example) after_dataset_create. These tests confirm a few things to do with these functions, but mainly they've been added to confirm that on 2.9 and 2.10 the hooks work correctly.
  • Loading branch information
jrdh committed Sep 21, 2023
1 parent ca6f95e commit 8d9883f
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from unittest.mock import patch, MagicMock

import pytest
from datacite.errors import DataCiteNotFoundError

from ckan.tests import factories
from ckan.tests.helpers import call_action
from ckanext.doi.model.crud import DOIQuery


@pytest.mark.filterwarnings("ignore::sqlalchemy.exc.SADeprecationWarning")
@pytest.mark.ckan_config("ckan.plugins", "doi")
@pytest.mark.ckan_config("ckanext.doi.prefix", "testing")
@pytest.mark.usefixtures("with_doi_table", "with_plugins")
class TestDOIPlugin:
def test_after_dataset_create(self):
# as well as testing the after_dataset_create functionality works on some level,
# this test is also here to confirm that after_dataset_create is called
# correctly whether you are on CKAN 2.9 or CKAN 2.10.

with patch("ckanext.doi.lib.api.DataCiteMDSClient") as mock_client_class:
# mock the datacite API to make it look like the DOI generated is new
mock_client = MagicMock(
metadata_get=MagicMock(side_effect=DataCiteNotFoundError())
)
mock_client_class.return_value = mock_client

# create a new dataset
dataset = factories.Dataset()

# check that a DOI is created in the database for this new dataset
assert DOIQuery.read_package(dataset["id"], create_if_none=False)

@pytest.mark.ckan_config("ckanext.doi.publisher", "argh!")
def test_after_dataset_update(self):
# as well as testing the after_dataset_update functionality works on some level,
# this test is also here to confirm that after_dataset_update is called
# correctly whether you are on CKAN 2.9 or CKAN 2.10.

# the udpate function has flashes in it which we don't care about
with patch("ckan.plugins.toolkit.h.flash_success"):
with patch("ckanext.doi.lib.api.DataCiteMDSClient") as mock_client_class:
# mock the datacite API to make it look like the DOI generated is new
mock_client = MagicMock(
metadata_get=MagicMock(side_effect=DataCiteNotFoundError())
)
mock_client_class.return_value = mock_client

# create a new dataset
dataset = factories.Dataset(title="test", author="Author, Test")

# reset our mock
mock_client.reset()

# update the dataset's title, this should trigger after_dataset_update
call_action("package_patch", id=dataset["id"], title="different")

# check that attempts have been made to mint the DOI
assert mock_client.metadata_post.called
assert mock_client.doi_post.called

@pytest.mark.ckan_config("ckanext.doi.publisher", "argh!")
@pytest.mark.ckan_config("ckanext.doi.site_url", "http://dois.are.great.org")
def test_after_dataset_show(self):
# as well as testing the after_dataset_show functionality works on some level,
# this test is also here to confirm that after_dataset_show is called correctly
# whether you are on CKAN 2.9 or CKAN 2.10.

with patch("ckanext.doi.lib.api.DataCiteMDSClient") as mock_client_class:
# mock the datacite API to make it look like the DOI generated is new
mock_client = MagicMock(
metadata_get=MagicMock(side_effect=DataCiteNotFoundError())
)
mock_client_class.return_value = mock_client

# create a new dataset
dataset = factories.Dataset()

doi = DOIQuery.read_package(dataset["id"], create_if_none=False)
package = call_action("package_show", id=dataset["id"])
assert package["doi"] == doi.identifier
assert package["doi_status"] is False
assert package["domain"] == "dois.are.great.org"
assert package["doi_date_published"] is None
assert package["doi_publisher"] == "argh!"

0 comments on commit 8d9883f

Please sign in to comment.