diff --git a/src/mijnbib/mijnbibliotheek.py b/src/mijnbib/mijnbibliotheek.py index b47a5f9..54ad5e8 100644 --- a/src/mijnbib/mijnbibliotheek.py +++ b/src/mijnbib/mijnbibliotheek.py @@ -75,6 +75,7 @@ def __init__(self, username: str, password: str, city: str | None = None, login_ # Open the door for overriding parsers (but still keep private for now) self._loans_page_parser = LoansListPageParser() self._accounts_page_parser = AccountsListPageParser() + self._reservations_parser = ReservationsPageParser() # *** PUBLIC METHODS *** @@ -141,7 +142,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: - holds = ReservationsPageParser(html_string).parse() + holds = self._reservations_parser.parse(html_string) except Exception as e: raise IncompatibleSourceError( f"Problem scraping reservations ({str(e)})", html_body="" diff --git a/src/mijnbib/parsers.py b/src/mijnbib/parsers.py index 48807f4..c2783b3 100644 --- a/src/mijnbib/parsers.py +++ b/src/mijnbib/parsers.py @@ -391,11 +391,8 @@ def _parse_item_count_from_li(acc_div, class_: str) -> int | None: return item_count -class ReservationsPageParser(Parser): - def __init__(self, html: str): - self._html = html - - def parse(self) -> list[Reservation]: +class ReservationsPageParser(ParserNew): + def parse(self, html: str) -> list[Reservation]: """Return list of holds. >>> html_string=''' @@ -435,13 +432,13 @@ def parse(self) -> list[Reservation]: ... ... ... ''' - >>> ReservationsPageParser(html_string).parse() # doctest: +NORMALIZE_WHITESPACE + >>> ReservationsPageParser().parse(html_string) # doctest: +NORMALIZE_WHITESPACE [Reservation(title='Vastberaden!', type='', url='https://city.bibliotheek.be/resolver.ashx?extid=%7Cwise-oostvlaanderen%7C12345', author='John Doe', location='MyCity', available=False, available_till=None, request_on=datetime.date(2023, 11, 25), valid_till=datetime.date(2024, 11, 24))] """ holds = [] - soup = BeautifulSoup(self._html, "html.parser") + soup = BeautifulSoup(html, "html.parser") holds_section_div = soup.find( "div", class_="my-library-user-library-account-holds__hold-wrapper" diff --git a/tests/test_mijnbibliotheek.py b/tests/test_mijnbibliotheek.py index dd815df..7fff699 100644 --- a/tests/test_mijnbibliotheek.py +++ b/tests/test_mijnbibliotheek.py @@ -9,7 +9,7 @@ from mijnbib import MijnBibliotheek from mijnbib.errors import AuthenticationError from mijnbib.login_handlers import LoginByForm, LoginByOAuth -from mijnbib.models import Account, Loan +from mijnbib.models import Account, Loan, Reservation CONFIG_FILE = "mijnbib.ini" @@ -181,7 +181,7 @@ def parse(self, _html, _base_url): return [acc] mb = MijnBibliotheek("user", "pwd") - # Fake both (a) valid login, and (b) some reponse on fetching loans page + # Fake both (a) valid login, and (b) some reponse on fetching accounts page mb._br = FakeMechanizeBrowser(form_response="Profiel") # type: ignore # Act @@ -189,3 +189,21 @@ def parse(self, _html, _base_url): # Assert assert mb.get_accounts() == [acc] + + def test_reservations_parser_can_be_overridden(self): + # Arrange + res = Reservation("title", "dvd", "some_url", "author", "brussels", True) + + class MyCustomReservationsParser: + def parse(self, _html): + return [res] + + mb = MijnBibliotheek("user", "pwd") + # Fake both (a) valid login, and (b) some reponse on fetching reservations page + mb._br = FakeMechanizeBrowser(form_response="Profiel") # type: ignore + + # Act + mb._reservations_parser = MyCustomReservationsParser() # type:ignore + + # Assert + assert mb.get_reservations(account_id="whatever") == [res] diff --git a/tests/test_parsers.py b/tests/test_parsers.py index 2bc9cfd..c1ddc4c 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -28,8 +28,8 @@ def test_parse_account_loans_page(self): class TestReservationsPageParser: def test_parse_account_reservations_page(self): # Happy flow test --> see doctest - assert ReservationsPageParser("").parse() == [] - assert ReservationsPageParser("bogus").parse() == [] + assert ReservationsPageParser().parse("") == [] + assert ReservationsPageParser().parse("bogus") == [] class TestExtendResponsePageParser: