"""Tests for the DLNAClient SOAP and DIDL-Lite parsers.""" from r36s_dlna_browser.dlna.client import _extract_browse_result, _parse_didl from r36s_dlna_browser.dlna.models import ItemType _SAMPLE_DIDL = """\ Music object.container.storageFolder Song.mp3 object.item.audioItem.musicTrack http://srv/art.jpg http://srv/song.mp3 Video.mkv object.item.videoItem http://srv/video.mkv """ class TestParseDIDL: def test_parses_container(self): items = _parse_didl(_SAMPLE_DIDL, "http://srv") containers = [i for i in items if i.is_container] assert len(containers) == 1 assert containers[0].title == "Music" assert containers[0].child_count == 3 def test_parses_audio_item(self): items = _parse_didl(_SAMPLE_DIDL, "http://srv") audio = [i for i in items if i.item_type == ItemType.AUDIO] assert len(audio) == 1 assert audio[0].title == "Song.mp3" assert audio[0].resource_url == "http://srv/song.mp3" assert audio[0].mime_type == "audio/mpeg" assert audio[0].size == 4000000 assert audio[0].duration == "0:03:21" assert audio[0].album_art_url == "http://srv/art.jpg" def test_parses_video_item(self): items = _parse_didl(_SAMPLE_DIDL, "http://srv") video = [i for i in items if i.item_type == ItemType.VIDEO] assert len(video) == 1 assert video[0].title == "Video.mkv" assert "video/x-matroska" in video[0].mime_type def test_total_count(self): items = _parse_didl(_SAMPLE_DIDL, "http://srv") assert len(items) == 3 def test_empty_didl(self): empty = '' assert _parse_didl(empty, "http://srv") == [] def test_malformed_xml(self): assert _parse_didl("<<>>", "http://srv") == [] def test_relative_url_resolution(self): didl = """\ Relative object.item.audioItem media/track.flac """ items = _parse_didl(didl, "http://myserver:8200") assert items[0].resource_url == "http://myserver:8200/media/track.flac" def test_prefers_video_resource_over_subtitle_resource(self): didl = """\ Movie object.item.videoItem http://srv/movie.vtt http://srv/movie.mkv """ items = _parse_didl(didl, "http://srv") assert items[0].resource_url == "http://srv/movie.mkv" assert items[0].mime_type == "video/x-matroska" class TestExtractBrowseResult: def test_extracts_unnamespaced_result(self): soap = """\ <DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"><container id="1" parentID="0" /></DIDL-Lite> """ result = _extract_browse_result(soap) assert result is not None assert "DIDL-Lite" in result assert 'container id="1"' in result def test_extracts_namespaced_result(self): soap = """\ <DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"></DIDL-Lite> """ assert _extract_browse_result(soap) is not None def test_extracts_embedded_xml_result(self): soap = """\ """ result = _extract_browse_result(soap) assert result is not None assert "DIDL-Lite" in result def test_returns_none_when_missing(self): soap = "" assert _extract_browse_result(soap) is None