|
7 | 7 | from tests.mock import MagicMock, call, patch
|
8 | 8 |
|
9 | 9 | from streamlink import Streamlink
|
| 10 | +from streamlink.plugin import PluginError |
10 | 11 | from streamlink.plugins.twitch import Twitch, TwitchHLSStream
|
11 | 12 |
|
12 | 13 |
|
@@ -274,6 +275,81 @@ def test_hls_low_latency_no_prefetch_disable_ads_has_preroll(self, mock_log):
|
274 | 275 | ])
|
275 | 276 |
|
276 | 277 |
|
| 278 | +class TestTwitchMetadata(unittest.TestCase): |
| 279 | + def setUp(self): |
| 280 | + self.mock = requests_mock.Mocker() |
| 281 | + self.mock.start() |
| 282 | + |
| 283 | + def tearDown(self): |
| 284 | + self.mock.stop() |
| 285 | + |
| 286 | + def subject(self, url): |
| 287 | + session = Streamlink() |
| 288 | + Twitch.bind(session, "tests.plugins.test_twitch") |
| 289 | + plugin = Twitch(url) |
| 290 | + return plugin.get_author(), plugin.get_title(), plugin.get_category() |
| 291 | + |
| 292 | + def subject_channel(self, data=True, failure=False): |
| 293 | + self.mock.get( |
| 294 | + "https://api.twitch.tv/kraken/users.json?login=foo", |
| 295 | + json={"users": [{"_id": 1234}]} |
| 296 | + ) |
| 297 | + self.mock.get( |
| 298 | + "https://api.twitch.tv/kraken/streams/1234.json", |
| 299 | + status_code=200 if not failure else 404, |
| 300 | + json={"stream": None} if not data else {"stream": { |
| 301 | + "channel": { |
| 302 | + "display_name": "channel name", |
| 303 | + "status": "channel status", |
| 304 | + "game": "channel game" |
| 305 | + } |
| 306 | + }} |
| 307 | + ) |
| 308 | + return self.subject("https://twitch.tv/foo") |
| 309 | + |
| 310 | + def subject_video(self, data=True, failure=False): |
| 311 | + self.mock.get( |
| 312 | + "https://api.twitch.tv/kraken/videos/1337.json", |
| 313 | + status_code=200 if not failure else 404, |
| 314 | + json={} if not data else { |
| 315 | + "title": "video title", |
| 316 | + "game": "video game", |
| 317 | + "channel": { |
| 318 | + "display_name": "channel name" |
| 319 | + } |
| 320 | + } |
| 321 | + ) |
| 322 | + return self.subject("https://twitch.tv/videos/1337") |
| 323 | + |
| 324 | + def test_metadata_channel_exists(self): |
| 325 | + author, title, category = self.subject_channel() |
| 326 | + self.assertEqual(author, "channel name") |
| 327 | + self.assertEqual(title, "channel status") |
| 328 | + self.assertEqual(category, "channel game") |
| 329 | + |
| 330 | + def test_metadata_channel_missing(self): |
| 331 | + metadata = self.subject_channel(data=False) |
| 332 | + self.assertEqual(metadata, (None, None, None)) |
| 333 | + |
| 334 | + def test_metadata_channel_invalid(self): |
| 335 | + with self.assertRaises(PluginError): |
| 336 | + self.subject_channel(failure=True) |
| 337 | + |
| 338 | + def test_metadata_video_exists(self): |
| 339 | + author, title, category = self.subject_video() |
| 340 | + self.assertEqual(author, "channel name") |
| 341 | + self.assertEqual(title, "video title") |
| 342 | + self.assertEqual(category, "video game") |
| 343 | + |
| 344 | + def test_metadata_video_missing(self): |
| 345 | + metadata = self.subject_video(data=False) |
| 346 | + self.assertEqual(metadata, (None, None, None)) |
| 347 | + |
| 348 | + def test_metadata_video_invalid(self): |
| 349 | + with self.assertRaises(PluginError): |
| 350 | + self.subject_video(failure=True) |
| 351 | + |
| 352 | + |
277 | 353 | @patch("streamlink.plugins.twitch.log")
|
278 | 354 | class TestTwitchReruns(unittest.TestCase):
|
279 | 355 | log_call = call("Reruns were disabled by command line option")
|
|
0 commit comments