Skip to content

Commit

Permalink
Refactor Reservations parser (params from instance to method)
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanhed committed Jul 5, 2024
1 parent bc7f056 commit 5f694ff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/mijnbib/mijnbibliotheek.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ***

Expand Down Expand Up @@ -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=""
Expand Down
11 changes: 4 additions & 7 deletions src/mijnbib/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='''
Expand Down Expand Up @@ -435,13 +432,13 @@ def parse(self) -> list[Reservation]:
... </div>
... </div>
... '''
>>> 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"
Expand Down
22 changes: 20 additions & 2 deletions tests/test_mijnbibliotheek.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -181,11 +181,29 @@ 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
mb._accounts_page_parser = MyCustomAccountsParser() # type:ignore

# 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]
4 changes: 2 additions & 2 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 5f694ff

Please sign in to comment.