def get_titles_and_notes_test(self): """ Test PresentationDocument.get_titles_and_notes method """ # GIVEN: A mocked open, get_thumbnail_folder and exists with patch('builtins.open', mock_open(read_data='uno\ndos\n')) as mocked_open, \ patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \ patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists: mocked_get_thumbnail_folder.return_value = 'test' mocked_exists.return_value = True # WHEN: calling get_titles_and_notes result_titles, result_notes = self.document.get_titles_and_notes() # THEN: it should return two items for the titles and two empty strings for the notes self.assertIs(type(result_titles), list, 'result_titles should be of type list') self.assertEqual(len(result_titles), 2, 'There should be two items in the titles') self.assertIs(type(result_notes), list, 'result_notes should be of type list') self.assertEqual(len(result_notes), 2, 'There should be two items in the notes') self.assertEqual(mocked_open.call_count, 3, 'Three files should be opened') mocked_open.assert_any_call(os.path.join('test', 'titles.txt'), encoding='utf-8') mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'), encoding='utf-8') mocked_open.assert_any_call(os.path.join('test', 'slideNotes2.txt'), encoding='utf-8') self.assertEqual(mocked_exists.call_count, 3, 'Three files should have been checked')
def test_get_titles_and_notes(self): """ Test PresentationDocument.get_titles_and_notes method """ # GIVEN: A mocked open, get_thumbnail_folder and exists with patch('builtins.open', mock_open(read_data='uno\ndos\n')) as mocked_open, \ patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder, \ patch('openlp.plugins.presentations.lib.presentationcontroller.os.path.exists') as mocked_exists: mocked_get_thumbnail_folder.return_value = 'test' mocked_exists.return_value = True # WHEN: calling get_titles_and_notes result_titles, result_notes = self.document.get_titles_and_notes() # THEN: it should return two items for the titles and two empty strings for the notes self.assertIs(type(result_titles), list, 'result_titles should be of type list') self.assertEqual(len(result_titles), 2, 'There should be two items in the titles') self.assertIs(type(result_notes), list, 'result_notes should be of type list') self.assertEqual(len(result_notes), 2, 'There should be two items in the notes') self.assertEqual(mocked_open.call_count, 3, 'Three files should be opened') mocked_open.assert_any_call(os.path.join('test', 'titles.txt'), encoding='utf-8') mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'), encoding='utf-8') mocked_open.assert_any_call(os.path.join('test', 'slideNotes2.txt'), encoding='utf-8') self.assertEqual(mocked_exists.call_count, 3, 'Three files should have been checked')
def test_serve_thumbnail_with_valid_params(self): """ Test the serve_thumbnail routine with valid params """ # GIVEN: Mocked send_header, send_response, end_headers and wfile self.router.send_response = MagicMock() self.router.send_header = MagicMock() self.router.end_headers = MagicMock() self.router.wfile = MagicMock() mocked_image_manager = MagicMock() Registry().register('image_manager', mocked_image_manager) file_name = 'another%20test/slide1.png' full_path = os.path.normpath(os.path.join('thumbnails', file_name)) width = 120 height = 90 with patch('openlp.core.lib.os.path.exists') as mocked_exists, \ patch('builtins.open', mock_open(read_data='123')), \ patch('openlp.plugins.remotes.lib.httprouter.AppLocation') as mocked_location, \ patch('openlp.plugins.remotes.lib.httprouter.image_to_byte') as mocked_image_to_byte: mocked_exists.return_value = True mocked_image_to_byte.return_value = '123' mocked_location.get_section_data_path.return_value = '' # WHEN: pass good controller and filename self.router.serve_thumbnail('presentations', '{0}x{1}'.format(width, height), file_name) # THEN: a file should be returned self.assertEqual(self.router.send_header.call_count, 1, 'One header') self.assertEqual(self.router.send_response.call_count, 1, 'Send response called once') self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once') mocked_exists.assert_called_with(urllib.parse.unquote(full_path)) self.assertEqual(mocked_image_to_byte.call_count, 1, 'Called once') mocked_image_manager.add_image.assert_any_call( os.path.normpath( os.path.join('thumbnails', 'another test', 'slide1.png')), 'slide1.png', None, width, height) mocked_image_manager.get_image.assert_any_call( os.path.normpath( os.path.join('thumbnails', 'another test', 'slide1.png')), 'slide1.png', width, height)
def serve_file_with_valid_params_test(self): """ Test the serve_file method with an existing file """ # GIVEN: mocked environment self.router.send_response = MagicMock() self.router.send_header = MagicMock() self.router.end_headers = MagicMock() self.router.wfile = MagicMock() self.router.html_dir = os.path.normpath('test/dir') self.router.template_vars = MagicMock() with patch('openlp.core.lib.os.path.exists') as mocked_exists, \ patch('builtins.open', mock_open(read_data='123')): mocked_exists.return_value = True # WHEN: call serve_file with an existing html file self.router.serve_file(os.path.normpath('test/dir/test.html')) # THEN: it should return a 200 and the file self.router.send_response.assert_called_once_with(200) self.router.send_header.assert_called_once_with('Content-type', 'text/html') self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')
def test_save_titles_and_notes(self): """ Test PresentationDocument.save_titles_and_notes method with two valid lists """ # GIVEN: two lists of length==2 and a mocked open and get_thumbnail_folder mocked_open = mock_open() with patch('builtins.open', mocked_open), patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder: titles = ['uno', 'dos'] notes = ['one', 'two'] # WHEN: calling save_titles_and_notes mocked_get_thumbnail_folder.return_value = 'test' self.document.save_titles_and_notes(titles, notes) # THEN: the last call to open should have been for slideNotes2.txt mocked_open.assert_any_call(os.path.join('test', 'titles.txt'), mode='wt', encoding='utf-8') mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'), mode='wt', encoding='utf-8') mocked_open.assert_any_call(os.path.join('test', 'slideNotes2.txt'), mode='wt', encoding='utf-8') self.assertEqual(mocked_open.call_count, 3, 'There should be exactly three files opened') mocked_open().writelines.assert_called_once_with(['uno', 'dos']) mocked_open().write.assert_any_call('one') mocked_open().write.assert_any_call('two')
def save_titles_and_notes_test(self): """ Test PresentationDocument.save_titles_and_notes method with two valid lists """ # GIVEN: two lists of length==2 and a mocked open and get_thumbnail_folder mocked_open = mock_open() with patch('builtins.open', mocked_open), patch(FOLDER_TO_PATCH) as mocked_get_thumbnail_folder: titles = ['uno', 'dos'] notes = ['one', 'two'] # WHEN: calling save_titles_and_notes mocked_get_thumbnail_folder.return_value = 'test' self.document.save_titles_and_notes(titles, notes) # THEN: the last call to open should have been for slideNotes2.txt mocked_open.assert_any_call(os.path.join('test', 'titles.txt'), mode='wt', encoding='utf-8') mocked_open.assert_any_call(os.path.join('test', 'slideNotes1.txt'), mode='wt', encoding='utf-8') mocked_open.assert_any_call(os.path.join('test', 'slideNotes2.txt'), mode='wt', encoding='utf-8') self.assertEqual(mocked_open.call_count, 3, 'There should be exactly three files opened') mocked_open().writelines.assert_called_once_with(['uno', 'dos']) mocked_open().write.assert_called_any('one') mocked_open().write.assert_called_any('two')
def test_serve_file_with_partial_params(self): """ Test the serve_file method with an existing file """ # GIVEN: mocked environment self.router.send_response = MagicMock() self.router.send_header = MagicMock() self.router.end_headers = MagicMock() self.router.wfile = MagicMock() self.router.html_dir = os.path.normpath('test/dir') self.router.template_vars = MagicMock() with patch('openlp.core.lib.os.path.exists') as mocked_exists, \ patch('builtins.open', mock_open(read_data='123')): mocked_exists.return_value = True # WHEN: call serve_file with an existing html file self.router.serve_file(os.path.normpath('test/dir/test')) # THEN: it should return a 200 and the file self.router.send_response.assert_called_once_with(200) self.router.send_header.assert_called_once_with('Content-type', 'text/html') self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')
def serve_thumbnail_with_valid_params_test(self): """ Test the serve_thumbnail routine with valid params """ # GIVEN: Mocked send_header, send_response, end_headers and wfile self.router.send_response = MagicMock() self.router.send_header = MagicMock() self.router.end_headers = MagicMock() self.router.wfile = MagicMock() mocked_image_manager = MagicMock() Registry.create() Registry().register("image_manager", mocked_image_manager) file_name = "another%20test/slide1.png" full_path = os.path.normpath(os.path.join("thumbnails", file_name)) width = 120 height = 90 with patch("openlp.core.lib.os.path.exists") as mocked_exists, patch( "builtins.open", mock_open(read_data="123") ), patch("openlp.plugins.remotes.lib.httprouter.AppLocation") as mocked_location, patch( "openlp.plugins.remotes.lib.httprouter.image_to_byte" ) as mocked_image_to_byte: mocked_exists.return_value = True mocked_image_to_byte.return_value = "123" mocked_location.get_section_data_path.return_value = "" # WHEN: pass good controller and filename self.router.serve_thumbnail("presentations", "{0}x{1}".format(width, height), file_name) # THEN: a file should be returned self.assertEqual(self.router.send_header.call_count, 1, "One header") self.assertEqual(self.router.send_response.call_count, 1, "Send response called once") self.assertEqual(self.router.end_headers.call_count, 1, "end_headers called once") mocked_exists.assert_called_with(urllib.parse.unquote(full_path)) self.assertEqual(mocked_image_to_byte.call_count, 1, "Called once") mocked_image_manager.assert_called_any( os.path.normpath("thumbnails\\another test"), "slide1.png", None, "120x90" ) mocked_image_manager.assert_called_any(os.path.normpath("thumbnails\\another test"), "slide1.png", "120x90")
def test_serve_thumbnail_with_valid_params(self): """ Test the serve_thumbnail routine with valid params """ # GIVEN: Mocked send_header, send_response, end_headers and wfile self.router.send_response = MagicMock() self.router.send_header = MagicMock() self.router.end_headers = MagicMock() self.router.wfile = MagicMock() mocked_image_manager = MagicMock() Registry().register('image_manager', mocked_image_manager) file_name = 'another%20test/slide1.png' full_path = os.path.normpath(os.path.join('thumbnails', file_name)) width = 120 height = 90 with patch('openlp.core.lib.os.path.exists') as mocked_exists, \ patch('builtins.open', mock_open(read_data='123')), \ patch('openlp.plugins.remotes.lib.httprouter.AppLocation') as mocked_location, \ patch('openlp.plugins.remotes.lib.httprouter.image_to_byte') as mocked_image_to_byte: mocked_exists.return_value = True mocked_image_to_byte.return_value = '123' mocked_location.get_section_data_path.return_value = '' # WHEN: pass good controller and filename self.router.serve_thumbnail('presentations', '{0}x{1}'.format(width, height), file_name) # THEN: a file should be returned self.assertEqual(self.router.send_header.call_count, 1, 'One header') self.assertEqual(self.router.send_response.call_count, 1, 'Send response called once') self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once') mocked_exists.assert_called_with(urllib.parse.unquote(full_path)) self.assertEqual(mocked_image_to_byte.call_count, 1, 'Called once') mocked_image_manager.add_image.assert_any_call(os.path.normpath(os.path.join('thumbnails', 'another test', 'slide1.png')), 'slide1.png', None, width, height) mocked_image_manager.get_image.assert_any_call(os.path.normpath(os.path.join('thumbnails', 'another test', 'slide1.png')), 'slide1.png', width, height)
def serve_file_with_valid_params_test(self): """ Test the serve_file method with an existing file """ # GIVEN: mocked environment self.router.send_response = MagicMock() self.router.send_header = MagicMock() self.router.end_headers = MagicMock() self.router.wfile = MagicMock() self.router.html_dir = os.path.normpath("test/dir") self.router.template_vars = MagicMock() with patch("openlp.core.lib.os.path.exists") as mocked_exists, patch( "builtins.open", mock_open(read_data="123") ): mocked_exists.return_value = True # WHEN: call serve_file with an existing html file self.router.serve_file(os.path.normpath("test/dir/test.html")) # THEN: it should return a 200 and the file self.router.send_response.assert_called_once_with(200) self.router.send_header.assert_called_once_with("Content-type", "text/html") self.assertEqual(self.router.end_headers.call_count, 1, "end_headers called once")