mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 22:45:44 +00:00
test_quries.py -> metadata error fixed
This commit is contained in:
parent
4f802c64a2
commit
b920ed3500
@ -100,9 +100,7 @@ def store_metadatas(connection, metadata: list):
|
||||
space = connection.space("meta_data")
|
||||
for meta in metadata:
|
||||
|
||||
data = meta["data"] if not "metadata" in meta else meta["metadata"]
|
||||
if data:
|
||||
space.insert((meta["id"], meta["data"] if not "metadata" in meta else meta["metadata"]))
|
||||
space.insert((meta["id"], meta["data"] if not "metadata" in meta else meta["metadata"]))
|
||||
|
||||
|
||||
@register_query(TarantoolDB)
|
||||
@ -117,18 +115,11 @@ def get_metadata(connection, transaction_ids: list):
|
||||
|
||||
|
||||
@register_query(TarantoolDB)
|
||||
# asset: {"id": "asset_id"}
|
||||
# asset: {"data": any} -> insert (tx_id, asset["data"]).
|
||||
# def store_asset(connection, asset: dict, tx_id=None):
|
||||
def store_asset(connection, asset: dict):
|
||||
space = connection.space("assets")
|
||||
# print(f"DATA store asset: {asset}")
|
||||
convert = lambda obj: obj if isinstance(obj, tuple) else (obj, obj["id"], obj["id"])
|
||||
try:
|
||||
space.insert(asset)
|
||||
# if tx_id is not None:
|
||||
# space.insert((asset, tx_id, tx_id))
|
||||
# else:
|
||||
# space.insert((asset, str(asset["id"]), str(asset["id"]))) # TODO Review this function
|
||||
space.insert(convert(asset))
|
||||
except: # TODO Add Raise For Duplicate
|
||||
print("DUPLICATE ERROR")
|
||||
|
||||
@ -136,9 +127,10 @@ def store_asset(connection, asset: dict):
|
||||
@register_query(TarantoolDB)
|
||||
def store_assets(connection, assets: list):
|
||||
space = connection.space("assets")
|
||||
convert = lambda obj: obj if isinstance(obj, tuple) else (obj, obj["id"], obj["id"])
|
||||
for asset in assets:
|
||||
try:
|
||||
space.insert(asset)
|
||||
space.insert(convert(asset))
|
||||
except Exception as ex: # TODO Raise ERROR for Duplicate
|
||||
print(f"EXCEPTION : {ex} ")
|
||||
|
||||
@ -148,7 +140,8 @@ def get_asset(connection, asset_id: str):
|
||||
space = connection.space("assets")
|
||||
_data = space.select(asset_id, index="txid_search")
|
||||
_data = _data.data
|
||||
return tuple(_data[0]) if len(_data) > 0 else []
|
||||
print(f"ASSSSSSET :: {_data} ::")
|
||||
return _data[0][0] if len(_data) > 0 else []
|
||||
|
||||
|
||||
@register_query(TarantoolDB)
|
||||
@ -157,7 +150,7 @@ def get_assets(connection, assets_ids: list) -> list:
|
||||
for _id in list(set(assets_ids)):
|
||||
asset = get_asset(connection, _id)
|
||||
_returned_data.append(asset)
|
||||
return sorted(_returned_data, key=lambda k: k[2], reverse=False)
|
||||
return sorted(_returned_data, key=lambda k: k["id"], reverse=False)
|
||||
|
||||
|
||||
@register_query(TarantoolDB)
|
||||
|
||||
@ -152,10 +152,9 @@ class TransactionCompose:
|
||||
return self.db_results["transaction"][0]
|
||||
|
||||
def _get_asset(self):
|
||||
# print( f" asset : {self.db_results}" )
|
||||
_asset = iter(self.db_results["asset"])
|
||||
# return _asset
|
||||
return next(iter(next(_asset, iter([]))), None)
|
||||
_res_asset = next(iter(next(_asset, iter([]))), None)
|
||||
return _res_asset
|
||||
|
||||
def _get_metadata(self):
|
||||
return self.db_results["metadata"][0][1] if len(self.db_results["metadata"]) == 1 else None
|
||||
@ -175,7 +174,6 @@ class TransactionCompose:
|
||||
def _get_outputs(self):
|
||||
_outputs = []
|
||||
for _output in self.db_results["outputs"]:
|
||||
# print (f"\noutput : {_output}")
|
||||
_out = copy.deepcopy(self._map["outputs"][_output[-1]])
|
||||
_out["amount"] = _output[1]
|
||||
_tmp_keys = [(_key[3], _key[4]) for _key in self.db_results["keys"] if _key[2] == _output[5]]
|
||||
|
||||
@ -51,11 +51,11 @@ def test_write_assets(db_conn):
|
||||
# conn = Connection().get_connection()
|
||||
conn = db_conn.get_connection()
|
||||
assets = [
|
||||
({'id': '1', 'data': '1'}, '1', '1'),
|
||||
({'id': '2', 'data': '2'}, '2', '2'),
|
||||
({'id': '3', 'data': '3'}, '3', '3'),
|
||||
{'id': '1', 'data': '1'},
|
||||
{'id': '2', 'data': '2'},
|
||||
{'id': '3', 'data': '3'},
|
||||
# Duplicated id. Should not be written to the database
|
||||
({'id': '1', 'data': '1'}, '1', '1'),
|
||||
{'id': '1', 'data': '1'},
|
||||
]
|
||||
|
||||
# write the assets
|
||||
@ -63,16 +63,15 @@ def test_write_assets(db_conn):
|
||||
query.store_asset(connection=conn, asset=asset)
|
||||
|
||||
# check that 3 assets were written to the database
|
||||
documents = query.get_assets(assets_ids=[asset[2] for asset in assets], connection=conn)
|
||||
|
||||
documents = query.get_assets(assets_ids=[asset["id"] for asset in assets], connection=conn)
|
||||
print(f"\nDOCUMENTS: {documents}")
|
||||
print(f"\nASSETS: {assets}")
|
||||
assert len(documents) == 3
|
||||
assert list(documents)[0] == assets[:-1][0]
|
||||
|
||||
|
||||
def test_get_assets(db_conn):
|
||||
# from planetmint.backend.connection import Connection
|
||||
from planetmint.backend.tarantool import query
|
||||
# conn = Connection().get_connection()
|
||||
conn = db_conn.get_connection()
|
||||
assets = [
|
||||
("1", '1', '1'),
|
||||
@ -210,7 +209,8 @@ def test_get_metadata(db_conn):
|
||||
query.store_metadatas(connection=conn, metadata=metadata)
|
||||
|
||||
for meta in metadata:
|
||||
assert query.get_metadata(connection=conn, transaction_ids=[meta["id"]])
|
||||
_m = query.get_metadata(connection=conn, transaction_ids=[meta["id"]])
|
||||
assert _m
|
||||
|
||||
|
||||
def test_get_owned_ids(signed_create_tx, user_pk, db_conn):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user