From 51b782077af92684aa90d3b147bee112d8b15a3b Mon Sep 17 00:00:00 2001 From: "Mr. Senko" Date: Sun, 1 Jul 2018 00:59:35 +0300 Subject: [PATCH] Refactor RawSQL.num_case_run_bugs. Refs #36 refactor into annotated Count --- tcms/core/utils/raw_sql.py | 11 +---------- tcms/report/views.py | 2 +- tcms/testruns/views.py | 7 +++---- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/tcms/core/utils/raw_sql.py b/tcms/core/utils/raw_sql.py index c4463e9146..975f3d75e6 100644 --- a/tcms/core/utils/raw_sql.py +++ b/tcms/core/utils/raw_sql.py @@ -1,14 +1,5 @@ # -*- coding: utf-8 -*- -class RawSQL: # pylint: disable=too-few-public-methods - """ - Record the Raw SQL for operate the database directly. - """ - num_case_run_bugs = 'SELECT COUNT(*) \ - FROM testcases_bug \ - WHERE testcases_bug.case_run_id = testruns_testcaserun.case_run_id' - - -class ReportSQL(object): # pylint: disable=too-few-public-methods +class RawSQL(object): # pylint: disable=too-few-public-methods # Index index_product_plans_count = 'SELECT COUNT(plan_id) \ FROM testplans_testplan \ diff --git a/tcms/report/views.py b/tcms/report/views.py index 67704e2cd8..a9c78e22a9 100755 --- a/tcms/report/views.py +++ b/tcms/report/views.py @@ -12,7 +12,7 @@ from tcms.management.models import Priority from tcms.management.models import Product from tcms.testruns.models import TestRun, TestCaseRunStatus, TestCaseRun -from tcms.core.utils.raw_sql import ReportSQL as RawSQL +from tcms.core.utils.raw_sql import RawSQL from tcms.report.forms import TestingReportForm from tcms.report.forms import TestingReportCaseRunsListForm from tcms.report.data import CustomDetailsReportData diff --git a/tcms/testruns/views.py b/tcms/testruns/views.py index 38197f9aa8..c23ffa8137 100755 --- a/tcms/testruns/views.py +++ b/tcms/testruns/views.py @@ -28,7 +28,6 @@ from tcms.core.utils import clean_request from tcms.core.utils import DataTableResult -from tcms.core.utils.raw_sql import RawSQL from tcms.core.utils.validations import validate_bug_id from tcms.management.models import Priority, EnvValue, Tag from tcms.search.forms import RunForm @@ -484,9 +483,9 @@ def open_run_get_case_runs(request, run): 'case__category__name') # Get the bug count for each case run # 5. have to show the number of bugs of each case run - tcrs = tcrs.extra(select={ - 'num_bug': RawSQL.num_case_run_bugs, - }) + tcrs = tcrs.annotate(num_bug=Count('case_run_bug', distinct=True)) + + # todo: is this last distinct necessary tcrs = tcrs.distinct() # Continue to search the case runs with conditions # 4. case runs preparing for render case runs table