# -*- coding: utf-8 -*- from __future__ import (division, absolute_import, print_function, unicode_literals) from test._common import unittest from test.helper import TestHelper from beetsplug.plexupdate import get_music_section, update_plex import responses class PlexUpdateTest(unittest.TestCase, TestHelper): def add_response_get_music_section(self, section_name='Music'): """Create response for mocking the get_music_section function. """ escaped_section_name = section_name.replace('"', '\\"') body = ( '' '' '' '' '' '' '' '' '' '' '' '') status = 200 content_type = 'text/xml;charset=utf-8' responses.add(responses.GET, 'http://localhost:32400/library/sections', body=body, status=status, content_type=content_type) def add_response_update_plex(self): """Create response for mocking the update_plex function. """ body = '' status = 200 content_type = 'text/html' responses.add(responses.GET, 'http://localhost:32400/library/sections/2/refresh', body=body, status=status, content_type=content_type) def setUp(self): self.setup_beets() self.load_plugins('plexupdate') self.config['plex'] = { u'host': u'localhost', u'port': 32400} def tearDown(self): self.teardown_beets() self.unload_plugins() @responses.activate def test_get_music_section(self): # Adding response. self.add_response_get_music_section() # Test if section key is "2" out of the mocking data. self.assertEqual(get_music_section( self.config['plex']['host'], self.config['plex']['port'], self.config['plex']['token'], self.config['plex']['library_name'].get()), '2') @responses.activate def test_get_named_music_section(self): # Adding response. self.add_response_get_music_section('My Music Library') self.assertEqual(get_music_section( self.config['plex']['host'], self.config['plex']['port'], self.config['plex']['token'], 'My Music Library'), '2') @responses.activate def test_update_plex(self): # Adding responses. self.add_response_get_music_section() self.add_response_update_plex() # Testing status code of the mocking request. self.assertEqual(update_plex( self.config['plex']['host'], self.config['plex']['port'], self.config['plex']['token'], self.config['plex']['library_name'].get()).status_code, 200) def suite(): return unittest.TestLoader().loadTestsFromName(__name__) if __name__ == b'__main__': unittest.main(defaultTest='suite')