def test_loan_utils(client, patron_martigny_no_email, patron2_martigny_no_email, circulation_policies, loan_pending_martigny, item_lib_martigny): """Test loan utils.""" loan = { 'item_pid': item_lib_martigny.pid, 'patron_pid': patron_martigny_no_email.pid } assert can_be_requested(loan) del loan['item_pid'] with pytest.raises(Exception): assert can_be_requested(loan) assert loan_pending_martigny.patron_pid == patron2_martigny_no_email.pid assert not loan_pending_martigny.is_active with pytest.raises(TypeError): assert get_loans_by_patron_pid() assert get_loans_by_patron_pid(patron2_martigny_no_email.pid) with pytest.raises(TypeError): assert get_last_transaction_loc_for_item() assert loan_pending_martigny.organisation_pid new_loan = deepcopy(loan_pending_martigny) assert new_loan.organisation_pid del new_loan['item_pid'] assert not new_loan.organisation_pid
def test_loan_utils(client, patron_martigny_no_email, patron2_martigny_no_email, circulation_policies, loan_pending_martigny, item_lib_martigny, loc_public_martigny): """Test loan utils.""" loan_metadata = dict(item_lib_martigny) if 'item_pid' not in loan_metadata: loan_metadata['item_pid'] = item_lib_martigny.pid if 'patron_pid' not in loan_metadata: loan_metadata['patron_pid'] = patron_martigny_no_email.pid # Create "virtual" Loan (not registered) loan = Loan(loan_metadata) assert can_be_requested(loan) del loan['item_pid'] with pytest.raises(Exception): assert can_be_requested(loan) assert loan_pending_martigny.patron_pid == patron2_martigny_no_email.pid assert not loan_pending_martigny.is_active with pytest.raises(TypeError): assert get_loans_by_patron_pid() assert get_loans_by_patron_pid(patron2_martigny_no_email.pid) with pytest.raises(TypeError): assert get_last_transaction_loc_for_item() assert loan_pending_martigny.organisation_pid new_loan = deepcopy(loan_pending_martigny) assert new_loan.organisation_pid del new_loan['item_pid'] with pytest.raises(IlsRecordError.PidDoesNotExist): new_loan.organisation_pid new_loan = deepcopy(loan_pending_martigny) assert can_be_requested(new_loan) loc_public_martigny['allow_request'] = False loc_public_martigny.update( loc_public_martigny, dbcommit=True, reindex=True ) flush_index(LocationsSearch.Meta.index) assert not can_be_requested(new_loan) loc_public_martigny['allow_request'] = True loc_public_martigny.update( loc_public_martigny, dbcommit=True, reindex=True ) flush_index(LocationsSearch.Meta.index)
def test_item_loans_elements(loan_pending_martigny, item_lib_fully, circ_policy_default_martigny): """Test loan elements.""" assert loan_pending_martigny.item_pid == item_lib_fully.pid loan = list(get_loans_by_patron_pid(loan_pending_martigny.patron_pid))[0] assert loan.pid == loan_pending_martigny.get('pid') new_loan = deepcopy(loan_pending_martigny) del new_loan['transaction_location_pid'] assert get_default_loan_duration(new_loan, None) == \ get_default_loan_duration(loan_pending_martigny, None) assert item_lib_fully.last_location_pid == item_lib_fully.location_pid del circ_policy_default_martigny['checkout_duration'] circ_policy_default_martigny.update(circ_policy_default_martigny, dbcommit=True, reindex=True) today = datetime.now() library = Library.get_record_by_pid(item_lib_fully.library_pid) eve_end_date = today \ + get_default_loan_duration(new_loan, None) \ - timedelta(days=1) end_date = library.next_open(eve_end_date) assert today.strftime('%Y-%m-%d') == end_date.strftime('%Y-%m-%d')
def test_loans_elemnts(loan_pending_martigny, item_lib_fully): """Test loan elements.""" assert loan_pending_martigny.item_pid == item_lib_fully.pid loan = list(get_loans_by_patron_pid(loan_pending_martigny.patron_pid))[0] assert loan.pid == loan_pending_martigny.get('pid') new_loan = deepcopy(loan_pending_martigny) del new_loan['transaction_location_pid'] assert get_default_loan_duration(new_loan) == \ get_default_loan_duration(loan_pending_martigny)
def test_item_loans_elements(loan_pending_martigny, item_lib_fully, circ_policy_default_martigny): """Test loan elements.""" assert loan_pending_martigny.item_pid == item_lib_fully.pid loan = list(get_loans_by_patron_pid(loan_pending_martigny.patron_pid))[0] assert loan.pid == loan_pending_martigny.get('pid') new_loan = deepcopy(loan_pending_martigny) del new_loan['transaction_location_pid'] assert get_default_loan_duration(new_loan, None) == \ get_default_loan_duration(loan_pending_martigny, None) assert item_lib_fully.last_location_pid == item_lib_fully.location_pid circ_policy_default_martigny['allow_checkout'] = False circ_policy_default_martigny.update(circ_policy_default_martigny, dbcommit=True, reindex=True) assert get_default_loan_duration(new_loan, None) == timedelta(0)
def test_loan_utils(client, patron_martigny_no_email, patron2_martigny_no_email, circulation_policies, item_lib_martigny, librarian_martigny_no_email, loc_public_martigny): """Test loan utils methods.""" loan_metadata = dict(item_lib_martigny) loan_metadata['item_pid'] = item_pid_to_object(item_lib_martigny.pid) if 'patron_pid' not in loan_metadata: loan_metadata['patron_pid'] = patron_martigny_no_email.pid # Create "virtual" Loan (not registered) loan = Loan(loan_metadata) # test that loan can successfully move to the pending state assert can_be_requested(loan) # test that loan without an item may not move to the pending state del loan['item_pid'] with pytest.raises(Exception): assert can_be_requested(loan) # test a pending loan will be attached at the right organisation and # will not be considered as an active loan params = { 'patron_pid': patron2_martigny_no_email.pid, 'transaction_location_pid': loc_public_martigny.pid, 'transaction_user_pid': librarian_martigny_no_email.pid, 'pickup_location_pid': loc_public_martigny.pid } item, loan_pending_martigny = item_record_to_a_specific_loan_state( item=item_lib_martigny, loan_state=LoanState.PENDING, params=params, copy_item=True) assert loan_pending_martigny.patron_pid == patron2_martigny_no_email.pid assert not loan_pending_martigny.is_active assert loan_pending_martigny.organisation_pid # test required parameters for get_loans_by_patron_pid with pytest.raises(TypeError): assert get_loans_by_patron_pid() assert get_loans_by_patron_pid(patron2_martigny_no_email.pid) # test required parameters for get_last_transaction_loc_for_item with pytest.raises(TypeError): assert get_last_transaction_loc_for_item() # test the organisation of the loan is based on the item new_loan = deepcopy(loan_pending_martigny) assert new_loan.organisation_pid del new_loan['item_pid'] with pytest.raises(IlsRecordError.PidDoesNotExist): new_loan.organisation_pid assert not can_be_requested(loan_pending_martigny) # test the allow request at the location level loc_public_martigny['allow_request'] = False loc_public_martigny.update( loc_public_martigny, dbcommit=True, reindex=True ) flush_index(LocationsSearch.Meta.index) new_loan = { 'patron_pid': patron_martigny_no_email.pid, 'transaction_location_pid': loc_public_martigny.pid, 'transaction_user_pid': librarian_martigny_no_email.pid, 'pickup_location_pid': loc_public_martigny.pid } with pytest.raises(RecordCannotBeRequestedError): item, loan_pending_martigny = item_record_to_a_specific_loan_state( item=item_lib_martigny, loan_state=LoanState.PENDING, params=params, copy_item=True) loc_public_martigny['allow_request'] = True loc_public_martigny.update( loc_public_martigny, dbcommit=True, reindex=True ) flush_index(LocationsSearch.Meta.index) item, loan_pending_martigny = item_record_to_a_specific_loan_state( item=item_lib_martigny, loan_state=LoanState.PENDING, params=params, copy_item=True) assert loan_pending_martigny['state'] == LoanState.PENDING