mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Problem: No test for memoization
Solution: Add tests for `to_dict` and `from_dict` memoization
This commit is contained in:
parent
9689072ea4
commit
0b9e4836c7
67
tests/common/test_memoize.py
Normal file
67
tests/common/test_memoize.py
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
# Copyright BigchainDB GmbH and BigchainDB contributors
|
||||||
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
from bigchaindb.models import Transaction
|
||||||
|
from bigchaindb.common.crypto import generate_key_pair
|
||||||
|
from bigchaindb.common.memoize import to_dict, from_dict
|
||||||
|
|
||||||
|
|
||||||
|
pytestmark = [pytest.mark.tendermint, pytest.mark.bdb]
|
||||||
|
|
||||||
|
|
||||||
|
def test_memoize_to_dict(b):
|
||||||
|
alice = generate_key_pair()
|
||||||
|
asset = {
|
||||||
|
'data': {'id': 'test_id'},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert to_dict.cache_info().hits == 0
|
||||||
|
assert to_dict.cache_info().misses == 0
|
||||||
|
|
||||||
|
tx = Transaction.create([alice.public_key],
|
||||||
|
[([alice.public_key], 1)],
|
||||||
|
asset=asset,)\
|
||||||
|
.sign([alice.private_key])
|
||||||
|
|
||||||
|
|
||||||
|
tx.to_dict()
|
||||||
|
|
||||||
|
assert to_dict.cache_info().hits == 0
|
||||||
|
assert to_dict.cache_info().misses == 1
|
||||||
|
|
||||||
|
tx.to_dict()
|
||||||
|
tx.to_dict()
|
||||||
|
|
||||||
|
assert to_dict.cache_info().hits == 2
|
||||||
|
assert to_dict.cache_info().misses == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_memoize_from_dict(b):
|
||||||
|
alice = generate_key_pair()
|
||||||
|
asset = {
|
||||||
|
'data': {'id': 'test_id'},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert from_dict.cache_info().hits == 0
|
||||||
|
assert from_dict.cache_info().misses == 0
|
||||||
|
|
||||||
|
tx = Transaction.create([alice.public_key],
|
||||||
|
[([alice.public_key], 1)],
|
||||||
|
asset=asset,)\
|
||||||
|
.sign([alice.private_key])
|
||||||
|
tx_dict = deepcopy(tx.to_dict())
|
||||||
|
|
||||||
|
Transaction.from_dict(tx_dict)
|
||||||
|
|
||||||
|
assert from_dict.cache_info().hits == 0
|
||||||
|
assert from_dict.cache_info().misses == 1
|
||||||
|
|
||||||
|
Transaction.from_dict(tx_dict)
|
||||||
|
Transaction.from_dict(tx_dict)
|
||||||
|
|
||||||
|
assert from_dict.cache_info().hits == 2
|
||||||
|
assert from_dict.cache_info().misses == 1
|
||||||
Loading…
x
Reference in New Issue
Block a user