From 90f2fdfc23943852953b7ef2d3566b8435bcd7f5 Mon Sep 17 00:00:00 2001 From: codegeschrei Date: Wed, 22 Aug 2018 10:48:43 +0200 Subject: [PATCH] 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 --- bigchaindb/backend/localmongodb/query.py | 10 -------- bigchaindb/backend/query.py | 14 ----------- bigchaindb/lib.py | 20 --------------- tests/commands/test_commands.py | 2 +- tests/commands/test_utils.py | 2 ++ tests/tendermint/test_lib.py | 32 +++++++++++++++--------- tests/test_core.py | 1 - tests/web/test_assets.py | 6 ++--- tests/web/test_block_tendermint.py | 13 +++++++--- tests/web/test_metadata.py | 6 ++--- 10 files changed, 39 insertions(+), 67 deletions(-) diff --git a/bigchaindb/backend/localmongodb/query.py b/bigchaindb/backend/localmongodb/query.py index b4508477..c85e0854 100644 --- a/bigchaindb/backend/localmongodb/query.py +++ b/bigchaindb/backend/localmongodb/query.py @@ -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') diff --git a/bigchaindb/backend/query.py b/bigchaindb/backend/query.py index 4feb0ea0..b2cef080 100644 --- a/bigchaindb/backend/query.py +++ b/bigchaindb/backend/query.py @@ -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. diff --git a/bigchaindb/lib.py b/bigchaindb/lib.py index a8cc14fa..cbabf06a 100644 --- a/bigchaindb/lib.py +++ b/bigchaindb/lib.py @@ -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 = [] diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index c82a5e29..d834ae6b 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -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 diff --git a/tests/commands/test_utils.py b/tests/commands/test_utils.py index 16484e28..6b00cd30 100644 --- a/tests/commands/test_utils.py +++ b/tests/commands/test_utils.py @@ -10,6 +10,8 @@ import pytest from unittest.mock import patch +pytestmark = pytest.mark.tendermint + @pytest.fixture def reset_bigchaindb_config(monkeypatch): diff --git a/tests/tendermint/test_lib.py b/tests/tendermint/test_lib.py index 37656b7e..cc511b3a 100644 --- a/tests/tendermint/test_lib.py +++ b/tests/tendermint/test_lib.py @@ -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'}], ) diff --git a/tests/test_core.py b/tests/test_core.py index b80a97f5..0e3a2f12 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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') diff --git a/tests/web/test_assets.py b/tests/web/test_assets.py index 13652047..800537ba 100644 --- a/tests/web/test_assets.py +++ b/tests/web/test_assets.py @@ -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') diff --git a/tests/web/test_block_tendermint.py b/tests/web/test_block_tendermint.py index bde82836..a726a117 100644 --- a/tests/web/test_block_tendermint.py +++ b/tests/web/test_block_tendermint.py @@ -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, diff --git a/tests/web/test_metadata.py b/tests/web/test_metadata.py index 7d6ae6c8..aa0a2382 100644 --- a/tests/web/test_metadata.py +++ b/tests/web/test_metadata.py @@ -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')