Skip to content

Commit

Permalink
Test package cached properties (#1977)
Browse files Browse the repository at this point in the history
* Wrote a test case for cached_properties of the 'package' module

Signed-off-by: Harmouch101 <[email protected]>

* vi test_cache_utils.py

Signed-off-by: Harmouch101 <[email protected]>

* add newsfragment

Co-authored-by: Marc Garreau <[email protected]>
  • Loading branch information
wiseaidev and wolovim authored May 5, 2021
1 parent 06803e2 commit 48ce4c2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ Each Contract Factory exposes the following methods.
'0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'
.. py:classmethod:: Contract.constructor(*args, **kwargs).estimateGas(transaction=None, block_identifier=None)
:noindex:

Estimate gas for constructing and deploying the contract.

Expand All @@ -264,6 +265,7 @@ Each Contract Factory exposes the following methods.
12563
.. py:classmethod:: Contract.constructor(*args, **kwargs).buildTransaction(transaction=None)
:noindex:

Construct the contract deploy transaction bytecode data.

Expand Down Expand Up @@ -945,6 +947,7 @@ For example:
.. _processReceipt:

.. py:method:: ContractEvents.myEvent(*args, **kwargs).processReceipt(transaction_receipt, errors=WARN)
:noindex:

Extracts the pertinent logs from a transaction receipt.

Expand Down
1 change: 1 addition & 0 deletions docs/ethpm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Methods
Each ``Package`` exposes the following methods.

.. autoclass:: ethpm.Package
:noindex:
:members: from_file, from_uri, update_w3, get_contract_factory, get_contract_instance


Expand Down
22 changes: 17 additions & 5 deletions ethpm/_utils/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import functools
from typing import (
Any,
Callable,
Expand All @@ -15,10 +14,9 @@ class cached_property:
"""

def __init__(self, func: Callable[..., Any], name: str = None) -> None:
self.func = func
self.name = name or func.__name__
# type ignored b/c self is 'cached_property' but expects 'Callable[..., Any]'
functools.update_wrapper(self, func) # type: ignore
self.wrapped_func = func
self.name = name
self.__doc__ = getattr(func, '__doc__')

def __get__(self, instance: Any, cls: Any = None) -> Any:
"""
Expand All @@ -30,3 +28,17 @@ def __get__(self, instance: Any, cls: Any = None) -> Any:
return self
res = instance.__dict__[self.name] = self.func(instance)
return res

def __set_name__(self, cls: Any = None, name: str = None) -> None:
"""
The function is called at the time the cls class is created.
The descriptor would be assigned to name.
"""
if self.name is None:
self.name = name
self.func = self.wrapped_func
if name != self.name:
raise TypeError(
"Unable to assign cached_property for two different names "
f"(%{self.name} and {name})."
)
1 change: 1 addition & 0 deletions newsfragments/1977.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test ethpm caching + bump Sphinx version.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"py-geth>=2.4.0,<3",
"py-solc>=0.4.0",
"pytest>=4.4.0,<5.0.0",
"sphinx>=2.4.4,<3",
"sphinx>=3.0,<4",
"sphinx_rtd_theme>=0.1.9",
"toposort>=1.4",
"towncrier>=19.2.0,<20",
Expand Down
20 changes: 20 additions & 0 deletions tests/ethpm/_utils/test_cache_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import random

from ethpm._utils.cache import (
cached_property,
)


def test_cached_property():
class FOO:
def __int__(self):
pass

@cached_property
def generate_number(self):
return random.random()

foo = FOO()
foo_number = foo.generate_number
foo_cached_number = foo.generate_number
assert foo_number == foo_cached_number
24 changes: 24 additions & 0 deletions tests/ethpm/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,27 @@ def test_package_object_properties(safe_math_package):
assert safe_math_package.uri is None
assert safe_math_package.__repr__() == "<Package safe-math-lib==1.0.0>"
assert safe_math_package.contract_types == ["SafeMathLib"]


def test_cached_properties(piper_coin_manifest, safe_math_lib_package,
safe_math_lib_package_with_alias, w3):
package1 = Package(piper_coin_manifest, w3)
package2 = Package(piper_coin_manifest, w3)
first_build_dependencies_package1 = package1.build_dependencies.items()
second_build_dependencies_package1 = package1.build_dependencies.items()
assert first_build_dependencies_package1 == second_build_dependencies_package1
first_build_dependencies_package2 = package2.build_dependencies.items()
second_build_dependencies_package2 = package2.build_dependencies.items()
assert first_build_dependencies_package2 == first_build_dependencies_package2
assert not first_build_dependencies_package1 == first_build_dependencies_package2
assert not second_build_dependencies_package1 == second_build_dependencies_package2
package1 = safe_math_lib_package
first_deployments_package1 = package1.deployments
second_deployments_package1 = package1.deployments
assert first_deployments_package1 == second_deployments_package1
package2 = safe_math_lib_package_with_alias
first_deployments_package2 = package2.deployments
second_deployments_package2 = package2.deployments
assert first_deployments_package2 == second_deployments_package2
assert not first_deployments_package1 == first_deployments_package2
assert not second_deployments_package1 == second_deployments_package2

0 comments on commit 48ce4c2

Please sign in to comment.