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_blocked_patron_cannot_request(client, librarian_martigny_no_email, item_lib_martigny, lib_martigny, patron_martigny_no_email, patron3_martigny_blocked_no_email, circulation_policies): login_user_via_session(client, librarian_martigny_no_email.user) res = client.get( url_for('api_item.can_request', item_pid=item_lib_martigny.pid, library_pid=lib_martigny.pid, patron_barcode=patron3_martigny_blocked_no_email.get( 'patron', {}).get('barcode'))) assert res.status_code == 200 data = get_json(res) assert not data['can'] # Check with valid patron res = client.get( url_for('api_item.can_request', item_pid=item_lib_martigny.pid, library_pid=lib_martigny.pid, patron_barcode=patron_martigny_no_email.get( 'patron', {}).get('barcode'))) assert res.status_code == 200 data = get_json(res) assert data['can'] # Create "virtual" Loan (not registered) loan = Loan({ 'item_pid': item_pid_to_object(item_lib_martigny.pid), 'library_pid': lib_martigny.pid, 'patron_pid': patron3_martigny_blocked_no_email.pid }) assert not can_be_requested(loan)
def test_location_disallow_request(item_lib_martigny, loc_public_martigny, loc_public_martigny_data, lib_martigny, patron_martigny): """Test a request when location disallow request.""" # update location to disallow request location = loc_public_martigny location['allow_request'] = False location.update(location, dbcommit=True, reindex=True) # Create "virtual" Loan (not registered) loan = Loan({ 'item_pid': item_pid_to_object(item_lib_martigny.pid), 'library_pid': lib_martigny.pid, 'patron_pid': patron_martigny.pid }) assert not can_be_requested(loan) # reset the location to original data location.update(loc_public_martigny_data, dbcommit=True, reindex=True)
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