diff --git a/aircox/controllers/diffusions.py b/aircox/controllers/diffusions.py deleted file mode 100644 index 2527ecb..0000000 --- a/aircox/controllers/diffusions.py +++ /dev/null @@ -1,58 +0,0 @@ -import datetime -import logging - -from django.db import transaction - -from aircox.models import Diffusion, Schedule - -logger = logging.getLogger("aircox.commands") - - -__all__ = ("Diffusions",) - - -class Diffusions: - """Handle generation and update of Diffusion instances.""" - - date = None - - def __init__(self, date): - self.date = date or datetime.date.today() - - def update(self): - episodes, diffusions = [], [] - for schedule in Schedule.objects.filter( - program__active=True, initial__isnull=True - ): - eps, diffs = schedule.diffusions_of_month(self.date) - if eps: - episodes += eps - if diffs: - diffusions += diffs - - logger.info( - "[update] %s: %d episodes, %d diffusions and reruns", - str(schedule), - len(eps), - len(diffs), - ) - - with transaction.atomic(): - logger.info( - "[update] save %d episodes and %d diffusions", - len(episodes), - len(diffusions), - ) - for episode in episodes: - episode.save() - for diffusion in diffusions: - # force episode id's update - diffusion.episode = diffusion.episode - diffusion.save() - - def clean(self): - qs = Diffusion.objects.filter( - type=Diffusion.TYPE_UNCONFIRMED, start__lt=self.date - ) - logger.info("[clean] %d diffusions will be removed", qs.count()) - qs.delete() diff --git a/aircox/test.py b/aircox/test.py index c67f2f3..d5c61b0 100644 --- a/aircox/test.py +++ b/aircox/test.py @@ -86,7 +86,7 @@ class WrapperMixin: ns_target = getattr(ns, ns_attr, None) if self.target is ns_target: return - elif self.target is not None: + elif self.target is not None and self.ns: raise RuntimeError( "self target already injected. It must be " "`release` before `inject`." @@ -97,12 +97,14 @@ class WrapperMixin: self.ns = ns self.ns_attr = ns_attr + return self def release(self): """Remove injection from previously injected parent, reset target.""" if self.ns_target is self.interface: setattr(self.ns, self.ns_attr, self.target) self.target = None + return self class SpoofMixin: @@ -190,6 +192,16 @@ class Interface: self.interface = interface super().__init__(**kwargs) + def clone(self, **kwargs): + """Return an Interface copying some values from self.""" + kwargs.update( + { + "target": self.target, + "funcs": self.funcs, + } + ) + return type(self.interface)(_imeta_kw=kwargs)._imeta + def __getitem__(self, name): return self.traces[name] diff --git a/aircox/tests/conftest.py b/aircox/tests/conftest.py index 7e6833b..37342cf 100644 --- a/aircox/tests/conftest.py +++ b/aircox/tests/conftest.py @@ -1,10 +1,20 @@ from datetime import time, timedelta import itertools +import logging import pytest from model_bakery import baker from aircox import models +from aircox.test import Interface + + +@pytest.fixture +def logger(): + logger = Interface( + logging, {"info": None, "debug": None, "error": None, "warning": None} + ) + return logger @pytest.fixture diff --git a/aircox/tests/controllers/test_diffusions.py b/aircox/tests/controllers/test_diffusions.py deleted file mode 100644 index dc4a153..0000000 --- a/aircox/tests/controllers/test_diffusions.py +++ /dev/null @@ -1,19 +0,0 @@ -import pytest - -from aircox.controllers.diffusions import Diffusions - - -@pytest.fixture -def diffusions(): - return Diffusions - - -class TestDiffusion: - def test___init__(self): - pass - - def test_update(self): - pass - - def test_clean(self): - pass diff --git a/aircox/tests/controllers/test_sound_monitor.py b/aircox/tests/controllers/test_sound_monitor.py index b792346..e163fec 100644 --- a/aircox/tests/controllers/test_sound_monitor.py +++ b/aircox/tests/controllers/test_sound_monitor.py @@ -1,5 +1,4 @@ from concurrent import futures -import logging import pytest from django.utils import timezone as tz @@ -18,14 +17,6 @@ def event(): return Interface(src_path="/tmp/src_path", dest_path="/tmp/dest_path") -@pytest.fixture -def logger(): - logger = Interface( - logging, {"info": None, "debug": None, "error": None, "warning": None} - ) - return logger - - @pytest.fixture def interfaces(): items = { diff --git a/aircox_streamer/tests/conftest.py b/aircox_streamer/tests/conftest.py index 2778272..782c1b0 100644 --- a/aircox_streamer/tests/conftest.py +++ b/aircox_streamer/tests/conftest.py @@ -18,36 +18,6 @@ local_tz = tzlocal.get_localzone() working_dir = os.path.join(os.path.dirname(__file__), "working_dir") -def interface_wrap(obj, attr, value): - if not isinstance(getattr(obj, "calls", None), dict): - obj.calls = {} - obj.calls[attr] = None - - def wrapper(*a, **kw): - call = obj.calls.get(attr) - if call is None: - obj.calls[attr] = (a, kw) - elif isinstance(call, tuple): - obj.calls[attr] = [call, (a, kw)] - else: - call.append((a, kw)) - return value - - setattr(obj, attr, wrapper) - - -def interface(obj, funcs): - """Override provided object's functions using dict of funcs, as ``{ - func_name: return_value}``. - - Attribute ``obj.calls`` is a dict - with all call done using those methods, as - ``{func_name: (args, kwargs)}``. - """ - for attr, value in funcs.items(): - interface_wrap(obj, attr, value) - - class FakeSocket: FAILING_ADDRESS = -1 """Connect with this address fails."""