bigchaindb/tests/db/test_utils.py
Sylvain Bellemare 50b0b3cef2 Rebase/feat/586/integrate tx model (#641)
* Adjust imports to bigchaindb_common

* Adjust get_spent function signature

* Adjust block serialization

* Fix BigchainApi Test

* Fix TestTransactionValidation tests

* Fix TestBlockValidation tests

* WIP: TestMultipleInputs

* Adjust tests to tx-model interface changes

- Fix old tests
- Fix tests in TestMultipleInputs class

* Remove fulfillment message tests

* Fix TransactionMalleability tests

* Remove Cryptoconditions tests

* Remove create_transaction

* Remove signing logic

* Remove consensus plugin

* Fix block_creation pipeline

* Fix election pipeline

* Replace some util functions with bdb_common ones

- timestamp ==> gen_timestamp
- serialize.

* Implement Block model

* Simplify function signatures for vote functions

Change parameter interface for the following functions:

- has_previous_vote
- verify_vote_signature
- block_election_status

so that they take a block's id and voters instead of a fake block.

* Integrate Block and Transaction model

* Fix leftover tests and cleanup conftest

* Add bigchaindb-common to install_requires

* Delete transactions after block is written (#609)

* delete transactions after block is written

* cleanup transaction_exists

* check for duplicate transactions

* delete invalid tx from backlog

* test duplicate transaction

* Remove dead code

* Test processes.py

* Test invalid tx in on server

* Fix tests for core.py

* Fix models tests

* Test commands main fn

* Add final coverage to vote pipeline

* Add more tests to voting pipeline

* Remove consensus plugin docs and misc

* Post rebase fixes

* Fix rebase mess

* Remove extra blank line

* Improve docstring

* Remove comment

handled in bigchaindb/cryptoconditions#27;
see https://github.com/bigchaindb/cryptoconditions/issues/27

* Fix block serialization in block creation

* Add signed_ prefix to transfer_tx

* Improve docs

* Add library documentation page on pipelines

* PR feedback for models.py

* Impr. readability of get_last_voted_block

* Use dict comprehension

* Add docker-compose file to build and serve docs

locally for development purposes

* Change private_key for signing_key

* Improve docstrings

* Remove consensus docs

* Document new consensus module

* Create different transactions for the block

* Cleanup variable names in block.py

* Create different transactions for the block

* Cleanup variable names in block.py
2016-09-29 10:29:41 +02:00

207 lines
6.6 KiB
Python

import builtins
from bigchaindb_common import exceptions
import pytest
import rethinkdb as r
import bigchaindb
from bigchaindb.db import utils
from .conftest import setup_database as _setup_database
# Since we are testing database initialization and database drop,
# we need to use the `setup_database` fixture on a function level
@pytest.fixture(scope='function', autouse=True)
def setup_database(request, node_config):
_setup_database(request, node_config)
def test_init_creates_db_tables_and_indexes():
conn = utils.get_conn()
dbname = bigchaindb.config['database']['name']
# The db is set up by fixtures so we need to remove it
r.db_drop(dbname).run(conn)
utils.init()
assert r.db_list().contains(dbname).run(conn) is True
assert r.db(dbname).table_list().contains('backlog', 'bigchain').run(conn) is True
assert r.db(dbname).table('bigchain').index_list().contains(
'block_timestamp').run(conn) is True
assert r.db(dbname).table('backlog').index_list().contains(
'transaction_timestamp',
'assignee__transaction_timestamp').run(conn) is True
def test_create_database():
conn = utils.get_conn()
dbname = utils.get_database_name()
# The db is set up by fixtures so we need to remove it
# and recreate it just with one table
r.db_drop(dbname).run(conn)
utils.create_database(conn, dbname)
assert r.db_list().contains(dbname).run(conn) is True
def test_create_bigchain_table():
conn = utils.get_conn()
dbname = utils.get_database_name()
# The db is set up by fixtures so we need to remove it
# and recreate it just with one table
r.db_drop(dbname).run(conn)
utils.create_database(conn, dbname)
utils.create_table(conn, dbname, 'bigchain')
assert r.db(dbname).table_list().contains('bigchain').run(conn) is True
assert r.db(dbname).table_list().contains('backlog').run(conn) is False
assert r.db(dbname).table_list().contains('votes').run(conn) is False
def test_create_bigchain_secondary_index():
conn = utils.get_conn()
dbname = utils.get_database_name()
# The db is set up by fixtures so we need to remove it
# and recreate it just with one table
r.db_drop(dbname).run(conn)
utils.create_database(conn, dbname)
utils.create_table(conn, dbname, 'bigchain')
utils.create_bigchain_secondary_index(conn, dbname)
assert r.db(dbname).table('bigchain').index_list().contains(
'block_timestamp').run(conn) is True
assert r.db(dbname).table('bigchain').index_list().contains(
'transaction_id').run(conn) is True
assert r.db(dbname).table('bigchain').index_list().contains(
'payload_uuid').run(conn) is True
def test_create_backlog_table():
conn = utils.get_conn()
dbname = utils.get_database_name()
# The db is set up by fixtures so we need to remove it
# and recreate it just with one table
r.db_drop(dbname).run(conn)
utils.create_database(conn, dbname)
utils.create_table(conn, dbname, 'backlog')
assert r.db(dbname).table_list().contains('backlog').run(conn) is True
assert r.db(dbname).table_list().contains('bigchain').run(conn) is False
assert r.db(dbname).table_list().contains('votes').run(conn) is False
def test_create_backlog_secondary_index():
conn = utils.get_conn()
dbname = utils.get_database_name()
# The db is set up by fixtures so we need to remove it
# and recreate it just with one table
r.db_drop(dbname).run(conn)
utils.create_database(conn, dbname)
utils.create_table(conn, dbname, 'backlog')
utils.create_backlog_secondary_index(conn, dbname)
assert r.db(dbname).table('backlog').index_list().contains(
'transaction_timestamp').run(conn) is True
assert r.db(dbname).table('backlog').index_list().contains(
'assignee__transaction_timestamp').run(conn) is True
def test_create_votes_table():
conn = utils.get_conn()
dbname = utils.get_database_name()
# The db is set up by fixtures so we need to remove it
# and recreate it just with one table
r.db_drop(dbname).run(conn)
utils.create_database(conn, dbname)
utils.create_table(conn, dbname, 'votes')
assert r.db(dbname).table_list().contains('votes').run(conn) is True
assert r.db(dbname).table_list().contains('bigchain').run(conn) is False
assert r.db(dbname).table_list().contains('backlog').run(conn) is False
def test_create_votes_secondary_index():
conn = utils.get_conn()
dbname = utils.get_database_name()
# The db is set up by fixtures so we need to remove it
# and recreate it just with one table
r.db_drop(dbname).run(conn)
utils.create_database(conn, dbname)
utils.create_table(conn, dbname, 'votes')
utils.create_votes_secondary_index(conn, dbname)
assert r.db(dbname).table('votes').index_list().contains(
'block_and_voter').run(conn) is True
def test_init_fails_if_db_exists():
conn = utils.get_conn()
dbname = bigchaindb.config['database']['name']
# The db is set up by fixtures
assert r.db_list().contains(dbname).run(conn) is True
with pytest.raises(exceptions.DatabaseAlreadyExists):
utils.init()
def test_drop_interactively_drops_the_database_when_user_says_yes(monkeypatch):
conn = utils.get_conn()
dbname = bigchaindb.config['database']['name']
# The db is set up by fixtures
assert r.db_list().contains(dbname).run(conn) is True
monkeypatch.setattr(builtins, 'input', lambda x: 'y')
utils.drop()
assert r.db_list().contains(dbname).run(conn) is False
def test_drop_programmatically_drops_the_database_when_assume_yes_is_true(monkeypatch):
conn = utils.get_conn()
dbname = bigchaindb.config['database']['name']
# The db is set up by fixtures
assert r.db_list().contains(dbname).run(conn) is True
utils.drop(assume_yes=True)
assert r.db_list().contains(dbname).run(conn) is False
def test_drop_interactively_does_not_drop_the_database_when_user_says_no(monkeypatch):
conn = utils.get_conn()
dbname = bigchaindb.config['database']['name']
# The db is set up by fixtures
print(r.db_list().contains(dbname).run(conn))
assert r.db_list().contains(dbname).run(conn) is True
monkeypatch.setattr(builtins, 'input', lambda x: 'n')
utils.drop()
assert r.db_list().contains(dbname).run(conn) is True
def test_drop_non_existent_db_raises_an_error():
conn = utils.get_conn()
dbname = bigchaindb.config['database']['name']
# The db is set up by fixtures
assert r.db_list().contains(dbname).run(conn) is True
utils.drop(assume_yes=True)
with pytest.raises(exceptions.DatabaseDoesNotExist):
utils.drop(assume_yes=True)