From 1e36fc7cccfb73b79e5bc5a582876987e902f94a Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Wed, 27 Oct 2021 13:27:50 +0200 Subject: [PATCH] catch `load_source` multiple times for same source --- src/phylm/phylm.py | 9 ++++++--- tests/unit/test_phylm.py | 30 ++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/phylm/phylm.py b/src/phylm/phylm.py index 6db6f67..7124879 100644 --- a/src/phylm/phylm.py +++ b/src/phylm/phylm.py @@ -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") diff --git a/tests/unit/test_phylm.py b/tests/unit/test_phylm.py index 335ea6a..8d7f80a 100644 --- a/tests/unit/test_phylm.py +++ b/tests/unit/test_phylm.py @@ -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.""" @@ -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, @@ -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, @@ -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, @@ -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, @@ -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,