Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/validators/finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def cusip(value: str):
(Literal[True]): If `value` is a valid CUSIP string.
(ValidationError): If `value` is an invalid CUSIP string.
"""
return len(value) == 9 and _cusip_checksum(value)
return len(value) == 9 and value[-1] in "0123456789" and _cusip_checksum(value)


@validator
Expand Down Expand Up @@ -117,7 +117,7 @@ def sedol(value: str):
(Literal[True]): If `value` is a valid SEDOL string.
(ValidationError): If `value` is an invalid SEDOL string.
"""
if len(value) != 7:
if len(value) != 7 or value[-1] not in "0123456789":
return False

weights = [1, 3, 1, 7, 3, 9, 1]
Expand Down
27 changes: 27 additions & 0 deletions tests/test_finance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Test Finance."""

# standard
from typing import Callable

# external
import pytest

Expand Down Expand Up @@ -49,3 +52,27 @@ def test_returns_true_on_valid_sedol(value: str):
def test_returns_failed_validation_on_invalid_sedol(value: str):
"""Test returns failed validation on invalid sedol."""
assert isinstance(sedol(value), ValidationError)


@pytest.mark.parametrize(
"validate, value",
[
(cusip, "03783310J"),
(cusip, "03783310j"),
(cusip, "03783310@"),
(sedol, "293692L"),
(sedol, "B12345G"),
(sedol, "B12345Q"),
],
)
def test_rejects_non_numeric_check_digit(validate: Callable[[str], object], value: str):
"""Reject letters and symbols that accidentally satisfy the checksum."""
assert isinstance(validate(value), ValidationError)


@pytest.mark.parametrize(
"validate, value", [(cusip, "037833100"), (sedol, "2936921"), (sedol, "B123456")]
)
def test_accepts_numeric_check_digit(validate: Callable[[str], object], value: str):
"""Keep valid numeric check digits, including alphanumeric identifiers."""
assert validate(value) is True