22import os
33from typing import Optional , Sized
44
5+ import mpegdash
56import pytest
67
7- from tidal_async import Album , Artist , AudioQuality , Playlist , TidalSession , Track , extract_client_id
8+ from tidal_async import (
9+ Album ,
10+ Artist ,
11+ AudioQuality ,
12+ Playlist ,
13+ TidalSession ,
14+ Track ,
15+ dash_mpd_from_data_url ,
16+ extract_client_id ,
17+ )
818
919# TODO [#63]: Unit tests!
1020# - [ ] login process (not sure how to do this - it's interactive oauth2)
@@ -283,24 +293,6 @@ async def test_cover_download(sess: TidalSession, object_url, cover_size, sha256
283293@pytest .mark .parametrize (
284294 "id_, required_quality, preferred_quality, file_size, mimetype, etag" ,
285295 (
286- (
287- 152676390 ,
288- AudioQuality .Normal ,
289- AudioQuality .Normal ,
290- 3114802 ,
291- "audio/mp4" ,
292- '"780130927f84364021b1300423d60f47"' ,
293- ),
294- # DASH Support needed (#53)
295- # (
296- # 152676390,
297- # AudioQuality.High,
298- # AudioQuality.High,
299- # 10347474,
300- # "audio/mp4",
301- # '"970df936b04363528662c9c74b714d13-2"',
302- # ),
303- (152676390 , AudioQuality .HiFi , AudioQuality .HiFi , 30980344 , "audio/flac" , '"3bb27f3e6d8f7fd987bcc0d3cdc7c452"' ),
304296 (
305297 152676390 ,
306298 AudioQuality .Master ,
@@ -311,13 +303,63 @@ async def test_cover_download(sess: TidalSession, object_url, cover_size, sha256
311303 ),
312304 ),
313305)
314- async def test_track_download (sess : TidalSession , id_ , required_quality , preferred_quality , file_size , mimetype , etag ):
306+ async def test_track_download_direct (
307+ sess : TidalSession , id_ , required_quality , preferred_quality , file_size , mimetype , etag
308+ ):
315309 track = await sess .track (id_ )
316310 file = await track .get_async_file (required_quality , preferred_quality )
317311
318312 assert file .mimetype == mimetype and file .resp_headers ["ETag" ] == etag and len (file ) == file_size
319313
320314
315+ @pytest .mark .asyncio
316+ @pytest .mark .parametrize (
317+ "id_, required_quality, preferred_quality, codec, bandwidth, length, segments" ,
318+ (
319+ (
320+ 152676390 ,
321+ AudioQuality .Normal ,
322+ AudioQuality .Normal ,
323+ "mp4a.40.5" ,
324+ 96984 ,
325+ "PT4M17.614S" ,
326+ 64 ,
327+ ),
328+ (
329+ 152676390 ,
330+ AudioQuality .High ,
331+ AudioQuality .High ,
332+ "mp4a.40.2" ,
333+ 321691 ,
334+ "PT4M17.545S" ,
335+ 64 ,
336+ ),
337+ (
338+ 152676390 ,
339+ AudioQuality .HiFi ,
340+ AudioQuality .HiFi ,
341+ "flac" ,
342+ 957766 ,
343+ "PT4M17.499S" ,
344+ 64 ,
345+ ),
346+ ),
347+ )
348+ async def test_track_download_dash (
349+ sess : TidalSession , id_ , required_quality , preferred_quality , codec , bandwidth , length , segments
350+ ):
351+ track = await sess .track (id_ )
352+ url = await track .get_file_url (required_quality , preferred_quality )
353+ mpd = dash_mpd_from_data_url (url )
354+
355+ rep = mpd .periods [0 ].adaptation_sets [0 ].representations [0 ]
356+
357+ assert rep .codecs == codec
358+ assert rep .bandwidth == bandwidth
359+ assert mpd .media_presentation_duration == length
360+ assert sum (s .r if s .r else 1 for s in rep .segment_templates [0 ].segment_timelines [0 ].Ss )
361+
362+
321363@pytest .mark .asyncio
322364async def test_client_id_extraction ():
323365 from io import BytesIO
0 commit comments