|
| 1 | +import pytest |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | + |
| 5 | +SAMPLE_RSS_XML = """<?xml version="1.0" encoding="utf-8"?> |
| 6 | +<rss version="2.0"> |
| 7 | +<channel> |
| 8 | + <title>Test Podcast</title> |
| 9 | + <link>https://example.com/</link> |
| 10 | + <description>A test podcast feed</description> |
| 11 | + <image> |
| 12 | + <url>https://example.com/image.png</url> |
| 13 | + <title>Test Image</title> |
| 14 | + <link>https://example.com/</link> |
| 15 | + </image> |
| 16 | + <item> |
| 17 | + <title>Episode One</title> |
| 18 | + <link>https://example.com/episode-one</link> |
| 19 | + <description>Description of episode one</description> |
| 20 | + <pubDate>Mon, 31 Jan 2022 18:43:00 +0100</pubDate> |
| 21 | + <enclosure url="https://example.com/ep1.mp3" |
| 22 | + type="audio/mpeg" length="1000"/> |
| 23 | + </item> |
| 24 | + <item> |
| 25 | + <title>Episode Two</title> |
| 26 | + <link>https://example.com/episode-two</link> |
| 27 | + <description>Description of episode two</description> |
| 28 | + <pubDate>Fri, 28 Jan 2022 10:00:00 +0100</pubDate> |
| 29 | + <enclosure url="https://example.com/ep2.ogg" |
| 30 | + type="audio/ogg" length="2000"/> |
| 31 | + </item> |
| 32 | +</channel> |
| 33 | +</rss>""" |
| 34 | + |
| 35 | +RSS_NO_DESCRIPTION = """<?xml version="1.0" encoding="utf-8"?> |
| 36 | +<rss version="2.0"> |
| 37 | +<channel> |
| 38 | + <title>Test Podcast</title> |
| 39 | + <link>https://example.com/</link> |
| 40 | + <image> |
| 41 | + <url>https://example.com/image.png</url> |
| 42 | + <title>Test</title> |
| 43 | + <link>https://example.com/</link> |
| 44 | + </image> |
| 45 | + <item> |
| 46 | + <title>Episode One</title> |
| 47 | + <link>https://example.com/episode-one</link> |
| 48 | + <pubDate>Mon, 31 Jan 2022 18:43:00 +0100</pubDate> |
| 49 | + <enclosure url="https://example.com/ep1.mp3" |
| 50 | + type="audio/mpeg" length="1000"/> |
| 51 | + </item> |
| 52 | +</channel> |
| 53 | +</rss>""" |
| 54 | + |
| 55 | +RSS_NO_LINK = """<?xml version="1.0" encoding="utf-8"?> |
| 56 | +<rss version="2.0"> |
| 57 | +<channel> |
| 58 | + <title>Test Podcast</title> |
| 59 | + <description>A test feed</description> |
| 60 | + <image> |
| 61 | + <url>https://example.com/image.png</url> |
| 62 | + <title>Test</title> |
| 63 | + <link>https://example.com/</link> |
| 64 | + </image> |
| 65 | + <item> |
| 66 | + <title>Episode One</title> |
| 67 | + <link>https://example.com/episode-one</link> |
| 68 | + <pubDate>Mon, 31 Jan 2022 18:43:00 +0100</pubDate> |
| 69 | + <enclosure url="https://example.com/ep1.mp3" |
| 70 | + type="audio/mpeg" length="1000"/> |
| 71 | + </item> |
| 72 | +</channel> |
| 73 | +</rss>""" |
| 74 | + |
| 75 | +RSS_NO_IMAGE = """<?xml version="1.0" encoding="utf-8"?> |
| 76 | +<rss version="2.0"> |
| 77 | +<channel> |
| 78 | + <title>Test Podcast</title> |
| 79 | + <link>https://example.com/</link> |
| 80 | + <description>A test feed</description> |
| 81 | + <item> |
| 82 | + <title>Episode One</title> |
| 83 | + <link>https://example.com/episode-one</link> |
| 84 | + <pubDate>Mon, 31 Jan 2022 18:43:00 +0100</pubDate> |
| 85 | + <enclosure url="https://example.com/ep1.mp3" |
| 86 | + type="audio/mpeg" length="1000"/> |
| 87 | + </item> |
| 88 | +</channel> |
| 89 | +</rss>""" |
| 90 | + |
| 91 | +RSS_NO_ENCLOSURE = """<?xml version="1.0" encoding="utf-8"?> |
| 92 | +<rss version="2.0"> |
| 93 | +<channel> |
| 94 | + <title>Test Podcast</title> |
| 95 | + <link>https://example.com/</link> |
| 96 | + <description>A test feed</description> |
| 97 | + <image> |
| 98 | + <url>https://example.com/image.png</url> |
| 99 | + <title>Test</title> |
| 100 | + <link>https://example.com/</link> |
| 101 | + </image> |
| 102 | + <item> |
| 103 | + <title>Episode Without Audio</title> |
| 104 | + <link>https://example.com/episode</link> |
| 105 | + <description>No audio file</description> |
| 106 | + <pubDate>Mon, 31 Jan 2022 18:43:00 +0100</pubDate> |
| 107 | + </item> |
| 108 | +</channel> |
| 109 | +</rss>""" |
| 110 | + |
| 111 | +RSS_ITEM_NO_LINK = """<?xml version="1.0" encoding="utf-8"?> |
| 112 | +<rss version="2.0"> |
| 113 | +<channel> |
| 114 | + <title>Test Podcast</title> |
| 115 | + <link>https://example.com/</link> |
| 116 | + <description>A test feed</description> |
| 117 | + <image> |
| 118 | + <url>https://example.com/image.png</url> |
| 119 | + <title>Test</title> |
| 120 | + <link>https://example.com/</link> |
| 121 | + </image> |
| 122 | + <item> |
| 123 | + <title>Episode Without Link</title> |
| 124 | + <description>No link element</description> |
| 125 | + <pubDate>Mon, 31 Jan 2022 18:43:00 +0100</pubDate> |
| 126 | + <enclosure url="https://example.com/ep.mp3" |
| 127 | + type="audio/mpeg" length="1000"/> |
| 128 | + </item> |
| 129 | +</channel> |
| 130 | +</rss>""" |
| 131 | + |
| 132 | +RSS_EMPTY_ITEMS = """<?xml version="1.0" encoding="utf-8"?> |
| 133 | +<rss version="2.0"> |
| 134 | +<channel> |
| 135 | + <title>Test Podcast</title> |
| 136 | + <link>https://example.com/</link> |
| 137 | + <description>A test feed</description> |
| 138 | + <image> |
| 139 | + <url>https://example.com/image.png</url> |
| 140 | + <title>Test</title> |
| 141 | + <link>https://example.com/</link> |
| 142 | + </image> |
| 143 | +</channel> |
| 144 | +</rss>""" |
| 145 | + |
| 146 | + |
| 147 | +def _make_mock_response(content, status_code=200): |
| 148 | + mock_resp = MagicMock() |
| 149 | + mock_resp.status_code = status_code |
| 150 | + mock_resp.content = ( |
| 151 | + content.encode("utf-8") if isinstance(content, str) |
| 152 | + else content |
| 153 | + ) |
| 154 | + mock_resp.headers = {"Content-Length": "1024"} |
| 155 | + mock_resp.iter_content = ( |
| 156 | + lambda chunk_size=1024: iter( |
| 157 | + [content.encode("utf-8")[:chunk_size]] |
| 158 | + ) |
| 159 | + ) |
| 160 | + return mock_resp |
| 161 | + |
| 162 | + |
| 163 | +@pytest.fixture(autouse=True) |
| 164 | +def reset_verbose(): |
| 165 | + import pypodget.globals as g |
| 166 | + original = g.__verbose__ |
| 167 | + g.__verbose__ = True |
| 168 | + yield |
| 169 | + g.__verbose__ = original |
| 170 | + |
| 171 | + |
| 172 | +@pytest.fixture |
| 173 | +def sample_rss_xml(): |
| 174 | + return SAMPLE_RSS_XML |
| 175 | + |
| 176 | + |
| 177 | +@pytest.fixture |
| 178 | +def mock_requests_get(sample_rss_xml): |
| 179 | + with pytest.importorskip("unittest.mock").patch( |
| 180 | + "pypodget.podcast.requests.get" |
| 181 | + ) as mock_get: |
| 182 | + mock_get.return_value = _make_mock_response(sample_rss_xml) |
| 183 | + yield mock_get |
| 184 | + |
| 185 | + |
| 186 | +@pytest.fixture |
| 187 | +def tmp_folder(tmp_path): |
| 188 | + return str(tmp_path) |
| 189 | + |
| 190 | + |
| 191 | +@pytest.fixture |
| 192 | +def sample_config_file(tmp_path): |
| 193 | + config_text = """[TestPodcast] |
| 194 | +url = https://example.com/feed.xml |
| 195 | +directory = ./test_output/ |
| 196 | +filename = $year-$month-$day - $title.$ext |
| 197 | +""" |
| 198 | + p = tmp_path / "test_config.ini" |
| 199 | + p.write_text(config_text) |
| 200 | + return str(p) |
| 201 | + |
| 202 | + |
| 203 | +@pytest.fixture |
| 204 | +def config_missing_url(tmp_path): |
| 205 | + config_text = """[TestPodcast] |
| 206 | +directory = ./test_output/ |
| 207 | +""" |
| 208 | + p = tmp_path / "bad_config.ini" |
| 209 | + p.write_text(config_text) |
| 210 | + return str(p) |
| 211 | + |
| 212 | + |
| 213 | +@pytest.fixture |
| 214 | +def config_missing_directory(tmp_path): |
| 215 | + config_text = """[TestPodcast] |
| 216 | +url = https://example.com/feed.xml |
| 217 | +""" |
| 218 | + p = tmp_path / "bad_config2.ini" |
| 219 | + p.write_text(config_text) |
| 220 | + return str(p) |
0 commit comments