From 050990aac1d17aa3f6517e1321d077a962dcb8ff Mon Sep 17 00:00:00 2001 From: Rodolphe Marques Date: Mon, 19 Dec 2016 15:45:02 +0100 Subject: [PATCH] Created tests for mongodb schema Removed some unused secondary indexes --- bigchaindb/backend/mongodb/schema.py | 25 ------ tests/backend/mongodb/__init__.py | 0 tests/backend/mongodb/test_schema.py | 109 +++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 25 deletions(-) create mode 100644 tests/backend/mongodb/__init__.py create mode 100644 tests/backend/mongodb/test_schema.py diff --git a/bigchaindb/backend/mongodb/schema.py b/bigchaindb/backend/mongodb/schema.py index 55fd955d..d56c4e40 100644 --- a/bigchaindb/backend/mongodb/schema.py +++ b/bigchaindb/backend/mongodb/schema.py @@ -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', diff --git a/tests/backend/mongodb/__init__.py b/tests/backend/mongodb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/backend/mongodb/test_schema.py b/tests/backend/mongodb/test_schema.py new file mode 100644 index 00000000..7cbaff64 --- /dev/null +++ b/tests/backend/mongodb/test_schema.py @@ -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()