Add config to blueprint and fix tests madness

There was a problem related to the import of the module
`bigchaindb.web.views`.
The module, when imported, inizialises a new `Bigchain` instance, and
this is wrong for testing and because it's a bad practice.

I spent more or less 2h finding out the problem.
This commit is contained in:
vrde 2016-03-03 01:40:40 +01:00
parent 456517443a
commit 3a714a7f8e
8 changed files with 36 additions and 25 deletions

View File

@ -2,6 +2,7 @@
from flask import Flask
from bigchaindb import Bigchain
from bigchaindb.web import views
@ -14,6 +15,7 @@ def create_app(debug=False):
app = Flask(__name__)
app.debug = debug
app.config['bigchain'] = Bigchain()
app.register_blueprint(views.basic_views, url_prefix='/api/v1')
return app

View File

@ -5,14 +5,22 @@ For more information please refer to the documentation in Apiary:
"""
import flask
from flask import request, Blueprint
from flask import current_app, request, Blueprint
from bigchaindb import util
from bigchaindb import Bigchain
basic_views = Blueprint('basic_views', __name__)
b = Bigchain()
@basic_views.record
def get_bigchain(state):
bigchain = state.app.config.get('bigchain')
if bigchain is None:
raise Exception('This blueprint expects you to provide '
'database access through `bigchain`')
@basic_views.route('/transactions/<tx_id>')
@ -26,7 +34,9 @@ def get_transaction(tx_id):
A JSON string containing the data about the transaction.
"""
tx = b.get_transaction(tx_id)
bigchain = current_app.config['bigchain']
tx = bigchain.get_transaction(tx_id)
return flask.jsonify(**tx)
@ -37,6 +47,7 @@ def create_transaction():
Return:
A JSON string containing the data about the transaction.
"""
bigchain = current_app.config['bigchain']
val = {}
@ -46,12 +57,12 @@ def create_transaction():
if tx['transaction']['operation'] == 'CREATE':
tx = util.transform_create(tx)
tx = util.sign_tx(tx, b.me_private)
tx = util.sign_tx(tx, bigchain.me_private)
if not util.verify_signature(tx):
val['error'] = 'Invalid transaction signature'
val = b.write_transaction(tx)
val = bigchain.write_transaction(tx)
return flask.jsonify(**tx)

View File

@ -7,13 +7,14 @@ Tasks:
"""
import os
import copy
import pytest
DB_NAME = 'bigchain_test_{}'.format(os.getpid())
config = {
CONFIG = {
'database': {
'name': DB_NAME
},
@ -36,7 +37,7 @@ def restore_config(request, node_config):
@pytest.fixture(scope='module')
def node_config():
return config
return copy.deepcopy(CONFIG)
@pytest.fixture
@ -50,7 +51,8 @@ def user_public_key():
@pytest.fixture
def b():
def b(request, node_config):
restore_config(request, node_config)
from bigchaindb import Bigchain
return Bigchain()

View File

@ -87,7 +87,7 @@ def test_bigchain_run_start_assume_yes_create_default_config(monkeypatch, mock_p
value['return'] = newconfig
monkeypatch.setattr(config_utils, 'write_config', mock_write_config)
monkeypatch.setattr(config_utils, 'file_config', lambda x: config_utils.dict_config(value['return']))
monkeypatch.setattr(config_utils, 'file_config', lambda x: config_utils.dict_config(expected_config))
monkeypatch.setattr('os.path.exists', lambda path: False)
args = Namespace(config=None, yes=True)

View File

@ -1,10 +1,8 @@
import copy
import pytest
@pytest.fixture
def config(request):
import bigchaindb
def config(request, monkeypatch):
config = {
'database': {
'host': 'host',
@ -18,13 +16,10 @@ def config(request):
'keyring': [],
'CONFIGURED': True,
}
bigchaindb.config.update(config)
def fin():
bigchaindb.config = bigchaindb._config
bigchaindb._config = copy.deepcopy(bigchaindb._config)
request.addfinalizer(fin)
return bigchaindb.config
monkeypatch.setattr('bigchaindb.config', config)
return config
def test_bigchain_class_default_initialization(config):

View File

@ -10,8 +10,8 @@ ORIGINAL_CONFIG = copy.deepcopy(bigchaindb._config)
@pytest.fixture(scope='function', autouse=True)
def clean_config():
bigchaindb.config = copy.deepcopy(ORIGINAL_CONFIG)
def clean_config(monkeypatch):
monkeypatch.setattr('bigchaindb.config', copy.deepcopy(ORIGINAL_CONFIG))
def test_bigchain_instance_is_initialized_when_conf_provided():

View File

@ -4,7 +4,8 @@ from ..db import conftest
@pytest.fixture(autouse=True)
def restore_config(request, node_config):
conftest.restore_config(request, node_config)
from bigchaindb import config_utils
config_utils.dict_config(node_config)
@pytest.fixture(scope='module', autouse=True)
@ -21,9 +22,10 @@ def cleanup_tables(request, node_config):
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)
restore_config(request, node_config)
from bigchaindb.web import server
app = server.create_app()
app = server.create_app(debug=True)
return app

View File

@ -12,7 +12,6 @@ TX_ENDPOINT = '/api/v1/transactions/'
def test_get_transaction_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_ENDPOINT + input_tx)
assert tx == res.json