Skip to content

Commit

Permalink
verification refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
klinga committed Feb 12, 2024
1 parent eb3e69a commit a10fba2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
32 changes: 17 additions & 15 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 Down Expand Up @@ -48,7 +48,7 @@ def verify_oclc_number(oclcNumber: Union[int, str]) -> str:
oclcNumber as str
"""
if oclcNumber is None:
if not oclcNumber:
raise InvalidOclcNumber("Argument 'oclcNumber' is missing.")

elif isinstance(oclcNumber, int):
Expand All @@ -58,7 +58,7 @@ def verify_oclc_number(oclcNumber: Union[int, str]) -> 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 @@ -74,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
30 changes: 16 additions & 14 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,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 @@ -50,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 @@ -99,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

0 comments on commit a10fba2

Please sign in to comment.