Commit 53aeae2a authored by Tanguy Le Carrour's avatar Tanguy Le Carrour
Browse files

Implement use case 'extract sub tracks'.

parent fcdf20b6
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import List, Tuple
from pathlib import Path
from splito.domain.audio_editing.model import Tracks, Interval
@dataclass(frozen=True)
class ExtractSubTracksRequest:
path: str
intervals: List[Tuple[str, str]]
class ExtractSubTracksPresenter(ABC):
@abstractmethod
def sub_track_extracted(self, a_path: str) -> None:
pass
class ExtractSubTracksInteractor:
def __init__(self, presenter: ExtractSubTracksPresenter, tracks: Tracks) -> None:
self.__presenter = presenter
self.__tracks = tracks
def execute(self, request: ExtractSubTracksRequest) -> None:
track = self.__tracks[request.path]
intervals = [Interval(start, end) for (start, end) in request.intervals]
for idx, interval in enumerate(intervals):
sub_track = track.extract(interval)
path = self.__sub_path(request.path, idx + 1)
self.__tracks[path] = sub_track
self.__presenter.sub_track_extracted(path)
def __sub_path(self, a_path: str, an_index: int) -> str:
path = Path(a_path)
return str(path.with_suffix(f".part-{an_index}{path.suffix}"))
import mock
from mamba import before, describe, it # type: ignore
from robber import expect # type: ignore
from splito.use_cases.extract_sub_tracks import (
ExtractSubTracksInteractor,
ExtractSubTracksPresenter,
ExtractSubTracksRequest,
)
from splito.domain.audio_editing.model import Tracks
with describe(ExtractSubTracksInteractor):
with before.each as self:
self.tracks = mock.create_autospec(spec=Tracks)
self.presenter = mock.create_autospec(spec=ExtractSubTracksPresenter)
with describe(ExtractSubTracksInteractor.execute):
with it("extracts a sub track"):
interactor = ExtractSubTracksInteractor(self.presenter, self.tracks)
request = ExtractSubTracksRequest("a_track.wav", [("1", "2")])
interactor.execute(request)
expect(self.tracks.__setitem__).to.have.been.called()
expect(self.presenter.sub_track_extracted).to.have.been.called_with(
"a_track.part-1.wav"
)
with it("extracts multiple sub tracks"):
interactor = ExtractSubTracksInteractor(self.presenter, self.tracks)
request = ExtractSubTracksRequest("a_track.wav", [("1", "2"), ("3", "4")])
interactor.execute(request)
expect(self.tracks.__setitem__.call_count).to.eq(2)
expect(self.presenter.sub_track_extracted.call_count).to.eq(2)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment