From d73bb15708e695610d15eae19068b231a315444a Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Wed, 14 Dec 2022 14:02:54 +0100 Subject: [PATCH] adjusted query test cases, removed unused code Signed-off-by: Lorenz Herzberger --- planetmint/backend/models/dbtransaction.py | 1 + planetmint/backend/tarantool/query.py | 4 +- planetmint/lib.py | 11 - tests/backend/tarantool/test_queries.py | 262 +-------------------- 4 files changed, 8 insertions(+), 270 deletions(-) diff --git a/planetmint/backend/models/dbtransaction.py b/planetmint/backend/models/dbtransaction.py index 37603f7..b065bf7 100644 --- a/planetmint/backend/models/dbtransaction.py +++ b/planetmint/backend/models/dbtransaction.py @@ -50,6 +50,7 @@ class DbTransaction: "operation": self.operation, "version": self.version, "inputs": Input.list_to_dict(self.inputs), + "outputs": Output.list_to_dict(self.outputs), "assets": Asset.list_to_dict(self.assets), "metadata": self.metadata.to_dict() if self.metadata is not None else None, "script": self.script.to_dict() if self.script is not None else None, diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index 5e084cc..8793e4a 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -169,7 +169,7 @@ def get_txids_filtered(connection, asset_ids: list[str], operation: str = "", la ) elif operation == "TRANSFER": transactions = connection.run( - connection.space(TARANT_TABLE_TRANSACTION).select(["", operation, asset_ids], index="transactions_by_id_and_operation") + connection.space(TARANT_TABLE_TRANSACTION).select(asset_ids, index="transactions_by_asset") ) else: txs = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(asset_ids, index=TARANT_ID_SEARCH)) @@ -232,7 +232,7 @@ def get_spending_transactions(connection, inputs): @register_query(TarantoolDBConnection) def get_block(connection, block_id=None): - _block = connection.run(connection.space("blocks").select(block_id, index="id", limit=1)) + _block = connection.run(connection.space("blocks").select(block_id, index="height", limit=1)) _block = Block.from_tuple(_block[0]) return _block.to_dict() diff --git a/planetmint/lib.py b/planetmint/lib.py index 49c54ac..68b7cb6 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -560,17 +560,6 @@ class Planetmint(object): tx_map[tx["id"]] = tx tx_ids.append(tx["id"]) - # TODO: this will break if more than one asset is used - assets = self.get_assets(tx_ids) - for asset in assets: - tx = tx_map[asset.id] - tx["assets"] = [asset.data] - - metadata_list = self.get_metadata(tx_ids) - for metadata in metadata_list: - tx = tx_map[metadata.id] - tx["metadata"] = metadata.metadata - if return_list: tx_list = [] for tx_id, tx in tx_map.items(): diff --git a/tests/backend/tarantool/test_queries.py b/tests/backend/tarantool/test_queries.py index ce1e23e..e4175bb 100644 --- a/tests/backend/tarantool/test_queries.py +++ b/tests/backend/tarantool/test_queries.py @@ -11,6 +11,7 @@ from transactions.common.transaction import Transaction from transactions.types.assets.create import Create from transactions.types.assets.transfer import Transfer from planetmint.backend.interfaces import Asset, MetaData +from planetmint.lib import remove_generated_fields pytestmark = pytest.mark.bdb @@ -41,171 +42,16 @@ def test_get_txids_filtered(signed_create_tx, signed_transfer_tx, db_conn): assert txids == {signed_transfer_tx.id} -def test_write_assets(db_conn): - from planetmint.backend.tarantool import query - - assets = [ - Asset("1", "1", "1"), - Asset("2", "2", "2"), - Asset("3", "3", "3"), - # Duplicated id. Should not be written to the database - Asset("1", "1", "1"), - ] - - # write the assets - for asset in assets: - query.store_asset(connection=db_conn, asset=asset) - - # check that 3 assets were written to the database - documents = query.get_assets(assets_ids=[asset.id for asset in assets], connection=db_conn) - - assert len(documents) == 3 - assert list(documents)[0] == assets[:-1][0] - - -def test_get_assets(db_conn): - from planetmint.backend.tarantool import query - - assets = [ - Asset("1", "1"), - Asset("2", "2"), - Asset("3", "3"), - ] - - query.store_assets(assets=assets, connection=db_conn) - - for asset in assets: - assert query.get_asset(asset_id=asset.id, connection=db_conn) - - -@pytest.mark.parametrize("table", ["assets", "metadata"]) -def test_text_search(table): - assert "PASS FOR NOW" - - # # Example data and tests cases taken from the mongodb documentation - # # https://docs.mongodb.com/manual/reference/operator/query/text/ - # objects = [ - # {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50}, - # {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, - # {'id': 3, 'subject': 'Baking a cake', 'author': 'abc', 'views': 90}, - # {'id': 4, 'subject': 'baking', 'author': 'xyz', 'views': 100}, - # {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200}, - # {'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80}, - # {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10}, - # {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10} - # ] - # - # # insert the assets - # conn.db[table].insert_many(deepcopy(objects), ordered=False) - # - # # test search single word - # assert list(query.text_search(conn, 'coffee', table=table)) == [ - # {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50}, - # {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, - # {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10}, - # ] - # - # # match any of the search terms - # assert list(query.text_search(conn, 'bake coffee cake', table=table)) == [ - # {'author': 'abc', 'id': 3, 'subject': 'Baking a cake', 'views': 90}, - # {'author': 'xyz', 'id': 1, 'subject': 'coffee', 'views': 50}, - # {'author': 'xyz', 'id': 4, 'subject': 'baking', 'views': 100}, - # {'author': 'efg', 'id': 2, 'subject': 'Coffee Shopping', 'views': 5}, - # {'author': 'efg', 'id': 7, 'subject': 'coffee and cream', 'views': 10} - # ] - # - # # search for a phrase - # assert list(query.text_search(conn, '\"coffee shop\"', table=table)) == [ - # {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, - # ] - # - # # exclude documents that contain a term - # assert list(query.text_search(conn, 'coffee -shop', table=table)) == [ - # {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50}, - # {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10}, - # ] - # - # # search different language - # assert list(query.text_search(conn, 'leche', language='es', table=table)) == [ - # {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200}, - # {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10} - # ] - # - # # case and diacritic insensitive search - # assert list(query.text_search(conn, 'сы́рники CAFÉS', table=table)) == [ - # {'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80}, - # {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200}, - # {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10} - # ] - # - # # case sensitive search - # assert list(query.text_search(conn, 'Coffee', case_sensitive=True, table=table)) == [ - # {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, - # ] - # - # # diacritic sensitive search - # assert list(query.text_search(conn, 'CAFÉ', diacritic_sensitive=True, table=table)) == [ - # {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200}, - # ] - # - # # return text score - # assert list(query.text_search(conn, 'coffee', text_score=True, table=table)) == [ - # {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50, 'score': 1.0}, - # {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5, 'score': 0.75}, - # {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10, 'score': 0.75}, - # ] - # - # # limit search result - # assert list(query.text_search(conn, 'coffee', limit=2, table=table)) == [ - # {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50}, - # {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, - # ] - - -def test_write_metadata(db_conn): - from planetmint.backend.tarantool import query - - metadata = [MetaData("1", "1"), MetaData("2", "2"), MetaData("3", "3")] - # write the assets - query.store_metadatas(connection=db_conn, metadata=metadata) - - # check that 3 assets were written to the database - metadatas = [] - for meta in metadata: - _data = db_conn.run(db_conn.space("meta_data").select(meta.id))[0] - metadatas.append(MetaData(_data[0], json.loads(_data[1]))) - - metadatas = sorted(metadatas, key=lambda k: k.id) - - assert len(metadatas) == 3 - assert list(metadatas) == metadata - - -def test_get_metadata(db_conn): - from planetmint.backend.tarantool import query - - metadata = [ - MetaData("dd86682db39e4b424df0eec1413cfad65488fd48712097c5d865ca8e8e059b64", None), - MetaData("55a2303e3bcd653e4b5bd7118d39c0e2d48ee2f18e22fbcf64e906439bdeb45d", {"key": "value"}), - ] - - # conn.db.metadata.insert_many(deepcopy(metadata), ordered=False) - query.store_metadatas(connection=db_conn, metadata=metadata) - - for meta in metadata: - _m = query.get_metadata(connection=db_conn, transaction_ids=[meta.id]) - assert _m - - def test_get_owned_ids(signed_create_tx, user_pk, db_conn): from planetmint.backend.tarantool import query # insert a transaction query.store_transactions(connection=db_conn, signed_transactions=[signed_create_tx.to_dict()]) - txns = list(query.get_owned_ids(connection=db_conn, owner=user_pk)) + + txns = query.get_owned_ids(connection=db_conn, owner=user_pk) tx_dict = signed_create_tx.to_dict() - founded = [tx for tx in txns if tx["transactions"].id == tx_dict["id"]] - assert founded[0]["transactions"].raw_transaction == tx_dict + owned_tx = remove_generated_fields(txns[0].to_dict()) + assert owned_tx == tx_dict def test_store_block(db_conn): @@ -231,104 +77,6 @@ def test_get_block(db_conn): assert block["height"] == 3 -# def test_delete_zero_unspent_outputs(db_context, utxoset): -# from planetmint.backend.tarantool import query -# return -# -# unspent_outputs, utxo_collection = utxoset -# -# delete_res = query.delete_unspent_outputs(db_context.conn) -# -# assert delete_res is None -# assert utxo_collection.count_documents({}) == 3 -# assert utxo_collection.count_documents( -# {'$or': [ -# {'transaction_id': 'a', 'output_index': 0}, -# {'transaction_id': 'b', 'output_index': 0}, -# {'transaction_id': 'a', 'output_index': 1}, -# ]} -# ) == 3 -# -# -# def test_delete_one_unspent_outputs(db_context, utxoset): -# return -# from planetmint.backend import query -# unspent_outputs, utxo_collection = utxoset -# delete_res = query.delete_unspent_outputs(db_context.conn, -# unspent_outputs[0]) -# assert delete_res.raw_result['n'] == 1 -# assert utxo_collection.count_documents( -# {'$or': [ -# {'transaction_id': 'a', 'output_index': 1}, -# {'transaction_id': 'b', 'output_index': 0}, -# ]} -# ) == 2 -# assert utxo_collection.count_documents( -# {'transaction_id': 'a', 'output_index': 0}) == 0 -# -# -# def test_delete_many_unspent_outputs(db_context, utxoset): -# return -# from planetmint.backend import query -# unspent_outputs, utxo_collection = utxoset -# delete_res = query.delete_unspent_outputs(db_context.conn, -# *unspent_outputs[::2]) -# assert delete_res.raw_result['n'] == 2 -# assert utxo_collection.count_documents( -# {'$or': [ -# {'transaction_id': 'a', 'output_index': 0}, -# {'transaction_id': 'b', 'output_index': 0}, -# ]} -# ) == 0 -# assert utxo_collection.count_documents( -# {'transaction_id': 'a', 'output_index': 1}) == 1 -# -# -# def test_store_zero_unspent_output(db_context, utxo_collection): -# return -# from planetmint.backend import query -# res = query.store_unspent_outputs(db_context.conn) -# assert res is None -# assert utxo_collection.count_documents({}) == 0 -# -# -# def test_store_one_unspent_output(db_context, -# unspent_output_1, utxo_collection): -# return -# from planetmint.backend import query -# res = query.store_unspent_outputs(db_context.conn, unspent_output_1) -# assert res.acknowledged -# assert len(res.inserted_ids) == 1 -# assert utxo_collection.count_documents( -# {'transaction_id': unspent_output_1['transaction_id'], -# 'output_index': unspent_output_1['output_index']} -# ) == 1 -# -# -# def test_store_many_unspent_outputs(db_context, -# unspent_outputs, utxo_collection): -# return -# from planetmint.backend import query -# res = query.store_unspent_outputs(db_context.conn, *unspent_outputs) -# assert res.acknowledged -# assert len(res.inserted_ids) == 3 -# assert utxo_collection.count_documents( -# {'transaction_id': unspent_outputs[0]['transaction_id']} -# ) == 3 -# -# -# def test_get_unspent_outputs(db_context, utxoset): -# return -# from planetmint.backend import query -# cursor = query.get_unspent_outputs(db_context.conn) -# assert cursor.collection.count_documents({}) == 3 -# retrieved_utxoset = list(cursor) -# unspent_outputs, utxo_collection = utxoset -# assert retrieved_utxoset == list( -# utxo_collection.find(projection={'_id': False})) -# assert retrieved_utxoset == unspent_outputs - - def test_store_pre_commit_state(db_conn): from planetmint.backend.tarantool import query