From 52dcfdc9cf8feab22d03a57350b9d330c72f461d Mon Sep 17 00:00:00 2001 From: PhilipMay Date: Mon, 1 Jan 2024 16:12:01 +0100 Subject: [PATCH] Refactor tests and add error handling for extract_one function --- mltb2/bs.py | 3 ++- tests/test_bs.py | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/mltb2/bs.py b/mltb2/bs.py index f469a53..a645b64 100644 --- a/mltb2/bs.py +++ b/mltb2/bs.py @@ -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 diff --git a/tests/test_bs.py b/tests/test_bs.py index 8f082b6..0a5e69c 100644 --- a/tests/test_bs.py +++ b/tests/test_bs.py @@ -39,12 +39,19 @@ 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 @@ -52,20 +59,26 @@ def test_extract_all(my_soup: BeautifulSoup): 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" + )