def test_list_with_double_filter_one_task(self, mock_print, mock_open, mock_isfile): with patch.object(sys, 'argv', [pypodo, list, 'test', 'linux']): mock_isfile.return_value = True list(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi( mock_print.getvalue().rstrip('\n')), '3 ma troisieme tache #linux #test')
def test_list_with_double_filter_one_task_return_empty(self, mock_print, mock_open, mock_isfile): with patch.object(sys, 'argv', [pypodo, list, 'test', 'pe']): mock_isfile.return_value = True list(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi( mock_print.getvalue().rstrip('\n')), 'warning : the filtered todolist is empty')
def test_list_if_todo_one_task(self, mock_print, mock_open, mock_isfile): with patch.object(sys, 'argv', [pypodo, list]): mock_isfile.return_value = True list(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi( mock_print.getvalue().rstrip('\n')), '1 ma tache')
def test_list_TAG_retourne_une_tache_avec_deux_parametres( self, mock_print, mock_open): with patch.object(sys, 'argv', [pypodo, list, 'test', 'linux']): list(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip('\n')), '3 ma troisieme tache #linux #test')
def test_update_existing_instance(self, mock_open): self.local_instance_repo._exist = MagicMock(return_value=True) new_mpc_instance = copy.deepcopy(self.mpc_instance) new_mpc_instance.game_name = "aggregator" path = TEST_BASE_DIR.joinpath(TEST_INSTANCE_ID) self.assertIsNone(self.local_instance_repo.update(new_mpc_instance)) mock_open.assert_called_with(path, "w")
def test_list_noTAG_retourne_une_tache_avec_un_tag(self, mock_print, mock_open): with patch.object(sys, 'argv', [pypodo, untag]): untag(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip('\n')), '2 ma seconde tache')
def test_success_with_multiple_profile(self, mock_open, redirect_mock, mvn_mock): with TempDirectory() as temp_dir: mvn_output_file_path = os.path.join(temp_dir.path, 'maven_output.txt') settings_file = '/fake/settings.xml' pom_file = '/fake/pom.xml' phases_and_goals = 'fake' run_maven(mvn_output_file_path=mvn_output_file_path, settings_file=settings_file, pom_file=pom_file, phases_and_goals=phases_and_goals, profiles=['fake-profile1', 'fake-profile2']) mock_open.assert_called_with(mvn_output_file_path, 'w') redirect_mock.assert_has_calls([ call([sys.stdout, mock_open.return_value]), call([sys.stderr, mock_open.return_value]) ]) mvn_mock.assert_called_once_with('fake', '-f', '/fake/pom.xml', '-s', '/fake/settings.xml', '-P', 'fake-profile1,fake-profile2', '--no-transfer-progress', _out=Any(StringIO), _err=Any(StringIO))
def test_get_excel_without_filename(self): """When no filename is given, the one in the headers is used.""" with patch("reportforce.report.open", mock_open, create=True): self.rf.get_report("report_id", excel=True) mock_open.assert_called_with("spreadsheet.xlsx", "wb")
def test_list_TAG_retourne_empty_avec_deux_parametres( self, mock_print, mock_open): with patch.object(sys, 'argv', [pypodo, list, 'test', 'pe']): list(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip('\n')), 'warning : the filtered todolist is empty')
def test_list_TAG_retourne_une_tache_avec_un_tag(self, mock_print, mock_open): with patch.object(sys, 'argv', [pypodo, list, 'test']): list(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip('\n')), '1 ma tache #test')
def test_get_excel_with_filename(self): """When a filename is given, it is used.""" with patch("reportforce.report.open", mock_open, create=True): self.rf.get_report("report_id", excel="filename.xlsx") mock_open.assert_called_with("filename.xlsx", "wb")
def testContentsWriter(self, mock_open, mock_remove): cp_writer = None with oc.ContentsWriter() as writer: mock_open.assert_called_with(writer.filename, "w") writer.file.write.assert_called_with("[") writer.file.reset_mock() writer.write({}) writer.file.write.assert_called_with("{}") writer.file.reset_mock() writer.write({}) writer.file.write.assert_has_calls( [mock.call(",\n"), mock.call("{}")]) cp_writer = writer cp_writer.file.close.assert_called() mock_remove.assert_not_called() try: with oc.ContentsWriter() as writer: cp_writer = writer raise Exception("Any exception") except Exception: pass mock_remove.assert_called_with(cp_writer.filename)
def test_list_tag(self, mock_print, mock_open, mock_isfile): with patch.object(sys, 'argv', [pypodo, tag]): mock_isfile.return_value = True tag(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi( mock_print.getvalue().rstrip('\n')), '#linux\n'+'#test')
def test_tag_task(self, mock_print, mock_open, mock_isfile): with patch.object(sys, 'argv', [pypodo, tag, 'test', '1', '2', '3']): mock_isfile.return_value = True tag(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'w') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip( '\n')), 'info : tag added to the task of the todolist - 1 ma tache -> 1 ma tache #test\ninfo : tag added to the task of the todolist - 2 ma seconde tache #tost -> 2 ma seconde tache #tost #test\nwarning : no task with number is in the todolist - 3')
def test_success_with_no_tls(self, mock_open, redirect_mock, mvn_mock): with TempDirectory() as temp_dir: mvn_output_file_path = os.path.join(temp_dir.path, 'maven_output.txt') settings_file = '/fake/settings.xml' pom_file = '/fake/pom.xml' phases_and_goals = 'fake' run_maven(mvn_output_file_path=mvn_output_file_path, settings_file=settings_file, pom_file=pom_file, phases_and_goals=phases_and_goals, tls_verify=False) mock_open.assert_called_with(mvn_output_file_path, 'w') redirect_mock.assert_has_calls([ call([sys.stdout, mock_open.return_value]), call([sys.stderr, mock_open.return_value]) ]) mvn_mock.assert_called_once_with( 'fake', '-f', '/fake/pom.xml', '-s', '/fake/settings.xml', '--no-transfer-progress', '-Dmaven.wagon.http.ssl.insecure=true', '-Dmaven.wagon.http.ssl.allowall=true', '-Dmaven.wagon.http.ssl.ignore.validity.dates=true', _out=Any(StringIO), _err=Any(StringIO))
def test_add_TACHE_ajoute_une_tache(self, mock_print, mock_open): with patch.object(sys, 'argv', [pypodo, add, 'task3']): add(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'a') mock_open().write.assert_called_once_with('1 task3\n') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip('\n')), 'info : task is added to the todolist - 1 task3')
def test_delete_INDEX_supprime_une_tache(self, mock_print, mock_open): with patch.object(sys, 'argv', [pypodo, delete, '2']): delete(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'w') mock_open().write.assert_called_once_with('1 ma tache #test\n') self.assertEqual( escape_ansi(mock_print.getvalue().rstrip('\n')), 'info : task deleted from the todolist - 2 ma seconde tache')
def test_untag_task(self, mock_print, mock_open, mock_isfile): with patch.object(sys, 'argv', [pypodo, untag, 'test', '1', '2', '3']): mock_isfile.return_value = True untag(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'w') mock_open().write.assert_called_with('3 ma seconde tache #titi\n') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip( '\n')), 'info : tag deleted from the task of the todolist - 1 ma tache #test #toto -> 1 ma tache #toto\nwarning : no tags is deleted from the todolist for the task - 2 ma seconde tache #tost #titi\ninfo : tag deleted from the task of the todolist - 3 ma seconde tache #test #titi -> 3 ma seconde tache #titi')
def test_transform_function_error(self, mock_rename, mock_subprocess, mock_open): mock_subprocess.side_effect = Exception('exception') sync = FileSync() result = sync.transform_file('file.txt') self.assertEqual(mock_subprocess.call_count, 1) self.assertEqual(result[0], 'file.txt') mock_open.assert_called_with('temp_files/lowriter_in.txt', 'rb')
def test_create_non_existing_instance(self, mock_dump, mock_open): self.local_instance_repo.repo._exist = MagicMock(return_value=False) path = TEST_BASE_DIR.joinpath(TEST_INSTANCE_ID) mock_dump.return_value = None stream = mock_open().__enter__.return_value self.assertIsNone(self.local_instance_repo.create(self.mpc_instance)) mock_open.assert_called_with(path, "wb") mock_dump.assert_called_with(self.mpc_instance, stream)
def test_add_task_todolist_not_empty(self, mock_print, mock_open, mock_isfile): with patch.object(sys, 'argv', [pypodo, add, 'task3']): mock_isfile.return_value = True add(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'a') mock_open().write.assert_called_once_with('5 task3\n') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip( '\n')), 'info : task is added to the todolist - 5 task3')
def test_transform_function_silent_fail(self, mock_rename, mock_path, mock_subprocess, mock_open): mock_subprocess.call.return_value = None mock_path.return_value = False expected_call_one = [ 'lowriter', '--convert-to', 'pdf:writer_pdf_Export', 'temp_files/lowriter_in.txt', '--outdir', 'temp_files' ] expected_call_two = [ 'ps2pdf', '-dPDFSETTINGS=/ebook', 'temp_files/lowriter_in.pdf', 'temp_files/compressed.pdf' ] expected_calls = [call(expected_call_one), call(expected_call_two)] sync = FileSync() result = sync.transform_file('file.txt') self.assertEqual(mock_subprocess.call_count, 2) self.assertEqual(result[0], 'file.txt') self.assertTrue(mock_subprocess.call_args_list[0], expected_call_one) mock_subprocess.assert_has_calls(expected_calls) mock_open.assert_called_with('temp_files/lowriter_in.txt', 'rb')
def test_success_failure(self, mock_open, redirect_mock, mvn_mock): with TempDirectory() as temp_dir: mvn_output_file_path = os.path.join(temp_dir.path, 'maven_output.txt') settings_file = '/fake/settings.xml' pom_file = '/fake/pom.xml' phases_and_goals = 'fake' mvn_mock.side_effect = sh.ErrorReturnCode('mvn', b'mock stdout', b'mock error') with self.assertRaisesRegex( StepRunnerException, re.compile( r"Error running maven." r".*RAN: mvn" r".*STDOUT:" r".*mock stdout" r".*STDERR:" r".*mock error", re.DOTALL)): run_maven( mvn_output_file_path=mvn_output_file_path, settings_file=settings_file, pom_file=pom_file, phases_and_goals=phases_and_goals, ) mock_open.assert_called_with(mvn_output_file_path, 'w') redirect_mock.assert_has_calls([ call([sys.stdout, mock_open.return_value]), call([sys.stderr, mock_open.return_value]) ]) mvn_mock.assert_called_once_with('fake', '-f', '/fake/pom.xml', '-s', '/fake/settings.xml', '--no-transfer-progress', _out=Any(StringIO), _err=Any(StringIO))
def test_generate_proto_definition(self, mock_open): generator = GRPCCodeGenerator() generator._write_header = MagicMock() generator._write_services = MagicMock() generator._write_messages = MagicMock() generator._absolute_path = lambda x: 'abs_' + x manager = Mock() manager.attach_mock(generator._write_header, '_write_header') manager.attach_mock(generator._write_services, '_write_services') manager.attach_mock(generator._write_messages, '_write_messages') generator.generate_proto_definition() mock_open.assert_called_with('abs_' + PROTO_LOCATION, 'w') self.assertEqual([ call._write_header(mock_open.return_value.__enter__.return_value), call._write_services( mock_open.return_value.__enter__.return_value), call._write_messages( mock_open.return_value.__enter__.return_value), ], manager.mock_calls)
def test_create_non_existing_instance(self, mock_open): self.local_instance_repo._exist = MagicMock(return_value=False) path = TEST_BASE_DIR.joinpath(TEST_INSTANCE_ID) self.assertIsNone(self.local_instance_repo.create(self.mpc_instance)) mock_open.assert_called_with(path, "w")
def test_dump(self, mock_dump, mock_open): mock_dump.return_value = None stream = mock_open().__enter__.return_value self.assertIsNone(dump(TEST_DICT, TEST_FILENAME)) mock_open.assert_called_with(TEST_FILENAME, "w") mock_dump.assert_called_with(TEST_DICT, stream)
def test_init(self, mock_open): reader = oc.ContentsReader("filename") mock_open.assert_called_with("filename", "r") self.assertIsNotNone(reader.file)
def test_list_TAGS_retourne_tags(self, mock_print, mock_open): with patch.object(sys, 'argv', [pypodo, tag]): tag(mock_open) mock_open.assert_called_with(STR_PATH_HOME__TODO_, 'r') self.assertEqual(escape_ansi(mock_print.getvalue().rstrip('\n')), '#linux\n' + '#test')