mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 06:25:45 +00:00
fix tests
Signed-off-by: cybnon <stefan.weber93@googlemail.com>
This commit is contained in:
parent
2028eb449d
commit
3edd365646
@ -42,7 +42,7 @@ class Input:
|
||||
|
||||
return Input(tx_id, fulfills, owners_before, fulfillment)
|
||||
|
||||
def to_input_dict(self) -> dict:
|
||||
def to_dict(self) -> dict:
|
||||
fulfills = {
|
||||
"transaction_id": self.fulfills.transaction_id,
|
||||
"output_index": self.fulfills.output_index
|
||||
|
||||
@ -67,7 +67,7 @@ class Output:
|
||||
),
|
||||
)
|
||||
|
||||
def to_output_dict(self) -> dict:
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"id": self.tx_id,
|
||||
"amount": self.amount,
|
||||
|
||||
@ -12,6 +12,7 @@ class Transaction:
|
||||
id: str = ""
|
||||
operation: str = ""
|
||||
version: str = ""
|
||||
raw_transaction: dict = dict
|
||||
|
||||
@staticmethod
|
||||
def from_dict(transaction: dict) -> Transaction:
|
||||
@ -19,6 +20,7 @@ class Transaction:
|
||||
id=transaction["id"],
|
||||
operation=transaction["operation"],
|
||||
version=transaction["version"],
|
||||
raw_transaction=transaction["transaction"],
|
||||
)
|
||||
|
||||
|
||||
@ -28,11 +30,14 @@ class Transaction:
|
||||
id=transaction[0],
|
||||
operation=transaction[1],
|
||||
version=transaction[2],
|
||||
raw_transaction=transaction[3],
|
||||
)
|
||||
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"id": self.id,
|
||||
"operation": self.operation,
|
||||
"version": self.version,
|
||||
"transaction": self.raw_transaction,
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ def store_transaction(connection, transaction):
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_transaction(conn, transaction_id):
|
||||
def get_transaction(connection, transaction_id):
|
||||
"""Get a transaction from the database."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
@ -26,9 +26,8 @@ register_query = module_dispatch_registrar(query)
|
||||
def _group_transaction_by_ids(connection, txids: list):
|
||||
_transactions = []
|
||||
for txid in txids:
|
||||
_txobject = connection.run(connection.space(TARANT_TABLE_TRANSACTION).get(txid, index=TARANT_ID_SEARCH))
|
||||
|
||||
if _txobject is None:
|
||||
tx = get_transaction(connection, txid)
|
||||
if tx is None:
|
||||
continue
|
||||
|
||||
_txinputs = get_inputs_by_tx_id(connection, txid)
|
||||
@ -38,17 +37,17 @@ def _group_transaction_by_ids(connection, txids: list):
|
||||
_txmeta = get_metadata_by_tx_id(connection, txid)
|
||||
_txscript = get_script_by_tx_id(connection, txid)
|
||||
|
||||
_transaction = get_transaction(connection, txid)
|
||||
_transaction[TARANT_TABLE_TRANSACTION] = [tx.to_dict for tx in _transactions]
|
||||
_transaction[TARANT_TABLE_INPUT] + [input.to_input_dict() for input in _txinputs]
|
||||
_transaction[TARANT_TABLE_OUTPUT] = [output.to_output_dict() for output in _txoutputs]
|
||||
_transaction[TARANT_TABLE_KEYS] = [key.to_dict() for key in _txkeys]
|
||||
_transaction["assets"] = [asset.data for asset in _txassets]
|
||||
_transaction["metadata"] = _txmeta.metadata
|
||||
tx = {
|
||||
TARANT_TABLE_TRANSACTION: tx,
|
||||
TARANT_TABLE_INPUT: [tx_input.to_dict() for tx_input in _txinputs],
|
||||
TARANT_TABLE_OUTPUT: [output.to_dict() for output in _txoutputs],
|
||||
TARANT_TABLE_KEYS: [key.to_dict() for key in _txkeys],
|
||||
TARANT_TABLE_ASSETS: _txassets,
|
||||
TARANT_TABLE_META_DATA: _txmeta,
|
||||
TARANT_TABLE_SCRIPT: _txscript.script if _txscript else None,
|
||||
}
|
||||
|
||||
if _txscript.script:
|
||||
_transaction[TARANT_TABLE_SCRIPT] = _txscript.script
|
||||
_transactions.append(_transaction)
|
||||
_transactions.append(tx)
|
||||
return _transactions
|
||||
|
||||
|
||||
@ -141,6 +140,7 @@ def store_transaction_keys(connection, keys: Keys, output_id: str, index: int):
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_transactions(connection, signed_transactions: list):
|
||||
for transaction in signed_transactions:
|
||||
store_transaction(connection, transaction)
|
||||
|
||||
[store_transaction_inputs(connection, Input.from_dict(input, transaction["id"]), index) for
|
||||
index, input in enumerate(transaction[TARANT_TABLE_INPUT])]
|
||||
@ -165,19 +165,20 @@ def store_transactions(connection, signed_transactions: list):
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_transaction(connection, transaction):
|
||||
tx = Transaction(id=transaction["id"], operation=transaction["operation"], version=transaction["version"])
|
||||
tx = (transaction["id"], transaction["operation"], transaction["version"],
|
||||
transaction)
|
||||
connection.run(connection.space(TARANT_TABLE_TRANSACTION).insert(
|
||||
tx.id,
|
||||
tx.operation,
|
||||
tx.version,
|
||||
tx
|
||||
),
|
||||
only_data=False)
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_transaction(connection, transaction_id: str) -> Transaction:
|
||||
return Transaction.from_tuple(
|
||||
connection.run(connection.space(TARANT_TABLE_TRANSACTION).get(transaction_id, index=TARANT_ID_SEARCH)))
|
||||
def get_transaction(connection, transaction_id):
|
||||
txs = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(transaction_id, index=TARANT_ID_SEARCH))
|
||||
if len(txs) == 0:
|
||||
return None
|
||||
return Transaction.from_tuple(txs[0])
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
|
||||
@ -8,11 +8,8 @@ MongoDB.
|
||||
|
||||
"""
|
||||
import logging
|
||||
from collections import namedtuple
|
||||
from uuid import uuid4
|
||||
from planetmint.backend.connection import Connection
|
||||
|
||||
from hashlib import sha3_256
|
||||
import json
|
||||
import rapidjson
|
||||
import requests
|
||||
@ -40,6 +37,7 @@ from transactions.common.transaction import VALIDATOR_ELECTION, CHAIN_MIGRATION_
|
||||
from transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT, BROADCAST_TX_ASYNC, BROADCAST_TX_SYNC
|
||||
from transactions.types.elections.election import Election
|
||||
from transactions.types.elections.validator_utils import election_id_to_public_key
|
||||
|
||||
from planetmint.config import Config
|
||||
from planetmint import backend, config_utils, fastquery
|
||||
from planetmint.tendermint_utils import (
|
||||
@ -230,8 +228,7 @@ class Planetmint(object):
|
||||
return bool(transaction)
|
||||
|
||||
def get_transaction(self, transaction_id):
|
||||
transaction = backend.query.get_transaction(self.connection, transaction_id)
|
||||
return Transaction.from_dict(transaction, False)
|
||||
return backend.query.get_transaction(self.connection, transaction_id)
|
||||
|
||||
def get_transactions(self, txn_ids):
|
||||
return backend.query.get_transactions(self.connection, txn_ids)
|
||||
|
||||
@ -204,8 +204,8 @@ def test_get_owned_ids(signed_create_tx, user_pk, db_conn):
|
||||
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))
|
||||
tx_dict = signed_create_tx.to_dict()
|
||||
founded = [tx for tx in txns if tx["id"] == tx_dict["id"]]
|
||||
assert founded[0] == tx_dict
|
||||
founded = [tx for tx in txns if tx["transactions"].id == tx_dict["id"]]
|
||||
assert founded[0]["transactions"].raw_transaction == tx_dict
|
||||
|
||||
|
||||
def test_get_spending_transactions(user_pk, user_sk, db_conn):
|
||||
@ -225,7 +225,8 @@ def test_get_spending_transactions(user_pk, user_sk, db_conn):
|
||||
txns = list(query.get_spending_transactions(connection=db_conn, inputs=links))
|
||||
|
||||
# tx3 not a member because input 1 not asked for
|
||||
assert txns == [tx2.to_dict(), tx4.to_dict()]
|
||||
assert txns[0]["transactions"].raw_transaction == tx2.to_dict()
|
||||
assert txns[1]["transactions"].raw_transaction == tx4.to_dict()
|
||||
|
||||
|
||||
def test_get_spending_transactions_multiple_inputs(db_conn):
|
||||
@ -261,7 +262,7 @@ def test_get_spending_transactions_multiple_inputs(db_conn):
|
||||
txns = list(query.get_spending_transactions(connection=db_conn, inputs=[li]))
|
||||
assert len(txns) == num
|
||||
if len(txns):
|
||||
assert [tx["id"] for tx in txns] == match
|
||||
assert [tx["transactions"].id for tx in txns] == match
|
||||
|
||||
|
||||
def test_store_block(db_conn):
|
||||
|
||||
@ -110,10 +110,10 @@ class TestBigchainApi(object):
|
||||
before = tx.to_dict()
|
||||
after = tx_from_db.to_dict()
|
||||
|
||||
assert before["assets"][0]["data"] == after["assets"][0]["data"]
|
||||
assert before["assets"][0]["data"] == after["transaction"]["assets"][0]["data"]
|
||||
before.pop("asset", None)
|
||||
after.pop("asset", None)
|
||||
assert before == after
|
||||
after["transaction"].pop("asset", None)
|
||||
assert before == after["transaction"]
|
||||
|
||||
|
||||
class TestTransactionValidation(object):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user