Skip to content

Commit e36c0c4

Browse files
Pawel Zembrzuskipazembrz
authored andcommitted
minter: add BAI minter
* INSPIR-3209
1 parent d37787f commit e36c0c4

File tree

4 files changed

+119
-9
lines changed

4 files changed

+119
-9
lines changed

backend/inspirehep/pidstore/api/authors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# inspirehep is free software; you can redistribute it and/or modify it under
66
# the terms of the MIT License; see LICENSE file for more details.
7-
7+
from inspirehep.pidstore.minters.bai import BAIMinter
88

99
from ..minters.control_number import AuthorsMinter
1010
from ..minters.orcid import OrcidMinter
@@ -13,4 +13,4 @@
1313

1414
class PidStoreAuthors(PidStoreBase):
1515

16-
minters = [AuthorsMinter, OrcidMinter]
16+
minters = [AuthorsMinter, OrcidMinter, BAIMinter]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2019 CERN.
4+
#
5+
# inspirehep is free software; you can redistribute it and/or modify it under
6+
# the terms of the MIT License; see LICENSE file for more details.
7+
8+
from inspire_utils.record import get_values_for_schema
9+
10+
from inspirehep.pidstore.minters.base import Minter
11+
12+
13+
class BAIMinter(Minter):
14+
pid_type = "bai"
15+
16+
def get_pid_values(self):
17+
return get_values_for_schema(self.data.get("ids", []), "INSPIRE BAI")

backend/tests/helpers/providers/record_provider.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,36 @@
1717

1818

1919
class RecordProvider(BaseProvider):
20-
def control_number(self):
20+
@staticmethod
21+
def control_number():
2122
return fake.random_number(digits=8, fix_len=True)
2223

23-
def doi(self):
24+
@staticmethod
25+
def doi():
2426
return "10.{}/{}".format(
2527
fake.random_number(digits=4, fix_len=True),
2628
fake.random_number(digits=8, fix_len=True),
2729
)
2830

29-
def arxiv(self):
31+
@staticmethod
32+
def arxiv():
3033
return "20{}.{}".format(
3134
fake.random_number(digits=2, fix_len=True),
3235
fake.random_number(digits=5, fix_len=True),
3336
)
3437

35-
def orcid(self):
38+
@staticmethod
39+
def orcid():
3640
return "0000-{}-{}-{}".format(
3741
fake.random_number(digits=4, fix_len=True),
3842
fake.random_number(digits=4, fix_len=True),
3943
fake.random_number(digits=4, fix_len=True),
4044
)
4145

46+
@staticmethod
47+
def bai():
48+
return f"{fake.name().replace(' ', '.')}.{fake.random_number(digits=1)}"
49+
4250
@staticmethod
4351
def hep_record():
4452
return {
@@ -48,8 +56,8 @@ def hep_record():
4856
"_collections": ["Literature"],
4957
}
5058

51-
@staticmethod
52-
def author_record():
59+
@classmethod
60+
def author_record(cls):
5361
return {
5462
"$schema": "http://localhost:5000/schemas/records/authors.json",
5563
"name": {"value": fake.name()},
@@ -124,6 +132,18 @@ def add_data_citations(citation_records):
124132
)
125133
return {"references": data}
126134

135+
@classmethod
136+
def add_other_pids(cls, pids):
137+
if not pids:
138+
return
139+
ids = []
140+
for pid in pids:
141+
if pid == "orcid":
142+
ids.append({"schema": "ORCID", "value": cls.orcid()})
143+
if pid == "bai":
144+
ids.append({"schema": "INSPIRE BAI", "value": cls.bai()})
145+
return ids
146+
127147
def record(
128148
self,
129149
record_type,
@@ -132,6 +152,7 @@ def record(
132152
literature_citations=[], # TODO: call `literature_references`
133153
data_citations=[],
134154
skip_validation=False,
155+
other_pids=[],
135156
):
136157
if record_type == "lit":
137158
record = self.hep_record()
@@ -149,7 +170,6 @@ def record(
149170
record = self.data_record()
150171
elif record_type == "ins":
151172
record = self.institutions_record()
152-
153173
if with_control_number:
154174
record["control_number"] = self.control_number()
155175
if data:
@@ -158,6 +178,10 @@ def record(
158178
record.update(self.add_citations(literature_citations))
159179
if data_citations:
160180
record.update(self.add_data_citations(data_citations))
181+
if other_pids:
182+
ids = record.get("ids", [])
183+
ids.extend(self.add_other_pids(other_pids))
184+
record["ids"] = ids
161185
if not skip_validation:
162186
schema_validate(record)
163187
return record
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2019 CERN.
4+
#
5+
# inspirehep is free software; you can redistribute it and/or modify it under
6+
# the terms of the MIT License; see LICENSE file for more details.
7+
8+
9+
import pytest
10+
from helpers.providers.faker import faker
11+
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
12+
13+
from inspirehep.pidstore.errors import PIDAlreadyExists
14+
from inspirehep.pidstore.minters.bai import BAIMinter
15+
from inspirehep.records.api import AuthorsRecord
16+
17+
18+
def test_minter_bai(base_app, db, es, create_record):
19+
data = {"ids": [{"schema": "JACOW", "value": "JACoW-12345678"}]}
20+
record_data = faker.record("aut", data=data, other_pids=["bai"])
21+
BAIMinter.mint(None, record_data)
22+
expected_pids_len = 1
23+
epxected_pids_values = [record_data["ids"][1]["value"]]
24+
expected_pids_provider = "external"
25+
expected_pids_status = PIDStatus.REGISTERED
26+
result_pids = (
27+
PersistentIdentifier.query.filter_by(object_uuid=None)
28+
.filter_by(pid_type="bai")
29+
.all()
30+
)
31+
result_pids_len = len(result_pids)
32+
33+
assert expected_pids_len == result_pids_len
34+
35+
pid = result_pids[0]
36+
assert expected_pids_provider == pid.pid_provider
37+
assert expected_pids_status == pid.status
38+
assert pid.pid_value in epxected_pids_values
39+
40+
41+
def test_minter_bai_empty(base_app, db, es, create_record):
42+
record_data = faker.record("aut")
43+
BAIMinter.mint(None, record_data)
44+
45+
expected_pids_len = 0
46+
47+
result_pids_len = PersistentIdentifier.query.filter_by(
48+
object_uuid=None, pid_type="bai"
49+
).count()
50+
51+
assert expected_pids_len == result_pids_len
52+
53+
54+
def test_minter_bai_already_existing(base_app, db, es, create_record):
55+
data = faker.record("aut", other_pids=["bai"])
56+
BAIMinter.mint(None, data)
57+
data2 = {"ids": data["ids"]}
58+
record_data2 = faker.record("aut", data2)
59+
with pytest.raises(PIDAlreadyExists):
60+
BAIMinter.mint(None, record_data2)
61+
62+
63+
def test_if_bai_is_processed_on_authors_record_creation(base_app, db, es):
64+
data = faker.record("aut", other_pids=["bai"])
65+
bai = data["ids"][0]["value"]
66+
rec = AuthorsRecord.create(data)
67+
assert (
68+
PersistentIdentifier.query.filter_by(pid_type="bai", pid_value=bai).count() == 1
69+
)

0 commit comments

Comments
 (0)