Joël Luijmes
02/01/2021, 3:27 PM@pytest.fixture
def mock_resource_manager(monkeypatch):
mock = MagicMock(return_value=None)
monkeypatch.setattr("prefect.tasks.core.resource_manager.ResourceManager.__call__", mock)
return mock
But this gives weird errors down the road, as I also want to mock prefect.config/context for other purposes.Josh Greenhalgh
02/01/2021, 3:30 PMsetattr
on the actual import;
# flow.py
from prefect.tasks.core.resource_manager import ResourceManager
...
# test_flow.py
@pytest.fixture
def mock_resource_manager(monkeypatch):
mock = MagicMock(return_value=None)
monkeypatch.setattr("flow.ResourceManager.__call__", mock)
return mock
Joël Luijmes
02/02/2021, 2:02 PM@resource_manager
class CloudSQLProxyManager:
def __init__(self, name, instance, keyfile):
self.instance = instance
self.keyfile = keyfile
self.name = name
self.skip_deployment = prefect.config.get('environment', 'development').lower() != 'production'
def setup(self):
if self.skip_deployment:
<http://prefect.context.logger.info|prefect.context.logger.info>(f"Skipping creation of CloudSQL Proxy")
raise signals.SUCCESS("Creation of CloudSQL Proxy skipped")
# ....
I tried your suggestion, without mocking:
`def test__setup__skips_on_non_production(mock_resource_manager):
prefect.config['environment'] = 'development'
flow = Flow("unit test")
manager = CloudSQLProxyManager("name", "instance", "keyfile", flow=flow)
with pytest.raises(signals.SUCCESS, match="Creation of CloudSQL Proxy skipped"):
print(manager)
manager.setup(manager)
But then manager is an instance of ResourceManager (and not CloudSQLLProxyManager)
I tried mocking the call (as shown in my initial post), but that makes manager None.@pytest.fixture
def CloudSQLProxyManager(monkeypatch):
monkeypatch.setattr("prefect.tasks.core.resource_manager.ResourceManager.__call__", lambda x: x)
from src.modules.cloudsql_proxy_manager import CloudSQLProxyManager
return CloudSQLProxyManager().resource_class
This fixture returns the original class - without decorations - and now I can finally unit test my setup function 🙂