json
07/20/2022, 2:14 PMjson
07/20/2022, 2:19 PMMike Geeves
07/20/2022, 2:47 PMMike Geeves
07/20/2022, 2:49 PMMike Geeves
07/20/2022, 2:49 PMjson
07/20/2022, 2:51 PMMike Geeves
07/20/2022, 2:53 PMjson
07/20/2022, 2:54 PMfrom function_lib import helper_function
class ThingDoer:
def do_thing(self):
value = helper_function()
# < snip out lots of logic on the value >
return value
json
07/20/2022, 2:55 PMdo_thing()
function, but not how to monkey patch over the helper_function()
call, which is ultimately the bit I want to overwriteMike Geeves
07/20/2022, 2:56 PMjson
07/20/2022, 2:57 PMMike Geeves
07/20/2022, 3:26 PMbearly_utils/ig.py
in a completely separate package
in my test code I can then do:
from bearly_utils import ig
and later:
class MockIgAuth(NamedTuple):
identifier: str
password: str
key: str
environment: str = "demo"
@pytest.fixture
def patch_bearly_utils(monkeypatch):
def get_auth(account):
account_json = json.loads(account)
mock_account = account_json["account"]["mock"]
return MockIgAuth(
identifier=mock_account["identifier"], password=mock_account["password"], key=mock_account["key"]
)
monkeypatch.setattr(ig, "get_auth", get_auth)
The key bit with the monkeypatch is you are importing something as ig
, and where you're using it for real, you have the same import line.... I mean here if I had just import bearly_utils
and then tried to do bearly_utils.ig.get_auth
it wouldn't match up with the monkeypatched one. I hope that makes sense 😄
When it's used in the actual non-test code it's called like:
auth = ig.get_auth(account=account)
json
07/20/2022, 4:00 PMMike Geeves
07/20/2022, 4:12 PMMike Geeves
07/20/2022, 4:13 PMMike Geeves
07/20/2022, 4:14 PMjson
07/20/2022, 4:15 PMMike Geeves
07/20/2022, 4:16 PMjson
07/20/2022, 4:16 PMjson
07/20/2022, 4:16 PMMike Geeves
07/20/2022, 4:16 PMjson
07/20/2022, 4:17 PMMike Geeves
07/20/2022, 4:18 PMMike Geeves
07/20/2022, 4:19 PMjson
07/20/2022, 4:19 PMjson
07/20/2022, 4:23 PMjson
07/20/2022, 4:23 PMMike Geeves
07/20/2022, 4:24 PMjson
07/20/2022, 4:25 PMMike Geeves
07/20/2022, 4:25 PMMike Geeves
07/20/2022, 4:26 PMjson
07/20/2022, 4:26 PMMike Geeves
07/20/2022, 4:33 PMif the provider logic was the samesame as? in principle i like the idea of a nice and consistent calling a cli if that's what you meant, but I've gone for wrapping it up everywhere so my ci/cd does a "make test" regardless of what it is where possible (again, it's an approach, rather than necessarily being the best route)
json
07/20/2022, 4:34 PMMike Geeves
07/20/2022, 4:35 PMMike Geeves
07/20/2022, 5:05 PMMike Geeves
07/26/2022, 8:53 AMjson
07/26/2022, 1:23 PM