<commit_before>import pytest
from sakia.data.entities import Transaction
from sakia.data.processors import TransactionsProcessor


@pytest.mark.asyncio
async def test_receive_source(application_with_one_connection, fake_server_with_blockchain, bob, alice):
    amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
    fake_server_with_blockchain.forge.push(alice.send_money(150, fake_server_with_blockchain.forge.user_identities[alice.key.pubkey].sources, bob,
                                            fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
    changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks)
    await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud)
    assert amount + 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
    await fake_server_with_blockchain.close()


@pytest.mark.asyncio
async def test_send_source(application_with_one_connection, fake_server_with_blockchain, bob, alice):
    amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
    fake_server_with_blockchain.forge.push(bob.send_money(150, fake_server_with_blockchain.forge.user_identities[bob.key.pubkey].sources, alice,
                                            fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
    changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks)
    await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud)
    assert amount - 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
    await fake_server_with_blockchain.close()


@pytest.mark.asyncio
async def test_destruction(application_with_one_connection, fake_server_with_blockchain, bob, alice):
    amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
    fake_server_with_blockchain.forge.push(bob.send_money(amount - 80, fake_server_with_blockchain.forge.user_identities[bob.key.pubkey].sources, alice,
                                            fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
    changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks)
    await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud)
    assert 0 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
    tx_after_parse = application_with_one_connection.transactions_service.transfers(bob.key.pubkey)
    assert tx_after_parse[-1].comment == "Too low balance"
    await fake_server_with_blockchain.close()


@pytest.mark.asyncio
async def test_send_tx_then_cancel(application_with_one_connection, fake_server_with_blockchain, bob, alice):
    tx_before_send = application_with_one_connection.transactions_service.transfers(bob.key.pubkey)
    sources_before_send = application_with_one_connection.sources_service.amount(bob.key.pubkey)
    bob_connection = application_with_one_connection.db.connections_repo.get_one(pubkey=bob.key.pubkey)
    fake_server_with_blockchain.reject_next_post = True
    await application_with_one_connection.documents_service.send_money(bob_connection,
                                                                       bob.salt,
                                                                       <PASSWORD>.password,
                                                                       alice.key.pubkey, 10, 0, "Test comment")
    tx_after_send = application_with_one_connection.transactions_service.transfers(bob.key.pubkey)
    sources_after_send = application_with_one_connection.sources_service.amount(bob.key.pubkey)<commit_msg>Fix test sources_service<commit_after>import pytest
from sakia.data.entities import Transaction
from sakia.data.processors import TransactionsProcessor


@pytest.mark.asyncio
async def test_receive_source(application_with_one_connection, fake_server_with_blockchain, bob, alice):
    amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
    fake_server_with_blockchain.forge.push(alice.send_money(150, fake_server_with_blockchain.forge.user_identities[alice.key.pubkey].sources, bob,
                                            fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
    changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks)
    await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud)
    assert amount + 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
    await fake_server_with_blockchain.close()


@pytest.mark.asyncio
async def test_send_source(application_with_one_connection, fake_server_with_blockchain, bob, alice):
    amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
    fake_server_with_blockchain.forge.push(bob.send_money(150, fake_server_with_blockchain.forge.user_identities[bob.key.pubkey].sources, alice,
                                            fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
    changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks)
    await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud)
    assert amount - 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
    await fake_server_with_blockchain.close()


@pytest.mark.asyncio
async def test_destruction(application_with_one_connection, fake_server_with_blockchain, bob, alice):
    amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
    fake_server_with_blockchain.forge.push(bob.send_money(amount - 80, fake_server_with_blockchain.forge.user_identities[bob.key.pubkey].sources, alice,
                                            fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    fake_server_with_blockchain.forge.forge_block()
    new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
    changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks)
    await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud)
    assert 0 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
    tx_after_parse = application_with_one_connection.transactions_service.transfers(bob.key.pubkey)
    assert "Too low balance" in [t.comment for t in tx_after_parse]
    await fake_server_with_blockchain.close()


@pytest.mark.asyncio
async def test_send_tx_then_cancel(application_with_one_connection, fake_server_with_blockchain, bob, alice):
    tx_before_send = application_with_one_connection.transactions_service.transfers(bob.key.pubkey)
    sources_before_send = application_with_one_connection.sources_service.amount(bob.key.pubkey)
    bob_connection = application_with_one_connection.db.connections_repo.get_one(pubkey=bob.key.pubkey)
    fake_server_with_blockchain.reject_next_post = True
    await application_with_one_connection.documents_service.send_money(bob_connection,
                                                                       bob.salt,
                                                                       bob.password,
                                                                       alice.key.pubkey, 10, 0, "Test comment")
    tx_after_send = application_with_one_connection.transactions_service.transfers(bob.key.pubkey)
    sources_after_send = application_with_one_connection.sources_service.amount(bob.key.pubkey)
{'fraction_non_alphanumeric': 0.07567507500833426, 'fraction_numerical': 0.005445049449938882, 'mean_word_length': 4.415162454873646, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3734123', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2649, 'n_words': 300}