Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: es_ES doi() to use standard DOI format #2162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions faker/providers/doi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from faker.providers import BaseProvider


class Provider(BaseProvider):
"""
Provider for Digital Object Identifier (DOI)
Source of info: https://en.wikipedia.org/wiki/Digital_object_identifier (English)
"""

def doi(self) -> str:
"""
Generate a valid Digital Object Identifier (DOI).
Format: 10.{4-9 digits}/{alphanumeric string}
Eg: 10.1000/xyz123
"""
prefix = "10"
registrant = str(self.generator.random.randint(1000, 99999999))
suffix = self.generator.bothify("?#?#?##").lower()

return f"{prefix}.{registrant}/{suffix}"
26 changes: 26 additions & 0 deletions tests/providers/test_doi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import re

from faker import Faker


def test_doi():
fake = Faker()

# Test standard DOI
doi = fake.doi()
assert doi.startswith("10.")
# DOI format: 10.{registrant}/{suffix}
assert re.match(r"^10\.\d{4,9}/[a-z0-9]+$", doi)


def test_doi_es_ES():
# Test Spanish locale no longer returns Spanish IDs
fake = Faker("es_ES")
doi = fake.doi()

# Should follow DOI format, not Spanish ID format
assert doi.startswith("10.")
assert re.match(r"^10\.\d{4,9}/[a-z0-9]+$", doi)
# Make sure it's not returning Spanish IDs
assert not re.match(r"^[XYZ]\d{7}[A-Z]$", doi) # NIE format
assert not re.match(r"^\d{8}[A-Z]$", doi) # NIF format