2020-07-10 17:32:11 +02:00
|
|
|
import sys
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
2020-07-16 13:16:38 +02:00
|
|
|
import pytest
|
|
|
|
|
2020-07-10 17:32:11 +02:00
|
|
|
from toddlers.utils.requests import make_session
|
|
|
|
|
2020-07-16 13:16:38 +02:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def toddler(request, monkeypatch):
|
|
|
|
"""Fixture creating a toddler for a class testing it
|
|
|
|
|
|
|
|
The test class must set the toddler class in its `toddler_cls` attribute.
|
2020-07-10 17:32:11 +02:00
|
|
|
The fixture will mock out the `make_session` function before creating the
|
|
|
|
object so that any request objects the toddler creates in its constructor
|
|
|
|
won't be real.
|
2020-07-16 13:16:38 +02:00
|
|
|
"""
|
|
|
|
test_cls = request.cls
|
|
|
|
if not hasattr(test_cls, "toddler_cls"):
|
|
|
|
raise RuntimeError(f"{test_cls} needs to set `toddler_cls`")
|
|
|
|
|
|
|
|
toddler_cls = test_cls.toddler_cls
|
2020-07-10 17:32:11 +02:00
|
|
|
toddler_module = sys.modules[toddler_cls.__module__]
|
|
|
|
|
|
|
|
for name in dir(toddler_module):
|
|
|
|
obj = getattr(toddler_module, name)
|
|
|
|
if obj is make_session:
|
|
|
|
monkeypatch.setattr(toddler_module, name, MagicMock())
|
2020-07-16 13:16:38 +02:00
|
|
|
|
|
|
|
toddler_obj = toddler_cls()
|
|
|
|
return toddler_obj
|