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

Ocn-parsing-refactor #72

Merged
merged 3 commits into from
Feb 12, 2024
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
47 changes: 25 additions & 22 deletions bookops_worldcat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def _str2list(s: str) -> List[str]:
"""Converts str into list - use for list of OCLC numbers"""
return [n.strip() for n in s.split(",")]
return [n.strip() for n in s.split(",") if n.strip()]


def prep_oclc_number_str(oclcNumber: str) -> str:
Expand All @@ -22,11 +22,12 @@ def prep_oclc_number_str(oclcNumber: str) -> str:
oclcNumber: OCLC record as string

Returns:
oclcNumber as int
oclcNumber as str
"""
if "ocm" in oclcNumber or "ocn" in oclcNumber:

if oclcNumber.strip().startswith("ocm") or oclcNumber.strip().startswith("ocn"):
oclcNumber = oclcNumber.strip()[3:]
elif "on" in oclcNumber:
elif oclcNumber.strip().startswith("on"):
oclcNumber = oclcNumber.strip()[2:]

try:
Expand All @@ -44,20 +45,20 @@ def verify_oclc_number(oclcNumber: Union[int, str]) -> str:
oclcNumber: OCLC record number

Returns:
oclcNumber
oclcNumber as str

"""
if oclcNumber is None:
if not oclcNumber:
raise InvalidOclcNumber("Argument 'oclcNumber' is missing.")

elif type(oclcNumber) is int:
elif isinstance(oclcNumber, int):
return str(oclcNumber)

elif type(oclcNumber) is str:
return prep_oclc_number_str(oclcNumber) # type: ignore
elif isinstance(oclcNumber, str):
return prep_oclc_number_str(oclcNumber)

else:
raise InvalidOclcNumber("Argument 'oclc_number' is of invalid type.")
raise InvalidOclcNumber("Argument 'oclcNumber' is of invalid type.")


def verify_oclc_numbers(oclcNumbers: Union[str, List[Union[str, int]]]) -> List[str]:
Expand All @@ -73,18 +74,20 @@ def verify_oclc_numbers(oclcNumbers: Union[str, List[Union[str, int]]]) -> List[
Returns:
vetted_numbers: list of vetted oclcNumbers
"""

# change to list if comma separated string
if type(oclcNumbers) is str and oclcNumbers != "":
oclcNumbers = _str2list(oclcNumbers) # type: ignore

if not oclcNumbers or type(oclcNumbers) is not list:
if isinstance(oclcNumbers, str):
oclcNumbers_lst = _str2list(oclcNumbers)
elif isinstance(oclcNumbers, list):
oclcNumbers_lst = oclcNumbers # type: ignore
else:
raise InvalidOclcNumber(
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #."
"Argument 'oclcNumbers' must be a list or comma separated string "
"of valid OCLC #s."
)
if not oclcNumbers_lst:
raise InvalidOclcNumber(
"Argument 'oclcNumbers' must be a list or comma separated string "
"of valid OCLC #s."
)

try:
vetted_numbers = [str(verify_oclc_number(n)) for n in oclcNumbers]
return vetted_numbers
except InvalidOclcNumber:
raise InvalidOclcNumber("One of passed OCLC #s is invalid.")
vetted_numbers = [verify_oclc_number(n) for n in oclcNumbers_lst]
return vetted_numbers
2 changes: 1 addition & 1 deletion tests/test_metadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def test_seach_current_control_numbers_passed_as_str(

@pytest.mark.parametrize("argm", [(None), (""), ([])])
def test_search_current_control_numbers_missing_numbers(self, stub_session, argm):
err_msg = "Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #."
err_msg = "Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #s."
with pytest.raises(WorldcatSessionError) as exc:
stub_session.search_current_control_numbers(argm)
assert err_msg in str(exc.value)
Expand Down
37 changes: 22 additions & 15 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class TestUtils:

@pytest.mark.parametrize(
"argm,expectation",
[("ocm00012345", "12345"), ("ocn00012346", "12346"), ("on000012347", "12347")],
[
("ocm00012345", "12345"),
("ocn00012346", "12346"),
("on000012347", "12347"),
(" ocm00012348", "12348"),
],
)
def test_prep_oclc_number_str(self, argm, expectation):
assert prep_oclc_number_str(argm) == expectation
Expand All @@ -34,6 +39,8 @@ def test_prep_oclc_number_str_exception(self):
("12345", ["12345"]),
("12345,67890", ["12345", "67890"]),
("12345, 67890", ["12345", "67890"]),
(" , ", []),
("", []),
],
)
def test_str2list(self, argm, expectation):
Expand All @@ -45,34 +52,34 @@ def test_str2list(self, argm, expectation):
(
None,
pytest.raises(InvalidOclcNumber),
"Argument 'oclc_number' is missing.",
"Argument 'oclcNumber' is missing.",
),
(
[12345],
pytest.raises(InvalidOclcNumber),
"Argument 'oclc_number' is of invalid type.",
"Argument 'oclcNumber' is of invalid type.",
),
(
12345.5,
pytest.raises(InvalidOclcNumber),
"Argument 'oclc_number' is of invalid type.",
"Argument 'oclcNumber' is of invalid type.",
),
(
"bt12345",
pytest.raises(InvalidOclcNumber),
"Argument 'oclc_number' does not look like real OCLC #.",
"Argument 'oclcNumber' does not look like real OCLC #.",
),
(
"odn12345",
pytest.raises(InvalidOclcNumber),
"Argument 'oclc_number' does not look like real OCLC #.",
"Argument 'oclcNumber' does not look like real OCLC #.",
),
],
)
def test_verify_oclc_number_exceptions(self, argm, expectation, msg):
with expectation as exp:
verify_oclc_number(argm)
assert msg == str(exp.value)
assert msg == str(exp.value)

@pytest.mark.parametrize(
"argm,expectation",
Expand All @@ -94,44 +101,44 @@ def test_verify_oclc_number_success(self, argm, expectation):
(
None,
pytest.raises(InvalidOclcNumber),
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #.",
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #s.",
),
(
"",
pytest.raises(InvalidOclcNumber),
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #.",
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #s.",
),
(
[],
pytest.raises(InvalidOclcNumber),
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #.",
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #s.",
),
(
",,",
pytest.raises(InvalidOclcNumber),
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #.",
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #s.",
),
(
12345.5,
pytest.raises(InvalidOclcNumber),
"One of passed OCLC #s is invalid.",
"Argument 'oclcNumbers' must be a list or comma separated string of valid OCLC #s.",
),
(
"bt12345",
pytest.raises(InvalidOclcNumber),
"One of passed OCLC #s is invalid.",
"Argument 'oclcNumber' does not look like real OCLC #.",
),
(
"odn12345",
pytest.raises(InvalidOclcNumber),
"One of passed OCLC #s is invalid.",
"Argument 'oclcNumber' does not look like real OCLC #.",
),
],
)
def test_verify_oclc_numbers_exceptions(self, argm, expectation, msg):
with expectation as exp:
verify_oclc_numbers(argm)
assert msg == str(exp.value)
assert msg == str(exp.value)

@pytest.mark.parametrize(
"argm,expectation",
Expand Down