doi2dataset/tests/test_doi2dataset.py

50 lines
1.7 KiB
Python

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from doi2dataset import NameProcessor, Phase, sanitize_filename, validate_email_address
def test_phase_check_year():
"""Test that check_year correctly determines if a year is within the phase boundaries."""
phase = Phase("TestPhase", 2000, 2010)
# Within boundaries
assert phase.check_year(2005) is True
# Outside boundaries
assert phase.check_year(1999) is False
assert phase.check_year(2011) is False
# Boundary cases
assert phase.check_year(2000) is True
assert phase.check_year(2010) is True
def test_sanitize_filename():
"""Test the sanitize_filename function to convert DOI to a valid filename."""
doi = "10.1234/abc.def"
expected = "10_1234_abc_def"
result = sanitize_filename(doi)
assert result == expected
def test_split_name_with_comma():
"""Test splitting a full name that contains a comma."""
full_name = "Doe, John"
given, family = NameProcessor.split_name(full_name)
assert given == "John"
assert family == "Doe"
def test_split_name_without_comma():
"""Test splitting a full name that does not contain a comma."""
full_name = "John Doe"
given, family = NameProcessor.split_name(full_name)
assert given == "John"
assert family == "Doe"
def test_validate_email_address_valid():
"""Test that a valid email address is correctly recognized."""
valid_email = "john.doe@iana.org"
assert validate_email_address(valid_email) is True
def test_validate_email_address_invalid():
"""Test that an invalid email address is correctly rejected."""
invalid_email = "john.doe@invalid_domain"
assert validate_email_address(invalid_email) is False