Пример #1
0
def acq_account_general_aproz(app, lib_saxon, acq_account_general_aproz_data,
                              budget_2020_sion):
    """Load acq_account lib aproz general record."""
    acac = AcqAccount.create(data=acq_account_general_aproz_data,
                             delete_pid=False,
                             dbcommit=True,
                             reindex=True)
    flush_index(AcqAccountsSearch.Meta.index)
    return acac
Пример #2
0
def acq_account_general_fully(app, lib_fully, acq_account_general_fully_data,
                              budget_2020_martigny):
    """Load acq_account lib fully general record."""
    acac = AcqAccount.create(data=acq_account_general_fully_data,
                             delete_pid=False,
                             dbcommit=True,
                             reindex=True)
    flush_index(AcqAccountsSearch.Meta.index)
    return acac
Пример #3
0
def acq_account_books_saxon(app, lib_saxon, acq_account_books_saxon_data,
                            budget_2020_martigny):
    """Load acq_account lib saxon books record."""
    acac = AcqAccount.create(data=acq_account_books_saxon_data,
                             delete_pid=False,
                             dbcommit=True,
                             reindex=True)
    flush_index(AcqAccountsSearch.Meta.index)
    return acac
Пример #4
0
def test_acq_accounts_es_mapping(es, db, acq_account_fiction_martigny_data,
                                 budget_2020_martigny, lib_martigny):
    """Test acquisition account elasticsearch mapping."""
    search = AcqAccountsSearch()
    mapping = get_mapping(search.Meta.index)
    assert mapping
    account = AcqAccount.create(acq_account_fiction_martigny_data,
                                dbcommit=True,
                                reindex=True,
                                delete_pid=True)
    assert mapping == get_mapping(search.Meta.index)
    account.delete(force=True, dbcommit=True, delindex=True)
def test_transfer_funds_api(client, rero_json_header, org_martigny,
                            lib_martigny, budget_2020_martigny,
                            librarian_martigny):
    """Scenario to test fund transfer between both accounts."""
    def _check_account(account):
        """Check amount available about an account."""
        return account['allocated_amount'], account.remaining_balance[0]

    login_user_via_session(client, librarian_martigny.user)

    # STEP 0 :: Create account tree
    #   Test structure account is described below. Each account are noted like
    #   A{x, y} where :
    #     * 'A' is the account name
    #     * 'x' is the account allocated amount
    #     * 'y' is the account remaining_balance
    #
    #   A{2000, 500}                E{200, 100}
    #   |-- B{500, 150}             +-- F{200, 100}
    #   |   |-- B1{300, 300}            +-- G{100, 100}
    #   |   +-- B2{50, 50}
    #   +-- C{1000, 700}
    #       |-- C1{100, 100}
    #       |-- C2{100, 30}
    #       |   |-- C21{50, 50}
    #       |   +-- C22{20, 20}
    #       +-- C3{100, 100}
    basic_data = {
        'allocated_amount': 1000,
        'budget': {
            '$ref': get_ref_for_pid('budg', budget_2020_martigny.pid)
        },
        'library': {
            '$ref': get_ref_for_pid('lib', lib_martigny.pid)
        }
    }
    account_a = dict(name='A', allocated_amount=2000)
    account_a = {**basic_data, **account_a}
    account_a = _make_resource(client, 'acac', account_a)
    a_ref = {'$ref': get_ref_for_pid('acac', account_a.pid)}

    account_b = dict(name='B', allocated_amount=500, parent=a_ref)
    account_b = {**basic_data, **account_b}
    account_b = _make_resource(client, 'acac', account_b)
    b_ref = {'$ref': get_ref_for_pid('acac', account_b.pid)}

    account_c = dict(name='C', allocated_amount=1000, parent=a_ref)
    account_c = {**basic_data, **account_c}
    account_c = _make_resource(client, 'acac', account_c)
    c_ref = {'$ref': get_ref_for_pid('acac', account_c.pid)}

    account_b1 = dict(name='B1', allocated_amount=300, parent=b_ref)
    account_b1 = {**basic_data, **account_b1}
    account_b1 = _make_resource(client, 'acac', account_b1)
    account_b2 = dict(name='B2', allocated_amount=50, parent=b_ref)
    account_b2 = {**basic_data, **account_b2}
    account_b2 = _make_resource(client, 'acac', account_b2)

    account_c1 = dict(name='C1', allocated_amount=100, parent=c_ref)
    account_c1 = {**basic_data, **account_c1}
    account_c1 = _make_resource(client, 'acac', account_c1)
    account_c2 = dict(name='C2', allocated_amount=100, parent=c_ref)
    account_c2 = {**basic_data, **account_c2}
    account_c2 = _make_resource(client, 'acac', account_c2)
    account_c3 = dict(name='C3', allocated_amount=100, parent=c_ref)
    account_c3 = {**basic_data, **account_c3}
    account_c3 = _make_resource(client, 'acac', account_c3)
    c2_ref = {'$ref': get_ref_for_pid('acac', account_c2.pid)}

    account_c21 = dict(name='C21', allocated_amount=50, parent=c2_ref)
    account_c21 = {**basic_data, **account_c21}
    account_c21 = _make_resource(client, 'acac', account_c21)
    account_c22 = dict(name='C22', allocated_amount=20, parent=c2_ref)
    account_c22 = {**basic_data, **account_c22}
    account_c22 = _make_resource(client, 'acac', account_c22)

    account_e = dict(name='E', allocated_amount=300)
    account_e = {**basic_data, **account_e}
    account_e = _make_resource(client, 'acac', account_e)
    e_ref = {'$ref': get_ref_for_pid('acac', account_e.pid)}

    account_f = dict(name='F', allocated_amount=200, parent=e_ref)
    account_f = {**basic_data, **account_f}
    account_f = _make_resource(client, 'acac', account_f)
    f_ref = {'$ref': get_ref_for_pid('acac', account_f.pid)}

    account_g = dict(name='G', allocated_amount=100, parent=f_ref)
    account_g = {**basic_data, **account_g}
    account_g = _make_resource(client, 'acac', account_g)

    # TEST 0 :: Try the API with invalid arguments.
    res = client.get(url_for('api_acq_account.transfer_funds'))
    assert res.status_code == 400
    assert 'argument is required' in res.get_data(as_text=True)
    cases_to_test = [{
        'source': 'dummy',
        'target': 'dummy',
        'amount': 'dummy',
        'error': 'Unable to load source account'
    }, {
        'source': account_a.pid,
        'target': 'dummy',
        'amount': 'dummy',
        'error': 'Unable to load target account'
    }, {
        'source': account_a.pid,
        'target': account_b.pid,
        'amount': 'dummy',
        'error': "could not convert"
    }, {
        'source': account_a.pid,
        'target': account_b.pid,
        'amount': -1.52,
        'error': "'amount' should be a positive number"
    }, {
        'source': account_a.pid,
        'target': account_a.pid,
        'amount': 1,
        'error': "Cannot transfer fund to myself"
    }, {
        'source': account_a.pid,
        'target': account_e.pid,
        'amount': 100000,
        'error': "Not enough available money from source account"
    }]
    for case in cases_to_test:
        res = client.get(
            url_for('api_acq_account.transfer_funds',
                    source=case['source'],
                    target=case['target'],
                    amount=case['amount']))
        assert res.status_code == 400
        data = get_json(res)
        assert case['error'] in data['message']

    # STATUS BEFORE NEXT TEST
    #   A{2000, 500}                E{300, 100}
    #   |-- B{500, 150}             +-- F{200, 100}
    #   |   |-- B1{300, 300}            +-- G{100, 100}
    #   |   +-- B2{50, 50}
    #   +-- C{1000, 700}
    #       |-- C1{100, 100}
    #       |-- C2{100, 30}
    #       |   |-- C21{50, 50}
    #       |   +-- C22{20, 20}
    #       +-- C3{100, 100}

    # TEST 1 :: Transfer to an ancestor account
    #   Transfer 25 from C21 account to C account. After this transfer, the
    #   C20 remaining balance should be equal to 25 ; the remaining balance for
    #   C account should be 725
    res = client.get(
        url_for('api_acq_account.transfer_funds',
                source=account_c21.pid,
                target=account_c.pid,
                amount=25))
    assert res.status_code == 200
    account_c21 = AcqAccount.get_record_by_pid(account_c21.pid)
    account_c2 = AcqAccount.get_record_by_pid(account_c2.pid)
    account_c = AcqAccount.get_record_by_pid(account_c.pid)
    assert _check_account(account_c) == (1000, 725)
    assert _check_account(account_c2) == (75, 30)
    assert _check_account(account_c21) == (25, 25)

    # STATUS BEFORE NEXT TEST
    #   A{2000, 500}                E{300, 100}
    #   |-- B{500, 150}             +-- F{200, 100}
    #   |   |-- B1{300, 300}            +-- G{100, 100}
    #   |   +-- B2{50, 50}
    #   +-- C{1000, 725}
    #       |-- C1{100, 100}
    #       |-- C2{75, 30}
    #       |   |-- C21{25, 25}
    #       |   +-- C22{20, 20}
    #       +-- C3{100, 100}

    # TEST 2 :: Transfer between accounts in the same tree
    #   Transfer 100 from A account to C22 account. After this transfer, the
    #   C22 remaining balance should be equal to 120 ; the remaining balance
    #   for A account should be 400. The remaining balance for intermediate
    #   accounts (C, C2) should be the same, but allocated amount should be
    #   increased by 100 (1100, 175)
    res = client.get(
        url_for('api_acq_account.transfer_funds',
                source=account_a.pid,
                target=account_c22.pid,
                amount=100))
    assert res.status_code == 200
    account_a = AcqAccount.get_record_by_pid(account_a.pid)
    account_c = AcqAccount.get_record_by_pid(account_c.pid)
    account_c2 = AcqAccount.get_record_by_pid(account_c2.pid)
    account_c22 = AcqAccount.get_record_by_pid(account_c22.pid)
    assert _check_account(account_a) == (2000, 400)
    assert _check_account(account_c) == (1100, 725)
    assert _check_account(account_c2) == (175, 30)
    assert _check_account(account_c22) == (120, 120)

    # STATUS BEFORE NEXT TEST
    #   A{2000, 400}                E{300, 100}
    #   |-- B{500, 150}             +-- F{200, 100}
    #   |   |-- B1{300, 300}            +-- G{100, 100}
    #   |   +-- B2{50, 50}
    #   +-- C{1100, 725}
    #       |-- C1{100, 100}
    #       |-- C2{175, 30}
    #       |   |-- C21{25, 25}
    #       |   +-- C22{120, 120}
    #       +-- C3{100, 100}

    # TEST 3 :: Transfer 300 from B1 account to C21 account.
    #   Same behavior than previous test, but source account isn't the common
    #   ancestor.
    res = client.get(
        url_for('api_acq_account.transfer_funds',
                source=account_b1.pid,
                target=account_c21.pid,
                amount=300))
    assert res.status_code == 200
    account_b1 = AcqAccount.get_record_by_pid(account_b1.pid)
    account_b = AcqAccount.get_record_by_pid(account_b.pid)
    account_a = AcqAccount.get_record_by_pid(account_a.pid)
    account_c = AcqAccount.get_record_by_pid(account_c.pid)
    account_c2 = AcqAccount.get_record_by_pid(account_c2.pid)
    account_c21 = AcqAccount.get_record_by_pid(account_c21.pid)
    assert _check_account(account_b1) == (0, 0)
    assert _check_account(account_b) == (200, 150)
    assert _check_account(account_a) == (2000, 400)
    assert _check_account(account_c) == (1400, 725)
    assert _check_account(account_c2) == (475, 30)
    assert _check_account(account_c21) == (325, 325)

    # STATUS BEFORE NEXT TEST
    #   A{2000, 400}                E{300, 100}
    #   |-- B{200, 150}             +-- F{200, 100}
    #   |   |-- B1{0, 0}                +-- G{100, 100}
    #   |   +-- B2{50, 50}
    #   +-- C{1400, 725}
    #       |-- C1{100, 100}
    #       |-- C2{475, 30}
    #       |   |-- C21{325, 325}
    #       |   +-- C22{120, 120}
    #       +-- C3{100, 100}

    # TEST 4 :: Transfer between two account from separate tree.
    #   We transfer 100 from F account to C3 account. As both accounts aren't
    #   in the same tree, they not exists a common ancestor. Each root tag
    #   should be update (E will decrease, A will increase)
    res = client.get(
        url_for('api_acq_account.transfer_funds',
                source=account_f.pid,
                target=account_c3.pid,
                amount=100))
    assert res.status_code == 200
    account_f = AcqAccount.get_record_by_pid(account_f.pid)
    account_e = AcqAccount.get_record_by_pid(account_e.pid)
    account_a = AcqAccount.get_record_by_pid(account_a.pid)
    account_c = AcqAccount.get_record_by_pid(account_c.pid)
    account_c3 = AcqAccount.get_record_by_pid(account_c3.pid)
    assert _check_account(account_f) == (100, 0)
    assert _check_account(account_e) == (200, 100)
    assert _check_account(account_a) == (2100, 400)
    assert _check_account(account_c) == (1500, 725)
    assert _check_account(account_c3) == (200, 200)

    # STATUS BEFORE NEXT TEST
    #   A{2100, 400}                E{200, 100}
    #   |-- B{200, 150}             +-- F{100, 0}
    #   |   |-- B1{0, 0}                +-- G{100, 100}
    #   |   +-- B2{50, 50}
    #   +-- C{1500, 725}
    #       |-- C1{100, 100}
    #       |-- C2{475, 30}
    #       |   |-- C21{325, 325}
    #       |   +-- C22{120, 120}
    #       +-- C3{200, 200}

    # delete accounts
    _del_resource(client, 'acac', account_g.pid)
    _del_resource(client, 'acac', account_f.pid)
    _del_resource(client, 'acac', account_e.pid)

    _del_resource(client, 'acac', account_c22.pid)
    _del_resource(client, 'acac', account_c21.pid)
    _del_resource(client, 'acac', account_c3.pid)
    _del_resource(client, 'acac', account_c2.pid)
    _del_resource(client, 'acac', account_c1.pid)
    _del_resource(client, 'acac', account_c.pid)
    _del_resource(client, 'acac', account_b2.pid)
    _del_resource(client, 'acac', account_b1.pid)
    _del_resource(client, 'acac', account_b.pid)
    _del_resource(client, 'acac', account_a.pid)