diff --git a/src/mijnbib/mijnbibliotheek.py b/src/mijnbib/mijnbibliotheek.py index 13fe6b9..74b3751 100644 --- a/src/mijnbib/mijnbibliotheek.py +++ b/src/mijnbib/mijnbibliotheek.py @@ -81,8 +81,7 @@ def get_loans(self, account_id: str) -> list[Loan]: url = self.BASE_URL + f"/mijn-bibliotheek/lidmaatschappen/{account_id}/uitleningen" html_string = self._open_account_loans_page(url) try: - p = LoansListPageParser(html_string, self.BASE_URL, account_id) - loans = p.parse() + loans = LoansListPageParser(html_string, self.BASE_URL, account_id).parse() except Exception as e: raise IncompatibleSourceError(f"Problem scraping loans ({str(e)})", "") from e return loans @@ -101,8 +100,7 @@ def get_reservations(self, account_id: str) -> list[Reservation]: url = self.BASE_URL + f"/mijn-bibliotheek/lidmaatschappen/{account_id}/reservaties" html_string = self._open_account_loans_page(url) # same structure as for loans try: - p = ReservationsPageParser(html_string) - holds = p.parse() + holds = ReservationsPageParser(html_string).parse() except Exception as e: raise IncompatibleSourceError( f"Problem scraping reservations ({str(e)})", "" @@ -123,8 +121,7 @@ def get_accounts(self) -> list[Account]: response = self._br.open(url) # pylint: disable=assignment-from-none html_string = response.read().decode("utf-8") # type:ignore try: - p = AccountsListPageParser(html_string, self.BASE_URL) - accounts = p.parse() + accounts = AccountsListPageParser(html_string, self.BASE_URL).parse() except Exception as e: raise IncompatibleSourceError(f"Problem scraping accounts ({str(e)})", "") from e return accounts diff --git a/tests/test_mijnbib.py b/tests/test_mijnbib.py new file mode 100644 index 0000000..e3499e8 --- /dev/null +++ b/tests/test_mijnbib.py @@ -0,0 +1,24 @@ +def test_mijnbib_available_imports(): + import mijnbib + + # make sure we don't expose too few, or too much + imps = [i for i in dir(mijnbib) if not i.startswith("__")] + assert set(imps) == set( + [ + "MijnBibliotheek", + "Loan", + "Reservation", + "Account", + "AccessError", + "AuthenticationError", + "CanNotConnectError", + "ExtendLoanError", + "IncompatibleSourceError", + "GeneralPluginError", + "PluginError", + "plugin_errors", + "mijnbibliotheek", + "parsers", + "models", + ] + ) diff --git a/tests/test_mijnbibliotheek.py b/tests/test_mijnbibliotheek.py deleted file mode 100644 index 3992d9e..0000000 --- a/tests/test_mijnbibliotheek.py +++ /dev/null @@ -1,132 +0,0 @@ -import datetime - -from mijnbib.parsers import ( - AccountsListPageParser, - ExtendResponsePageParser, - LoansListPageParser, - ReservationsPageParser, -) - - -def test_mijnbib_available_imports(): - import mijnbib - - # make sure we don't expose too few, or too much - imps = [i for i in dir(mijnbib) if not i.startswith("__")] - assert set(imps) == set( - [ - "MijnBibliotheek", - "Loan", - "Reservation", - "Account", - "AccessError", - "AuthenticationError", - "CanNotConnectError", - "ExtendLoanError", - "IncompatibleSourceError", - "GeneralPluginError", - "PluginError", - "plugin_errors", - "mijnbibliotheek", - "parsers", - "models", - ] - ) - - -def test_parse_accounts_list_page(): - # Happy flow test --> see doctest - assert AccountsListPageParser("", "").parse() == [] - - -def test_parse_item_count_from_li(): - assert AccountsListPageParser._parse_item_count_from_li("", "") is None - - -def test_parse_account_loans_page(): - # Happy flow test --> see doctest - assert LoansListPageParser("", "", "").parse() == [] - - -def test_parse_account_reservations_page(): - # Happy flow test --> see doctest - assert ReservationsPageParser("").parse() == [] - - -def test_extract_html_from_response_script_tag(): - raw_html = r""" - ... - - ... - """ - - expected_result = """ -
- -
- """ - - def clean_whitespace(s: str) -> str: - return s.replace(" ", "").replace("\n", "") - - actual_result = ExtendResponsePageParser(raw_html)._extract_html_from_response_script_tag() - assert clean_whitespace(actual_result) == clean_whitespace(expected_result) - - -def test_parse_extend_response_status_blob__success_case(): - html_string = """ -
- -
- """ - - actual_result = ExtendResponsePageParser._parse_extend_response_status_blob(html_string) - assert actual_result == { - "likely_success": True, - "count": 1, - "details": [{"title": "Het schip der doden", "until": datetime.date(2024, 1, 8)}], - } - - -def test_parse_extend_response_status_blob__foutmelding_case(): - html_string = """ -
- -
- """ - - actual_result = ExtendResponsePageParser._parse_extend_response_status_blob(html_string) - assert actual_result == { - "likely_success": False, - "count": 0, - "details": [], - } diff --git a/tests/test_parsers.py b/tests/test_parsers.py new file mode 100644 index 0000000..b158fda --- /dev/null +++ b/tests/test_parsers.py @@ -0,0 +1,113 @@ +import datetime + +from mijnbib.parsers import ( + AccountsListPageParser, + ExtendResponsePageParser, + LoansListPageParser, + ReservationsPageParser, +) + + +class TestAccountsListPageParser: + def test_parse_accounts_list_page(self): + # Happy flow test --> see doctest + assert AccountsListPageParser("", "").parse() == [] + + def test_parse_item_count_from_li(self): + assert AccountsListPageParser._parse_item_count_from_li("", "") is None + + +class TestLoansListPageParser: + def test_parse_account_loans_page(self): + # Happy flow test --> see doctest + assert LoansListPageParser("", "", "").parse() == [] + + +class TestReservationsPageParser: + def test_parse_account_reservations_page(self): + # Happy flow test --> see doctest + assert ReservationsPageParser("").parse() == [] + + +class TestExtendResponsePageParser: + def test_extract_html_from_response_script_tag(self): + raw_html = r""" + ... + + ... + """ + + expected_result = """ +
+ +
+ """ + + def clean_whitespace(s: str) -> str: + return s.replace(" ", "").replace("\n", "") + + actual_result = ExtendResponsePageParser( + raw_html + )._extract_html_from_response_script_tag() + assert clean_whitespace(actual_result) == clean_whitespace(expected_result) + + def test_parse_extend_response_status_blob__success_case(self): + html_string = """ +
+ +
+ """ + + actual_result = ExtendResponsePageParser._parse_extend_response_status_blob( + html_string + ) + assert actual_result == { + "likely_success": True, + "count": 1, + "details": [{"title": "Het schip der doden", "until": datetime.date(2024, 1, 8)}], + } + + def test_parse_extend_response_status_blob__foutmelding_case(self): + html_string = """ +
+ +
+ """ + + actual_result = ExtendResponsePageParser._parse_extend_response_status_blob( + html_string + ) + assert actual_result == { + "likely_success": False, + "count": 0, + "details": [], + }