From a055a5b5ac7488f897a5acf9dc5b3a086907c6af Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Wed, 3 Nov 2021 09:17:27 +0100 Subject: [PATCH 1/3] allow `imdb_id` at `phylm` instantiation --- docs/imdb.md | 21 +++++++++++++++------ src/phylm/phylm.py | 15 +++++++++++---- tests/unit/test_phylm.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/docs/imdb.md b/docs/imdb.md index 2e6eea8..b455d3c 100644 --- a/docs/imdb.md +++ b/docs/imdb.md @@ -17,17 +17,26 @@ imdb = Imdb(raw_title="The Matrix") ## Movie ID -If you know the IMDb movie id you can pass it to `load_sources` to perform a more -accurate search for the movie: +If you know the IMDb movie ID you can instantiate the `Phylm` class with an `imdb_id` +property: ```python -phylm.load_source("imdb", movie_id="0133093") +from phylm.sources import Imdb + +imdb = Imdb(raw_title="The Matrix", imdb_id="0133093") ``` -`phylm` will first perform a search based on the ID. If the ID is valid the result will -be selected, if not then it will fall back to a title search. +Then, when running `load_source` for `imdb`, `phylm` will first perform a search based +on the ID. If the ID is valid the result will be selected, if not then it will fall back +to a title search. + +Alternatively, you can pass it to `load_source` with `"imdb"` as the first argument: + +```python +phylm.load_source("imdb", imdb_id="0133093") +``` -Alternatively, instantiate the IMDb source class with the ID: +Or instantiate the IMDb source class with the ID: ```python from phylm.sources import Imdb diff --git a/src/phylm/phylm.py b/src/phylm/phylm.py index 53d5c03..31b83e1 100644 --- a/src/phylm/phylm.py +++ b/src/phylm/phylm.py @@ -12,9 +12,15 @@ class Phylm: """Main `Phylm` entrypoint.""" - def __init__(self, title: str) -> None: - """Initialize a `Phylm` object.""" - self.title: str = title + def __init__(self, title: str, imdb_id: Optional[str] = None) -> None: + """Initialize a `Phylm` object. + + Args: + title: the title of the movie + imdb_id: an optional `IMDb` ID of the movie + """ + self.title = title + self.imdb_id = imdb_id self._imdb: Optional[Imdb] = None self._mtc: Optional[Mtc] = None self._rt: Optional[Rt] = None @@ -84,7 +90,8 @@ def load_source(self, source: str, imdb_id: Optional[str] = None) -> "Phylm": """ if source == "imdb": if not self._imdb: - self._imdb = Imdb(raw_title=self.title, movie_id=imdb_id) + movie_id = imdb_id or self.imdb_id + self._imdb = Imdb(raw_title=self.title, movie_id=movie_id) return self if source == "mtc": diff --git a/tests/unit/test_phylm.py b/tests/unit/test_phylm.py index ccd1abe..ad5bf6c 100644 --- a/tests/unit/test_phylm.py +++ b/tests/unit/test_phylm.py @@ -26,6 +26,18 @@ def test_init_instance_variables(self) -> None: assert phylm.title == title + def test_imdb_id(self) -> None: + """ + Given an `imdb_id`, + When a `Phylm` object is created with the id, + Then `imdb_id` is added as an instance variable + """ + imdb_id = "bar" + + phylm = Phylm(title="foo", imdb_id=imdb_id) + + assert phylm.imdb_id == imdb_id + class TestLoadSource: """Tests for the `load_source` method.""" @@ -76,6 +88,22 @@ def test_recognized_source_imdb_with_movie_id(self, mock_imdb: MagicMock) -> Non assert phylm.imdb == mock_imdb.return_value mock_imdb.assert_called_once_with(raw_title="bar", movie_id="abc") + @patch(f"{MODULE_PATH}.Imdb", autospec=True) + def test_recognized_source_imdb_with_movie_id_instance_variable( + self, mock_imdb: MagicMock + ) -> None: + """ + Given a `Phylm` instance, + When the `load_source` method is invoked with an `imdb_id` provided at `init`, + Then the source is loaded with the `movie_id` instance variable + """ + phylm = Phylm(title="foo", imdb_id="abc") + + phylm.load_source("imdb") + + assert phylm.imdb == mock_imdb.return_value + mock_imdb.assert_called_once_with(raw_title="foo", movie_id="abc") + @patch(f"{MODULE_PATH}.Mtc", autospec=True) def test_recognized_source_mtc(self, mock_mtc: MagicMock) -> None: """ From c5b8f699132053b1a81f2d5be2b452d550474693 Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Wed, 3 Nov 2021 09:27:01 +0100 Subject: [PATCH 2/3] add `Phylm` `__repr__` --- src/phylm/phylm.py | 12 ++++++++++++ tests/unit/test_phylm.py | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/phylm/phylm.py b/src/phylm/phylm.py index 31b83e1..d46dc68 100644 --- a/src/phylm/phylm.py +++ b/src/phylm/phylm.py @@ -25,6 +25,18 @@ def __init__(self, title: str, imdb_id: Optional[str] = None) -> None: self._mtc: Optional[Mtc] = None self._rt: Optional[Rt] = None + def __repr__(self) -> str: + """Return the string representation. + + Returns: + the string representation + """ + return f"" + + def __str__(self) -> str: + """Return the string representation.""" + return self.__repr__() + @property def imdb(self) -> Imdb: """Return the IMDb data. diff --git a/tests/unit/test_phylm.py b/tests/unit/test_phylm.py index ad5bf6c..41d1400 100644 --- a/tests/unit/test_phylm.py +++ b/tests/unit/test_phylm.py @@ -39,6 +39,21 @@ def test_imdb_id(self) -> None: assert phylm.imdb_id == imdb_id +class TestRepr: + """Tests for the __repr__ method.""" + + def test_repr(self) -> None: + """ + Given a `Phylm` object, + When the string representation is retrieved, + Then a informative string is returned + """ + phylm = Phylm(title="foo") + + assert repr(phylm) == "" + assert str(phylm) == "" + + class TestLoadSource: """Tests for the `load_source` method.""" From 6b126156f486b6b319f8a9df52dea62c56fd1e15 Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Wed, 3 Nov 2021 09:27:41 +0100 Subject: [PATCH 3/3] bump version to 4.0.5 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index c95dcee..9287cb1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "phylm" -version = "4.0.4" +version = "4.0.5" description = "Phylm" authors = ["Dom Batten "] license = "MIT"