Merge pull request #971 from bigchaindb/feat/959/test-mongodb-schema

Created tests for mongodb schema
This commit is contained in:
Rodolphe Marques 2016-12-20 13:56:32 +01:00 committed by GitHub
commit 61cfe1b370
3 changed files with 109 additions and 25 deletions

View File

@ -49,9 +49,6 @@ def drop_database(conn, dbname):
def create_bigchain_secondary_index(conn, dbname):
logger.info('Create `bigchain` secondary index.')
# to select blocks by id
conn.conn[dbname]['bigchain'].create_index('id', name='block_id')
# to order blocks by timestamp
conn.conn[dbname]['bigchain'].create_index([('block.timestamp',
ASCENDING)],
@ -62,33 +59,15 @@ def create_bigchain_secondary_index(conn, dbname):
name='transaction_id',
unique=True)
# secondary index for payload data by UUID, this field is unique
conn.conn[dbname]['bigchain']\
.create_index('block.transactions.transaction.metadata.id',
name='metadata_id', unique=True)
# secondary index for asset uuid, this field is unique
conn.conn[dbname]['bigchain']\
.create_index('block.transactions.transaction.asset.id',
name='asset_id', unique=True)
# compound index on fulfillment and transactions id
conn.conn[dbname]['bigchain']\
.create_index([('block.transactions.transaction.fulfillments.txid',
ASCENDING),
('block.transactions.transaction.fulfillments.cid',
ASCENDING)],
name='tx_and_fulfillment')
def create_backlog_secondary_index(conn, dbname):
logger.info('Create `backlog` secondary index.')
# to order transactions by timestamp
conn.conn[dbname]['backlog'].create_index([('transaction.timestamp',
ASCENDING)],
name='transaction_timestamp')
# compound index to read transactions from the backlog per assignee
conn.conn[dbname]['backlog']\
.create_index([('assignee', ASCENDING),
@ -99,10 +78,6 @@ def create_backlog_secondary_index(conn, dbname):
def create_votes_secondary_index(conn, dbname):
logger.info('Create `votes` secondary index.')
# index on block id to quickly poll
conn.conn[dbname]['votes'].create_index('vote.voting_for_block',
name='voting_for')
# is the first index redundant then?
# compound index to order votes by block id and node
conn.conn[dbname]['votes'].create_index([('vote.voting_for_block',

View File

View File

@ -0,0 +1,109 @@
import pytest
@pytest.mark.usefixtures('setup_database')
def test_init_creates_db_tables_and_indexes():
import bigchaindb
from bigchaindb import backend
from bigchaindb.backend.schema import init_database
conn = backend.connect()
dbname = bigchaindb.config['database']['name']
# the db is set up by the fixture so we need to remove it
conn.conn.drop_database(dbname)
init_database()
collection_names = conn.conn[dbname].collection_names()
assert sorted(collection_names) == ['backlog', 'bigchain', 'votes']
indexes = conn.conn[dbname]['bigchain'].index_information().keys()
assert sorted(indexes) == ['_id_', 'asset_id', 'block_timestamp',
'transaction_id']
indexes = conn.conn[dbname]['backlog'].index_information().keys()
assert sorted(indexes) == ['_id_', 'assignee__transaction_timestamp']
indexes = conn.conn[dbname]['votes'].index_information().keys()
assert sorted(indexes) == ['_id_', 'block_and_voter']
@pytest.mark.usefixtures('setup_database')
def test_init_database_fails_if_db_exists():
import bigchaindb
from bigchaindb import backend
from bigchaindb.backend.schema import init_database
from bigchaindb.common import exceptions
conn = backend.connect()
dbname = bigchaindb.config['database']['name']
# The db is set up by the fixtures
assert dbname in conn.conn.database_names()
with pytest.raises(exceptions.DatabaseAlreadyExists):
init_database()
@pytest.mark.usefixtures('setup_database')
def test_create_tables():
import bigchaindb
from bigchaindb import backend
from bigchaindb.backend import schema
conn = backend.connect()
dbname = bigchaindb.config['database']['name']
# The db is set up by the fixtures so we need to remove it
conn.conn.drop_database(dbname)
schema.create_database(conn, dbname)
schema.create_tables(conn, dbname)
collection_names = conn.conn[dbname].collection_names()
assert sorted(collection_names) == ['backlog', 'bigchain', 'votes']
@pytest.mark.usefixtures('setup_database')
def test_create_secondary_indexes():
import bigchaindb
from bigchaindb import backend
from bigchaindb.backend import schema
conn = backend.connect()
dbname = bigchaindb.config['database']['name']
# The db is set up by the fixtures so we need to remove it
conn.conn.drop_database(dbname)
schema.create_database(conn, dbname)
schema.create_tables(conn, dbname)
schema.create_indexes(conn, dbname)
# Bigchain table
indexes = conn.conn[dbname]['bigchain'].index_information().keys()
assert sorted(indexes) == ['_id_', 'asset_id', 'block_timestamp',
'transaction_id']
# Backlog table
indexes = conn.conn[dbname]['backlog'].index_information().keys()
assert sorted(indexes) == ['_id_', 'assignee__transaction_timestamp']
# Votes table
indexes = conn.conn[dbname]['votes'].index_information().keys()
assert sorted(indexes) == ['_id_', 'block_and_voter']
@pytest.mark.usefixtures('setup_database')
def test_drop():
import bigchaindb
from bigchaindb import backend
from bigchaindb.backend import schema
conn = backend.connect()
dbname = bigchaindb.config['database']['name']
# The db is set up by fixtures
assert dbname in conn.conn.database_names()
schema.drop_database(conn, dbname)
assert dbname not in conn.conn.database_names()