add aircox.test utilities
This commit is contained in:
		@ -5,6 +5,7 @@ from datetime import datetime, time
 | 
			
		||||
import tzlocal
 | 
			
		||||
 | 
			
		||||
import pytest
 | 
			
		||||
from model_bakery import baker
 | 
			
		||||
 | 
			
		||||
from aircox import models
 | 
			
		||||
from aircox_streamer import controllers
 | 
			
		||||
@ -17,6 +18,33 @@ local_tz = tzlocal.get_localzone()
 | 
			
		||||
working_dir = os.path.join(os.path.dirname(__file__), "working_dir")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def interface(self, 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)}``.
 | 
			
		||||
    """
 | 
			
		||||
    if not isinstance(getattr(obj, "calls", None), dict):
 | 
			
		||||
        obj.calls = {}
 | 
			
		||||
 | 
			
		||||
    for attr, value in funcs.items():
 | 
			
		||||
 | 
			
		||||
        def func(*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
 | 
			
		||||
 | 
			
		||||
        obj.calls[attr] = None
 | 
			
		||||
        setattr(obj, attr, func)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FakeSocket:
 | 
			
		||||
    FAILING_ADDRESS = -1
 | 
			
		||||
    """Connect with this address fails."""
 | 
			
		||||
@ -142,6 +170,26 @@ def stream(program):
 | 
			
		||||
    return stream
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def episode(program):
 | 
			
		||||
    episode = baker.make(models.Episode, title="test episode", program=program)
 | 
			
		||||
    episode.playlist = lambda: ["/tmp/a", "/tmp/b"]
 | 
			
		||||
    return episode
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def sound(program, episode):
 | 
			
		||||
    return baker.make(
 | 
			
		||||
        models.Sound,
 | 
			
		||||
        program=program,
 | 
			
		||||
        episode=episode,
 | 
			
		||||
        name="sound",
 | 
			
		||||
        type=models.Sound.TYPE_ARCHIVE,
 | 
			
		||||
        position=0,
 | 
			
		||||
        file="sound.mp3",
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def sounds(program):
 | 
			
		||||
    items = [
 | 
			
		||||
@ -219,6 +267,9 @@ def metadata_string(metadata_data):
 | 
			
		||||
class FakeStreamer(controllers.Streamer):
 | 
			
		||||
    calls = {}
 | 
			
		||||
 | 
			
		||||
    def __init__(self, **kwargs):
 | 
			
		||||
        self.__dict__.update(**kwargs)
 | 
			
		||||
 | 
			
		||||
    def fetch(self):
 | 
			
		||||
        self.calls["fetch"] = True
 | 
			
		||||
 | 
			
		||||
@ -236,7 +287,7 @@ class FakeSource(controllers.Source):
 | 
			
		||||
    def sync(self):
 | 
			
		||||
        self.calls["sync"] = True
 | 
			
		||||
 | 
			
		||||
    def push(self, path):
 | 
			
		||||
    def push(self, *path):
 | 
			
		||||
        self.calls["push"] = path
 | 
			
		||||
        return path
 | 
			
		||||
 | 
			
		||||
@ -250,6 +301,24 @@ class FakeSource(controllers.Source):
 | 
			
		||||
        self.calls["seek"] = c
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FakePlaylist(FakeSource, controllers.PlaylistSource):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FakeQueueSource(FakeSource, controllers.QueueSource):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def streamer(station, station_ports):
 | 
			
		||||
    streamer = FakeStreamer(station)
 | 
			
		||||
    streamer.sources = [
 | 
			
		||||
        FakePlaylist(i, uri=f"source-{i}") for i in range(0, 3)
 | 
			
		||||
    ]
 | 
			
		||||
    streamer.sources.append(FakeQueueSource(len(streamer.sources)))
 | 
			
		||||
    return streamer
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def streamers(stations, stations_ports):
 | 
			
		||||
    streamers = controllers.Streamers(streamer_class=FakeStreamer)
 | 
			
		||||
@ -257,6 +326,9 @@ def streamers(stations, stations_ports):
 | 
			
		||||
    streamers.streamers = {
 | 
			
		||||
        station.pk: FakeStreamer(station) for station in stations
 | 
			
		||||
    }
 | 
			
		||||
    for streamer in streamers.values():
 | 
			
		||||
        streamer.sources = [FakeSource(i) for i in range(0, 3)]
 | 
			
		||||
    for j, streamer in enumerate(streamers.values()):
 | 
			
		||||
        streamer.sources = [
 | 
			
		||||
            FakePlaylist(i, uri=f"source-{j}-{i}") for i in range(0, 3)
 | 
			
		||||
        ]
 | 
			
		||||
        streamer.sources.append(FakeQueueSource(len(streamer.sources)))
 | 
			
		||||
    return streamers
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user