def test_setup_and_run_handles_exception(self, mock_config_list, mock_parser, mock_failure): mock_parser.return_value = [] mock_config_list.return_value = [self._path] Configuration.NAMESPACE = 'pyccata.core' report = ReportManager() # never actually thrown from the filter but useful for testing and coverage ;-) mock_failure.return_value = InvalidFilenameError('The specified file does not exist') report.add_callback('test', 'test_callback') self.assertEquals('test_callback', report.get_callback('test')) self.assertIsInstance(Configuration().replacements, Replacements) Config = namedtuple('Config', 'query fields collate output_path') config = Config( query='project=test and attachments is not empty', fields=[ 'key', 'attachments' ], collate='zip', output_path='/tmp/{FIX_VERSION}' ) Replacements().find('FIX_VERSION').value = '28/Jul/2016' attachments = None with patch('os.makedirs') as mock_os: attachments = Attachments(self._thread_manager, config) mock_os.assert_called_with('/tmp/28/Jul/2016') with patch('pyccata.core.filter.Filter.failed', return_value=True): attachments.run() self.assertEquals(str(attachments._content.failure), 'The specified file does not exist')
def test_run_raises_type_error_if_attachments_callback_function_is_not_set( self, mock_config_list, mock_results, mock_jira_results, mock_jira_client, mock_open ): mock_jira_client.return_value = None mock_config_list.return_value = [self._path] Configuration.NAMESPACE = 'pyccata.core' report = ReportManager() report.add_callback('attachments', None) mock_jira_results.return_value = DataProviders._test_data_for_attachments() server = namedtuple('Server', 'server_address attachments') mock_results.return_value = server(server_address=None, attachments=None) self.assertIsInstance(Configuration().replacements, Replacements) Config = namedtuple('Config', 'query fields collate output_path') config = Config( query='project=test and attachments is not empty', fields=[ 'key', 'attachments' ], collate='zip', output_path='/tmp/{FIX_VERSION}' ) attachments = None with patch('os.path.exists') as mock_exists: mock_exists.return_value = False with patch('os.makedirs') as mock_os: attachments = Attachments(self._thread_manager, config) mock_os.assert_called_with('/tmp/' + Configuration().replacements.replace('{FIX_VERSION}')) self._thread_manager.append(attachments) with patch('pycurl.Curl') as mock_curl: with patch('pycurl.Curl.setopt') as mock_setopt: with patch('pycurl.Curl.perform') as mock_perform: with patch('pycurl.Curl.close') as mock_close: Curl = namedtuple('Curl', 'URL WRITEDATA setopt perform close getinfo') mock_curl.return_value = Curl( URL=None, WRITEDATA=None, setopt=mock_setopt, perform=mock_perform, close=mock_close, getinfo=lambda x: 200 ) with self.assertRaises(InvalidCallbackError): self._thread_manager.execute() self.assertIsInstance(attachments.failure, InvalidCallbackError) mock_setopt.assert_not_called() mock_perform.assert_not_called() mock_close.assert_not_called() mock_open.assert_not_called()
def test_report_callback_returns_callback(self, mock_config_list, mock_parser): mock_parser.return_value = [] mock_config_list.return_value = [self._path] Configuration.NAMESPACE = 'pyccata.core' config = Configuration(filename='valid_config.json') config.check = True report = ReportManager() report.add_callback('test', 'test_callback') self.assertEquals('test_callback', report.get_callback('test'))
def test_add_page_break(self, mock_document, mock_load): with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_reporting: with patch( 'pyccata.core.configuration.Configuration._configuration', new_callable=PropertyMock) as mock_config: mock_config.return_value = DataProviders._get_config_for_test() mock_reporting.return_value = 'docx' r = ReportManager() r.add_page_break() mock_document.assert_called_with()
def test_add_paragraph(self, mock_document, mock_load, mock_locations): with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_reporting: with patch( 'pyccata.core.configuration.Configuration._configuration', new_callable=PropertyMock) as mock_config: mock_locations.return_value = [self._path] mock_config.return_value = DataProviders._get_config_for_test() mock_reporting.return_value = 'docx' r = ReportManager() r.add_paragraph('hello world') mock_document.assert_called_with('hello world', style=None)
def test_report_raises_import_error_if_report_class_does_not_implement_reporting_interface( self, mock_load): Config = namedtuple( 'Config', 'reporting report path title subtitle abstract sections') Report = Config(reporting=None, report=None, path='/path/to', title='Test report', subtitle='A Subtitle', abstract='', sections='') self.tearDown() Configuration.NAMESPACE = 'tests.mocks' mock_config = Config(reporting='pdf', report=Report, path=None, title=None, subtitle=None, abstract=None, sections=None) Configuration._configuration = mock_config with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_report: mock_report.return_value = 'pdf' with self.assertRaises(ImportError): a = ReportManager()
def test_table_renders_filter_results_from_cell_query_continues_if_filter_errors( self, mock_heading, mock_table): self.tearDown() Config = namedtuple('Config', 'rows columns style') with patch('pyccata.core.filter.Filter.results', new_callable=PropertyMock) as mock_results: ResultsList = namedtuple('ResultsList', 'total results') Result = namedtuple('Result', 'key release_text business_representative') mock_results.return_value = ResultsList( total=1, results=[ Result(key='testproj-123', release_text='I am test text', business_representative='Bob Smith') ]) Filter = namedtuple('Filter', 'max_results namespace') rows = Filter(max_results=5, namespace='pyccata.core') columns = ['Name', 'Description'] config = Config(rows=[['Data', rows]], columns=columns, style='Light heading 1') table = Table(self._thread_manager, config) table.title = 'Test Title' document = ReportManager() table.render(document) self.assertEquals(1, mock_table.call_count) self.assertEquals(1, mock_heading.call_count) mock_heading.assert_called_with('Test Title', 3)
def test_section_render(self, mock_paragraph): Config = namedtuple('Config', 'title, abstract level structure') config = Config(title='hello world', abstract='section_test_text', level=0, structure=None) section = Section(self._thread_manager, config) section.render(ReportManager())
def test_add_table(self, mock_document, mock_load): with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_reporting: with patch( 'pyccata.core.configuration.Configuration._configuration', new_callable=PropertyMock) as mock_config: mock_config.return_value = DataProviders._get_config_for_test() mock_reporting.return_value = 'docx' mock_document.return_value = self._document.add_table(rows=1, cols=3) r = ReportManager() headers = ['Test header', 'Another Test Header', 'Value'] data = [[1, 2, 3], [4, 5, 9], [3, 9., 12]] r.add_table(headings=headers, data=data, style='Test Style') mock_document.assert_called_with(rows=1, cols=3, style='Test Style')
def test_report_raises_invalid_module_error_if_report_module_does_not_exist( self, mock_load): with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_report: mock_report.return_value = 'iamanoneexistantreport' with self.assertRaisesRegexp( InvalidModuleError, 'iamanoneexistantreport.*{0}.*'.format( Configuration.NAMESPACE)): ReportManager()
def reportmanager(self): """ load the reportmanager """ try: if self._report_manager is None: return ReportManager() except (ImportError, AttributeError, NotImplementedError, RequiredKeyError) as exception: ControllerAbstract._raise_and_terminate('ReportManager', exception) return self._report_manager
def test_maxwidth(self, mock_load): with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_reporting: with patch( 'pyccata.core.configuration.Configuration._configuration', new_callable=PropertyMock) as mock_config: mock_config.return_value = DataProviders._get_config_for_test() mock_reporting.return_value = 'docx' r = ReportManager() self.assertEquals(5.7, r.maxwidth)
def setUp(self, mock_config, mock_parser, mock_log): path = os.path.dirname(os.path.realpath(__file__ + '../../../../')) self._path = os.path.join(path, os.path.join('tests', 'conf')) mock_config.return_value = [self._path] mock_parser.return_value = [] mock_log.return_value = None Logger._instance = mock_log Configuration(filename='config_sections.json') self._report_manager = ReportManager() self._thread_manager = ThreadManager()
def test_report_raises_invalid_class_error_if_report_class_does_not_exist( self, mock_load): Configuration.NAMESPACE = 'tests.mocks' with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_report: mock_report.return_value = 'reportmodule' with self.assertRaisesRegexp( InvalidClassError, 'Reportmodule .* {0}.*'.format(Configuration.NAMESPACE)): ReportManager()
def test_render_image(self, width, expected, mock_picture): Config = namedtuple('Config', 'filename width') config = Config(filename='department.png', width=width) picture = Picture(self._thread_manager, config) picture.run() picture.render(ReportManager()) mock_picture.assert_called_with(os.path.join( os.getcwd(), Configuration().report.datapath, config.filename), width=expected)
def test_setup_and_run_doesnt_download_if_attachments_is_empty( self, collation, result_count, result_filename, mock_config_list, mock_results, mock_jira_client, mock_open ): mock_jira_client.return_value = None mock_config_list.return_value = [self._path] Configuration.NAMESPACE = 'pyccata.core' report = ReportManager() mock_results.return_value = [] report.add_callback('attachments', lambda x,y: '') self.assertIsInstance(Configuration().replacements, Replacements) Config = namedtuple('Config', 'query fields collate output_path') config = Config( query='project=test and attachments is not empty', fields=[ 'key', 'attachments' ], collate=collation, output_path='/tmp/{FIX_VERSION}' ) Replacements().find('FIX_VERSION').value = '28/Jul/2016' attachments = None with patch('os.makedirs') as mock_os: attachments = Attachments(self._thread_manager, config) mock_os.assert_called_with('/tmp/28/Jul/2016') with patch('pyccata.core.parts.attachments.Attachments._download_attachments') as mock_download: self._thread_manager.execute() mock_download.assert_not_called()
def setUp(self, mock_config, mock_parser, mock_log): self.tearDown() mock_log.return_value = None Logger._instance = mock_log path = os.path.dirname(os.path.realpath(__file__ + '../../../../')) self._path = os.path.join(path, os.path.join('tests', 'conf')) mock_config.return_value = [self._path] mock_parser.return_value = [] config = None with patch('argparse.ArgumentParser.add_argument'): config = Configuration(filename='valid_config.json') config.check = True self._report_manager = ReportManager() self._thread_manager = ThreadManager()
def test_render_calls_report_manager_add_methods(self, mock_paragraph): Configuration.report_text = 'tests/pyccata.core/data' config = DataProviders.get_paragraph_config_for_section() section = Section(self._thread_manager, config) self.assertEquals(4, len(section._structure)) for item in section._structure: self.assertIsInstance(item, Paragraph) section.render(ReportManager()) calls = [ call('This is paragraph number 1'), call('This is paragraph number 3'), call('This is paragraph number 4'), call('This is paragraph number 5') ] mock_paragraph.assert_has_calls(calls)
def test_render_returns_if_section_contains_empty_table( self, mock_load, mock_jira_client): Rows = namedtuple('Row', 'query fields') row_config = Rows(query="project=bob and issuetype=Bug", fields=["key", "description", "priority"]) #rows = Filter(row_config.query, fields=row_config.fields) #self._thread_manager.append(rows) columns = ['Name', 'Description'] TableConfig = namedtuple('TableConfig', 'rows columns style') table_config = TableConfig(rows=row_config, columns=columns, style='Light heading 1') StructureConfig = namedtuple('StructureConfig', 'type title content') structure_config = StructureConfig(type='table', title='Hello world', content=table_config) SectionConfig = namedtuple('SectionConfig', 'title abstract level structure') section_config = SectionConfig( title='Test', abstract='This is a test for empty tables', level=1, structure=[structure_config]) mock_jira_client.return_value = None with patch('pyccata.core.configuration.Configuration.manager', new_callable=PropertyMock) as mock_manager: with patch( 'pyccata.core.configuration.Configuration._configuration', new_callable=PropertyMock) as mock_config: mock_config.return_value = DataProviders._get_config_for_test() mock_manager.return_value = 'jira' section = Section(self._thread_manager, section_config) rows = section._structure[0].rows rows._thread_manager = self._thread_manager rows._project_manager = ProjectManager() rows.projectmanager._client._client = DataProviders._get_client_without_results( ) document = ReportManager() self._thread_manager.execute() section.render(document) self.assertEquals(0, rows.results.total)
def test_format_for_email(self, mock_run, mock_load): with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_reporting: with patch( 'pyccata.core.configuration.Configuration._configuration', new_callable=PropertyMock) as mock_config: mock_config.return_value = DataProviders._get_config_for_test_no_template( ) mock_reporting.return_value = 'docx' r = ReportManager() r.add_paragraph('hello world') r.add_run('This is a paragraph run', style='bold') mock_run.assert_called_with('This is a paragraph run') self.assertTrue(mock_run.bold) r.format_for_email() self.assertIsInstance(r._client._client._body._body[0], CT_Tbl) self.assertIsInstance(r._client._client._body._body[1], CT_SectPr)
def test_add_run_with_no_style(self, mock_run, mock_load): with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_reporting: with patch( 'pyccata.core.configuration.Configuration._configuration', new_callable=PropertyMock) as mock_config: mock_config.return_value = DataProviders._get_config_for_test() mock_reporting.return_value = 'docx' r = ReportManager() r.add_paragraph('hello world') r.add_run('This is a paragraph run') mock_run.assert_called_with('This is a paragraph run')
def test_table_doesnt_render_with_empty_filter_results( self, mock_heading, mock_table): self.tearDown() Config = namedtuple('Config', 'rows columns style') Filter = namedtuple('Filter', 'query max_results namespace') rows = Filter(query='project=mssportal', max_results=5, namespace='pyccata.core') columns = ['Name', 'Description'] config = Config(rows=rows, columns=columns, style='Light heading 1') table = Table(self._thread_manager, config) document = ReportManager() table.render(document) mock_table.assert_not_called() mock_heading.assert_not_called()
def test_add_picture(self, mock_document, mock_load): with patch('pyccata.core.configuration.Configuration.reporting', new_callable=PropertyMock) as mock_reporting: with patch( 'pyccata.core.configuration.Configuration._configuration', new_callable=PropertyMock) as mock_config: mock_config.return_value = DataProviders._get_config_for_test() mock_reporting.return_value = 'docx' r = ReportManager() r.add_picture('/path/to/image.png') mock_document.assert_called_with('/path/to/image.png', width=5212080) r.add_picture('/another/path/to/image.png', width=75) mock_document.assert_called_with('/another/path/to/image.png', width=68580000)
def test_table_renders_filter_results_multi_results_without_combine( self, mock_heading, mock_table): self.tearDown() Config = namedtuple('Config', 'rows columns style') with patch('pyccata.core.filter.Filter.results', new_callable=PropertyMock) as mock_results: results_set_one = ResultList(name='test results') result_issue = Issue() result_issue.description = 'This is a test item' results_set_one.append(result_issue) results_set_two = ResultList(name='another set of tests') another_result_issue = Issue() another_result_issue.description = 'This is a test item' results_set_two.append(another_result_issue) multi_results = MultiResultList() multi_results.combine = False multi_results.append(results_set_one) multi_results.append(results_set_two) mock_results.return_value = multi_results Filter = namedtuple('Filter', 'query max_results namespace') rows = Filter(query='project=mssportal', max_results=5, namespace='pyccata.core') columns = ['Name', 'Description'] config = Config(rows=rows, columns=columns, style='Light heading 1') table = Table(self._thread_manager, config) document = ReportManager() table.render(document) self.assertEquals(2, mock_table.call_count) self.assertEquals(2, mock_heading.call_count)
def test_setup_and_run( self, collation, result_count, result_filename, mock_config_list, mock_results, mock_jira_client, mock_open ): mock_jira_client.return_value = None mock_config_list.return_value = [self._path] Configuration.NAMESPACE = 'pyccata.core' report = ReportManager() mock_results.return_value = DataProviders._test_data_for_attachments() report.add_callback('attachments', getattr(DataProviders, 'test_callback')) self.assertIsInstance(Configuration().replacements, Replacements) Config = namedtuple('Config', 'query fields collate output_path') config = Config( query='project=test and attachments is not empty', fields=[ 'key', 'attachments' ], collate=collation, output_path='/tmp/{FIX_VERSION}' ) Replacements().find('FIX_VERSION').value = '28/Jul/2016' attachments = None with patch('os.makedirs') as mock_os: attachments = Attachments(self._thread_manager, config) mock_os.assert_called_with('/tmp/28/Jul/2016') self._thread_manager.append(attachments) with patch('pycurl.Curl') as mock_curl: with patch('pycurl.Curl.setopt') as mock_setopt: with patch('pycurl.Curl.perform') as mock_perform: with patch('pycurl.Curl.close') as mock_close: Curl = namedtuple('Curl', 'URL WRITEDATA setopt perform close getinfo') mock_curl.return_value = Curl( URL=None, WRITEDATA=None, setopt=mock_setopt, perform=mock_perform, close=mock_close, getinfo=lambda x: 200 ) self._thread_manager.execute() self.assertEquals(result_count, len(attachments._content)) self.assertEquals((3 * result_count), mock_setopt.call_count) self.assertEquals((1 * result_count), mock_perform.call_count) self.assertEquals((1 * result_count), mock_close.call_count) self.assertEquals((1 * result_count), mock_open.call_count) calls = [] if isinstance(result_filename, list): for filename in result_filename: calls.append(call(filename, 'wb')) else: calls.append(call(result_filename, 'wb')) mock_open.assert_has_calls(calls, any_order=True) with patch('pyccata.core.managers.report.ReportManager.add_paragraph') as mock_paragraph: with patch('pyccata.core.managers.report.ReportManager.add_list') as mock_list: attachments.render(report) mock_paragraph.assert_called_with('The following file(s) have been attached to this document:') mock_list.assert_called_with('TestFile.zip')
def test_render_fails_if_thread_has_not_been_executed(self): Config = namedtuple('Config', 'filename width') config = Config(filename='department.png', width=20) picture = Picture(self._thread_manager, config) with self.assertRaises(ThreadFailedError): picture.render(ReportManager())