def test_init_using_list_of_queries_creates_list(self, mock_list, mock_manager): result_list = DataProviders( ).get_results_for_list_init_with_list_of_queries_in_config() list_contents = [ Filter('project=mssportal', max_results=5, fields=['id', 'description', 'priority']), Filter('project=svcdesk', max_results=5, fields=['id', 'description', 'priority']), Filter('project=mvs', max_results=5, fields=['id', 'description', 'priority']), ] mock_manager.side_effect = result_list Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='description', prepend=None) calls = [ call(item[0].description, style='ListBullet') for item in result_list ] unordered = List(self._thread_manager, config) self._thread_manager.execute() unordered.render(self._report_manager) mock_list.assert_has_calls(calls)
def test_append_appends_to_self_when_queries_differ(self): mock_filter = Filter('assignee = "Foo"') another_filter = Filter('assignee = "Bar"') self._manager.append(mock_filter) self._manager.append(another_filter) self.assertEquals(2, len(self._manager)) self.assertFalse(another_filter.observing)
def test_append_appends_observer_when_queries_match(self): mock_filter = Filter('assignee = "Bob"') another_filter = Filter('assignee = "Bob"') self._manager.append(mock_filter) self._manager.append(another_filter) self.assertEquals(1, len(self._manager)) self.assertTrue(self._manager[0].hasobservers) self.assertEquals(1, len(self._manager[0]._observers)) self.assertTrue(another_filter.observing)
def test_run_completes_if_filter_fails(self): mock_filter = Filter('bob', max_results=5, fields=None, namespace='pyccata.core') mock_filter._failure = InvalidQueryError('query is wrong') Config = namedtuple('Config', 'rows columns style') config = Config(rows=[['My search', mock_filter]], columns=['Test column', 'Test Results'], style=None) table = Table(self._thread_manager, config) table.run() self.assertTrue(table._complete)
def test_run_notifies_observers(self, mock_load, mock_jira_client): 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' mock_filter = Filter('assignee = "Bob"', namespace='pyccata.core') mock_filter.projectmanager = ProjectManager() mock_filter.projectmanager._client._client = DataProviders._get_client( ) another_filter = Filter('assignee = "Bob"', namespace='pyccata.core') mock_filter.append(another_filter) mock_filter.run() self.assertEquals(mock_filter.failure, None) self.assertTrue(mock_filter.complete) self.assertEqual( len(mock_filter.results), len(DataProviders._test_data_for_search_results())) self.assertEqual(mock_filter._results, another_filter._results)
def test_init_using_list_of_queries_and_strings_continues_if_query_is_delayed( self, mock_list, mock_manager): result_list = DataProviders( ).get_results_for_list_init_with_list_of_queries_and_strings_continues( ) list_contents = [ "This is a string entry", Filter('project=msportal', max_results=5, fields=['id', 'description', 'priority']) ] effect = [item for item in result_list if isinstance(item, ResultList)] effect[1] = InvalidQueryError('The query you have provided is invalid') mock_manager.side_effect = effect Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='description', prepend=None) with patch('pyccata.core.filter.Filter.complete', new_callable=PropertyMock) as mock_thread_complete: mock_thread_complete.side_effect = [False, False, True] unordered = List(self._thread_manager, config) unordered.run() self.assertTrue(unordered._complete)
def test_init_using_single_query_creates_list_of_items( self, mock_list, mock_results): results = ["test 1", "test 2", "test 3", "test 4", "test 5"] resultset = Issue() resultset.pipelines = results list_contents = Filter('project=msportal', max_results=5, fields=['id', 'description', 'priority']) Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='pipelines', prepend=None) calls = [call(item, style='ListBullet') for item in results] with patch('pyccata.core.filter.Filter.complete', new_callable=PropertyMock): unordered = List(self._thread_manager, config) mock_results.return_value = [resultset] list_contents.complete.return_value = True unordered.run() unordered.render(self._report_manager) mock_list.assert_has_calls(calls)
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 setup(self, query, fields=None, collate=None, output_path='/tmp'): """ Set up the attachments object """ # pylint: disable=arguments-differ self._content = Filter(query, max_results=50, fields=fields) self.threadmanager.append(self._content) self.projectmanager = Configuration().manager self._collate = collate.split(',') self._output_path = Replacements().replace(output_path) create_directory(self._output_path)
def test_run_raises_exception_if_query_fails(self, mock_load, mock_jira_client): mock_jira_client.return_value = DataProviders._get_client() mock_jira_client.search_issues.side_effect = InvalidQueryError( 'The specified query is invalid') 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' mock_filter = Filter('iamnotavalidquery', namespace='pyccata.core') mock_filter.projectmanager = ProjectManager() mock_filter.start() self.assertTrue(mock_filter.failed) self.assertIsInstance(mock_filter.failure, InvalidQueryError)
def test_create_table_manages_exception_if_filter_cannot_be_created( self, mock_manager): Config = namedtuple('Config', 'rows columns style') Filter = namedtuple('SubFilter', 'search_for bob') rows = Filter(search_for='project=mssportal', bob='fish') columns = ['Name', 'Description'] config = Config(rows=rows, columns=columns, style='Light heading 1') table = Table(self._thread_manager, config)
def test_run_marks_thread_as_complete_on_success(self, mock_load, mock_jira_client): 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' mock_filter = Filter('assignee = "Bob"', namespace='pyccata.core') mock_filter.projectmanager = ProjectManager() mock_filter.projectmanager._client._client = DataProviders._get_client( ) mock_filter.run() self.assertTrue(mock_filter.complete) self.assertEqual( len(mock_filter.results), len(DataProviders._test_data_for_search_results()))
def test_init_using_list_of_queries_and_strings_continues_if_query_errors( self, mock_list, mock_manager): result_list = DataProviders( ).get_results_for_list_init_with_list_of_queries_and_strings_continues( ) list_contents = [ Filter('project=msportal', max_results=5, fields=['id', 'description', 'priority']), "This is a string entry", Filter('project=svdesk', max_results=5, fields=['id', 'description', 'priority']), Filter('project=mvp', max_results=5, fields=['id', 'description', 'priority']), "This is another string entry" ] effect = [item for item in result_list if isinstance(item, ResultList)] effect[1] = InvalidQueryError('The query you have provided is invalid') mock_manager.side_effect = effect Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='description', prepend=None) calls = [ call(item[0].description if isinstance(item, list) else item, style='ListBullet') for item in result_list ] del calls[2] unordered = List(self._thread_manager, config) self._thread_manager.execute() unordered.render(self._report_manager) mock_list.assert_has_calls(calls)
def test_set_item_raises_error_if_content_is_filter_and_filter_results_is_none( self): list_contents = Filter('project=mssportal', max_results=5, fields=['id', 'description', 'priority']) Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='description', prepend=None) unordered = List(self._thread_manager, config) with self.assertRaises(IndexError): unordered[0] = 'hello world'
def test_adding_filter_adds_project_manager(self, mock_load, mock_jira_client): mock_jira_client.return_value = DataProviders._get_client() 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' manager = ThreadManager() self.assertIsInstance(manager.projectmanager, ProjectManager) self.assertIsInstance(manager.querymanager, QueryManager) self.assertIsInstance(manager.configuration, Configuration) mock_filter = Filter('assignee = "Foo"') manager.append(mock_filter) self.assertIsInstance(mock_filter.projectmanager, ProjectManager)
def test_results_with_collation(self, collation, distinct, results, mock_load, mock_jira_client): 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' mock_filter = Filter('assignee = "Bob"', collate=collation, distinct=distinct, namespace='pyccata.core') mock_filter.projectmanager = ProjectManager() mock_filter.projectmanager._client._client = DataProviders._get_client_for_collation( ) mock_filter.run() self.assertTrue(mock_filter.complete) self.assertIsInstance(_today(), datetime) with patch('pyccata.core.collation._today', new_callable=(lambda: datetime.strptime( '2016-08-18', '%Y-%m-%d'))): self.assertEqual(results, mock_filter.results)
def test_run_completes_if_filter_delays_in_completing(self): mock_filter = Filter('bob', max_results=5, fields=None, namespace='pyccata.core') Config = namedtuple('Config', 'rows columns style') config = Config(rows=[['My search', mock_filter]], columns=['Test column', 'Test Results'], style=None) with patch('pyccata.core.threading.Threadable.complete', new_callable=PropertyMock) as mock_thread: table = Table(self._thread_manager, config) mock_thread.side_effect = [False, False, True] table.run() self.assertTrue(table._complete)
def test_list_fails_if_content_is_invalid_tuple(self, mock_manager): result_list = DataProviders( ).get_results_for_list_init_with_single_query_in_config() Filter = namedtuple('Filter', 'bob fields') list_contents = Filter('project=msportal', fields=['id', 'description', 'priority']) mock_manager.return_value = result_list Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='description', prepend=None) with self.assertRaises(ArgumentValidationError): unordered = List(self._thread_manager, config)
def test_results_with_collation_method_that_doesnt_exist( self, mock_load, mock_jira_client): collation = 'iamamethodwhichwillneverexist' distinct = False 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' with self.assertRaises(InvalidModuleError): mock_filter = Filter('assignee = "Bob"', collate=collation, distinct=distinct, namespace='pyccata.core')
def test_list_contains_item_if_content_is_filter(self, mock_manager): result_list = DataProviders( ).get_results_for_list_init_with_single_query_in_config() list_contents = Filter('project=msportal', max_results=5, fields=['id', 'description', 'priority']) mock_manager.return_value = result_list Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='description', prepend=None) unordered = List(self._thread_manager, config) self._thread_manager.execute() self.assertTrue((result_list[3] in unordered))
def test_execute_retries_observers_on_error(self, mock_load, mock_jira_client): 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' mock_broken = BrokenConnectionFilter('assignee = "Bob"') mock_filter = Filter('assignee = "Bob"') manager = ThreadManager() manager.append(mock_broken) manager.append(mock_filter) mock_filter.projectmanager._client._client = DataProviders._get_client() self.assertEquals(1, len(manager)) self.assertEquals(1, len(manager[0]._observers)) manager.start()
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_list_completes_if_content_filter_fails(self, mock_manager): result_list = DataProviders( ).get_results_for_list_init_with_single_query_in_config() list_contents = Filter('project=msportal', max_results=5, fields=['id', 'description', 'priority']) mock_manager.return_value = result_list Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='description', prepend=None) unordered = List(self._thread_manager, config) with patch('pyccata.core.filter.Filter.failure', new_callable=PropertyMock) as mock_failure: mock_failure.return_value = RuntimeError('I blew up') self._thread_manager.execute()
def _parse_filter(cell_content): """ Parses a configuration tuple and forms a filter object @param cell_content tuple @return Filter """ return Filter(cell_content.query, max_results=(cell_content.max_results if hasattr( cell_content, 'max_results') else Table.MAX_ROWS), fields=(cell_content.fields if hasattr( cell_content, 'fields') else []), collate=(cell_content.collate if hasattr( cell_content, 'collate') else None), distinct=(cell_content.distinct if hasattr( cell_content, 'distinct') else False), group_by=(cell_content.group_by if hasattr( cell_content, 'group_by') else None), namespace=Configuration.NAMESPACE)
def setup(self, content=None, style='unordered', field='description', prepend=''): """ Set up the list object """ # pylint: disable=arguments-differ if style.lower() not in List.STYLE_MAPPINGS.keys(): raise ArgumentValidationError('style', '__init__', self.STYLE_MAPPINGS.keys(), style) self._style = style.lower() self._field = field self._prepend = prepend if isinstance(content, tuple): try: content = Filter(content.query, max_results=getattr(content, 'max_results') if hasattr(content, 'max_results') else 0, fields=getattr(content, 'fields') if hasattr( content, 'fields') else None, collate=(content.collate if hasattr( content, 'collate') else None), distinct=(content.distinct if hasattr( content, 'distinct') else False), namespace=Configuration.NAMESPACE) # pylint: disable=broad-except # Any reason can come back here. Just need to # log the exception and move on. except Exception as exception: Logger().warning('Got type tuple but cannot create filter') Logger().warning(exception) self._content = List._parse(content) if isinstance(self._content, Filter): Logger().debug('Appending list content filter to thread manager') self.threadmanager.append(self._content) else: for item in self: if isinstance(item, Filter): self.threadmanager.append(item)
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_reverse_list_if_content_is_filter(self, mock_manager): result_list = DataProviders( ).get_results_for_list_init_with_single_query_in_config() list_contents = Filter('project=msportal', max_results=5, fields=['id', 'description', 'priority']) mock_manager.return_value = result_list Config = namedtuple('Config', 'content style field prepend') config = Config(content=list_contents, style='unordered', field='description', prepend=None) unordered = List(self._thread_manager, config) self._thread_manager.execute() unordered = list(reversed(unordered)) self.assertEquals('This is test item 5', unordered[0].description) self.assertEquals('This is test item 4', unordered[1].description) self.assertEquals('This is test item 3', unordered[2].description) self.assertEquals('This is test item 2', unordered[3].description) self.assertEquals('This is test item 1', unordered[4].description)
def setup(self, query=None, width=100, graphtype=None, structure=None): """ Set up the graph object """ # pylint: disable=arguments-differ self.configuration = self.threadmanager.configuration self._width = width if isinstance(width, int) and width < 100 else 100 self._filename = Graph._get_filename(graphtype, query.query) self._filenames = [] self._graph = structure self._path = self.configuration.csv.output_directory if not os.path.exists(self._path): os.makedirs(self._path) if not hasattr(self, graphtype): if not hasattr(self, graphtype + '_graph'): raise InvalidGraphError( 'No graph type found for graph type \'{0}\''.format( graphtype)) graphtype = graphtype + '_graph' self._graph_type = graphtype if hasattr(query, 'fields') and hasattr( query, 'group_by') and query.group_by not in query.fields: query.fields.append(query.group_by) self._content = Filter( query.query, max_results=(query.max_results if hasattr(query, 'max_results') else False), fields=(query.fields if hasattr(query, 'fields') else []), collate=(query.collate if hasattr(query, 'collate') else None), distinct=(query.distinct if hasattr(query, 'distinct') else False), namespace=Configuration.NAMESPACE, group_by=(query.group_by if hasattr(query, 'group_by') else None)) self.threadmanager.append(self._content)
def test_run_raises_exception_if_manager_is_not_provided(self): mock_filter = Filter('issuetype = "Bug"', namespace='pyccata.core') mock_filter.start() self.assertTrue(mock_filter.failed) self.assertIsInstance(mock_filter.failure, AssertionError)