Exemple #1
0
def test_patron_transaction_create(db, es_clear,
                                   patron_transaction_overdue_martigny,
                                   org_martigny):
    """Test patron transaction creation."""
    patron_transaction = deepcopy(patron_transaction_overdue_martigny)
    patron_transaction['status'] = 'no_status'
    import jsonschema
    with pytest.raises(jsonschema.exceptions.ValidationError):
        PatronTransaction.create(patron_transaction, delete_pid=True)
    db.session.rollback()

    next_pid = PatronTransaction.provider.identifier.next()
    patron_transaction['status'] = 'open'
    record = PatronTransaction.create(patron_transaction, delete_pid=True)
    next_pid += 1
    assert record == patron_transaction
    assert record.get('pid') == str(next_pid)

    pttr = PatronTransaction.get_record_by_pid(str(next_pid))
    assert pttr == patron_transaction

    fetched_pid = fetcher(pttr.id, pttr)
    assert fetched_pid.pid_value == str(next_pid)
    assert fetched_pid.pid_type == 'pttr'

    can, reasons = patron_transaction_overdue_martigny.can_delete
    assert not can
    assert reasons['links']['events']

    assert patron_transaction_overdue_martigny.currency == \
        org_martigny.get('default_currency')
def test_patron_transaction_es_mapping(es, db,
                                       patron_transaction_overdue_martigny):
    """Test patron_transaction elasticsearch mapping."""
    search = PatronTransactionsSearch()
    mapping = get_mapping(search.Meta.index)
    assert mapping
    PatronTransaction.create(patron_transaction_overdue_martigny,
                             dbcommit=True,
                             reindex=True,
                             delete_pid=True)
    assert mapping == get_mapping(search.Meta.index)
def test_patron_transaction_create(db, es_clear,
                                   patron_transaction_overdue_martigny):
    """Test patron transaction creation."""
    patron_transaction = deepcopy(patron_transaction_overdue_martigny)
    patron_transaction['status'] = 'no_status'
    import jsonschema
    with pytest.raises(jsonschema.exceptions.ValidationError):
        record = PatronTransaction.create(patron_transaction, delete_pid=True)

    db.session.rollback()

    patron_transaction['status'] = 'open'
    record = PatronTransaction.create(patron_transaction, delete_pid=True)
    assert record == patron_transaction
    assert record.get('pid') == '2'

    pttr = PatronTransaction.get_record_by_pid('2')
    assert pttr == patron_transaction

    fetched_pid = fetcher(pttr.id, pttr)
    assert fetched_pid.pid_value == '2'
    assert fetched_pid.pid_type == 'pttr'
def test_holding_requests(client, patron_martigny, loc_public_martigny,
                          circulation_policies, librarian_martigny,
                          holding_lib_martigny_w_patterns, lib_martigny,
                          item_lib_martigny, org_martigny):
    """Test holding patron request."""
    login_user_via_session(client, patron_martigny.user)
    holding = holding_lib_martigny_w_patterns
    description = 'Year: 2000 / volume: 15 / number: 22 / pages: 11-12'
    # test fails when there is a missing description or holding_pid
    res, data = postdata(
        client, 'api_holding.patron_request',
        dict(holding_pid=holding.pid,
             pickup_location_pid=loc_public_martigny.pid))
    assert res.status_code == 400
    res, data = postdata(
        client, 'api_holding.patron_request',
        dict(description=description,
             pickup_location_pid=loc_public_martigny.pid))
    assert res.status_code == 404
    # test passes when all required parameters are given
    res, data = postdata(
        client, 'api_holding.patron_request',
        dict(holding_pid=holding.pid,
             pickup_location_pid=loc_public_martigny.pid,
             description=description))
    assert res.status_code == 200
    loan = Loan.get_record_by_pid(
        data.get('action_applied')[LoanAction.REQUEST].get('pid'))
    assert loan.state == LoanState.PENDING
    item = Item.get_record_by_pid(loan.item_pid)
    assert item.get('type') == TypeOfItem.PROVISIONAL
    assert item.status == ItemStatus.ON_SHELF
    assert item.holding_pid == holding.pid
    assert item.get('enumerationAndChronology') == description
    # checkout the item to the requested patron
    login_user_via_session(client, librarian_martigny.user)
    res, data = postdata(
        client, 'api_item.checkout',
        dict(
            item_pid=item.pid,
            patron_pid=patron_martigny.pid,
            transaction_location_pid=loc_public_martigny.pid,
            transaction_user_pid=librarian_martigny.pid,
        ))
    assert res.status_code == 200
    loan_pid = data.get('action_applied')[LoanAction.CHECKOUT].get('pid')
    assert loan_pid == loan.pid
    item = Item.get_record_by_pid(item.pid)
    assert item.status == ItemStatus.ON_LOAN
    # return the item at the owning library
    res, data = postdata(
        client, 'api_item.checkin',
        dict(item_pid=item.pid,
             transaction_location_pid=loc_public_martigny.pid,
             transaction_user_pid=librarian_martigny.pid))
    assert res.status_code == 200
    item = Item.get_record_by_pid(item.pid)
    assert item.status == ItemStatus.ON_SHELF

    # test requests made by a librarian
    # test fails when there are missing parameters
    res, data = postdata(
        client, 'api_holding.librarian_request',
        dict(holding_pid=holding.pid,
             pickup_location_pid=loc_public_martigny.pid,
             description=description,
             transaction_library_pid=lib_martigny.pid,
             transaction_user_pid=librarian_martigny.pid))
    assert res.status_code == 400
    res, data = postdata(
        client, 'api_holding.librarian_request',
        dict(holding_pid=holding.pid,
             pickup_location_pid=loc_public_martigny.pid,
             description=description,
             patron_pid=patron_martigny.pid,
             transaction_library_pid=lib_martigny.pid))
    assert res.status_code == 400
    res, data = postdata(
        client, 'api_holding.librarian_request',
        dict(holding_pid=holding.pid,
             pickup_location_pid=loc_public_martigny.pid,
             patron_pid=patron_martigny.pid,
             transaction_library_pid=lib_martigny.pid,
             transaction_user_pid=librarian_martigny.pid))
    assert res.status_code == 400
    # test passes when all required parameters are given
    res, data = postdata(
        client, 'api_holding.librarian_request',
        dict(holding_pid=holding.pid,
             pickup_location_pid=loc_public_martigny.pid,
             description=description,
             patron_pid=patron_martigny.pid,
             transaction_library_pid=lib_martigny.pid,
             transaction_user_pid=librarian_martigny.pid))
    assert res.status_code == 200
    loan_2 = Loan.get_record_by_pid(
        data.get('action_applied')[LoanAction.REQUEST].get('pid'))
    assert loan_2.state == LoanState.PENDING
    item_2 = Item.get_record_by_pid(loan_2.item_pid)
    assert item_2.get('type') == TypeOfItem.PROVISIONAL
    assert item_2.status == ItemStatus.ON_SHELF
    assert item_2.holding_pid == holding.pid
    assert item_2.get('enumerationAndChronology') == description
    assert item_2.pid != item.pid

    all_item_pids = [pid for pid in Item.get_all_pids()]
    assert all_item_pids

    # test delete provisional items with no active fees/loans
    report = delete_provisional_items()
    assert report.get('numner_of_deleted_items')
    assert report.get('number_of_candidate_items_to_delete')
    # assert that not deleted items are either having loans/fees or not
    # provisional items
    left_item_pids = [pid for pid in Item.get_all_pids()]
    assert left_item_pids
    for pid in left_item_pids:
        record = Item.get_record_by_pid(pid)
        can, _ = record.can_delete
        assert not can or record.get('type') != TypeOfItem.PROVISIONAL
    # item_2 has pending loans then it should not be removed
    assert item_2.pid in left_item_pids
    assert item_2.pid in get_provisional_items_pids_candidate_to_delete()
    # add fee to item_2 and make sure it will not be candidate at the deletion.
    data = {
        'loan': {
            '$ref': get_ref_for_pid('loanid', loan_2.pid)
        },
        'patron': {
            '$ref': get_ref_for_pid('patrons', patron_martigny.pid)
        },
        'organisation': {
            '$ref': get_ref_for_pid('org', org_martigny.pid)
        },
        'status': 'open',
        'total_amount': 0.6,
        'type': 'overdue',
        'creation_date': datetime.now(timezone.utc).isoformat()
    }
    PatronTransaction.create(data, dbcommit=True, reindex=True)
    assert item_2.pid not in get_provisional_items_pids_candidate_to_delete()