Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Aug 23, 2024
1 parent caf545b commit e8dcc4f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ def providers(self): # -> dict[str, Partial[ProviderAPI]]
from ape.plugins._utils import clean_plugin_name

providers = {}
for _, plugin_tuple in self.plugin_manager.providers:
for _, plugin_tuple in self._get_plugin_providers():
ecosystem_name, network_name, provider_class = plugin_tuple
provider_name = clean_plugin_name(provider_class.__module__.split(".")[0])
is_custom_with_config = self._is_custom and self.default_provider_name == provider_name
Expand Down Expand Up @@ -1048,6 +1048,10 @@ def providers(self): # -> dict[str, Partial[ProviderAPI]]

return providers

def _get_plugin_providers(self):
# NOTE: Abstracted for testing purposes.
return self.plugin_manager.providers

def get_provider(
self,
provider_name: Optional[str] = None,
Expand Down
40 changes: 40 additions & 0 deletions tests/functional/test_network_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,43 @@ def test_create_network_type_fork():
actual = create_network_type(chain_id, chain_id, is_fork=True)
assert issubclass(actual, NetworkAPI)
assert issubclass(actual, ForkedNetworkAPI)


def test_providers(ethereum):
network = ethereum.local
providers = network.providers
assert "test" in providers
assert "node" in providers


def test_providers_custom_network(project, custom_networks_config_dict, ethereum):
with project.temp_config(**custom_networks_config_dict):
network = ethereum.apenet
actual = network.providers
assert "node" in actual


def test_providers_custom_non_fork_network_does_not_use_fork_provider(
mocker, project, custom_networks_config_dict, ethereum
):
# NOTE: Have to a mock a Fork providers since none ship with Ape core.
with project.temp_config(**custom_networks_config_dict):
network = ethereum.apenet
network.__dict__.pop("providers", None) # de-cache

# Setup mock fork provider.
orig = network._get_plugin_providers
network._get_plugin_providers = mocker.MagicMock()
name = "foobar"

class MyForkProvider:
__module__ = "foobar.test"

network._get_plugin_providers.return_value = iter(
[(name, ("ethereum", "local", MyForkProvider))]
)
try:
actual = network.providers
assert name not in actual
finally:
network._get_plugin_providers = orig

0 comments on commit e8dcc4f

Please sign in to comment.