def test_url_sanitize() -> None: """ Test that the _get_dest helper sanitizes correctly on non-Windows. Only / should need to be replaced """ test_sub = subscription.Subscription(url="test", name="test", directory="test") test_sub.settings["use_title_as_filename"] = True # pylint: disable=protected-access filename = test_sub._get_dest( "https://www.example.com?foo=1/bar.mp3?baz=2", "p/////uck", "/test") assert filename == "/test/p-----uck.mp3" # pylint: disable=protected-access filename = test_sub._get_dest( "https://www.example.com?foo=1/bar.mp3?baz=2", "p🤔🤔🤔🤔uck", "/test") assert filename == "/test/p🤔🤔🤔🤔uck.mp3" # pylint: disable=protected-access filename = test_sub._get_dest( "https://www.example.com?foo=1/bar.mp3?baz=2", "p*%$^\\1uck", "/test") assert filename == "/test/p*%$^\\1uck.mp3"
def sub(strdir: str) -> subscription.Subscription: """Create a test subscription.""" test_sub = subscription.Subscription(url="test", name="test", directory=strdir) return test_sub
def test_empty_url_cons(strdir: str) -> None: """ Constructing a subscription with an empty URL should return a None object. """ with pytest.raises(error.MalformedSubscriptionError) as exception: subscription.Subscription(url="", name="emptyConstruction", directory=strdir) assert exception.value.desc == "URL '' is None or empty - can't create subscription."
def test_none_url_cons(strdir: str) -> None: """ Constructing a subscription with a URL that is None should throw a MalformedSubscriptionError. """ with pytest.raises(error.MalformedSubscriptionError) as exception: subscription.Subscription(name="noneConstruction", directory=strdir) assert exception.value.desc == "URL 'None' is None or empty - can't create subscription."
def test_none_name_cons(strdir: str) -> None: """ Constructing a subscription with a name that is None should throw a MalformedSubscriptionError. """ with pytest.raises(error.MalformedSubscriptionError) as exception: subscription.Subscription(url="foo", name=None, directory=strdir) assert exception.value.desc == "Name 'None' is None or empty - can't create subscription."
def test_new_attempt_update(strdir: str) -> None: """Attempting update on a new subscription (no backlog) should download nothing.""" test_dir = strdir test_sub = subscription.Subscription(url="foo", name="foo", directory=test_dir) test_sub.downloader = generate_fake_downloader() test_sub.parser = generate_feedparser() test_sub.attempt_update() assert len(os.listdir(test_dir)) == 0
def test_empty_name_construction_errors(self): """ Constructing a subscription with a name that is empty should throw a MalformedSubscriptionError. """ with PT.raises(PE.MalformedSubscriptionError) as e: SUB.Subscription(url="foo", name="", directory=TestSubscription.d) assert e.value.desc == "No name provided."
def test_get_feed_max(strdir: str) -> None: """ If we try more than MAX_RECURSIVE_ATTEMPTS to retrieve a URL, we should fail.""" test_sub = subscription.Subscription(url=PERM_REDIRECT, name="tooManyAttemptsTest", directory=strdir) test_sub.get_feed(attempt_count=subscription.MAX_RECURSIVE_ATTEMPTS + 1) assert test_sub.feed_state.feed == {} assert test_sub.feed_state.entries == []
def test_none_url_construction_errors(self): """ Constructing a subscription with a URL that is None should throw a MalformedSubscriptionError. """ with PT.raises(PE.MalformedSubscriptionError) as exception: SUB.Subscription(name="noneConstruction", directory=TestSubscription.d) assert exception.value.desc == "No URL provided."
def test_get_feed_helper_fails_after_max(self): """If we try more than MAX_RECURSIVE_ATTEMPTS to retrieve a URL, we should fail.""" sub = SUB.Subscription(url=HTTP_302_ADDRESS, name="tooManyAttemptsTest", directory=TestSubscription.d) # TODO tests need to be rewritten to check log output or something. sub.get_feed(attempt_count=SUB.MAX_RECURSIVE_ATTEMPTS + 1) assert sub.feed_state.feed == {} assert sub.feed_state.entries == []
def _test_url_helper(strdir: str, given: str, name: str, expected_current: str, expected_original: str, ) -> None: test_sub = subscription.Subscription(url=given, name=name, directory=strdir) test_sub.downloader = generate_fake_downloader() test_sub.parser = generate_feedparser() test_sub.get_feed() assert test_sub.url == expected_current assert test_sub.original_url == expected_original
def test_attempt_download_backlog(strdir: str) -> None: """Should download full backlog if backlog limit set to None.""" test_sub = subscription.Subscription(url=RSS_ADDRESS, name="testfeed", directory=strdir) test_sub.downloader = generate_fake_downloader() test_sub.parser = generate_feedparser() test_sub.settings["backlog_limit"] = None test_sub.settings["use_title_as_filename"] = False test_sub.attempt_update() assert len(test_sub.feed_state.entries) == 10 assert len(os.listdir(test_sub.directory)) == 10
def test_tagging_skipped(strdir: str) -> None: test_sub = subscription.Subscription(url=RSS_ADDRESS, name="testfeed", directory=strdir) test_sub.downloader = generate_fake_downloader(filetype="m4a") test_sub.parser = generate_feedparser(filetype="m4a") test_sub.settings["backlog_limit"] = 4 test_sub.settings["set_tags"] = True test_sub.attempt_update() for i in range(0, 4): _check_tag_absence(i, test_sub.directory)
def test_attempt_update_new_entry(strdir: str) -> None: """Attempting update on a podcast with a new entry should download the new entry only.""" test_dir = strdir test_sub = subscription.Subscription(url=RSS_ADDRESS, name="bar", directory=test_dir) test_sub.downloader = generate_fake_downloader() test_sub.parser = generate_feedparser() assert len(os.listdir(test_dir)) == 0 test_sub.feed_state.latest_entry_number = 9 test_sub.attempt_update() assert test_sub.feed_state.latest_entry_number == 10 assert len(os.listdir(test_dir)) == 1
def test_gone_fails(strdir: str) -> None: """If the URL is Gone, the current url should be set to None, and we should return None.""" test_sub = subscription.Subscription(url=GONE, name="410Test", directory=strdir) test_sub.settings["backlog_limit"] = 1 test_sub.settings["use_title_as_filename"] = False test_sub.downloader = generate_fake_downloader() test_sub.parser = generate_feedparser() test_sub.get_feed() assert test_sub.url == "" assert test_sub.original_url == GONE
def test_attempt_download_partial_backlog(strdir: str) -> None: """Should download partial backlog if limit is specified.""" test_sub = subscription.Subscription(url=RSS_ADDRESS, name="testfeed", directory=strdir) test_sub.settings["backlog_limit"] = 5 test_sub.downloader = generate_fake_downloader() test_sub.parser = generate_feedparser() # TODO find a cleaner way to set these. # Maybe test_subscription should handle these attributes missing better? # Maybe have a cleaner way to hack them in in tests? test_sub.settings["backlog_limit"] = 4 test_sub.settings["use_title_as_filename"] = False test_sub.attempt_update()
def test_url_with_qparams() -> None: """Test that the _get_dest helper handles query parameters properly.""" test_sub = subscription.Subscription(url="test", name="test", directory="test") test_sub.settings["use_title_as_filename"] = True # pylint: disable=protected-access filename = test_sub._get_dest("https://www.example.com?foo=1/bar.mp3?baz=2", "puck", "/test") assert filename == "/test/puck.mp3" test_sub.settings["use_title_as_filename"] = False # pylint: disable=protected-access filename = test_sub._get_dest("https://www.example.com?foo=1/bar.mp3?baz=2", "puck", "/test") assert filename == "/test/bar.mp3"
def test_attempt_download_backlog(self): """Should download full backlog by default.""" sub = SUB.Subscription(url=RSS_ADDRESS, name="testfeed", directory=TestSubscription.d) sub.use_backlog = True sub.backlog_limit = None sub.use_title_as_filename = False sub.attempt_update() assert len(sub.feed_state.entries) == 10 for i in range(1, 9): _check_hi_contents(i, sub.directory)
def test_gone_fails(self): """If the URL is Gone, the current url should be set to None, and we should return None.""" sub = SUB.Subscription(url=HTTP_410_ADDRESS, name="410Test", directory=TestSubscription.d) sub.use_backlog = True sub.backlog_limit = 1 sub.use_title_as_filename = False sub.get_feed() assert sub.url is None assert sub.original_url == HTTP_410_ADDRESS
def test_attempt_download_partial_backlog(self): """Should download partial backlog if limit is specified.""" sub = SUB.Subscription(url=RSS_ADDRESS, name="testfeed", backlog_limit=5, directory=TestSubscription.d) # TODO find a cleaner way to set these. # Maybe Subscription should handle these attributes missing better? # Maybe have a cleaner way to hack them in in tests? sub.use_backlog = True sub.use_title_as_filename = False sub.attempt_update() assert len(sub.feed_state.entries) == 10 for i in range(0, 4): _check_hi_contents(i, sub.directory)
def subscriptions(tmpdir: Any) -> List[subscription.Subscription]: """Generate subscriptions for config testing.""" sub_dir = str(tmpdir.mkdir("foo")) subs = [] for i in range(0, 3): name = "test" + str(i) url = "testurl" + str(i) directory = os.path.join(sub_dir, "dir" + str(i)) sub = subscription.Subscription(name=name, url=url, directory=directory) sub.settings["backlog_limit"] = 1 sub.settings["use_title_as_filename"] = False subs.append(sub) return subs
def setup_class(cls): # Mock XDG spec dirs to ensure we do the correct thing, and also that we don't put files in # strange places during testing. cls.old_environ = dict(os.environ) cls.default_config_dir = os.path.join(tempfile.mkdtemp(), "puckfetcher") cls.default_config_file = os.path.join(cls.default_config_dir, "config.yaml") cls.default_cache_dir = os.path.join(tempfile.mkdtemp(), "puckfetcher") cls.default_log_file = os.path.join(cls.default_cache_dir, "puckfetcher.log") logging.getLogger("root") cls.default_cache_file = os.path.join(cls.default_cache_dir, "puckcache") cls.default_data_dir = os.path.join(tempfile.mkdtemp(), "puckfetcher") cls.files = [ cls.default_config_file, cls.default_log_file, cls.default_cache_file ] cls.subscriptions = [] for i in range(0, 3): name = "test" + str(i) url = "testurl" + str(i) directory = os.path.join(cls.default_data_dir, "dir" + str(i)) sub = PS.Subscription(name=name, url=url, directory=directory) sub.download_backlog = True sub.backlog_limit = 1 sub.use_title_as_filename = False cls.subscriptions.append(sub)
def test_tagging(strdir: str) -> None: test_sub = subscription.Subscription(url=RSS_ADDRESS, name="testfeed", directory=strdir) test_sub.downloader = generate_fake_downloader() test_sub.parser = generate_feedparser() metadata = { "name": test_sub.metadata["name"], "artist": "hello-artist", "album": test_sub.metadata["name"], "album_artist": "hello-artist-album", } test_sub.metadata = metadata test_sub.settings["backlog_limit"] = 4 test_sub.settings["set_tags"] = True test_sub.attempt_update() for i in range(0, 4): _check_tag_presence(i, test_sub.directory, metadata=metadata)
def _test_url_helper(given, name, expected_current, expected_original): sub = SUB.Subscription(url=given, name=name, directory=TestSubscription.d) sub.get_feed() assert sub.url == expected_current assert sub.original_url == expected_original