Skip to content

Commit

Permalink
Tighten up utils.safe_ia_handle and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Dec 22, 2022
1 parent cc7ba25 commit 8abd32c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
21 changes: 18 additions & 3 deletions newshomepages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,25 @@
LEADING_UNDERSCORES = re.compile("^(_+)")


def safe_ia_handle(s):
"""Santize a handle so its safe to use as an archive.org slug."""
def safe_ia_handle(handle: str) -> str:
"""Santize a handle so its safe to use as an archive.org slug.
Args:
handle (str): The unique string identifier of the site.
Returns a lowercase string that's ready to use.
"""
# Take it down
s = s.lower()
s = handle.lower()

# Trim it
s = s.strip()

# If there is whitespace in the handle, throw an error
if " " in s:
raise ValueError(
f"Site handles cannot contain whitespace. You submitted: '{handle}'"
)

# Replace any leading underscores, which don't work on archive.org
s = LEADING_UNDERSCORES.sub("", s)
Expand Down
26 changes: 18 additions & 8 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
from newshomepages import utils


def test_safe_ia_handle():
"""Test safe_ia_handle."""
assert utils.safe_ia_handle("_leadingunderscore") == "leadingunderscore"
assert utils.safe_ia_handle("CamelCase") == "camelcase"
try:
utils.safe_ia_handle("white space")
except ValueError:
pass


def test_sites():
"""Test sites utils."""
# Read in the list
Expand Down Expand Up @@ -61,16 +71,16 @@ def test_url_parse():
)


def test_get_extract():
"""Test the method to pull in an extract CSV."""
utils.get_extract_df("sites.csv")
# def test_get_extract():
# """Test the method to pull in an extract CSV."""
# utils.get_extract_df("sites.csv")


def test_get_url():
"""Test utils for getting data off the web."""
utils.get_json_url(
"https://archive.org/download/signalcleveland-2022/signalcleveland-2022-11-17T02%3A50%3A58.280867-05%3A00.wayback.json"
)
# def test_get_url():
# """Test utils for getting data off the web."""
# utils.get_json_url(
# "https://archive.org/download/signalcleveland-2022/signalcleveland-2022-11-17T02%3A50%3A58.280867-05%3A00.wayback.json"
# )


def test_get_local_time():
Expand Down

0 comments on commit 8abd32c

Please sign in to comment.