def test_loan_extension_end_date(app, client, json_headers, users, testdata, loan_params): """Test loan end date after extension.""" params = deepcopy(loan_params) del params["transaction_date"] record = _checkout_loan_pid1(client, json_headers, users, params) extend_url = record["links"]["actions"]["extend"] loan = record["metadata"] item = Item.get_record_by_pid(loan["item_pid"]["value"]) item_loan_duration = circulation_default_loan_duration_for_item(item) user_login(client, "patron1", users) def _set_loan_end_date(loan_pid, new_end_date): """Set loan end date.""" loan = Loan.get_record_by_pid(loan_pid) loan["end_date"] = new_end_date.date().isoformat() loan.commit() db.session.commit() def test_extension_end_date_loan_not_overdue(loan_pid): """Test that new end date is added to the current end date.""" now = arrow.utcnow() new_end_date = now + timedelta(days=10) # loan is not overdue _set_loan_end_date(loan_pid, new_end_date) expected_end_date = new_end_date + item_loan_duration res = client.post(extend_url, headers=json_headers, data=json.dumps(params)) assert res.status_code == 202 new_loan = res.get_json()["metadata"] assert new_loan["end_date"] == expected_end_date.date().isoformat() def test_extension_end_date_loan_overdue(loan_pid): """Test that new end date is added to today.""" now = arrow.utcnow() new_end_date = now - timedelta(days=2) # loan is overdue since 2 days _set_loan_end_date(loan_pid, new_end_date) expected_end_date = now + item_loan_duration res = client.post(extend_url, headers=json_headers, data=json.dumps(params)) assert res.status_code == 202 new_loan = res.get_json()["metadata"] # loan new end date should start from now assert new_loan["end_date"] == expected_end_date.date().isoformat() test_extension_end_date_loan_not_overdue(loan["pid"]) test_extension_end_date_loan_overdue(loan["pid"])
def circulation_default_extension_duration(loan, initial_loan): """Return a default extension duration in timedelta.""" item_pid = loan["item_pid"] validator = validate_item_pid(item_pid) if validator.is_brw_req: # the loan has been already changed with a new end_date # return 0 to avoid to add again a new duration to the end_date return timedelta(days=0) # physical item Item = current_app_ils.item_record_cls item = Item.get_record_by_pid(item_pid["value"]) return circulation_default_loan_duration_for_item(item)
def circulation_default_loan_duration(loan, _): """Return a default loan duration in timedelta.""" item_pid = loan["item_pid"] validator = validate_item_pid(item_pid) if validator.is_brw_req: # the loan end_date should be set when checking out, no duration # to be added return timedelta(days=0) # physical item Item = current_app_ils.item_record_cls item = Item.get_record_by_pid(item_pid["value"]) return circulation_default_loan_duration_for_item(item)
def circulation_default_extension_duration(loan, initial_loan): """Return a default extension duration in timedelta.""" item_pid = loan["item_pid"] validator = validate_item_pid(item_pid) if validator.is_brw_req: # the loan has been already changed with a new end_date # return 0 to avoid to add again a new duration to the end_date return timedelta(days=0) # physical item Item = current_app_ils.item_record_cls item = Item.get_record_by_pid(item_pid["value"]) duration = circulation_default_loan_duration_for_item(item) today = arrow.utcnow().date() end_date = arrow.get(loan["end_date"]).date() is_overdue = today > end_date if is_overdue: # when overdue, the extended end date is today + duration end_date_to_today = today - end_date return end_date_to_today + duration else: # when not overdue, the extended end date is end date + duration return duration