Skip to content

Commit

Permalink
Refactor: reorganize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanhed committed Dec 19, 2023
1 parent 48a06ed commit 490196d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 138 deletions.
9 changes: 3 additions & 6 deletions src/mijnbib/mijnbibliotheek.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)})", ""
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/test_mijnbib.py
Original file line number Diff line number Diff line change
@@ -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",
]
)
132 changes: 0 additions & 132 deletions tests/test_mijnbibliotheek.py

This file was deleted.

113 changes: 113 additions & 0 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
@@ -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"""
...
<script type="application/vnd.drupal-ajax"
data-big-pipe-replacement-for-placeholder-with-id="callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&amp;args%5B0%5D&amp;token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA">
[{"command":"insert","method":"replaceWith","selector":"[data-big-pipe-placeholder-id=\u0022callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages\u0026args%5B0%5D\u0026token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA\u0022]","data":"\u003Cdiv data-drupal-messages\u003E\n \u003Cdiv role=\u0022contentinfo\u0022 aria-label=\u0022Statusbericht\u0022 class=\u0022messages messages--status\u0022\u003E\n \u003Ci class=\u0022icon fa fa-exclamation-triangle\u0022 aria-hidden=\u0022true\u0022\u003E\u003C\/i\u003E\n \u003Ch2 class=\u0022visually-hidden\u0022\u003EStatusbericht\u003C\/h2\u003E\n \u003Cul class=\u0022messages__list\u0022\u003E\n \u003Cli class=\u0022messages__item\u0022\u003EDeze uitleningen werden succesvol verlengd:\u003C\/li\u003E\n \u003Cli class=\u0022messages__item\u0022\u003E\u0022\u003Cem class=\u0022placeholder\u0022\u003EHet schip der doden\u003C\/em\u003E\u0022 tot 08\/01\/2024.\u003C\/li\u003E\n \u003C\/ul\u003E\n \u003C\/div\u003E\n \u003C\/div\u003E\n","settings":null}]
</script>
...
"""

expected_result = """
<div data-drupal-messages>
<div role="contentinfo" aria-label="Statusbericht" class="messages messages--status">
<i class="icon fa fa-exclamation-triangle" aria-hidden="true"></i>
<h2 class="visually-hidden">Statusbericht</h2>
<ul class="messages__list">
<li class="messages__item">Deze uitleningen werden succesvol verlengd:</li>
<li class="messages__item">"<em class="placeholder">Het schip der doden</em>" tot 08/01/2024.</li>
</ul>
</div>
</div>
"""

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 = """
<div data-drupal-messages>
<div role="contentinfo" aria-label="Statusbericht" class="messages messages--status">
<i class="icon fa fa-exclamation-triangle" aria-hidden="true"></i>
<h2 class="visually-hidden">Statusbericht</h2>
<ul class="messages__list">
<li class="messages__item">Deze uitleningen werden succesvol verlengd:</li>
<li class="messages__item">"<em class="placeholder">Het schip der doden</em>" tot 08/01/2024.</li>
</ul>
</div>
</div>
"""

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 = """
<div data-drupal-messages="">
<div role="contentinfo" aria-label="Foutmelding" class="messages messages--error"
data-gtm-vis-recent-on-screen9349158_360="48" data-gtm-vis-first-on-screen9349158_360="48"
data-gtm-vis-total-visible-time9349158_360="100" data-gtm-vis-has-fired9349158_360="1">
<i class="icon fa fa-exclamation-triangle" aria-hidden="true"></i>
<div role="alert">
<h2 class="visually-hidden">Foutmelding</h2>
<ul class="messages__list">
<li class="messages__item">Er ging iets fout bij het verlengen van deze uitleningen:</li>
<li class="messages__item"><strong>"<em class="placeholder">De Helvetii</em>"</strong><br>Reden: Er ging iets
fout, gelieve het later opnieuw te proberen.</li>
</ul>
</div>
</div>
</div>
"""

actual_result = ExtendResponsePageParser._parse_extend_response_status_blob(
html_string
)
assert actual_result == {
"likely_success": False,
"count": 0,
"details": [],
}

0 comments on commit 490196d

Please sign in to comment.