Use unique db names in tests to allow xdist

This commit is contained in:
vrde 2016-02-19 00:10:44 +01:00
parent 5ebe9e9349
commit 6ab2b03260
3 changed files with 29 additions and 26 deletions

View File

@ -6,12 +6,16 @@ Tasks:
2. delete test database after running the tests 2. delete test database after running the tests
""" """
import os
import pytest import pytest
DB_NAME = 'bigchain_test_{}'.format(os.getpid())
config = { config = {
'database': { 'database': {
'name': 'bigchain_test' 'name': DB_NAME
}, },
'keypair': { 'keypair': {
'private': '3i2FDXp87N9ExXSvWxqBAw9EgzoxxGTQNKbtxmWBpTyL', 'private': '3i2FDXp87N9ExXSvWxqBAw9EgzoxxGTQNKbtxmWBpTyL',
@ -30,7 +34,7 @@ def restore_config(request, node_config):
config_utils.dict_config(node_config) config_utils.dict_config(node_config)
@pytest.fixture @pytest.fixture(scope='module')
def node_config(): def node_config():
return config return config

View File

@ -13,9 +13,6 @@ from bigchaindb import Bigchain
from bigchaindb.db import get_conn from bigchaindb.db import get_conn
NOOP = None
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def restore_config(request, node_config): def restore_config(request, node_config):
from bigchaindb import config_utils from bigchaindb import config_utils
@ -23,58 +20,60 @@ def restore_config(request, node_config):
@pytest.fixture(scope='module', autouse=True) @pytest.fixture(scope='module', autouse=True)
def setup_database(request): def setup_database(request, node_config):
print('Initializing test db') print('Initializing test db')
db_name = node_config['database']['name']
get_conn().repl() get_conn().repl()
try: try:
r.db_create('bigchain_test').run() r.db_create(db_name).run()
except r.ReqlOpFailedError as e: except r.ReqlOpFailedError as e:
if e.message == 'Database `bigchain_test` already exists.': if e.message == 'Database `{}` already exists.'.format(db_name):
r.db_drop('bigchain_test').run() r.db_drop(db_name).run()
r.db_create('bigchain_test').run() r.db_create(db_name).run()
else: else:
raise raise
print('Finished initializing test db') print('Finished initializing test db')
# setup tables # setup tables
r.db('bigchain_test').table_create('bigchain').run() r.db(db_name).table_create('bigchain').run()
r.db('bigchain_test').table_create('backlog').run() r.db(db_name).table_create('backlog').run()
# create the secondary indexes # create the secondary indexes
# to order blocks by timestamp # to order blocks by timestamp
r.db('bigchain_test').table('bigchain').index_create('block_timestamp', r.row['block']['timestamp']).run() r.db(db_name).table('bigchain').index_create('block_timestamp', r.row['block']['timestamp']).run()
# to order blocks by block number # to order blocks by block number
r.db('bigchain_test').table('bigchain').index_create('block_number', r.row['block']['block_number']).run() r.db(db_name).table('bigchain').index_create('block_number', r.row['block']['block_number']).run()
# to order transactions by timestamp # to order transactions by timestamp
r.db('bigchain_test').table('backlog').index_create('transaction_timestamp', r.row['transaction']['timestamp']).run() r.db(db_name).table('backlog').index_create('transaction_timestamp', r.row['transaction']['timestamp']).run()
# compound index to read transactions from the backlog per assignee # compound index to read transactions from the backlog per assignee
r.db('bigchain_test').table('backlog')\ r.db(db_name).table('backlog')\
.index_create('assignee__transaction_timestamp', [r.row['assignee'], r.row['transaction']['timestamp']])\ .index_create('assignee__transaction_timestamp', [r.row['assignee'], r.row['transaction']['timestamp']])\
.run() .run()
def fin(): def fin():
print('Deleting `bigchain_test` database') print('Deleting `{}` database'.format(db_name))
get_conn().repl() get_conn().repl()
try: try:
r.db_drop('bigchain_test').run() r.db_drop(db_name).run()
except r.ReqlOpFailedError as e: except r.ReqlOpFailedError as e:
if e.message != 'Database `bigchain_test` does not exist.': if e.message != 'Database `{}` does not exist.'.format(db_name):
raise raise
print('Finished deleting `bigchain_test`') print('Finished deleting `{}`'.format(db_name))
request.addfinalizer(fin) request.addfinalizer(fin)
@pytest.fixture(scope='function', autouse=True) @pytest.fixture(scope='function', autouse=True)
def cleanup_tables(request): def cleanup_tables(request, node_config):
db_name = node_config['database']['name']
def fin(): def fin():
get_conn().repl() get_conn().repl()
try: try:
r.db('bigchain_test').table('bigchain').delete().run() r.db(db_name).table('bigchain').delete().run()
r.db('bigchain_test').table('backlog').delete().run() r.db(db_name).table('backlog').delete().run()
except r.ReqlOpFailedError as e: except r.ReqlOpFailedError as e:
if e.message != 'Database `bigchain_test` does not exist.': if e.message != 'Database `{}` does not exist.'.format(db_name):
raise raise
request.addfinalizer(fin) request.addfinalizer(fin)

View File

@ -11,8 +11,8 @@ from .conftest import setup_database as _setup_database
# Since we are testing database initialization and database drop, # Since we are testing database initialization and database drop,
# we need to use the `setup_database` fixture on a function level # we need to use the `setup_database` fixture on a function level
@pytest.fixture(scope='function', autouse=True) @pytest.fixture(scope='function', autouse=True)
def setup_database(request): def setup_database(request, node_config):
_setup_database(request) _setup_database(request, node_config)
def test_init_creates_db_tables_and_indexes(): def test_init_creates_db_tables_and_indexes():