Skip to content

Commit

Permalink
tests: fix problems with daylight saving time
Browse files Browse the repository at this point in the history
* Fixes test that have problem twice a year with daylight saving time
  timezone

Co-Authored-by: Olivier DOSSMANN <[email protected]>
  • Loading branch information
blankoworld committed Mar 5, 2020
1 parent bb70fbb commit 88e4db9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
31 changes: 12 additions & 19 deletions tests/api/test_loans_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from invenio_accounts.testutils import login_user_via_session
from invenio_circulation.api import get_loan_for_item
from invenio_circulation.search.api import LoansSearch
from utils import flush_index, postdata
from utils import check_timezone_date, flush_index, postdata

from rero_ils.modules.items.api import Item
from rero_ils.modules.libraries.api import Library
Expand Down Expand Up @@ -170,27 +170,20 @@ def test_due_soon_loans(client, librarian_martigny_no_email,
loan_pid = data.get('action_applied')[LoanAction.CHECKOUT].get('pid')
due_soon_loans = get_due_soon_loans()
assert due_soon_loans[0].get('pid') == loan_pid

# test due date hour, should be 22:59 UTC+0 (Europe/Zurich)
assert get_last_transaction_loc_for_item(
item_pid) == loc_public_martigny.pid
# test due date hour

# test due date regarding multiple timezones
checkout_loan = Loan.get_record_by_pid(loan_pid)
end_date = ciso8601.parse_datetime(
checkout_loan.get('end_date'))
assert end_date.minute == 59 and end_date.hour == 22

# test due date hour in US/Pacific, should be 14:59
new_timezone = pytz.timezone('US/Pacific')
end_date = ciso8601.parse_datetime(
checkout_loan.get('end_date')).astimezone(new_timezone)
assert end_date.minute == 59 and end_date.hour == 14

# test due date hour in Europe/Amsterdam, should be 23:59
new_timezone = pytz.timezone('Europe/Amsterdam')
end_date = ciso8601.parse_datetime(
checkout_loan.get('end_date')).astimezone(new_timezone)
assert end_date.minute == 59 and end_date.hour == 23
loan_date = ciso8601.parse_datetime(checkout_loan.get('end_date'))

# should be 22:59 in UTC+0
tocheck_date = loan_date
assert tocheck_date.minute == 59 and tocheck_date.hour == 22

# should be 14:59/15:59 in US/Pacific (because of daylight saving time)
check_timezone_date(pytz.timezone('US/Pacific'), loan_date, [14, 15])
check_timezone_date(pytz.timezone('Europe/Amsterdam'), loan_date)

# checkin the item to put it back to it's original state
res, _ = postdata(
Expand Down
19 changes: 19 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,22 @@ def mock_response(status=200, content="CONTENT", json_data=None,
if json_data:
mock_resp.json = mock.Mock(return_value=json_data)
return mock_resp


def get_timezone_difference(timezone, date):
"""Get timezone offset difference, in hours."""
if date.tzinfo is not None:
date = date.replace(tzinfo=None)
return int(timezone.utcoffset(date).total_seconds()/3600)


def check_timezone_date(timezone, date, expected=[]):
"""Check hour and minute of given date regarding given timezone."""
difference = get_timezone_difference(timezone, date)
hour = date.hour + difference
# Expected list defines accepted hours for tests
if expected:
assert hour in expected
tocheck_date = date.astimezone(timezone)
assert tocheck_date.minute == date.minute
assert tocheck_date.hour == hour

0 comments on commit 88e4db9

Please sign in to comment.