From d9e96958aadca411f1af4809ad94c72b99ab3f9c Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Sat, 28 Oct 2017 19:25:20 +0200 Subject: [PATCH] Split fetchers test module into package --- tests/fetchers/__init__.py | 22 +++ tests/fetchers/test_base.py | 113 ++++++++++++++ tests/fetchers/test_fimfarchive.py | 87 +++++++++++ .../test_fimfiction.py} | 146 +----------------- tox.ini | 3 +- 5 files changed, 226 insertions(+), 145 deletions(-) create mode 100644 tests/fetchers/__init__.py create mode 100644 tests/fetchers/test_base.py create mode 100644 tests/fetchers/test_fimfarchive.py rename tests/{test_fetchers.py => fetchers/test_fimfiction.py} (50%) diff --git a/tests/fetchers/__init__.py b/tests/fetchers/__init__.py new file mode 100644 index 0000000..fa31b4a --- /dev/null +++ b/tests/fetchers/__init__.py @@ -0,0 +1,22 @@ +""" +Fetcher tests. +""" + + +# +# Fimfarchive, preserves stories from Fimfiction. +# Copyright (C) 2015 Joakim Soderlund +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# diff --git a/tests/fetchers/test_base.py b/tests/fetchers/test_base.py new file mode 100644 index 0000000..1ff757b --- /dev/null +++ b/tests/fetchers/test_base.py @@ -0,0 +1,113 @@ +""" +Base fetcher tests. +""" + + +# +# Fimfarchive, preserves stories from Fimfiction. +# Copyright (C) 2015 Joakim Soderlund +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + + +from unittest.mock import MagicMock + + +VALID_STORY_KEY = 9 + + +class TestFetcher: + """ + Fetcher tests. + """ + + def test_fetch_with_prefetch_meta(self, fetcher): + """ + Tests `fetch_meta` is called when prefetch_meta is enabled. + """ + fetcher.fetch(VALID_STORY_KEY, prefetch_meta=True) + fetcher.fetch_meta.assert_called_once_with(VALID_STORY_KEY) + + def test_fetch_without_prefetch_meta(self, fetcher): + """ + Tests `fetch_meta` is not called when prefetch_meta is disabled. + """ + fetcher.fetch(VALID_STORY_KEY, prefetch_meta=False) + fetcher.fetch_meta.assert_not_called() + + def test_fetch_with_prefetch_data(self, fetcher): + """ + Tests `fetch_data` is called when prefetch_data is enabled. + """ + fetcher.fetch(VALID_STORY_KEY, prefetch_data=True) + fetcher.fetch_data.assert_called_once_with(VALID_STORY_KEY) + + def test_fetch_without_prefetch_data(self, fetcher): + """ + Tests `fetch_data` is not called when prefetch_data is disabled. + """ + fetcher.fetch(VALID_STORY_KEY, prefetch_data=False) + fetcher.fetch_data.assert_not_called() + + def test_fetch_with_default_prefetch(self, fetcher): + """ + Tests prefetching can be enabled using attributes. + """ + fetcher.prefetch_meta = True + fetcher.prefetch_data = True + + fetcher.fetch(VALID_STORY_KEY) + + fetcher.fetch_meta.assert_called_once_with(VALID_STORY_KEY) + fetcher.fetch_data.assert_called_once_with(VALID_STORY_KEY) + + def test_fetch_without_default_prefetch(self, fetcher): + """ + Tests prefetching can be disabled using attributes. + """ + fetcher.prefetch_meta = False + fetcher.prefetch_data = False + + fetcher.fetch(VALID_STORY_KEY) + + fetcher.fetch_meta.assert_not_called() + fetcher.fetch_data.assert_not_called() + + def test_close_is_called_on_exit(self, fetcher): + """ + Test `close` is called on exit in with statement. + """ + fetcher.close = MagicMock(method='close') + + with fetcher: + pass + + fetcher.close.assert_called_once_with() + + def test_empty_flavors_are_passed_to_story(self, fetcher): + """ + Tests story contains empty flavors from fetcher. + """ + fetcher.flavors = set() + story = fetcher.fetch(VALID_STORY_KEY) + assert story.flavors == set() + + def test_custom_flavors_are_passed_to_story(self, fetcher, flavor): + """ + Tests story contains custom flavors from fetcher. + """ + fetcher.flavors = {flavor.A} + story = fetcher.fetch(VALID_STORY_KEY) + assert story.flavors == {flavor.A} diff --git a/tests/fetchers/test_fimfarchive.py b/tests/fetchers/test_fimfarchive.py new file mode 100644 index 0000000..b5f4c49 --- /dev/null +++ b/tests/fetchers/test_fimfarchive.py @@ -0,0 +1,87 @@ +""" +Fimfarchive fetcher tests. +""" + + +# +# Fimfarchive, preserves stories from Fimfiction. +# Copyright (C) 2015 Joakim Soderlund +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + + +import pytest + +from fimfarchive.exceptions import InvalidStoryError, StorySourceError +from fimfarchive.fetchers import FimfarchiveFetcher + + +VALID_STORY_KEY = 9 +INVALID_STORY_KEY = 7 + +FIMFARCHIVE_PATH = 'fimfarchive-20170601.zip' + + +class TestFimfarchiveFetcher: + """ + FimfarchiveFetcher tests. + """ + + @pytest.yield_fixture(scope='module') + def fetcher(self): + """ + Returns the fetcher instance to test. + """ + with FimfarchiveFetcher(FIMFARCHIVE_PATH) as fetcher: + yield fetcher + + def test_closed_fetcher_raises_exception(self): + """ + Tests `StorySourceError` is raised when fetcher is closed. + """ + with FimfarchiveFetcher(FIMFARCHIVE_PATH) as fetcher: + fetcher.lookup(VALID_STORY_KEY) + + with pytest.raises(StorySourceError): + fetcher.lookup(VALID_STORY_KEY) + + def test_fetch_meta_for_valid_story(self, fetcher): + """ + Tests meta is returned if story is valid + """ + meta = fetcher.fetch_meta(VALID_STORY_KEY) + assert meta['id'] == VALID_STORY_KEY + assert meta['words'] != 0 + + def test_fetch_meta_for_invalid_story(self, fetcher): + """ + Tests `InvalidStoryError` is raised if story is invalid. + """ + with pytest.raises(InvalidStoryError): + fetcher.fetch_meta(INVALID_STORY_KEY) + + def test_fetch_data_for_valid_story(self, fetcher): + """ + Tests data is returned if story is valid. + """ + data = fetcher.fetch_data(VALID_STORY_KEY) + assert len(data) != 0 + + def test_fetch_data_for_invalid_story(self, fetcher): + """ + Tests `InvalidStoryError` is raised if story is invalid. + """ + with pytest.raises(InvalidStoryError): + fetcher.fetch_data(INVALID_STORY_KEY) diff --git a/tests/test_fetchers.py b/tests/fetchers/test_fimfiction.py similarity index 50% rename from tests/test_fetchers.py rename to tests/fetchers/test_fimfiction.py index 6a67cf8..0d80a73 100644 --- a/tests/test_fetchers.py +++ b/tests/fetchers/test_fimfiction.py @@ -1,5 +1,5 @@ """ -Fetcher tests. +Fimfiction fetcher tests. """ @@ -22,12 +22,10 @@ Fetcher tests. # -from unittest.mock import MagicMock - import pytest from fimfarchive.exceptions import InvalidStoryError, StorySourceError -from fimfarchive.fetchers import FimfarchiveFetcher, FimfictionFetcher +from fimfarchive.fetchers import FimfictionFetcher VALID_STORY_KEY = 9 @@ -35,93 +33,6 @@ INVALID_STORY_KEY = 7 EMPTY_STORY_KEY = 8 PROTECTED_STORY_KEY = 208799 -FIMFARCHIVE_PATH = 'fimfarchive-20170601.zip' - - -class TestFetcher: - """ - Fetcher tests. - """ - - def test_fetch_with_prefetch_meta(self, fetcher): - """ - Tests `fetch_meta` is called when prefetch_meta is enabled. - """ - fetcher.fetch(VALID_STORY_KEY, prefetch_meta=True) - fetcher.fetch_meta.assert_called_once_with(VALID_STORY_KEY) - - def test_fetch_without_prefetch_meta(self, fetcher): - """ - Tests `fetch_meta` is not called when prefetch_meta is disabled. - """ - fetcher.fetch(VALID_STORY_KEY, prefetch_meta=False) - fetcher.fetch_meta.assert_not_called() - - def test_fetch_with_prefetch_data(self, fetcher): - """ - Tests `fetch_data` is called when prefetch_data is enabled. - """ - fetcher.fetch(VALID_STORY_KEY, prefetch_data=True) - fetcher.fetch_data.assert_called_once_with(VALID_STORY_KEY) - - def test_fetch_without_prefetch_data(self, fetcher): - """ - Tests `fetch_data` is not called when prefetch_data is disabled. - """ - fetcher.fetch(VALID_STORY_KEY, prefetch_data=False) - fetcher.fetch_data.assert_not_called() - - def test_fetch_with_default_prefetch(self, fetcher): - """ - Tests prefetching can be enabled using attributes. - """ - fetcher.prefetch_meta = True - fetcher.prefetch_data = True - - fetcher.fetch(VALID_STORY_KEY) - - fetcher.fetch_meta.assert_called_once_with(VALID_STORY_KEY) - fetcher.fetch_data.assert_called_once_with(VALID_STORY_KEY) - - def test_fetch_without_default_prefetch(self, fetcher): - """ - Tests prefetching can be disabled using attributes. - """ - fetcher.prefetch_meta = False - fetcher.prefetch_data = False - - fetcher.fetch(VALID_STORY_KEY) - - fetcher.fetch_meta.assert_not_called() - fetcher.fetch_data.assert_not_called() - - def test_close_is_called_on_exit(self, fetcher): - """ - Test `close` is called on exit in with statement. - """ - fetcher.close = MagicMock(method='close') - - with fetcher: - pass - - fetcher.close.assert_called_once_with() - - def test_empty_flavors_are_passed_to_story(self, fetcher): - """ - Tests story contains empty flavors from fetcher. - """ - fetcher.flavors = set() - story = fetcher.fetch(VALID_STORY_KEY) - assert story.flavors == set() - - def test_custom_flavors_are_passed_to_story(self, fetcher, flavor): - """ - Tests story contains custom flavors from fetcher. - """ - fetcher.flavors = {flavor.A} - story = fetcher.fetch(VALID_STORY_KEY) - assert story.flavors == {flavor.A} - class TestFimfictionFetcher: """ @@ -226,56 +137,3 @@ class TestFimfictionFetcher: """ with pytest.raises(InvalidStoryError): fetcher.fetch_data(PROTECTED_STORY_KEY) - - -class TestFimfarchiveFetcher: - """ - FimfarchiveFetcher tests. - """ - - @pytest.yield_fixture(scope='module') - def fetcher(self): - """ - Returns the fetcher instance to test. - """ - with FimfarchiveFetcher(FIMFARCHIVE_PATH) as fetcher: - yield fetcher - - def test_closed_fetcher_raises_exception(self): - """ - Tests `StorySourceError` is raised when fetcher is closed. - """ - with FimfarchiveFetcher(FIMFARCHIVE_PATH) as fetcher: - fetcher.lookup(VALID_STORY_KEY) - - with pytest.raises(StorySourceError): - fetcher.lookup(VALID_STORY_KEY) - - def test_fetch_meta_for_valid_story(self, fetcher): - """ - Tests meta is returned if story is valid - """ - meta = fetcher.fetch_meta(VALID_STORY_KEY) - assert meta['id'] == VALID_STORY_KEY - assert meta['words'] != 0 - - def test_fetch_meta_for_invalid_story(self, fetcher): - """ - Tests `InvalidStoryError` is raised if story is invalid. - """ - with pytest.raises(InvalidStoryError): - fetcher.fetch_meta(INVALID_STORY_KEY) - - def test_fetch_data_for_valid_story(self, fetcher): - """ - Tests data is returned if story is valid. - """ - data = fetcher.fetch_data(VALID_STORY_KEY) - assert len(data) != 0 - - def test_fetch_data_for_invalid_story(self, fetcher): - """ - Tests `InvalidStoryError` is raised if story is invalid. - """ - with pytest.raises(InvalidStoryError): - fetcher.fetch_data(INVALID_STORY_KEY) diff --git a/tox.ini b/tox.ini index e19cc5a..9d24598 100644 --- a/tox.ini +++ b/tox.ini @@ -20,7 +20,8 @@ deps = [pytest] addopts = --ignore venv - --ignore tests/test_fetchers.py + --ignore tests/fetchers/test_fimfarchive.py + --ignore tests/fetchers/test_fimfiction.py [flake8] filenames = *.py