Skip to content

Commit

Permalink
refactor: enum int
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 13, 2024
1 parent 6b2b8b3 commit e147720
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
24 changes: 12 additions & 12 deletions src/ape/pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, config_wrapper: ConfigWrapper, isolation_manager: "IsolationM
self.config_wrapper = config_wrapper
self.isolation_manager = isolation_manager

@pytest.fixture(scope=Scope.SESSION.value)
@pytest.fixture(scope="session")
def accounts(self) -> list[TestAccountAPI]:
"""
A collection of pre-funded accounts.
Expand All @@ -44,52 +44,52 @@ def compilers(self):
"""
return self.compiler_manager

@pytest.fixture(scope=Scope.SESSION.value)
@pytest.fixture(scope="session")
def chain(self) -> ChainManager:
"""
Manipulate the blockchain, such as mine or change the pending timestamp.
"""
return self.chain_manager

@pytest.fixture(scope=Scope.SESSION.value)
@pytest.fixture(scope="session")
def networks(self) -> NetworkManager:
"""
Connect to other networks in your tests.
"""
return self.network_manager

@pytest.fixture(scope=Scope.SESSION.value)
@pytest.fixture(scope="session")
def project(self) -> ProjectManager:
"""
Access contract types and dependencies.
"""
return self.local_project

@pytest.fixture(scope=Scope.SESSION.value)
@pytest.fixture(scope="session")
def Contract(self):
"""
Instantiate a reference to an on-chain contract
using its address (same as ``ape.Contract``).
"""
return self.chain_manager.contracts.instance_at

@pytest.fixture(scope=Scope.SESSION.value)
@pytest.fixture(scope="session")
def _session_isolation(self) -> Iterator[None]:
yield from self.isolation_manager.isolation(Scope.SESSION)

@pytest.fixture(scope=Scope.PACKAGE.value)
@pytest.fixture(scope="package")
def _package_isolation(self) -> Iterator[None]:
yield from self.isolation_manager.isolation(Scope.PACKAGE)

@pytest.fixture(scope=Scope.MODULE.value)
@pytest.fixture(scope="module")
def _module_isolation(self) -> Iterator[None]:
yield from self.isolation_manager.isolation(Scope.MODULE)

@pytest.fixture(scope=Scope.CLASS.value)
@pytest.fixture(scope="class")
def _class_isolation(self) -> Iterator[None]:
yield from self.isolation_manager.isolation(Scope.CLASS)

@pytest.fixture(scope=Scope.FUNCTION.value)
@pytest.fixture(scope="function")
def _function_isolation(self) -> Iterator[None]:
yield from self.isolation_manager.isolation(Scope.FUNCTION)

Expand Down Expand Up @@ -139,8 +139,8 @@ def clear_snapshot_id(self, scope: Scope):
self[scope].identifier = None

def next_snapshots(self, scope: Scope) -> Iterator[Snapshot]:
for lower_scope in scope.lower_scopes:
yield self[lower_scope]
for scope_value in range(scope + 1, Scope.FUNCTION + 1):
yield self[scope_value] # type: ignore

def extend_fixtures(self, scope: Scope, fixtures: Iterable[str]):
self[scope].fixtures.extend(fixtures)
Expand Down
29 changes: 8 additions & 21 deletions src/ape/pytest/utils.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
from enum import Enum


class Scope(str, Enum):
SESSION = "session"
PACKAGE = "package"
MODULE = "module"
CLASS = "class"
FUNCTION = "function"
class Scope(int, Enum):
SESSION = 0
PACKAGE = 1
MODULE = 2
CLASS = 3
FUNCTION = 4

def __str__(self) -> str:
return self.value

@property
def lower_scopes(self) -> tuple["Scope", ...]:
if self is Scope.CLASS:
return (Scope.FUNCTION,)
elif self is Scope.MODULE:
return (Scope.CLASS, Scope.FUNCTION)
elif self is Scope.PACKAGE:
return (Scope.MODULE, Scope.CLASS, Scope.FUNCTION)
elif self is Scope.SESSION:
return (Scope.PACKAGE, Scope.MODULE, Scope.CLASS, Scope.FUNCTION)

return ()
return self.name.lower()

@property
def isolation_fixturename(self) -> str:
return f"_{self.value}_isolation"
return f"_{self}_isolation"

0 comments on commit e147720

Please sign in to comment.