Skip to content

Commit

Permalink
fetchers: add new recordid fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
fenekku committed Nov 11, 2019
1 parent 1566994 commit 20f37d0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 15 deletions.
4 changes: 2 additions & 2 deletions invenio_pidstore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
- :py:class:`invenio_pidstore.providers.datacite.DataCiteProvider` which
creates and manages a PersistentIdentifier given a valid DOI.
- :py:class:`invenio_pidstore.providers.recordid.RecordIdProvider` which
creates Invenio legacy integer record identifiers. (Deprecated)
creates Invenio legacy integer record identifiers. (Not Recommended)
>>> from invenio_pidstore.providers.recordid_v2 import RecordIdProviderV2
>>> provider = RecordIdProviderV2.create()
Expand All @@ -232,7 +232,7 @@
'3sbk2-5j060'
Configure ``PIDSTORE_RECORDID_OPTIONS`` in ``config.py``, to construct a
`pid_value` as you wish.
custom `pid_value`.
Creating your own provider
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion invenio_pidstore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""Provide a DOI prefix here."""

PIDSTORE_RECORDID_OPTIONS = {
'suffix_length': 10,
'length': 10,
'split_every': 5,
'checksum': True
}
18 changes: 17 additions & 1 deletion invenio_pidstore/fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,30 @@ def my_fetcher(record_uuid, data):
from flask import current_app

from .providers.recordid import RecordIdProvider
from .providers.recordid_v2 import RecordIdProviderV2

FetchedPID = namedtuple('FetchedPID', ['provider', 'pid_type', 'pid_value'])
"""A pid fetcher."""


def recid_fetcher(record_uuid, data):
def recid_fetcher_v2(record_uuid, data):
"""Fetch a record's identifiers.
:param record_uuid: The record UUID.
:param data: The record metadata.
:returns: A :data:`invenio_pidstore.fetchers.FetchedPID` instance.
"""
pid_field = current_app.config['PIDSTORE_RECID_FIELD']
return FetchedPID(
provider=RecordIdProviderV2,
pid_type=RecordIdProviderV2.pid_type,
pid_value=str(data[pid_field])
)


def recid_fetcher(record_uuid, data):
"""Legacy way to fetch a record's identifiers.
:param record_uuid: The record UUID.
:param data: The record metadata.
:returns: A :data:`invenio_pidstore.fetchers.FetchedPID` instance.
Expand Down
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
'Flask-BabelEx>=0.9.3',
'Flask>=0.11.1',
'six>=1.12.0',
'base32-lib>=1.0.0a1'
'base32-lib>=1.0.0'
]

packages = find_packages()
Expand Down Expand Up @@ -112,9 +112,11 @@
],
'invenio_pidstore.minters': [
'recid = invenio_pidstore.minters:recid_minter',
'recid_v2 = invenio_pidstore.minters:recid_minter_v2',
],
'invenio_pidstore.fetchers': [
'recid = invenio_pidstore.fetchers:recid_fetcher',
'recid_v2 = invenio_pidstore.fetchers:recid_fetcher_v2',
],
'invenio_admin.views': [
'invenio_pidstore_pid = invenio_pidstore.admin:pid_adminview',
Expand Down
21 changes: 18 additions & 3 deletions tests/test_fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import uuid

from invenio_pidstore import current_pidstore
from invenio_pidstore.fetchers import recid_fetcher
from invenio_pidstore.minters import recid_minter
from invenio_pidstore.fetchers import recid_fetcher, recid_fetcher_v2
from invenio_pidstore.minters import recid_minter, recid_minter_v2


def test_recid_fetcher(app, db):
"""Test base provider."""
"""Test legacy recid fetcher."""
with app.app_context():
rec_uuid = uuid.uuid4()
data = {}
Expand All @@ -29,6 +29,21 @@ def test_recid_fetcher(app, db):
assert fetched_pid.pid_type == 'recid'


def test_recid_fetcher_v2(app, db):
"""Test recommended recid fetcher."""
with app.app_context():
rec_uuid = uuid.uuid4()
data = {}
minted_pid = recid_minter_v2(rec_uuid, data)

fetched_pid = recid_fetcher_v2(rec_uuid, data)

assert minted_pid.pid_value == fetched_pid.pid_value
assert minted_pid.pid_type == fetched_pid.pid_type
assert fetched_pid.pid_type == 'recid'
assert fetched_pid.pid_value == minted_pid.pid_value


def test_register_fetcher(app):
"""Test base provider."""
with app.app_context():
Expand Down
16 changes: 10 additions & 6 deletions tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ def test_recordid_provider_v2(app, db):
assert provider.pid
assert provider.pid.pid_type == 'recid'
pid_value = provider.pid.pid_value
assert len(pid_value) == 10 + 1
assert pid_value.count('-') == 1
part1, part2 = pid_value.split('-')
assert len(part1) == 5
assert len(part2) == 5
assert provider.pid.pid_provider is None
assert provider.pid.status == PIDStatus.RESERVED
assert provider.pid.object_type is None
Expand All @@ -113,8 +114,9 @@ def test_recordid_provider_v2(app, db):
assert provider.pid
assert provider.pid.pid_type == 'recid'
pid_value = provider.pid.pid_value
assert len(pid_value) == 10 + 1
assert pid_value.count('-') == 1
part1, part2 = pid_value.split('-')
assert len(part1) == 5
assert len(part2) == 5
assert provider.pid.pid_provider is None
assert provider.pid.status == PIDStatus.REGISTERED
assert provider.pid.object_type == 'rec'
Expand All @@ -127,8 +129,10 @@ def test_recordid_provider_v2(app, db):
options={'length': 3, 'checksum': True, 'split_every': 1}
)
pid_value = provider.pid.pid_value
assert len(pid_value) == 3 + 2
assert pid_value.count('-') == 2
part1, part2, part3 = pid_value.split('-')
assert len(part1) == 1
assert len(part2) == 1
assert len(part3) == 1


def test_datacite_create_get(app, db):
Expand Down

0 comments on commit 20f37d0

Please sign in to comment.