def test_loan_extend(loan_created, params, mock_ensure_item_is_available_for_checkout): """Test loan extend action.""" mock_ensure_item_is_available_for_checkout.side_effect = None loan = current_circulation.circulation.trigger( loan_created, **dict(params, trigger="checkout")) end_date = str2datetime(loan["end_date"]) default_extension_duration = timedelta(days=30) end_date_with_ext = end_date + default_extension_duration with SwappedNestedConfig( ["CIRCULATION_POLICIES", "extension", "max_count"], lambda x: 0): with pytest.raises(LoanMaxExtensionError): loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="extend")) with SwappedNestedConfig( ["CIRCULATION_POLICIES", "extension", "max_count"], lambda x: 2): loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="extend")) new_end_date = loan["end_date"] assert str2datetime(new_end_date) == end_date_with_ext assert loan["extension_count"] == 1 loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="extend")) # test too many extensions with pytest.raises(LoanMaxExtensionError): loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="extend"))
def test_checkin_end_date_is_transaction_date( mock_get_pending_loans_by_doc_pid, mock_ensure_item_is_available_for_checkout, loan_created, params, ): """Test date the checkin date is the transaction date.""" mock_get_pending_loans_by_doc_pid.return_value = [] mock_ensure_item_is_available_for_checkout.side_effect = None start_date = arrow.utcnow() end_date = start_date + timedelta(days=10) loan = current_circulation.circulation.trigger( loan_created, **dict( params, start_date=start_date, end_date=end_date, trigger="checkout", )) assert loan["state"] == "ITEM_ON_LOAN" assert loan["transaction_date"] transaction_date = loan["transaction_date"] same_location = params["transaction_location_pid"] params["transaction_date"] = arrow.utcnow() with SwappedConfig("CIRCULATION_ITEM_LOCATION_RETRIEVER", lambda x: same_location): loan = current_circulation.circulation.trigger(loan, **dict(params)) assert loan["state"] == "ITEM_RETURNED" expected_end_date = str2datetime(transaction_date).date().isoformat() assert loan["end_date"] == expected_end_date
def test_checkout_extend_with_timedelta( loan_created, params, mock_ensure_item_is_available_for_checkout ): """Test checkout with a timedelta duration.""" mock_ensure_item_is_available_for_checkout.side_effect = None duration = timedelta(hours=4) with SwappedNestedConfig( ["CIRCULATION_POLICIES", "checkout", "duration_default"], lambda _loan, _initial: duration, ): loan = current_circulation.circulation.trigger( loan_created, **dict(params, trigger="checkout") ) expected_start_date = str2datetime(loan["transaction_date"]) expected_end_date = str2datetime(loan["start_date"]) + duration assert loan["state"] == "ITEM_ON_LOAN" assert loan["start_date"] == expected_start_date.date().isoformat() assert loan["end_date"] == expected_end_date.date().isoformat() end_date = loan["end_date"] duration = timedelta(days=1) with SwappedNestedConfig( ["CIRCULATION_POLICIES", "extension", "duration_default"], lambda _loan, _initial: duration, ): # perform an extension loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="extend") ) expected_end_date = str2datetime(end_date) + duration assert loan["end_date"] == expected_end_date.date().isoformat() end_date = loan["end_date"] duration = timedelta(days=3, minutes=33) with SwappedNestedConfig( ["CIRCULATION_POLICIES", "extension", "duration_default"], lambda _loan, _initial: duration, ): # extend again loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="extend") ) expected_end_date = str2datetime(end_date) + duration assert loan["end_date"] == expected_end_date.date().isoformat()
def test_checkout_start_is_transaction_date( loan_created, params, mock_ensure_item_is_available_for_checkout): """Test checkout start date to transaction date when not set.""" mock_ensure_item_is_available_for_checkout.side_effect = None duration = timedelta(days=10) with SwappedNestedConfig( ["CIRCULATION_POLICIES", "checkout", "duration_default"], lambda x: duration, ): loan = current_circulation.circulation.trigger( loan_created, **dict(params, trigger="checkout")) expected_start_date = str2datetime(loan["transaction_date"]) expected_end_date = str2datetime(loan["start_date"]) + duration assert loan["state"] == "ITEM_ON_LOAN" assert loan["start_date"] == expected_start_date.date().isoformat() assert loan["end_date"] == expected_end_date.date().isoformat()
def test_loan_extend_from_enddate(loan_created, params, mock_ensure_item_is_available_for_checkout): """Test loan extend action from transaction date.""" mock_ensure_item_is_available_for_checkout.side_effect = None loan = current_circulation.circulation.trigger( loan_created, **dict(params, trigger="checkout")) extension_date = str2datetime(loan["transaction_date"]) with SwappedNestedConfig( ["CIRCULATION_POLICIES", "extension", "from_end_date"], False): loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="extend")) default_extension_duration = timedelta(days=30) end_date_with_ext = extension_date + default_extension_duration assert loan["end_date"] == end_date_with_ext.date().isoformat() assert loan["extension_count"] == 1
def date_fields2datetime(self): """Convert string datetime fields to Python datetime.""" for field in self.DATE_FIELDS + self.DATETIME_FIELDS: if field in self: self[field] = str2datetime(self[field])