Add simple flask webserver and tests

This commit is contained in:
vrde 2016-02-23 03:37:33 +01:00
parent 0f3a15ba6e
commit 56357e978d
9 changed files with 101 additions and 27 deletions

View File

15
bigchaindb/web/server.py Normal file
View File

@ -0,0 +1,15 @@
from flask import Flask
from bigchaindb.web import views
def create_app(debug=False):
app = Flask(__name__)
app.debug = debug
app.register_blueprint(views.basic_views)
return app
if __name__ == '__main__':
create_app().run()

13
bigchaindb/web/views.py Normal file
View File

@ -0,0 +1,13 @@
import flask
from flask import Blueprint
from bigchaindb import Bigchain
basic_views = Blueprint('basic_views', __name__)
b = Bigchain()
@basic_views.route('/tx/<tx_id>')
def show(tx_id):
tx = b.get_transaction(tx_id)
return flask.jsonify(**tx)

View File

@ -15,6 +15,7 @@ tests_require = [
'pytest',
'pytest-cov',
'pytest-xdist',
'pytest-flask',
]
dev_require = [
@ -72,6 +73,7 @@ setup(
'logstats==0.2.1',
'base58==0.2.2',
'bitcoin==1.1.42',
'flask==0.10.1',
],
setup_requires=['pytest-runner'],
tests_require=tests_require,

View File

@ -9,6 +9,7 @@ Tasks:
import pytest
import rethinkdb as r
import bigchaindb
from bigchaindb import Bigchain
from bigchaindb.db import get_conn
@ -83,3 +84,24 @@ def cleanup_tables(request, node_config):
def b():
return Bigchain()
@pytest.fixture
def inputs(user_public_key, amount=1, b=None):
# 1. create the genesis block
b = b or Bigchain()
try:
b.create_genesis_block()
except bigchaindb.core.GenesisBlockAlreadyExistsError:
pass
# 2. create block with transactions for `USER` to spend
transactions = []
for i in range(amount):
tx = b.create_transaction(b.me, user_public_key, None, 'CREATE')
tx_signed = b.sign_transaction(tx, b.me_private)
transactions.append(tx_signed)
b.write_transaction(tx_signed)
block = b.create_block(transactions)
b.write_block(block, durability='hard')
return block

View File

@ -8,37 +8,11 @@ import rethinkdb as r
import bigchaindb
from bigchaindb import util
from bigchaindb import exceptions
from bigchaindb import Bigchain
from bigchaindb.crypto import hash_data, PrivateKey, PublicKey, generate_key_pair
from bigchaindb.voter import Voter
from bigchaindb.block import Block
def create_inputs(user_public_key, amount=1, b=None):
# 1. create the genesis block
b = b or Bigchain()
try:
b.create_genesis_block()
except bigchaindb.core.GenesisBlockAlreadyExistsError:
pass
# 2. create block with transactions for `USER` to spend
transactions = []
for i in range(amount):
tx = b.create_transaction(b.me, user_public_key, None, 'CREATE')
tx_signed = b.sign_transaction(tx, b.me_private)
transactions.append(tx_signed)
b.write_transaction(tx_signed)
block = b.create_block(transactions)
b.write_block(block, durability='hard')
return block
@pytest.fixture
def inputs(user_public_key):
return create_inputs(user_public_key)
@pytest.mark.skipif(reason='Some tests throw a ResourceWarning that might result in some weird '
'exceptions while running the tests. The problem seems to *not* '
@ -130,11 +104,11 @@ class TestBigchainApi(object):
# check if the assignee is the current node
assert response['assignee'] == b.me
@pytest.mark.usefixtures('inputs')
def test_assign_transaction_multiple_nodes(self, b, user_public_key, user_private_key):
# create 5 federation nodes
for _ in range(5):
b.federation_nodes.append(b.generate_keys()[1])
create_inputs(user_public_key, amount=20, b=b)
# test assignee for several transactions
for _ in range(20):

0
tests/web/__init__.py Normal file
View File

38
tests/web/conftest.py Normal file
View File

@ -0,0 +1,38 @@
import pytest
from ..db import conftest
@pytest.fixture(autouse=True)
def restore_config(request, node_config):
conftest.restore_config(request, node_config)
@pytest.fixture(scope='module', autouse=True)
def setup_database(request, node_config):
conftest.setup_database(request, node_config)
@pytest.fixture(scope='function', autouse=True)
def cleanup_tables(request, node_config):
conftest.cleanup_tables(request, node_config)
@pytest.fixture
def b():
return conftest.b()
@pytest.fixture
def app(request, node_config):
# XXX: For whatever reason this fixture runs before `restore_config`,
# so we need to manually call it.
conftest.restore_config(request, node_config)
from bigchaindb.web import server
app = server.create_app()
return app
@pytest.fixture
def inputs(user_public_key):
conftest.inputs(user_public_key)

View File

@ -0,0 +1,10 @@
import pytest
@pytest.mark.usefixtures('inputs')
def test_tx_endpoint(b, client, user_public_key):
input_tx = b.get_owned_ids(user_public_key).pop()
tx = b.get_transaction(input_tx)
res = client.get('/tx/{}'.format(input_tx))
assert tx == res.json