Problem: several tests are skipped (#2452)

* Problem: several tests are skipped
Solution: activate or remove tests

* Problem: store_transaction is deprecated
Solution: replace it with store_bulk_transaction

* Problem: we don't test the transaction split
Solution: undelete `test_store_transaction`

* Problem: failing tests
Solution: merge master and change deprecated store_transaction
This commit is contained in:
codegeschrei 2018-08-22 10:48:43 +02:00 committed by Zachary Bowen
parent 55a9151c14
commit 90f2fdfc23
10 changed files with 39 additions and 67 deletions

View File

@ -15,16 +15,6 @@ from bigchaindb.common.transaction import Transaction
register_query = module_dispatch_registrar(backend.query)
@register_query(LocalMongoDBConnection)
def store_transaction(conn, signed_transaction):
try:
return conn.run(
conn.collection('transactions')
.insert_one(signed_transaction))
except DuplicateKeyError:
pass
@register_query(LocalMongoDBConnection)
def store_transactions(conn, signed_transactions):
return conn.run(conn.collection('transactions')

View File

@ -12,20 +12,6 @@ VALIDATOR_UPDATE_ID = 'a_unique_id_string'
PRE_COMMIT_ID = 'a_unique_id_string'
@singledispatch
def store_transaction(connection, signed_transaction):
"""Write a signed transaction to the 'transactions' table.
Args:
signed_transaction (dict): a signed transaction.
Returns:
The result of the operation.
"""
raise NotImplementedError
@singledispatch
def store_asset(connection, asset):
"""Write an asset to the asset table.

View File

@ -8,7 +8,6 @@ MongoDB.
"""
import logging
from collections import namedtuple
from copy import deepcopy
from uuid import uuid4
try:
@ -119,25 +118,6 @@ class BigchainDB(object):
def process_status_code(self, status_code, failure_msg):
return (202, '') if status_code == 0 else (500, failure_msg)
def store_transaction(self, transaction):
"""Store a valid transaction to the transactions collection."""
# self.update_utxoset(transaction)
transaction = deepcopy(transaction.to_dict())
if transaction['operation'] == 'CREATE':
asset = transaction.pop('asset')
asset['id'] = transaction['id']
if asset['data']:
backend.query.store_asset(self.connection, asset)
metadata = transaction.pop('metadata')
transaction_metadata = {'id': transaction['id'],
'metadata': metadata}
backend.query.store_metadatas(self.connection, [transaction_metadata])
return backend.query.store_transaction(self.connection, transaction)
def store_bulk_transactions(self, transactions):
txns = []
assets = []

View File

@ -396,7 +396,7 @@ def test_upsert_validator_new_without_tendermint(b, priv_validator_path, user_sk
]
def mock_write(tx, mode):
b.store_transaction(tx)
b.store_bulk_transactions([tx])
return (202, '')
b.get_validators = mock_get

View File

@ -10,6 +10,8 @@ import pytest
from unittest.mock import patch
pytestmark = pytest.mark.tendermint
@pytest.fixture
def reset_bigchaindb_config(monkeypatch):

View File

@ -22,6 +22,7 @@ pytestmark = pytest.mark.tendermint
@pytest.mark.bdb
def test_asset_is_separated_from_transaciton(b):
import copy
from bigchaindb.models import Transaction
from bigchaindb.common.crypto import generate_key_pair
@ -43,10 +44,16 @@ def test_asset_is_separated_from_transaciton(b):
asset=asset)\
.sign([alice.private_key])
b.store_transaction(tx)
# with store_bulk_transactions we use `insert_many` where PyMongo
# automatically adds an `_id` field to the tx, therefore we need the
# deepcopy, for more info see:
# https://api.mongodb.com/python/current/faq.html#writes-and-ids
tx_dict = copy.deepcopy(tx.to_dict())
b.store_bulk_transactions([tx])
assert 'asset' not in backend.query.get_transaction(b.connection, tx.id)
assert backend.query.get_asset(b.connection, tx.id)['data'] == asset
assert b.get_transaction(tx.id) == tx
assert b.get_transaction(tx.id).to_dict() == tx_dict
@pytest.mark.bdb
@ -183,21 +190,22 @@ def test_update_utxoset(tb, signed_create_tx, signed_transfer_tx, db_context):
@pytest.mark.bdb
def test_store_transaction(mocker, tb, signed_create_tx,
signed_transfer_tx, db_context):
mocked_store_asset = mocker.patch('bigchaindb.backend.query.store_asset')
mocked_store_asset = mocker.patch('bigchaindb.backend.query.store_assets')
mocked_store_metadata = mocker.patch(
'bigchaindb.backend.query.store_metadatas')
mocked_store_transaction = mocker.patch(
'bigchaindb.backend.query.store_transaction')
tb.store_transaction(signed_create_tx)
'bigchaindb.backend.query.store_transactions')
tb.store_bulk_transactions([signed_create_tx])
# mongo_client = MongoClient(host=db_context.host, port=db_context.port)
# utxoset = mongo_client[db_context.name]['utxos']
# assert utxoset.count() == 1
# utxo = utxoset.find_one()
# assert utxo['transaction_id'] == signed_create_tx.id
# assert utxo['output_index'] == 0
mocked_store_asset.assert_called_once_with(
tb.connection,
{'id': signed_create_tx.id, 'data': signed_create_tx.asset['data']},
[{'id': signed_create_tx.id, 'data': signed_create_tx.asset['data']}],
)
mocked_store_metadata.assert_called_once_with(
tb.connection,
@ -205,13 +213,13 @@ def test_store_transaction(mocker, tb, signed_create_tx,
)
mocked_store_transaction.assert_called_once_with(
tb.connection,
{k: v for k, v in signed_create_tx.to_dict().items()
if k not in ('asset', 'metadata')},
[{k: v for k, v in signed_create_tx.to_dict().items()
if k not in ('asset', 'metadata')}],
)
mocked_store_asset.reset_mock()
mocked_store_metadata.reset_mock()
mocked_store_transaction.reset_mock()
tb.store_transaction(signed_transfer_tx)
tb.store_bulk_transactions([signed_transfer_tx])
# assert utxoset.count() == 1
# utxo = utxoset.find_one()
# assert utxo['transaction_id'] == signed_transfer_tx.id
@ -219,12 +227,12 @@ def test_store_transaction(mocker, tb, signed_create_tx,
assert not mocked_store_asset.called
mocked_store_metadata.asser_called_once_with(
tb.connection,
{'id': signed_transfer_tx.id, 'metadata': signed_transfer_tx.metadata},
[{'id': signed_transfer_tx.id, 'metadata': signed_transfer_tx.metadata}],
)
mocked_store_transaction.assert_called_once_with(
tb.connection,
{k: v for k, v in signed_transfer_tx.to_dict().items()
if k != 'metadata'},
[{k: v for k, v in signed_transfer_tx.to_dict().items()
if k != 'metadata'}],
)

View File

@ -7,7 +7,6 @@ import pytest
pytestmark = pytest.mark.tendermint
@pytest.mark.skipif(reason='will be fixed in another PR')
@pytest.fixture
def config(request, monkeypatch):
backend = request.config.getoption('--database-backend')

View File

@ -37,7 +37,7 @@ def test_get_assets_tendermint(client, tb, alice):
tx = Transaction.create([alice.public_key], [([alice.public_key], 1)],
asset=asset).sign([alice.private_key])
tb.store_transaction(tx)
tb.store_bulk_transactions([tx])
# test that asset is returned
res = client.get(ASSETS_ENDPOINT + '?search=abc')
@ -64,8 +64,8 @@ def test_get_assets_limit_tendermint(client, tb, alice):
tx2 = Transaction.create([alice.public_key], [([alice.public_key], 1)],
asset=asset2).sign([alice.private_key])
b.store_transaction(tx1)
b.store_transaction(tx2)
b.store_bulk_transactions([tx1])
b.store_bulk_transactions([tx2])
# test that both assets are returned without limit
res = client.get(ASSETS_ENDPOINT + '?search=abc')

View File

@ -15,10 +15,17 @@ pytestmark = pytest.mark.tendermint
@pytest.mark.bdb
@pytest.mark.usefixtures('inputs')
def test_get_block_endpoint(tb, client, alice):
import copy
b = tb
tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], asset={'cycle': 'hero'})
tx = tx.sign([alice.private_key])
b.store_transaction(tx)
# with store_bulk_transactions we use `insert_many` where PyMongo
# automatically adds an `_id` field to the tx, therefore we need the
# deepcopy, for more info see:
# https://api.mongodb.com/python/current/faq.html#writes-and-ids
tx_dict = copy.deepcopy(tx.to_dict())
b.store_bulk_transactions([tx])
block = Block(app_hash='random_utxo',
height=31,
@ -26,7 +33,7 @@ def test_get_block_endpoint(tb, client, alice):
b.store_block(block._asdict())
res = client.get(BLOCKS_ENDPOINT + str(block.height))
expected_response = {'height': block.height, 'transactions': [tx.to_dict()]}
expected_response = {'height': block.height, 'transactions': [tx_dict]}
assert res.json == expected_response
assert res.status_code == 200
@ -46,7 +53,7 @@ def test_get_block_containing_transaction(tb, client, alice):
b = tb
tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], asset={'cycle': 'hero'})
tx = tx.sign([alice.private_key])
b.store_transaction(tx)
b.store_bulk_transactions([tx])
block = Block(app_hash='random_utxo',
height=13,

View File

@ -39,7 +39,7 @@ def test_get_metadata_tendermint(client, tb, alice):
tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=metadata,
asset=asset).sign([alice.private_key])
b.store_transaction(tx)
b.store_bulk_transactions([tx])
# test that metadata is returned
res = client.get(METADATA_ENDPOINT + '?search=my_meta')
@ -63,13 +63,13 @@ def test_get_metadata_limit_tendermint(client, tb, alice):
meta1 = {'key': 'meta 1'}
tx1 = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=meta1,
asset=asset1).sign([alice.private_key])
b.store_transaction(tx1)
b.store_bulk_transactions([tx1])
asset2 = {'msg': 'abc 2'}
meta2 = {'key': 'meta 2'}
tx2 = Transaction.create([alice.public_key], [([alice.public_key], 1)], metadata=meta2,
asset=asset2).sign([alice.private_key])
b.store_transaction(tx2)
b.store_bulk_transactions([tx2])
# test that both assets are returned without limit
res = client.get(METADATA_ENDPOINT + '?search=meta')