mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-06-28 00:27:45 +02:00
Filter out assets from invalid transactions
- Created docstrings - Created tests - Raise an exception when trying to use text search with RethinkDB.
This commit is contained in:
parent
ccaae91601
commit
6900e86458
|
@ -330,7 +330,7 @@ def get_unvoted_blocks(conn, node_pubkey):
|
||||||
|
|
||||||
|
|
||||||
@register_query(MongoDBConnection)
|
@register_query(MongoDBConnection)
|
||||||
def text_search(conn, search, language='english', case_sensitive=False,
|
def text_search(conn, search, *, language='english', case_sensitive=False,
|
||||||
diacritic_sensitive=False, text_score=False, limit=0):
|
diacritic_sensitive=False, text_score=False, limit=0):
|
||||||
cursor = conn.run(
|
cursor = conn.run(
|
||||||
conn.collection('assets')
|
conn.collection('assets')
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
from functools import singledispatch
|
from functools import singledispatch
|
||||||
|
|
||||||
|
from bigchaindb.backend.exceptions import OperationError
|
||||||
|
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
def write_transaction(connection, signed_transaction):
|
def write_transaction(connection, signed_transaction):
|
||||||
|
@ -328,8 +330,28 @@ def get_txids_filtered(connection, asset_id, operation=None):
|
||||||
|
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
def text_search(conn, search, language='english', case_sensitive=False,
|
def text_search(conn, search, *, language='english', case_sensitive=False,
|
||||||
diacritic_sensitive=False, text_score=False, limit=0):
|
diacritic_sensitive=False, text_score=False, limit=0):
|
||||||
# TODO: docstring
|
"""Return all the assets that match the text search.
|
||||||
|
|
||||||
raise NotImplementedError
|
The results are sorted by text score.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
search (str): Text search string to query the text index
|
||||||
|
language (str, optional): The language for the search and the rules for
|
||||||
|
stemmer and tokenizer. If the language is `None` text search uses
|
||||||
|
simple tokenization and no stemming.
|
||||||
|
case_sensitive (bool, optional): Enable or disable case sensitive
|
||||||
|
search.
|
||||||
|
diacritic_sensitive (bool, optional): Enable or disable case sensitive
|
||||||
|
diacritic search.
|
||||||
|
text_score (bool, optional): If `True` returns the text score with
|
||||||
|
each document.
|
||||||
|
limit (int, optional): Limit the number of returned documents.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
:obj:`list` of :obj:`dict`: a list of assets
|
||||||
|
"""
|
||||||
|
|
||||||
|
raise OperationError('This query is only supported when running '
|
||||||
|
'BigchainDB with MongoDB as the backend.')
|
||||||
|
|
|
@ -662,3 +662,14 @@ class Bigchain(object):
|
||||||
the database.
|
the database.
|
||||||
"""
|
"""
|
||||||
return backend.query.write_assets(self.connection, assets)
|
return backend.query.write_assets(self.connection, assets)
|
||||||
|
|
||||||
|
def text_search(self, search, *, limit=0):
|
||||||
|
assets = backend.query.text_search(self.connection, search, limit=limit)
|
||||||
|
|
||||||
|
# TODO: This is not efficient. There may be a more efficient way to
|
||||||
|
# query by storing block ids with the assets and using fastquery.
|
||||||
|
# See https://github.com/bigchaindb/bigchaindb/issues/1496
|
||||||
|
for asset in assets:
|
||||||
|
tx, status = self.get_transaction(asset['id'], True)
|
||||||
|
if status == self.TX_VALID:
|
||||||
|
yield asset
|
||||||
|
|
|
@ -213,6 +213,89 @@ class TestBigchainApi(object):
|
||||||
assert b.get_transaction(tx1.id) is None
|
assert b.get_transaction(tx1.id) is None
|
||||||
assert b.get_transaction(tx2.id) == tx2
|
assert b.get_transaction(tx2.id) == tx2
|
||||||
|
|
||||||
|
@pytest.mark.genesis
|
||||||
|
def test_text_search(self, b):
|
||||||
|
from bigchaindb.models import Transaction
|
||||||
|
from bigchaindb.backend.exceptions import OperationError
|
||||||
|
from bigchaindb.backend.mongodb.connection import MongoDBConnection
|
||||||
|
|
||||||
|
# define the assets
|
||||||
|
asset1 = {'msg': 'BigchainDB 1'}
|
||||||
|
asset2 = {'msg': 'BigchainDB 2'}
|
||||||
|
asset3 = {'msg': 'BigchainDB 3'}
|
||||||
|
|
||||||
|
# create the transactions
|
||||||
|
tx1 = Transaction.create([b.me], [([b.me], 1)],
|
||||||
|
asset=asset1).sign([b.me_private])
|
||||||
|
tx2 = Transaction.create([b.me], [([b.me], 1)],
|
||||||
|
asset=asset2).sign([b.me_private])
|
||||||
|
tx3 = Transaction.create([b.me], [([b.me], 1)],
|
||||||
|
asset=asset3).sign([b.me_private])
|
||||||
|
|
||||||
|
# create the block
|
||||||
|
block = b.create_block([tx1, tx2, tx3])
|
||||||
|
b.write_block(block)
|
||||||
|
|
||||||
|
# vote valid
|
||||||
|
vote = b.vote(block.id, b.get_last_voted_block().id, True)
|
||||||
|
b.write_vote(vote)
|
||||||
|
|
||||||
|
# get the assets through text search
|
||||||
|
# this query only works with MongoDB
|
||||||
|
try:
|
||||||
|
assets = list(b.text_search('bigchaindb'))
|
||||||
|
except OperationError as exc:
|
||||||
|
assert not isinstance(b.connection, MongoDBConnection)
|
||||||
|
return
|
||||||
|
|
||||||
|
assert len(assets) == 3
|
||||||
|
|
||||||
|
@pytest.mark.genesis
|
||||||
|
def test_text_search_returns_valid_only(self, monkeypatch, b):
|
||||||
|
from bigchaindb.models import Transaction
|
||||||
|
from bigchaindb.backend.exceptions import OperationError
|
||||||
|
from bigchaindb.backend.mongodb.connection import MongoDBConnection
|
||||||
|
|
||||||
|
asset_valid = {'msg': 'Hello BigchainDB!'}
|
||||||
|
asset_invalid = {'msg': 'Goodbye BigchainDB!'}
|
||||||
|
|
||||||
|
monkeypatch.setattr('time.time', lambda: 1000000000)
|
||||||
|
tx1 = Transaction.create([b.me], [([b.me], 1)],
|
||||||
|
asset=asset_valid)
|
||||||
|
tx1 = tx1.sign([b.me_private])
|
||||||
|
block1 = b.create_block([tx1])
|
||||||
|
b.write_block(block1)
|
||||||
|
|
||||||
|
monkeypatch.setattr('time.time', lambda: 1000000020)
|
||||||
|
tx2 = Transaction.create([b.me], [([b.me], 1)],
|
||||||
|
asset=asset_invalid)
|
||||||
|
tx2 = tx2.sign([b.me_private])
|
||||||
|
block2 = b.create_block([tx2])
|
||||||
|
b.write_block(block2)
|
||||||
|
|
||||||
|
# vote the first block valid
|
||||||
|
vote = b.vote(block1.id, b.get_last_voted_block().id, True)
|
||||||
|
b.write_vote(vote)
|
||||||
|
|
||||||
|
# vote the second block invalid
|
||||||
|
vote = b.vote(block2.id, b.get_last_voted_block().id, False)
|
||||||
|
b.write_vote(vote)
|
||||||
|
|
||||||
|
# get assets with text search
|
||||||
|
try:
|
||||||
|
assets = list(b.text_search('bigchaindb'))
|
||||||
|
except OperationError:
|
||||||
|
assert not isinstance(b.connection, MongoDBConnection)
|
||||||
|
return
|
||||||
|
|
||||||
|
# should only return one asset
|
||||||
|
assert len(assets) == 1
|
||||||
|
# should return the asset created by tx1
|
||||||
|
assert assets[0] == {
|
||||||
|
'data': {'msg': 'Hello BigchainDB!'},
|
||||||
|
'id': tx1.id
|
||||||
|
}
|
||||||
|
|
||||||
@pytest.mark.usefixtures('inputs')
|
@pytest.mark.usefixtures('inputs')
|
||||||
def test_write_transaction(self, b, user_pk, user_sk):
|
def test_write_transaction(self, b, user_pk, user_sk):
|
||||||
from bigchaindb import Bigchain
|
from bigchaindb import Bigchain
|
||||||
|
|
Loading…
Reference in New Issue
Block a user