2018-08-16 12:31:32 +02:00
|
|
|
# Copyright BigchainDB GmbH and BigchainDB contributors
|
|
|
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
|
|
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
|
|
|
|
2018-01-31 14:42:41 +01:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from bigchaindb.models import Transaction
|
Refactor tendermint directory to project root (#2401)
* Problem: core.py contains an unused class, `Bigchain`
Solution: Remove core.py. Refactor BigchainDB Class to remove inheritance from Bigchain.
* Problem: core.py contains an unused class, `Bigchain`
Solution: Remove core.py. Refactor BigchainDB Class to remove inheritance from Bigchain.
* Fixed flake8 complaint about too many blank lines
* Attempting to fix Sphinx docs. This may result in some redundant commits, as I don't know what I'm doing, and I can't experiment without running the CI...
Sorry in advance!
* Attempting to fix Sphinx docs. This may result in some redundant commits, as I don't know what I'm doing, and I can't experiment without running the CI...
Sorry in advance!
* Updating from master changed BigchainDB.process_post_response to a private method, so I had to align with that.
* Fixed a couple stale references to bigchaindb.Bigchain in docstrings
* Missed a reference to `Bigchain` in a patch call...
* Problem: BigchainDB class should be part of project root
Solution: Removed the /tendermint directory and moved its contents to project root
* Problem: Flake8 complained that imports were not at the top of the file
Solution: Had to play around with the order of imports to avoid cyclic dependencies, but its working and style compliant now
* Problem: Stale reference to /tendermint directory in the index
Solution: Removed the references to /tendermint
* Problem: Flake8 complaining of unused import in __init__.py
Solution: The import is there so I can import App directly from bigchaindb, rather than from bigchaindb.core (which I think improves code readability. I added a # noqa to silence Flake8.
* Problem: Stale references to `bigchaindb.tendermint.BigchainDB` in the rst files cause Sphinx to fail
Solution: Updated the autodoc files to use `bigchaindb.BigchainDB` instead
* Problem: Stale reference to the `tendermint` directory in an @patch in a disabled test
Solution: Updated the @patch for completeness
* Problem: BigchainDB class should be part of project root
Solution: Removed the /tendermint directory and moved its contents to project root
* Problem: Flake8 complained that imports were not at the top of the file
Solution: Had to play around with the order of imports to avoid cyclic dependencies, but its working and style compliant now
* Problem: Stale reference to /tendermint directory in the index
Solution: Removed the references to /tendermint
* Problem: Flake8 complaining of unused import in __init__.py
Solution: The import is there so I can import App directly from bigchaindb, rather than from bigchaindb.core (which I think improves code readability. I added a # noqa to silence Flake8.
* Problem: Stale references to `bigchaindb.tendermint.BigchainDB` in the rst files cause Sphinx to fail
Solution: Updated the autodoc files to use `bigchaindb.BigchainDB` instead
* Problem: Stale reference to the `tendermint` directory in an @patch in a disabled test
Solution: Updated the @patch for completeness
2018-07-25 16:59:25 +02:00
|
|
|
from bigchaindb.lib import Block
|
2018-01-31 14:42:41 +01:00
|
|
|
|
|
|
|
BLOCKS_ENDPOINT = '/api/v1/blocks/'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.bdb
|
|
|
|
@pytest.mark.usefixtures('inputs')
|
2018-08-27 09:48:20 +02:00
|
|
|
def test_get_block_endpoint(b, client, alice):
|
2018-08-22 10:48:43 +02:00
|
|
|
import copy
|
2018-06-08 15:50:50 +02:00
|
|
|
tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], asset={'cycle': 'hero'})
|
|
|
|
tx = tx.sign([alice.private_key])
|
2018-08-22 10:48:43 +02:00
|
|
|
|
|
|
|
# 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])
|
2018-01-31 14:42:41 +01:00
|
|
|
|
|
|
|
block = Block(app_hash='random_utxo',
|
|
|
|
height=31,
|
|
|
|
transactions=[tx.id])
|
|
|
|
b.store_block(block._asdict())
|
|
|
|
|
|
|
|
res = client.get(BLOCKS_ENDPOINT + str(block.height))
|
2018-08-22 10:48:43 +02:00
|
|
|
expected_response = {'height': block.height, 'transactions': [tx_dict]}
|
2018-01-31 14:42:41 +01:00
|
|
|
assert res.json == expected_response
|
|
|
|
assert res.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.bdb
|
|
|
|
@pytest.mark.usefixtures('inputs')
|
|
|
|
def test_get_block_returns_404_if_not_found(client):
|
|
|
|
res = client.get(BLOCKS_ENDPOINT + '123')
|
|
|
|
assert res.status_code == 404
|
|
|
|
|
|
|
|
res = client.get(BLOCKS_ENDPOINT + '123/')
|
|
|
|
assert res.status_code == 404
|
2018-02-08 14:32:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.bdb
|
2018-08-27 09:48:20 +02:00
|
|
|
def test_get_block_containing_transaction(b, client, alice):
|
2018-06-08 15:50:50 +02:00
|
|
|
tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], asset={'cycle': 'hero'})
|
|
|
|
tx = tx.sign([alice.private_key])
|
2018-08-22 10:48:43 +02:00
|
|
|
b.store_bulk_transactions([tx])
|
2018-02-08 14:32:21 +01:00
|
|
|
|
|
|
|
block = Block(app_hash='random_utxo',
|
|
|
|
height=13,
|
|
|
|
transactions=[tx.id])
|
|
|
|
b.store_block(block._asdict())
|
|
|
|
|
|
|
|
res = client.get('{}?transaction_id={}'.format(BLOCKS_ENDPOINT, tx.id))
|
|
|
|
expected_response = [block.height]
|
|
|
|
assert res.json == expected_response
|
|
|
|
assert res.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.bdb
|
|
|
|
def test_get_blocks_by_txid_endpoint_returns_empty_list_not_found(client):
|
|
|
|
res = client.get(BLOCKS_ENDPOINT + '?transaction_id=')
|
|
|
|
assert res.status_code == 200
|
|
|
|
assert len(res.json) == 0
|
|
|
|
|
|
|
|
res = client.get(BLOCKS_ENDPOINT + '?transaction_id=123')
|
|
|
|
assert res.status_code == 200
|
|
|
|
assert len(res.json) == 0
|