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

View File

@ -5,14 +5,22 @@ For more information please refer to the documentation in Apiary:
""" """
import flask import flask
from flask import request, Blueprint from flask import current_app, request, Blueprint
from bigchaindb import util from bigchaindb import util
from bigchaindb import Bigchain
basic_views = Blueprint('basic_views', __name__) 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>') @basic_views.route('/transactions/<tx_id>')
@ -26,7 +34,9 @@ def get_transaction(tx_id):
A JSON string containing the data about the transaction. 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) return flask.jsonify(**tx)
@ -37,6 +47,7 @@ def create_transaction():
Return: Return:
A JSON string containing the data about the transaction. A JSON string containing the data about the transaction.
""" """
bigchain = current_app.config['bigchain']
val = {} val = {}
@ -46,12 +57,12 @@ def create_transaction():
if tx['transaction']['operation'] == 'CREATE': if tx['transaction']['operation'] == 'CREATE':
tx = util.transform_create(tx) 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): if not util.verify_signature(tx):
val['error'] = 'Invalid transaction signature' val['error'] = 'Invalid transaction signature'
val = b.write_transaction(tx) val = bigchain.write_transaction(tx)
return flask.jsonify(**tx) return flask.jsonify(**tx)

View File

@ -7,13 +7,14 @@ Tasks:
""" """
import os import os
import copy
import pytest import pytest
DB_NAME = 'bigchain_test_{}'.format(os.getpid()) DB_NAME = 'bigchain_test_{}'.format(os.getpid())
config = { CONFIG = {
'database': { 'database': {
'name': DB_NAME 'name': DB_NAME
}, },
@ -36,7 +37,7 @@ def restore_config(request, node_config):
@pytest.fixture(scope='module') @pytest.fixture(scope='module')
def node_config(): def node_config():
return config return copy.deepcopy(CONFIG)
@pytest.fixture @pytest.fixture
@ -50,7 +51,8 @@ def user_public_key():
@pytest.fixture @pytest.fixture
def b(): def b(request, node_config):
restore_config(request, node_config)
from bigchaindb import Bigchain from bigchaindb import Bigchain
return 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 value['return'] = newconfig
monkeypatch.setattr(config_utils, 'write_config', mock_write_config) 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) monkeypatch.setattr('os.path.exists', lambda path: False)
args = Namespace(config=None, yes=True) args = Namespace(config=None, yes=True)

View File

@ -1,10 +1,8 @@
import copy
import pytest import pytest
@pytest.fixture @pytest.fixture
def config(request): def config(request, monkeypatch):
import bigchaindb
config = { config = {
'database': { 'database': {
'host': 'host', 'host': 'host',
@ -18,13 +16,10 @@ def config(request):
'keyring': [], 'keyring': [],
'CONFIGURED': True, 'CONFIGURED': True,
} }
bigchaindb.config.update(config)
def fin(): monkeypatch.setattr('bigchaindb.config', config)
bigchaindb.config = bigchaindb._config
bigchaindb._config = copy.deepcopy(bigchaindb._config) return config
request.addfinalizer(fin)
return bigchaindb.config
def test_bigchain_class_default_initialization(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) @pytest.fixture(scope='function', autouse=True)
def clean_config(): def clean_config(monkeypatch):
bigchaindb.config = copy.deepcopy(ORIGINAL_CONFIG) monkeypatch.setattr('bigchaindb.config', copy.deepcopy(ORIGINAL_CONFIG))
def test_bigchain_instance_is_initialized_when_conf_provided(): def test_bigchain_instance_is_initialized_when_conf_provided():

View File

@ -4,7 +4,8 @@ from ..db import conftest
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def restore_config(request, node_config): 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) @pytest.fixture(scope='module', autouse=True)
@ -21,9 +22,10 @@ def cleanup_tables(request, node_config):
def app(request, node_config): def app(request, node_config):
# XXX: For whatever reason this fixture runs before `restore_config`, # XXX: For whatever reason this fixture runs before `restore_config`,
# so we need to manually call it. # so we need to manually call it.
conftest.restore_config(request, node_config) restore_config(request, node_config)
from bigchaindb.web import server from bigchaindb.web import server
app = server.create_app() app = server.create_app(debug=True)
return app return app

View File

@ -12,7 +12,6 @@ TX_ENDPOINT = '/api/v1/transactions/'
def test_get_transaction_endpoint(b, client, user_public_key): def test_get_transaction_endpoint(b, client, user_public_key):
input_tx = b.get_owned_ids(user_public_key).pop() input_tx = b.get_owned_ids(user_public_key).pop()
tx = b.get_transaction(input_tx) tx = b.get_transaction(input_tx)
res = client.get(TX_ENDPOINT + input_tx) res = client.get(TX_ENDPOINT + input_tx)
assert tx == res.json assert tx == res.json