Skip to content

Commit

Permalink
Refactor tests and add error handling for extract_one function
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipMay committed Jan 1, 2024
1 parent 2e45773 commit 52dcfdc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion mltb2/bs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def extract_one(soup: BeautifulSoup, name=None, attrs: Optional[dict] = None, **
if attrs is None:
attrs = {}
result = soup.find_all(name, attrs, **kwargs)
assert len(result) == 1, len(result)
if len(result) != 1:
raise RuntimeError(f"Expected exactly one result, but got {len(result)}!")
result = result[0]
return result

Expand Down
31 changes: 22 additions & 9 deletions tests/test_bs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,46 @@ def test_extract_text(my_soup: BeautifulSoup):
"and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well. ..."
)

def test_extract_one(my_soup: BeautifulSoup):

def test_extract_one__happy_case(my_soup: BeautifulSoup):
result = extract_one(my_soup, name="head")
assert result is not None
assert result.name == "head"
assert result.text == "The Dormouse's story"


def test_extract_one__multiple_results(my_soup: BeautifulSoup):
with pytest.raises(RuntimeError):
_ = extract_one(my_soup, name="a")


def test_extract_all(my_soup: BeautifulSoup):
result = extract_all(my_soup, name="a")
assert result is not None
assert len(result) == 3
assert result[0].name == "a"
assert result[0].text == "Elsie"


def test_soup_to_md(my_soup: BeautifulSoup):
result = soup_to_md(my_soup)
assert result is not None
assert result == "The Dormouse's story **The Dormouse's story**\n\n"\
"Once upon a time there were three little sisters; "\
"and their names were [Elsie](http://example.com/elsie), "\
"[Lacie](http://example.com/lacie) and [Tillie](http://example.com/tillie); "\
assert (
result == "The Dormouse's story **The Dormouse's story**\n\n"
"Once upon a time there were three little sisters; "
"and their names were [Elsie](http://example.com/elsie), "
"[Lacie](http://example.com/lacie) and [Tillie](http://example.com/tillie); "
"and they lived at the bottom of a well.\n\n...\n"
)


def test_html_to_md():
result = html_to_md(html_doc)
assert result is not None
assert result == "The Dormouse's story **The Dormouse's story**\n\n"\
"Once upon a time there were three little sisters; "\
"and their names were [Elsie](http://example.com/elsie), "\
"[Lacie](http://example.com/lacie) and [Tillie](http://example.com/tillie); "\
assert (
result == "The Dormouse's story **The Dormouse's story**\n\n"
"Once upon a time there were three little sisters; "
"and their names were [Elsie](http://example.com/elsie), "
"[Lacie](http://example.com/lacie) and [Tillie](http://example.com/tillie); "
"and they lived at the bottom of a well.\n\n...\n"
)

0 comments on commit 52dcfdc

Please sign in to comment.