def set_test_build_attrs(tb, show_detail=False): case_runs_count = 0 case_runs_passed_count = 0 case_runs_failed_count = 0 plans_count = 0 manual_count = 0 auto_count = 0 both_count = 0 trs = TestRun.objects.filter(build=tb).select_related('case_run', 'plan') if trs: manual_count = reduce(operator.add, [tr.case_run.get_manual_case_count() for tr in trs]) auto_count = reduce(operator.add, [tr.case_run.get_automated_case_count() for tr in trs]) both_count = reduce(operator.add, [tr.case_run.get_both() for tr in trs]) case_runs_count = count_items('total_num_caseruns', trs) plans_count = len(set([tr.plan.pk for tr in trs])) if show_detail: tcr_statuses = TestCaseRunStatus.objects.all() else: tcr_statuses = TestCaseRunStatus.objects.filter(name__in = ['passed', 'failed']) for tcr_status in tcr_statuses: status_name = tcr_status.name.lower() setattr(tb, 'case_runs_%s_count' % status_name, reduce(lambda x, y: x+y, [tr.get_status_case_run_num(status_name) for tr in trs])) setattr(tb, 'case_runs_passed_percent', calc_percent(tb.case_runs_passed_count, case_runs_count)) setattr(tb, 'case_runs_failed_percent', calc_percent(tb.case_runs_failed_count, case_runs_count)) setattr(tb, 'manual_count', manual_count) setattr(tb, 'auto_count', auto_count) setattr(tb, 'both_count', both_count) setattr(tb, 'case_runs_count', case_runs_count) setattr(tb, 'plans_count', plans_count) return tb
def set_test_build_attrs(tb, show_detail=False): case_runs_count = 0 case_runs_passed_count = 0 case_runs_failed_count = 0 plans_count = 0 manual_count = 0 auto_count = 0 both_count = 0 trs = TestRun.objects.filter(build=tb).select_related('case_run', 'plan') if trs: manual_count = reduce( operator.add, [tr.case_run.get_manual_case_count() for tr in trs]) auto_count = reduce( operator.add, [tr.case_run.get_automated_case_count() for tr in trs]) both_count = reduce(operator.add, [tr.case_run.get_both() for tr in trs]) case_runs_count = count_items('total_num_caseruns', trs) plans_count = len(set([tr.plan.pk for tr in trs])) if show_detail: tcr_statuses = TestCaseRunStatus.objects.all() else: tcr_statuses = TestCaseRunStatus.objects.filter( name__in=['passed', 'failed']) for tcr_status in tcr_statuses: status_name = tcr_status.name.lower() setattr( tb, 'case_runs_%s_count' % status_name, reduce(lambda x, y: x + y, [tr.get_status_case_run_num(status_name) for tr in trs])) setattr(tb, 'case_runs_passed_percent', calc_percent(tb.case_runs_passed_count, case_runs_count)) setattr(tb, 'case_runs_failed_percent', calc_percent(tb.case_runs_failed_count, case_runs_count)) setattr(tb, 'manual_count', manual_count) setattr(tb, 'auto_count', auto_count) setattr(tb, 'both_count', both_count) setattr(tb, 'case_runs_count', case_runs_count) setattr(tb, 'plans_count', plans_count) return tb
def get_case_runs_passed_percent(self): if hasattr(self, 'case_runs_passed_count'): return calc_percent(self.case_runs_passed_count, self.case_runs_count) else: return None
def custom_details(request, template_name='report/custom_details.html'): """Custom report with search function""" from tcms.apps.management.models import TestBuild from tcms.apps.testplans.models import TestPlan from tcms.apps.testruns.models import TestCaseRun, TestCaseRunStatus, TestRun from forms import CustomSearchDetailsForm SUB_MODULE_NAME = 'custom_search' default_case_run_status = TestCaseRunStatus.objects.all() auto_count = manual_count = both_count = total_count = 0 tbs = tps = trs = tcrs = tcrses = None form = CustomSearchDetailsForm(request.REQUEST) form.populate(product_id = request.REQUEST.get('product')) if form.is_valid(): tcrses = TestCaseRunStatus.objects.all() tbs = TestBuild.objects.filter(pk__in = request.REQUEST.getlist('pk__in')) tps = TestPlan.objects.filter(run__build__in = tbs) trs = TestRun.objects.filter(build__in = tbs) tcrs = TestCaseRun.objects.select_related('case', 'run', 'case_run_status', 'tested_by') tcrs = tcrs.filter(run__build__in = tbs) # if form.cleaned_data['product']: # tps = tps.filter(run__build__product = form.cleaned_data['product']) # trs = trs.filter(build__product = form.cleaned_data['product']) # tcrs = tcrs.filter(run__build__product = form.cleaned_data['product']) # # if form.cleaned_data['build_run__product_version']: # tps = tps.filter(run__product_version = form.cleaned_data['build_run__product_version']) # trs = trs.filter(product_version = form.cleaned_data['build_run__product_version']) # tcrs = tcrs.filter(run__product_version = form.cleaned_data['build_run__product_version']) extra_query = { 'plans_count': RawSQL.custom_search_plans_count, 'runs_count': RawSQL.custom_search_runs_count, 'case_runs_count': RawSQL.custom_search_case_runs_count_under_run, } for tcrss in default_case_run_status: extra_query['case_runs_' + tcrss.name.lower() + '_count'] = RawSQL.custom_search_case_runs_count_by_status_under_run % tcrss.pk tbs = tbs.distinct() tbs = tbs.extra(select=extra_query) tps = tps.distinct() trs = trs.filter(plan__in = tps).distinct() for tp in tps: tp.runs = [] for tr in trs: if tp.plan_id == tr.plan_id: tp.runs.append(tr) tcrs = tcrs.distinct() Manual = 0 Automated = 1 Both = 2 both_count = tcrs.filter(case__is_automated = Both).count() auto_count = tcrs.filter(case__is_automated = Automated).count() manual_count = tcrs.filter(case__is_automated = Manual).count() total_count = both_count + auto_count + manual_count cursor = connection.cursor() for tr in trs: cursor.execute(RawSQL.custom_details_case_run_count % tr.pk) for s, n in cursor.fetchall(): setattr(tr, s, n) for tcrss in default_case_run_status: for tb in tbs: setattr(tb, 'case_runs_%s_percent' % tcrss.name.lower(), calc_percent(getattr(tb, 'case_runs_%s_count' % tcrss.name.lower()), tb.case_runs_count)) return direct_to_template(request, template_name, { 'module': MODULE_NAME, 'sub_module': SUB_MODULE_NAME, 'form': form, 'builds': tbs, 'test_plans': tps, 'test_runs': trs, 'test_case_runs': tcrs, 'test_case_run_status': tcrses, 'total_count': total_count, 'manual_count': manual_count, 'auto_count': auto_count, 'both_count': both_count, })
def custom_search(request, template_name='report/custom_search.html'): """Custom report with search function""" from tcms.apps.management.models import TestBuild from tcms.apps.testruns.models import TestCaseRunStatus from forms import CustomSearchForm SUB_MODULE_NAME = 'custom_search' total_plans_count = 0 total_runs_count = 0 auto_count = manual_count = both_count = total_count = 0 default_case_run_status = TestCaseRunStatus.objects.filter(name__in = ['passed', 'failed']) if request.REQUEST.get('a', '').lower() == 'search': form = CustomSearchForm(request.REQUEST) form.populate(product_id = request.REQUEST.get('product')) if form.is_valid(): tbs = TestBuild.objects for k, v in form.fields.items(): if form.cleaned_data[k]: tbs = tbs.filter(**{k: form.cleaned_data[k]}) extra_query = { 'plans_count': RawSQL.custom_search_plans_count, 'runs_count': RawSQL.custom_search_runs_count, 'case_runs_count': RawSQL.custom_search_case_runs_count_under_run, } for tcrss in default_case_run_status: extra_query['case_runs_' + tcrss.name.lower() + '_count'] = RawSQL.custom_search_case_runs_count_by_status_under_run % tcrss.pk tbs = tbs.distinct() tbs = tbs.extra(select=extra_query) total_plans_count = sum(filter(lambda s: s is not None, tbs.values_list('plans_count', flat = True))) total_runs_count = sum(filter(lambda s: s is not None, tbs.values_list('runs_count', flat = True))) trs = TestRun.objects.select_related('build', 'case_run') trs = TestRun.objects.filter(build__in = tbs) for tr in trs: manual_count += tr.case_run.get_manual_case_count() auto_count += tr.case_run.get_automated_case_count() both_count += tr.case_run.get_both() else: tbs = TestBuild.objects.none() else: form = CustomSearchForm() tbs = TestBuild.objects.none() for tcrss in default_case_run_status: for tb in tbs: setattr(tb, 'case_runs_%s_percent' % tcrss.name.lower(), calc_percent(getattr(tb, 'case_runs_%s_count' % tcrss.name.lower()), tb.case_runs_count)) return direct_to_template(request, template_name, { 'module': MODULE_NAME, 'sub_module': SUB_MODULE_NAME, 'form': form, 'builds': tbs, 'total_plans_count': total_plans_count, 'total_runs_count': total_runs_count, 'manual_count': manual_count, 'auto_count': auto_count, 'both_count': both_count, 'total_count': manual_count + auto_count + both_count, })
def get_case_runs_failed_percent(self): if hasattr(self, 'case_runs_failed_count'): return calc_percent(self.case_runs_failed_count, self.case_runs_count) return None