Skip to content

Commit

Permalink
minters: add new recordid minter
Browse files Browse the repository at this point in the history
  • Loading branch information
fenekku committed Nov 6, 2019
1 parent eae2dfa commit 1566994
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
23 changes: 23 additions & 0 deletions invenio_pidstore/minters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@
from flask import current_app

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


def recid_minter_v2(record_uuid, data):
"""Mint record identifiers with RecordIDProviderV2.
This minter is recommended to be used when creating records to get
PersistentIdentifier with ``object_type='rec'`` and the new random
alphanumeric `pid_value`.
Raises ``AssertionError`` if a ``PIDSTORE_RECID_FIELD`` entry is already in
``data``. The minted ``pid_value`` will be stored in that field.
:param record_uuid: The object UUID of the record.
:param data: The record metadata.
:returns: A fresh `invenio_pidstore.models.PersistentIdentifier` instance.
"""
pid_field = current_app.config['PIDSTORE_RECID_FIELD']
assert pid_field not in data
provider = RecordIdProviderV2.create(
object_type='rec', object_uuid=record_uuid)
data[pid_field] = provider.pid.pid_value
return provider.pid


def recid_minter(record_uuid, data):
Expand Down
27 changes: 24 additions & 3 deletions tests/test_minters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,44 @@

import uuid

import pytest

from invenio_pidstore import current_pidstore
from invenio_pidstore.minters import recid_minter
from invenio_pidstore.minters import recid_minter, recid_minter_v2


def test_recid_minter(app, db):
"""Test base provider."""
"""Test legacy recid minter."""
with app.app_context():

rec_uuid = uuid.uuid4()
data = {}

pid = recid_minter(rec_uuid, data)

assert pid
assert data[app.config['PIDSTORE_RECID_FIELD']] == pid.pid_value
assert pid.object_type == 'rec'
assert pid.object_uuid == rec_uuid


def test_recid_minter_v2(app, db):
"""Test recommended recid minter."""
with app.app_context():
rec_uuid = uuid.uuid4()
data = {}
recid_field = app.config['PIDSTORE_RECID_FIELD']

pid = recid_minter_v2(rec_uuid, data)

assert pid
assert data[recid_field] == pid.pid_value
assert pid.object_type == 'rec'
assert pid.object_uuid == rec_uuid

with pytest.raises(AssertionError):
recid_minter_v2(rec_uuid, {recid_field: '1'})


def test_register_minter(app):
"""Test base provider."""
with app.app_context():
Expand Down

0 comments on commit 1566994

Please sign in to comment.