Skip to content

Commit

Permalink
openaire: migrated openaire backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo committed Sep 18, 2023
1 parent c350874 commit 6639000
Show file tree
Hide file tree
Showing 13 changed files with 793 additions and 1 deletion.
2 changes: 2 additions & 0 deletions site/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ invenio_base.blueprints =
invenio_base.apps =
zenodo_rdm_legacy = zenodo_rdm.legacy.ext:ZenodoLegacy
profiler = zenodo_rdm.profiler:Profiler
invenio_openaire = zenodo_rdm.openaire.ext:OpenAIRE
invenio_base.api_apps =
zenodo_rdm_legacy = zenodo_rdm.legacy.ext:ZenodoLegacy
profiler = zenodo_rdm.profiler:Profiler
invenio_openaire = zenodo_rdm.openaire.ext:OpenAIRE
invenio_base.api_blueprints =
zenodo_rdm_legacy = zenodo_rdm.legacy.views:blueprint
zenodo_rdm_legacy_stub = zenodo_rdm.legacy.views:stub_blueprint
Expand Down
94 changes: 93 additions & 1 deletion site/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@ def app_config(app_config):
app_config["BLUEPRINTS_URL_PREFIXES"] = {
"invenio_files_rest": "/legacy-files",
}
# OpenAIRE configs
app_config["OPENAIRE_PORTAL_URL"] = "https://explore.openaire.eu"
app_config["OPENAIRE_COMMUNITIES"] = {
"foo": {
"name": "Foo Optimization Organization",
"communities": [
"c1",
"c2",
],
"types": {
"software": [
{"id": "foo:t1", "name": "Foo sft type one"},
{"id": "foo:t2", "name": "Foo sft type two"},
],
"other": [
{"id": "foo:t4", "name": "Foo other type four"},
{"id": "foo:t5", "name": "Foo other type five"},
],
},
},
"bar": {
"name": "Bar Association Resources",
"communities": ["c3", "c1"],
"types": {
"software": [
{"id": "bar:t3", "name": "Bar sft type three"},
],
"other": [
{"id": "bar:t6", "name": "Bar other type six"},
],
},
},
}

return app_config


Expand Down Expand Up @@ -313,12 +347,70 @@ def resource_type_v(app, resource_type_type):
},
)

vocab = vocabulary_service.create(
vocabulary_service.create(
system_identity,
{
"id": "image-photo",
"tags": ["depositable", "linkable"],
"type": "resourcetypes",
"props": {
"csl": "graphic",
"datacite_general": "Image",
"datacite_type": "Photo",
"openaire_resourceType": "0025",
"openaire_type": "dataset",
"eurepo": "info:eu-repo/semantics/other",
"schema.org": "https://schema.org/Photograph",
"subtype": "image-photo",
"type": "image",
},
},
)

vocabulary_service.create(
system_identity,
{
"id": "software",
"icon": "code",
"type": "resourcetypes",
"props": {
"csl": "software",
"datacite_general": "Software",
"datacite_type": "",
"openaire_resourceType": "0029",
"openaire_type": "software",
"eurepo": "info:eu-repo/semantics/other",
"schema.org": "https://schema.org/SoftwareSourceCode",
"subtype": "",
"type": "software",
},
"title": {"en": "Software", "de": "Software"},
"tags": ["depositable", "linkable"],
},
)

vocab = vocabulary_service.create(
system_identity,
{
"id": "other",
"icon": "asterisk",
"type": "resourcetypes",
"props": {
"csl": "article",
"datacite_general": "Other",
"datacite_type": "",
"openaire_resourceType": "0020",
"openaire_type": "other",
"eurepo": "info:eu-repo/semantics/other",
"schema.org": "https://schema.org/CreativeWork",
"subtype": "",
"type": "other",
},
"title": {
"en": "Other",
"de": "Sonstige",
},
"tags": ["depositable", "linkable"],
},
)

Expand Down
23 changes: 23 additions & 0 deletions site/tests/openaire/test_openaire.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
#
# Zenodo-RDM is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""OpenAIRE tests."""


from zenodo_rdm.openaire.proxies import current_openaire


def test_openaire_type(app):
"""Test OpenAIRE type."""

state = current_openaire.state
assert set(state.inverse_openaire_community_map.keys()) == set(["c1", "c2", "c3"])

assert set(state.inverse_openaire_community_map["c1"]) == set(["foo", "bar"])
assert set(state.inverse_openaire_community_map["c2"]) == set(["foo"])
assert set(state.inverse_openaire_community_map["c3"]) == set(["bar"])

assert set(state.openaire_communities.keys()) == set(["foo", "bar"])
130 changes: 130 additions & 0 deletions site/tests/openaire/test_openaire_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
#
# Invenio-RDM is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""Test for OpenAIRE helpers."""

from copy import deepcopy

import pytest

from zenodo_rdm.openaire.utils import (
OA_DATASET,
OA_OTHER,
OA_PUBLICATION,
OA_SOFTWARE,
openaire_id,
openaire_link,
openaire_type,
)


@pytest.fixture(scope="function")
def minimal_open_record(minimal_record):
"""Minimal record with open access."""
r = deepcopy(minimal_record)

# Test only open records
r["access"] = {"record": "public", "files": "public"}
return r


@pytest.mark.parametrize(
"resource_type,expected",
[
("image-photo", OA_DATASET),
("other", OA_OTHER),
("publication-book", OA_PUBLICATION),
("publication", OA_PUBLICATION),
],
)
def test_openaire_type_serialization(
running_app, minimal_open_record, resource_type, expected
):
"""Test OpenAIRE type.
It uses only open records.
"""
r = deepcopy(minimal_open_record)

r["metadata"]["resource_type"] = {"id": resource_type}

assert openaire_type(r) == expected


def test_embardoged_publication_serialization(running_app, minimal_record):
"""Test OpenAIRE serialization for an embargoed (restricted) publication."""
r = deepcopy(minimal_record)

r["metadata"]["resource_type"] = {"id": "publication"}

# Non-open publications
r["access_right"] = {
"files": "restricted",
"record": "restricted",
"embargo": {"active": True},
}
assert openaire_type(r) is None


def test_funded_publication_serialization(running_app, minimal_record):
"""Test a publication that is funded (e.g. has grants)."""
r = deepcopy(minimal_record)

r["metadata"]["resource_type"] = {"id": "publication"}

# with grants
r["metadata"]["funding"] = [{"funder": {"id": "someid"}, "award": {"id": "someid"}}]
assert openaire_type(r) == OA_PUBLICATION

# in ecfunded community
del r["metadata"]["funding"]
r["parent"] = {"communities": {"ids": ["ecfunded"]}}
assert openaire_type(r) == OA_PUBLICATION
r["parent"] = {"communities": {"ids": ["zenodo"]}}
assert openaire_type(r) is None


@pytest.mark.parametrize(
"resource_type,expected",
[
("software", "od______2659::47287d1800c112499a117ca17aa1909d"),
("other", "od______2659::47287d1800c112499a117ca17aa1909d"),
("dataset", "od______2659::204007f516ddcf0a452c2f22d48695ca"),
("publication", "od______2659::47287d1800c112499a117ca17aa1909d"),
],
)
def test_openaire_id(running_app, minimal_open_record, resource_type, expected):
"""Test OpenAIRE ID."""
r = deepcopy(minimal_open_record)
r["pids"] = {
"doi": {"identifier": "10.5281/zenodo.123"},
"oai": {"identifier": "oai:zenodo.org:123"},
}
r["metadata"]["resource_type"] = {"id": resource_type}
assert openaire_id(r) == expected


@pytest.mark.parametrize(
"resource_type,oatype",
[
("software", OA_SOFTWARE),
("image-photo", OA_DATASET),
("other", OA_OTHER),
("dataset", OA_DATASET),
("publication", OA_PUBLICATION),
],
)
def test_openaire_link(running_app, minimal_open_record, resource_type, oatype):
"""Test OpenAIRE ID."""
r = deepcopy(minimal_open_record)

doi = "10.5281/zenodo.123"
r["pids"] = {
"doi": {"identifier": doi},
"oai": {"identifier": "oai:zenodo.org:123"},
}
r["metadata"]["resource_type"] = {"id": resource_type}
assert openaire_link(r) == f"https://explore.openaire.eu/search/{oatype}?pid={doi}"
31 changes: 31 additions & 0 deletions site/tests/openaire/test_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
#
# Zenodo-RDM is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""Tests for openaire serializer."""

import pytest
from zenodo_rdm.openaire.serializers import OpenAIREV1Serializer
from invenio_rdm_records.proxies import current_rdm_records_service as records_service
from invenio_access.permissions import system_identity


@pytest.fixture(scope="module")
def openaire_serializer():
yield OpenAIREV1Serializer()


def test_record_serializer(running_app, minimal_record, openaire_serializer):
serializer = openaire_serializer

# Disable files, we don't care about files for this test.
minimal_record["files"]["enabled"] = False

# We use system identity since we just want a record to be serialized, we don't care about permissions.
draft = records_service.create(system_identity, minimal_record)
record = records_service.publish(id_=draft.id, identity=system_identity)

serialized_record = serializer.dump_obj(record)
assert serialized_record
30 changes: 30 additions & 0 deletions site/zenodo_rdm/openaire/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
#
# Zendo-RDM is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""OpenAire related configs."""

# OpenAIRE API configs
OPENAIRE_API_USERNAME = ""
"""OpenAIRE API authentication - username."""

OPENAIRE_API_PASSWORD = ""
"""OpenAIRE API authentication - password."""

OPENAIRE_API_URL = "http://dev.openaire.research-infrastructures.eu/is/mvc/api/results"
"""OpenAIRE API endpoint."""

# Beta configs
OPENAIRE_API_URL_BETA = None

# Other configs
OPENAIRE_COMMUNITIES = {}
"""OpenAIRE communities resource subtypes configuration."""

OPENAIRE_PORTAL_URL = "https://explore.openaire.eu"
"""OpenAIRE portal url."""

OPENAIRE_DIRECT_INDEXING_ENABLED = False
"""Enable sending published records for direct indexing at OpenAIRE."""
11 changes: 11 additions & 0 deletions site/zenodo_rdm/openaire/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
#
# Zenodo-RDM is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""OpenAIRE errors."""


class OpenAIRERequestError(Exception):
"""Error for failed requests on the OpenAIRE API."""
Loading

0 comments on commit 6639000

Please sign in to comment.