def test_load_supported_file_format(self, mock_zipfile): """Test load method from ComicZipLoader class. Load only image files. """ mock_open_fn = mock.mock_open(read_data='') # Insert data in ZipFile mock object for f in self.files: self.mock_zip_file.write(f) mock_zipfile.return_value = self.mock_zip_file mock_zipfile.return_value.open = mock_open_fn with mock.patch.object(builtins, 'open', mock_open_fn): # Verify if data is empty self.assertListEqual(self.loader.data, []) # Call load method self.loader.load(self.comic_name) # The data list not is empty. All images files were loaded self.assertTrue(self.loader.data) # The data list must be sorted in alphabetical order # then we sort this list to make easy compare self.files.sort() self.files.remove('text_file.txt') # Compare loaded file with files in self.files for idx, page in enumerate(self.loader.data): # Verify with exist data self.assertTrue(page.data) # Verify page title self.assertEqual(page.title, os.path.join(self.files[idx])) # Verify if all loaded image is support format file self.assertIn(get_file_extension(page.title), IMAGE_FILE_FORMATS) # Verify page number self.assertEqual(page.number, idx + 1)
def test_load_supported_file_format(self, mock_rarfile): """Test load method from ComicZipLoader class. Load only image files. """ mock_open_fn = mock.mock_open(read_data='') for f in self.files: self.mock_rar_file.write(f) mock_rarfile.return_value = self.mock_rar_file mock_rarfile.return_value.open = mock_open_fn with mock.patch.object(builtins, 'open', mock_open_fn): # Verify if data is empty self.assertListEqual(self.loader.data, []) # Call load method self.loader.load(self.comic_name) # The data list not is empty. All images files were loaded self.assertTrue(self.loader.data) # The data list must be sorted in alphabetical order # then we sort this list to make easy compare self.files.sort() self.files.remove('text_file_01.txt') # Compare loaded file with files in self.files for idx, page in enumerate(self.loader.data): # Verify with exist data self.assertTrue(page.data) # Verify page title self.assertEqual(page.title, os.path.join(self.files[idx])) # Verify if all loaded image is support format file self.assertIn(get_file_extension(page.title), IMAGE_FILE_FORMATS) # Verify page number self.assertEqual(page.number, idx + 1)
def test_get_file_extension(self): self.assertEqual(get_file_extension('myfile.zip'), '.zip') self.assertEqual(get_file_extension('myfile'), '')