def status_matrix(self, form): sql, params = self._prepare_sql( form, sqls.by_plan_build_detail_status_matrix) rows = SQLExecution(sql, params, with_field_name=False).rows status_matrix = GroupByResult() plan_ids = build_ids = run_ids = [] for plan_id, build_id, run_id, status_name, _ in rows: plan_ids.append(plan_id) build_ids.append(build_id) run_ids.append(run_id) plans = { plan.pk: plan for plan in TestPlan.objects.filter( pk__in=plan_ids).only('pk', 'name') } test_builds = { build.pk: build for build in TestBuild.objects.filter( pk__in=build_ids).only('pk', 'name') } runs = { run.pk: run for run in TestRun.objects.filter( pk__in=run_ids).only('pk', 'summary') } for row in rows: plan_id, build_id, run_id, status_name, total_count = row builds = status_matrix.setdefault(plans[plan_id], GroupByResult()) runs = builds.setdefault(test_builds[build_id], GroupByResult()) status_subtotal = runs.setdefault(runs[run_id], GroupByResult()) status_subtotal[status_name] = total_count return status_matrix
def status_matrix_groupby_builds(self, form): builds = GroupByResult({}) query = self._filter_query( form, TestCaseRun.objects.values( 'build', 'tested_by', 'case_run_status__name' ).annotate( total_count=Count('pk') ) ) for row in query: build_id = row['build'] tested_by_id = row['tested_by'] name = row['case_run_status__name'] total_count = row['total_count'] tested_by_ids = builds.setdefault(build_id, GroupByResult({})) status_subtotal = tested_by_ids.setdefault(tested_by_id, GroupByResult({})) status_subtotal[name] = total_count return builds
def status_matrix(self, form): status_matrix = GroupByResult() query = self._filter_query( form, TestCaseRun.objects.values( 'run__plan', 'run__plan__name', 'case_run_status__name' ).filter( case_run_status__name__in=('PASSED', 'FAILED') ).annotate( total_count=Count('run', distinct=True) ) ) for row in query: plan_id = row['run__plan'] plan_name = row['run__plan__name'] status_name = row['case_run_status__name'] total_count = row['total_count'] plan = TestPlan(pk=plan_id, name=plan_name) status_subtotal = status_matrix.setdefault(plan, GroupByResult()) status_subtotal[status_name] = total_count return status_matrix
def status_matrix(self, form): status_matrix = GroupByResult() query = self._filter_query( form, TestCaseRun.objects.values( 'build', 'build__name', 'run__plan__tag', 'run__plan', 'run__plan__name', 'run', 'run__summary', 'case_run_status__name' ).annotate(total_count=Count('pk')) ) for row in query: tag_id = row['run__plan__tag'] build_id = row['build'] build_name = row['build__name'] plan_id = row['run__plan'] plan_name = row['run__plan__name'] run_id = row['run'] run_summary = row['run__summary'] status_name = row['case_run_status__name'] total_count = row['total_count'] builds = status_matrix.setdefault(tag_id, GroupByResult()) plans = builds.setdefault(TestBuild(pk=build_id, name=build_name), GroupByResult()) runs = plans.setdefault(TestPlan(pk=plan_id, name=plan_name), GroupByResult()) status_subtotal = runs.setdefault( TestRun(pk=run_id, summary=run_summary), GroupByResult()) status_subtotal[status_name] = total_count return status_matrix
def status_matrix(self, form): sql, params = self._prepare_sql(form, sqls.by_plan_build_status_matrix) rows = SQLExecution(sql, params, with_field_name=False).rows status_matrix = GroupByResult() for plan_id, plan_name, status_name, total_count in rows: plan = TestPlan(pk=plan_id, name=plan_name) status_subtotal = status_matrix.setdefault(plan, GroupByResult()) status_subtotal[status_name] = total_count return status_matrix
def status_matrix(self): """Case run status matrix used to render progress bar""" sql, params = self._prepare_sql( sqls.custom_builds_case_runs_subtotal_by_status) rows = SQLExecution(sql, params, with_field_name=False).rows builds = GroupByResult() for build_id, case_run_status_id, total_count in rows: status_subtotal = builds.setdefault(build_id, GroupByResult()) status_subtotal[case_run_status_id] = total_count return builds
def status_matrix(self, form): sql, params = self._prepare_sql(form, sqls.by_plan_build_status_matrix) rows = list(SQLExecution(sql, params, with_field_name=False).rows) plans = { p.pk: p for p in TestPlan.objects.filter( pk__in=[plan_id for plan_id, _, _ in rows]).only('pk', 'name') } status_matrix = GroupByResult() for plan_id, status_name, total_count in rows: status_subtotal = status_matrix.setdefault(plans[plan_id], GroupByResult()) status_subtotal[status_name] = total_count return status_matrix
def passed_failed_case_runs_subtotal(self, form): sql, params = self._prepare_sql( form, sqls.by_plan_tags_passed_failed_case_runs_subtotal) sql_executor = SQLExecution(sql, params, with_field_name=False) rows = sql_executor.rows tags = GroupByResult() for tag_id, name, total_count in rows: status_subtotal = tags.setdefault(tag_id, GroupByResult()) status_subtotal[name] = total_count return tags
def runs_subtotal_groupby_builds(self, form): sql, params = self._prepare_sql( form, sqls.by_case_run_tester_runs_subtotal_groupby_build) sql_executor = SQLExecution(sql, params, with_field_name=False) rows = sql_executor.rows builds = GroupByResult({}) for build_id, tested_by_id, total_count in rows: tested_by_ids = builds.setdefault(build_id, GroupByResult({})) tested_by_ids[tested_by_id] = total_count return builds
def status_matrix(self, form): sql, params = self._prepare_sql(form, sqls.by_case_run_tester_status_matrix) sql_executor = SQLExecution(sql, params, with_field_name=False) status_matrix = GroupByResult({}) rows = sql_executor.rows for tested_by_id, name, total_count in rows: status_subtotal = status_matrix.setdefault(tested_by_id, GroupByResult({})) status_subtotal[name] = total_count return status_matrix
def status_matrix(self, form): sql, params = self._prepare_sql( form, sqls.by_case_run_tester_status_matrix) sql_executor = SQLExecution(sql, params, with_field_name=False) status_matrix = GroupByResult({}) rows = sql_executor.rows for tested_by_id, name, total_count in rows: status_subtotal = status_matrix.setdefault( tested_by_id, GroupByResult({})) status_subtotal[name] = total_count return status_matrix
def status_matrix(self, form): sql, params = self._prepare_sql(form, sqls.by_case_priority_subtotal) sql_executor = SQLExecution(sql, params, with_field_name=False) rows = sql_executor.rows builds = GroupByResult() for build_id, priority_id, priority_value, name, total_count in rows: priorities = builds.setdefault(build_id, GroupByResult()) priority = Priority(pk=priority_id, value=priority_value) status_subtotal = priorities.setdefault(priority, GroupByResult()) status_subtotal[name] = total_count return builds
def status_matrix_groupby_builds(self, form): sql, params = self._prepare_sql( form, sqls.by_case_run_tester_status_matrix_groupby_build) sql_executor = SQLExecution(sql, params, with_field_name=False) builds = GroupByResult({}) for build_id, tested_by_id, name, total_count in sql_executor.rows: tested_by_ids = builds.setdefault(build_id, GroupByResult({})) status_subtotal = tested_by_ids.setdefault(tested_by_id, GroupByResult({})) status_subtotal[name] = total_count return builds
def generate_status_matrix(self, build_ids): matrix_dataset = {} status_total_line = GroupByResult() rows = TestCaseRun.objects.filter(run__build__in=build_ids).values( 'run__plan', 'run', 'case_run_status').annotate(subtotal=Count('pk')).order_by( 'run__plan', 'run', 'case_run_status').values_list('run__plan', 'run', 'case_run_status', 'subtotal') plan_ids = run_ids = case_run_status_ids = [] for plan_id, run_id, case_run_status_id, _ in rows: plan_ids.append(plan_id) run_ids.append(run_id) case_run_status_ids.append(case_run_status_id) plans = { plan.pk: plan for plan in TestPlan.objects.filter( pk__in=plan_ids).only('pk', 'name') } runs = { run.pk: run for run in TestRun.objects.filter( pk__in=run_ids).only('pk', 'summary') } case_run_status_names = { pk: name for pk, name in TestCaseRunStatus.objects.filter( pk__in=case_run_status_ids).values_list('pk', 'name') } for row in rows: plan_id, run_id, case_run_status_id, status_count = row plan_node = matrix_dataset.setdefault(plans[plan_id], {}) run_node = plan_node.setdefault(runs[run_id], GroupByResult()) status_name = case_run_status_names[case_run_status_id] run_node[status_name] = status_count # calculate the last total line status_total_line[status_name] = \ status_total_line.setdefault(status_name, 0) + status_count # Add total line to final data set matrix_dataset[None] = status_total_line return matrix_dataset
def status_matrix_groupby_builds(self, form): sql, params = self._prepare_sql( form, sqls.by_case_run_tester_status_matrix_groupby_build) sql_executor = SQLExecution(sql, params, with_field_name=False) builds = GroupByResult({}) for build_id, tested_by_id, name, total_count in sql_executor.rows: tested_by_ids = builds.setdefault( build_id, GroupByResult({})) status_subtotal = tested_by_ids.setdefault(tested_by_id, GroupByResult({})) status_subtotal[name] = total_count return builds
def test_percentage(self): result = GroupByResult({ 'IDLE': 20, 'PASSED': 20, 'RUNNING': 10, }) self.assertEqual(40.0, result.PASSED_percent)
class GroupByResultCalculationTest(TestCase): '''Test calculation of GroupByResult''' def setUp(self): self.groupby_result = GroupByResult({ 1: 100, 2: 300, 4: 400, }) self.nested_groupby_result = GroupByResult({ 1: GroupByResult({ 'a': 1, 'b': 2, 'c': 3 }), 2: GroupByResult({ 1: 1, 2: 2 }), 3: GroupByResult({ 'PASSED': 10, 'WAIVED': 20, 'FAILED': 30, 'PAUSED': 40 }), }) def _sample_total(self): return sum(count for key, count in self.groupby_result.iteritems()) def _sample_nested_total(self): total = 0 for key, nested_result in self.nested_groupby_result.iteritems(): for n, count in nested_result.iteritems(): total += count return total def test_total(self): total = self.groupby_result.total self.assertEqual(total, self._sample_total()) def test_nested_total(self): total = self.nested_groupby_result.total self.assertEqual(total, self._sample_nested_total())
def runs_subtotal(self, form): sql, params = self._prepare_sql(form, sqls.by_plan_build_runs_subtotal) rows = SQLExecution(sql, params, with_field_name=False).rows result = GroupByResult() for plan_id, plan_name, runs_count in rows: plan = TestPlan(pk=plan_id, name=plan_name) result[plan] = runs_count return result
def runs_subtotal_groupby_builds(self, form): builds = GroupByResult({}) query = self._filter_query( form, TestCaseRun.objects.values('tested_by', 'run', 'build').distinct()) for row in query: build_id = row['build'] tested_by_id = row['tested_by'] tested_by_ids = builds.setdefault(build_id, GroupByResult({})) if tested_by_id in tested_by_ids.keys(): tested_by_ids[tested_by_id] += 1 else: tested_by_ids[tested_by_id] = 1 return builds
def test_arithmetic_operation(self): result = GroupByResult({'IDLE': 1, 'RUNNING': 1, 'FAILED': 2}) result['IDLE'] += 1 result['RUNNING'] += 100 result['FAILED'] -= 2 self.assertEqual(2, result['IDLE']) self.assertEqual(101, result['RUNNING']) self.assertEqual(0, result['FAILED'])
def status_matrix(self, form): status_matrix = GroupByResult() query = self._filter_query( form, TestCaseRun.objects.values( 'build', 'build__name', 'run__plan__tag', 'run__plan', 'run__plan__name', 'run', 'run__summary', 'case_run_status__name').annotate(total_count=Count('pk'))) for row in query: tag_id = row['run__plan__tag'] build_id = row['build'] build_name = row['build__name'] plan_id = row['run__plan'] plan_name = row['run__plan__name'] run_id = row['run'] run_summary = row['run__summary'] status_name = row['case_run_status__name'] total_count = row['total_count'] builds = status_matrix.setdefault(tag_id, GroupByResult()) plans = builds.setdefault(Build(pk=build_id, name=build_name), GroupByResult()) runs = plans.setdefault(TestPlan(pk=plan_id, name=plan_name), GroupByResult()) status_subtotal = runs.setdefault( TestRun(pk=run_id, summary=run_summary), GroupByResult()) status_subtotal[status_name] = total_count return status_matrix
def setUp(self): self.groupby_result = GroupByResult({ 1: 100, 2: 300, 4: 400, }) self.nested_groupby_result = GroupByResult({ 1: GroupByResult({ 'a': 1, 'b': 2, 'c': 3 }), 2: GroupByResult({ 1: 1, 2: 2 }), 3: GroupByResult({ 'PASSED': 10, 'WAIVED': 20, 'FAILED': 30, 'PAUSED': 40 }), })
def subtotal_plans(filter_=None, by=None): group_by = by or 'product' stats = TestPlan.objects if filter_: stats = stats.filter(**filter_) return GroupByResult({ item[group_by]: item['subtotal'] for item in stats.values(group_by).annotate(subtotal=Count('pk')) })
def subtotal_case_run_status(filter_=None, by=None): group_by = by or 'name' stats = TestCaseRunStatus.objects if filter_: stats = stats.filter(**filter_) return GroupByResult({ item[group_by]: item['subtotal'] for item in stats.values(group_by).annotate(subtotal=Count('pk')) })
def runs_subtotal_groupby_builds(self, form): builds = GroupByResult({}) query = self._filter_query( form, TestCaseRun.objects.values('tested_by', 'run', 'build').distinct() ) for row in query: build_id = row['build'] tested_by_id = row['tested_by'] tested_by_ids = builds.setdefault(build_id, GroupByResult({})) if tested_by_id in tested_by_ids.keys(): tested_by_ids[tested_by_id] += 1 else: tested_by_ids[tested_by_id] = 1 return builds
def setUp(self): self.groupby_result = GroupByResult({ 1: 100, 2: 300, 4: 400, }) self.nested_groupby_result = GroupByResult({ 1: GroupByResult({'a': 1, 'b': 2, 'c': 3}), 2: GroupByResult({1: 1, 2: 2}), 3: GroupByResult({'PASSED': 10, 'WAIVED': 20, 'FAILED': 30, 'PAUSED': 40}), })
def status_matrix(self, form): status_matrix = GroupByResult({}) query = self._filter_query( form, TestCaseRun.objects.values( 'tested_by', 'status__name').annotate(total_count=Count('pk'))) for row in query: tested_by_id = row['tested_by'] if tested_by_id is None: tested_by_id = 0 name = row['status__name'] total_count = row['total_count'] status_subtotal = status_matrix.setdefault(tested_by_id, GroupByResult({})) status_subtotal[name] = total_count return status_matrix
def overview_view_get_running_runs_count(product_id): stats = TestRun.objects.extra( select={ 'stop_status': "CASE WHEN stop_date is NULL THEN 'running' " "ELSE 'finished' END" }).values('stop_status').annotate(subtotal=Count('pk')) return GroupByResult( {item['stop_status']: item['subtotal'] for item in stats})
def status_matrix(self, form): builds = GroupByResult() query = self._filter_query( form, TestCaseRun.objects.values( 'build', 'case__priority', 'case__priority__value', 'case_run_status__name').annotate(total_count=Count('pk'))) for row in query: build_id = row['build'] priority_id = row['case__priority'] priority_value = row['case__priority__value'] name = row['case_run_status__name'] total_count = row['total_count'] priorities = builds.setdefault(build_id, GroupByResult()) priority = Priority(pk=priority_id, value=priority_value) status_subtotal = priorities.setdefault(priority, GroupByResult()) status_subtotal[name] = total_count return builds
def status_matrix(self, form): sql, params = self._prepare_sql(form, sqls.by_plan_tags_detail_status_matrix) rows = SQLExecution(sql, params, with_field_name=False).rows status_matrix = GroupByResult() for row in rows: tag_id, build_id, build_name, plan_id, plan_name, run_id, \ run_summary, status_name, total_count = row builds = status_matrix.setdefault(tag_id, GroupByResult()) plans = builds.setdefault(TestBuild(pk=build_id, name=build_name), GroupByResult()) runs = plans.setdefault(TestPlan(pk=plan_id, name=plan_name), GroupByResult()) status_subtotal = runs.setdefault( TestRun(pk=run_id, summary=run_summary), GroupByResult()) status_subtotal[status_name] = total_count return status_matrix
def runs_subtotal(self, form): sql, params = self._prepare_sql(form, sqls.by_plan_build_runs_subtotal) rows = list(SQLExecution(sql, params, with_field_name=False).rows) plans = { p.pk: p for p in TestPlan.objects.filter( pk__in=[plan_id for plan_id, _ in rows]).only('pk', 'name') } result = GroupByResult() for plan_id, runs_count in rows: result[plans[plan_id]] = runs_count return result
def passed_failed_case_runs_subtotal(self, form): # pylint: disable=invalid-name query = self._filter_query( form, TestCaseRun.objects.values( 'run__plan__tag', 'case_run_status__name').filter( case_run_status__name__in=['PASSED', 'FAILED']).annotate( total_count=Count('case_run_status'))) tags = GroupByResult() for row in query: tag_id = row['run__plan__tag'] name = row['case_run_status__name'] total_count = row['total_count'] status_subtotal = tags.setdefault(tag_id, GroupByResult()) status_subtotal[name] = total_count from pprint import pprint pprint(tags) return tags
class GroupByResultCalculationTest(TestCase): '''Test calculation of GroupByResult''' def setUp(self): self.groupby_result = GroupByResult({ 1: 100, 2: 300, 4: 400, }) self.nested_groupby_result = GroupByResult({ 1: GroupByResult({'a': 1, 'b': 2, 'c': 3}), 2: GroupByResult({1: 1, 2: 2}), 3: GroupByResult({'PASSED': 10, 'WAIVED': 20, 'FAILED': 30, 'PAUSED': 40}), }) def _sample_total(self): return sum(count for key, count in self.groupby_result.iteritems()) def _sample_nested_total(self): total = 0 for key, nested_result in self.nested_groupby_result.iteritems(): for n, count in nested_result.iteritems(): total += count return total def test_total(self): total = self.groupby_result.total self.assertEqual(total, self._sample_total()) def test_nested_total(self): total = self.nested_groupby_result.total self.assertEqual(total, self._sample_nested_total())
def status_matrix(self, form): status_matrix = GroupByResult({}) query = self._filter_query( form, TestCaseRun.objects.values( 'tested_by', 'case_run_status__name' ).annotate( total_count=Count('pk') ) ) for row in query: tested_by_id = row['tested_by'] name = row['case_run_status__name'] total_count = row['total_count'] status_subtotal = status_matrix.setdefault( tested_by_id, GroupByResult({})) status_subtotal[name] = total_count return status_matrix
class GroupByResultDictLikeTest(unittest.TestCase): """Test dict like behaviors""" def setUp(self): self.groupby_result = GroupByResult({'total': 100}) def test_in(self): self.assert_('a' not in self.groupby_result) self.assert_('total' in self.groupby_result) def test_key(self): self.assert_(self.groupby_result.keys(), ['total']) def test_setdefault(self): ret_val = self.groupby_result.setdefault('count', {}) self.assertEqual(ret_val, {}) ret_val = self.groupby_result.setdefault('total', 200) self.assertEqual(ret_val, 100) def test_getitem(self): ret_val = self.groupby_result['total'] self.assertEqual(ret_val, 100) try: ret_val = self.groupby_result['xxx'] except KeyError: pass else: self.fail('xxx does not exist. KeyError should be raised.') def test_setitem(self): self.groupby_result['count'] = 200 self.assertEqual(self.groupby_result['count'], 200) self.groupby_result['total'] = 999 self.assertEqual(self.groupby_result['total'], 999) def test_get(self): ret_val = self.groupby_result.get('total') self.assertEqual(ret_val, 100) ret_val = self.groupby_result.get('count', 999) self.assertEqual(ret_val, 999) ret_val = self.groupby_result.get('xxx') self.assertEqual(ret_val, None) def test_len(self): self.assertEqual(len(self.groupby_result), 1) def test_del(self): self.groupby_result['count'] = 200 del self.groupby_result['total'] self.assert_('total' not in self.groupby_result) del self.groupby_result['count'] self.assert_('count' not in self.groupby_result) self.assertEqual(len(self.groupby_result), 0)
class GroupByResultDictLikeTest(TestCase): '''Test dict like behaviors''' def setUp(self): self.groupby_result = GroupByResult({'total': 100}) def test_in(self): self.assert_('a' not in self.groupby_result) self.assert_('total' in self.groupby_result) def test_key(self): self.assert_(self.groupby_result.keys(), ['total']) def test_setdefault(self): ret_val = self.groupby_result.setdefault('count', {}) self.assertEqual(ret_val, {}) ret_val = self.groupby_result.setdefault('total', 200) self.assertEqual(ret_val, 100) def test_getitem(self): ret_val = self.groupby_result['total'] self.assertEqual(ret_val, 100) try: ret_val = self.groupby_result['xxx'] except KeyError: pass else: self.fail('xxx does not exist. KeyError should be raised.') def test_setitem(self): self.groupby_result['count'] = 200 self.assertEqual(self.groupby_result['count'], 200) self.groupby_result['total'] = 999 self.assertEqual(self.groupby_result['total'], 999) def test_get(self): ret_val = self.groupby_result.get('total') self.assertEqual(ret_val, 100) ret_val = self.groupby_result.get('count', 999) self.assertEqual(ret_val, 999) ret_val = self.groupby_result.get('xxx') self.assertEqual(ret_val, None) def test_len(self): self.assertEqual(len(self.groupby_result), 1) def test_del(self): self.groupby_result['count'] = 200 del self.groupby_result['total'] self.assert_('total' not in self.groupby_result) del self.groupby_result['count'] self.assert_('count' not in self.groupby_result) self.assertEqual(len(self.groupby_result), 0)
def passed_failed_case_runs_subtotal(self, form): query = self._filter_query( form, TestCaseRun.objects.values( 'run__plan__tag', 'case_run_status__name' ).filter(case_run_status__name__in=['PASSED', 'FAILED']).annotate( total_count=Count('case_run_status') ) ) tags = GroupByResult() for row in query: tag_id = row['run__plan__tag'] name = row['case_run_status__name'] total_count = row['total_count'] status_subtotal = tags.setdefault(tag_id, GroupByResult()) status_subtotal[name] = total_count from pprint import pprint pprint(tags) return tags
def runs_subtotal(self, form): result = GroupByResult() query = self._filter_query( form, TestCaseRun.objects.values( 'run__plan', 'run__plan__name').annotate( total_count=Count('run', distinct=True))) for row in query: plan_id = row['run__plan'] plan_name = row['run__plan__name'] runs_count = row['total_count'] plan = TestPlan(pk=plan_id, name=plan_name) result[plan] = runs_count return result
def status_matrix(self, form): builds = GroupByResult() query = self._filter_query( form, TestCaseRun.objects.values( 'build', 'case__priority', 'case__priority__value', 'case_run_status__name' ).annotate(total_count=Count('pk')) ) for row in query: build_id = row['build'] priority_id = row['case__priority'] priority_value = row['case__priority__value'] name = row['case_run_status__name'] total_count = row['total_count'] priorities = builds.setdefault(build_id, GroupByResult()) priority = Priority(pk=priority_id, value=priority_value) status_subtotal = priorities.setdefault(priority, GroupByResult()) status_subtotal[name] = total_count return builds
class GroupByResultDictLikeTest(unittest.TestCase): """Test dict like behaviors""" def setUp(self): self.groupby_result = GroupByResult({'total': 100}) def test_in(self): self.assertNotIn('a', self.groupby_result) self.assertIn('total', self.groupby_result) def test_key(self): self.assertTrue(self.groupby_result.keys(), ['total']) def test_setdefault(self): ret_val = self.groupby_result.setdefault('count', {}) self.assertEqual(ret_val, {}) ret_val = self.groupby_result.setdefault('total', 200) self.assertEqual(ret_val, 100) def test_setitem(self): self.groupby_result['count'] = 200 self.assertEqual(self.groupby_result['count'], 200) self.groupby_result['total'] = 999 self.assertEqual(self.groupby_result['total'], 999) def test_get(self): ret_val = self.groupby_result.get('total') self.assertEqual(ret_val, 100) ret_val = self.groupby_result.get('count', 999) self.assertEqual(ret_val, 999) ret_val = self.groupby_result.get('xxx') self.assertEqual(ret_val, None) def test_len(self): self.assertEqual(len(self.groupby_result), 1) def test_del(self): self.groupby_result['count'] = 200 del self.groupby_result['total'] self.assertNotIn('total', self.groupby_result) del self.groupby_result['count'] self.assertNotIn('count', self.groupby_result) self.assertEqual(len(self.groupby_result), 0) def test_raise_key_error(self): with self.assertRaises(KeyError): self.groupby_result['unknown_key']
class GroupByResultDictLikeTest(unittest.TestCase): """Test dict like behaviors""" def setUp(self): self.groupby_result = GroupByResult({'total': 100}) def test_in(self): self.assertNotIn('a', self.groupby_result) self.assertIn('total', self.groupby_result) def test_key(self): self.assertTrue(self.groupby_result.keys(), ['total']) def test_setdefault(self): ret_val = self.groupby_result.setdefault('count', {}) self.assertEqual(ret_val, {}) ret_val = self.groupby_result.setdefault('total', 200) self.assertEqual(ret_val, 100) def test_setitem(self): self.groupby_result['count'] = 200 self.assertEqual(self.groupby_result['count'], 200) self.groupby_result['total'] = 999 self.assertEqual(self.groupby_result['total'], 999) def test_get(self): ret_val = self.groupby_result.get('total') self.assertEqual(ret_val, 100) ret_val = self.groupby_result.get('count', 999) self.assertEqual(ret_val, 999) ret_val = self.groupby_result.get('xxx') self.assertEqual(ret_val, None) def test_len(self): self.assertEqual(len(self.groupby_result), 1) def test_del(self): self.groupby_result['count'] = 200 del self.groupby_result['total'] self.assertNotIn('total', self.groupby_result) del self.groupby_result['count'] self.assertNotIn('count', self.groupby_result) self.assertEqual(len(self.groupby_result), 0)
def status_matrix(self, form): sql, params = self._prepare_sql(form, sqls.by_plan_tags_detail_status_matrix) rows = SQLExecution(sql, params, with_field_name=False).rows status_matrix = GroupByResult() for row in rows: (tag_id, build_id, build_name, plan_id, plan_name, run_id, run_summary, status_name, total_count) = row builds = status_matrix.setdefault(tag_id, GroupByResult()) plans = builds.setdefault(TestBuild(pk=build_id, name=build_name), GroupByResult()) runs = plans.setdefault(TestPlan(pk=plan_id, name=plan_name), GroupByResult()) status_subtotal = runs.setdefault( TestRun(pk=run_id, summary=run_summary), GroupByResult()) status_subtotal[status_name] = total_count return status_matrix
def setUp(self): self.levels_groupby_result = GroupByResult({ 'build_1': GroupByResult({ 'plan_1': GroupByResult({ 'run_1': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_2': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_3': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), }), 'plan_2': GroupByResult({ 'run_1': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_2': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), }), }), 'build_2': GroupByResult({ 'plan_1': GroupByResult({ 'run_1': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_4': GroupByResult( {'paused': 2, 'failed': 2, 'waived': 6, }), 'run_5': GroupByResult( {'paused': 1, 'failed': 2, 'waived': 3, }), }), 'plan_2': GroupByResult({ 'run_1': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_4': GroupByResult( {'paused': 2, 'failed': 2, 'waived': 6, }), 'run_5': GroupByResult( {'paused': 1, 'failed': 2, 'waived': 3, }), }), }), })
def status_matrix(self, form): status_matrix = GroupByResult() query = self._filter_query( form, TestCaseRun.objects.values( 'build', 'build__name', 'run__plan__tag', 'run__plan', 'run__plan__name', 'run', 'run__summary', 'status__name').annotate(total_count=Count('pk'))) for row in query: builds = status_matrix.setdefault(row['run__plan__tag'], GroupByResult()) plans = builds.setdefault( Build(pk=row['build'], name=row['build__name']), GroupByResult()) runs = plans.setdefault( TestPlan(pk=row['run__plan'], name=row['run__plan__name']), GroupByResult()) status_subtotal = runs.setdefault( TestRun(pk=row['run'], summary=row['run__summary']), GroupByResult()) status_subtotal[row['status__name']] = row['total_count'] return status_matrix
class GroupByResultLevelTest(TestCase): def setUp(self): self.levels_groupby_result = GroupByResult({ 'build_1': GroupByResult({ 'plan_1': GroupByResult({ 'run_1': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_2': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_3': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), }), 'plan_2': GroupByResult({ 'run_1': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_2': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), }), }), 'build_2': GroupByResult({ 'plan_1': GroupByResult({ 'run_1': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_4': GroupByResult( {'paused': 2, 'failed': 2, 'waived': 6, }), 'run_5': GroupByResult( {'paused': 1, 'failed': 2, 'waived': 3, }), }), 'plan_2': GroupByResult({ 'run_1': GroupByResult( {'passed': 1, 'failed': 2, 'error': 3, }), 'run_4': GroupByResult( {'paused': 2, 'failed': 2, 'waived': 6, }), 'run_5': GroupByResult( {'paused': 1, 'failed': 2, 'waived': 3, }), }), }), }) def test_value_leaf_count(self): value_leaf_count = self.levels_groupby_result.leaf_values_count() self.assertEqual(value_leaf_count, 33) value_leaf_count = self.levels_groupby_result[ 'build_1'].leaf_values_count() self.assertEqual(value_leaf_count, 15) level_node = self.levels_groupby_result['build_2']['plan_2'] value_leaf_count = level_node.leaf_values_count() self.assertEqual(value_leaf_count, 9) def test_value_leaf_in_row_count(self): value_leaf_count = self.levels_groupby_result.leaf_values_count( value_in_row=True) self.assertEqual(value_leaf_count, 11) level_node = self.levels_groupby_result['build_2'] value_leaf_count = level_node.leaf_values_count(value_in_row=True) self.assertEqual(value_leaf_count, 6) level_node = self.levels_groupby_result['build_1']['plan_2'] value_leaf_count = level_node.leaf_values_count(value_in_row=True) self.assertEqual(value_leaf_count, 2)
class GroupByResultCalculationTest(unittest.TestCase): """Test calculation of GroupByResult""" def setUp(self): self.groupby_result = GroupByResult({ 1: 100, 2: 300, 4: 400, }) self.nested_groupby_result = GroupByResult({ 1: GroupByResult({'a': 1, 'b': 2, 'c': 3}), 2: GroupByResult({1: 1, 2: 2}), 3: GroupByResult({'PASSED': 10, 'WAIVED': 20, 'FAILED': 30, 'PAUSED': 40}), }) def _sample_total(self): return sum(count for key, count in self.groupby_result.iteritems()) def _sample_nested_total(self): total = 0 for key, nested_result in self.nested_groupby_result.iteritems(): for n, count in nested_result.iteritems(): total += count return total def test_total(self): total = self.groupby_result.total self.assertEqual(total, self._sample_total()) def test_nested_total(self): total = self.nested_groupby_result.total self.assertEqual(total, self._sample_nested_total()) def test_get_total_after_add_data_based_on_empty_initial_data(self): result = GroupByResult() result['RUNNING'] = 100 result['PASSED'] = 100 self.assertEqual(200, result.total) def test_get_total_after_add_data_based_on_initial_data(self): result = GroupByResult({'FAILED': 20}) result['RUNNING'] = 100 result['PASSED'] = 100 self.assertEqual(220, result.total) def test_total_is_updated_after_del_item(self): result = GroupByResult({'FAILED': 20, 'RUNNING': 20, 'PASSED': 10}) del result['RUNNING'] self.assertEqual(30, result.total) def test_total_is_updated_after_del_item_several_times(self): result = GroupByResult({'FAILED': 20, 'RUNNING': 20, 'PASSED': 10}) del result['RUNNING'] del result['FAILED'] self.assertEqual(10, result.total) def test_percentage(self): result = GroupByResult({ 'IDLE': 20, 'PASSED': 20, 'RUNNING': 10, }) self.assertEqual(40.0, result.PASSED_percent) def test_zero_percentage(self): result = GroupByResult({}) self.assertEqual(.0, result.PASSED_percent) def test_default_zero(self): result = GroupByResult() result['RUNNING'] += 1 self.assertEqual(1, result['RUNNING'])
def setUp(self): self.groupby_result = GroupByResult({'total': 100})