Skip to content

Commit

Permalink
catch load_source multiple times for same source
Browse files Browse the repository at this point in the history
  • Loading branch information
dbatten5 committed Oct 27, 2021
1 parent 213e5d9 commit 1e36fc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/phylm/phylm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,18 @@ def load_source(self, source: str) -> "Phylm":
UnrecognizedSourceError: if the source is not recognized
"""
if source == "imdb":
self._imdb = Imdb(raw_title=self.title)
if not self._imdb:
self._imdb = Imdb(raw_title=self.title)
return self

if source == "mtc":
self._mtc = Mtc(raw_title=self.title)
if not self._mtc:
self._mtc = Mtc(raw_title=self.title)
return self

if source == "rt":
self._rt = Rt(raw_title=self.title)
if not self._rt:
self._rt = Rt(raw_title=self.title)
return self

raise UnrecognizedSourceError(f"{source} is not a recognized source")
Expand Down
30 changes: 24 additions & 6 deletions tests/unit/test_phylm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from phylm.errors import SourceNotLoadedError
from phylm.errors import UnrecognizedSourceError

MODULE_PATH = "phylm.phylm"


class TestInit:
"""Tests for the __init__ method."""
Expand Down Expand Up @@ -41,7 +43,7 @@ def test_source_not_found(self) -> None:
):
phylm.load_source("bar")

@patch("phylm.phylm.Imdb", autospec=True)
@patch(f"{MODULE_PATH}.Imdb", autospec=True)
def test_recognized_source_imdb(self, mock_imdb: MagicMock) -> None:
"""
Given a `Phylm` instance,
Expand All @@ -59,7 +61,7 @@ def test_recognized_source_imdb(self, mock_imdb: MagicMock) -> None:
assert phylm.imdb == mock_imdb.return_value
mock_imdb.assert_called_once_with(raw_title="bar")

@patch("phylm.phylm.Mtc", autospec=True)
@patch(f"{MODULE_PATH}.Mtc", autospec=True)
def test_recognized_source_mtc(self, mock_mtc: MagicMock) -> None:
"""
Given a `Phylm` instance,
Expand All @@ -78,7 +80,7 @@ def test_recognized_source_mtc(self, mock_mtc: MagicMock) -> None:
assert phylm.mtc == mock_mtc.return_value
mock_mtc.assert_called_once_with(raw_title="bar")

@patch("phylm.phylm.Rt", autospec=True)
@patch(f"{MODULE_PATH}.Rt", autospec=True)
def test_recognized_source_rt(self, mock_rt: MagicMock) -> None:
"""
Given a `Phylm` instance,
Expand All @@ -97,12 +99,28 @@ def test_recognized_source_rt(self, mock_rt: MagicMock) -> None:
assert phylm.rt == mock_rt.return_value
mock_rt.assert_called_once_with(raw_title="bar")

@pytest.mark.parametrize("source_class", ("Rt", "Mtc", "Imdb"))
def test_source_already_loaded(self, source_class: str) -> None:
"""
Given phylm instance with a source already loaded,
When `load_source` is invoked with the same source,
Then nothing happens
"""
phylm = Phylm(title="foo")

with patch(f"{MODULE_PATH}.{source_class}") as mock_source:
source_name = source_class.lower()
phylm.load_source(source_name)
phylm.load_source(source_name)

assert mock_source.call_count == 1


class TestLoadSources:
"""Tests for the `load_sources` method."""

@patch("phylm.phylm.Mtc", autospec=True)
@patch("phylm.phylm.Rt", autospec=True)
@patch(f"{MODULE_PATH}.Mtc", autospec=True)
@patch(f"{MODULE_PATH}.Rt", autospec=True)
def test_success(self, mock_rt: MagicMock, mock_mtc: MagicMock) -> None:
"""
Given a list of sources,
Expand All @@ -118,7 +136,7 @@ def test_success(self, mock_rt: MagicMock, mock_mtc: MagicMock) -> None:
with pytest.raises(SourceNotLoadedError):
assert phylm.imdb is None

@patch("phylm.phylm.Rt", autospec=True)
@patch(f"{MODULE_PATH}.Rt", autospec=True)
def test_one_source_not_recognised(self, mock_rt: MagicMock) -> None:
"""
Given a list of sources where one is unrecognised,
Expand Down

0 comments on commit 1e36fc7

Please sign in to comment.