Skip to content

Commit

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

# *** PUBLIC METHODS ***

Expand Down Expand Up @@ -163,7 +164,7 @@ def get_accounts(self) -> list[Account]:
response = self._br.open(url, timeout=TIMEOUT) # pylint: disable=assignment-from-none
html_string = response.read().decode("utf-8") # type:ignore
try:
accounts = AccountsListPageParser(html_string, self.BASE_URL).parse()
accounts = self._accounts_page_parser.parse(html_string, self.BASE_URL)
except Exception as e:
raise IncompatibleSourceError(
f"Problem scraping accounts ({str(e)})", html_body=""
Expand Down
18 changes: 7 additions & 11 deletions src/mijnbib/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,8 @@ def _get_loan_info_from_div(
)


class AccountsListPageParser(Parser):
def __init__(self, html: str, base_url: str):
self._html = html
self._base_url = base_url

def parse(self) -> list[Account]:
class AccountsListPageParser(ParserNew):
def parse(self, html: str, base_url: str) -> list[Account]:
"""Return list of accounts.
>>> html_string = '''
Expand Down Expand Up @@ -266,13 +262,13 @@ def parse(self) -> list[Account]:
... </div>
... ...
... '''
>>> AccountsListPageParser(html_string,"https://example.com").parse() # doctest: +NORMALIZE_WHITESPACE
>>> AccountsListPageParser().parse(html_string,"https://example.com") # doctest: +NORMALIZE_WHITESPACE
[Account(library_name='Dijk92', user='Johny', id='374047', loans_count=0, loans_url='https://example.com/mijn-bibliotheek/lidmaatschappen/374047/uitleningen',
reservations_count=5, reservations_url='https://example.com/mijn-bibliotheek/lidmaatschappen/384767/reservaties',
open_amounts=0, open_amounts_url='')]
"""
accounts = []
soup = BeautifulSoup(self._html, "html.parser")
soup = BeautifulSoup(html, "html.parser")

library_divs = soup.find_all(
"div", class_="my-library-user-library-account-list__library"
Expand Down Expand Up @@ -320,7 +316,7 @@ def parse(self) -> list[Account]:
)

try:
loans_url = self._base_url + acc_div.find(
loans_url = base_url + acc_div.find(
"a", href=re.compile("uitleningen")
).get("href")
except AttributeError:
Expand All @@ -331,7 +327,7 @@ def parse(self) -> list[Account]:
)

try:
holds_url = self._base_url + acc_div.find(
holds_url = base_url + acc_div.find(
"a", href=re.compile("reservaties")
).get("href")
except AttributeError:
Expand All @@ -357,7 +353,7 @@ def parse(self) -> list[Account]:
open_amounts = 0

try:
open_amounts_url = self._base_url + acc_div.find(
open_amounts_url = base_url + acc_div.find(
"a", href=re.compile("betalen")
).get("href")
except AttributeError:
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 Loan
from mijnbib.models import Account, Loan

CONFIG_FILE = "mijnbib.ini"

Expand Down Expand Up @@ -156,7 +156,7 @@ def test_login_by_oauth_already_logged_in(self, creds_config, caplog):


class TestCustomParser:
def test_loan_page_parser_can_be_overridden(self):
def test_loans_page_parser_can_be_overridden(self):
# Arrange
class MyCustomLoanParser:
def parse(self, _html, _base_url, _account_id):
Expand All @@ -171,3 +171,21 @@ def parse(self, _html, _base_url, _account_id):

# Assert
assert mb.get_loans(account_id="whatever") == [Loan("some title")]

def test_accounts_page_parser_can_be_overridden(self):
# Arrange
acc = Account("libname", "user", "id", 1, "loans_url", 1, "res_url", 1, "oa_url")

class MyCustomAccountsParser:
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
mb._br = FakeMechanizeBrowser(form_response="Profiel") # type: ignore

# Act
mb._accounts_page_parser = MyCustomAccountsParser() # type:ignore

# Assert
assert mb.get_accounts() == [acc]
2 changes: 1 addition & 1 deletion tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class TestAccountsListPageParser:
def test_parse_accounts_list_page(self):
# Happy flow test --> see doctest
assert AccountsListPageParser("", "https://example.com").parse() == []
assert AccountsListPageParser().parse("", "https://example.com") == []

def test_parse_item_count_from_li(self):
assert AccountsListPageParser._parse_item_count_from_li("", "") is None
Expand Down

0 comments on commit bc7f056

Please sign in to comment.