Passed
Push — master ( 62d7d9...8230b1 )
by Alexander
03:29
created

TestExecutionDataMixin.get_execution_bugs()   A

Complexity

Conditions 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 28
rs 9.55
c 0
b 0
f 0
cc 4
nop 1
1
# -*- coding: utf-8 -*-
2
3
from itertools import groupby
4
5
from django.conf import settings
6
from django.db.models import F
7
from django.contrib.contenttypes.models import ContentType
8
from django_comments.models import Comment
9
10
from tcms.testruns.models import TestExecution
11
from tcms.testruns.models import TestExecutionStatus
12
13
14
class TestExecutionDataMixin:
15
    """Data for test executions"""
16
17
    @staticmethod
18
    def stats_mode_executions(executions):
19
        """
20
            Statistics from executions mode
21
22
            :param executions: iteratable object to access each execution
23
            :type executions: iterable, list, tuple
24
            :return: mapping between mode and the count. Example return value is
25
                     `{ 'manual': I, 'automated': J }`
26
            :rtype: dict
27
        """
28
        manual_count = 0
29
        automated_count = 0
30
31
        for execution in executions:
32
            if execution.case.is_automated:
33
                automated_count += 1
34
            else:
35
                manual_count += 1
36
37
        return {
38
            'manual': manual_count,
39
            'automated': automated_count,
40
        }
41
42
    @staticmethod
43
    def get_execution_comments(run_pk):
44
        """Get executions' comments
45
46
        :param run_pk: run's pk whose comments will be retrieved.
47
        :type run_pk: int
48
        :return: the mapping between execution id and comments
49
        :rtype: dict
50
        """
51
        # note: cast to string b/c object_pk is a Textield and PostgreSQL
52
        # doesn't like TEXT in <list of integers>
53
        # in Django 1.10 we have the Cast() function for similar cases, see
54
        # https://docs.djangoproject.com/en/1.10/ref/models/database-functions/#cast
55
        object_pks = []
56
        for execution in TestExecution.objects.filter(run=run_pk).only('pk'):
57
            object_pks.append(str(execution.pk))
58
59
        comments = Comment.objects.filter(
60
            site=settings.SITE_ID,
61
            content_type=ContentType.objects.get_for_model(TestExecution).pk,
62
            is_public=True,
63
            is_removed=False,
64
            object_pk__in=object_pks
65
        ).annotate(
66
            case_run_id=F('object_pk')
67
        ).values(
68
            'case_run_id',
69
            'submit_date',
70
            'comment',
71
            'user_name'
72
        ).order_by('case_run_id')
73
74
        rows = []
75
        for row in comments:
76
            rows.append(row)
77
78
        case_run_comments = {}
79
        for key, groups in groupby(rows, lambda row: row['case_run_id']):
80
            case_run_comments[int(key)] = list(groups)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable int does not seem to be defined.
Loading history...
81
82
        return case_run_comments
83
84
    @staticmethod
85
    def get_summary_stats(executions):
86
        """Get summary statistics from executions
87
88
        Statistics targets:
89
90
        - the number of pending test executionss, whose status is IDLE
91
        - the number of completed test executionss, whose status are PASSED,
92
          ERROR, FAILED, WAIVED
93
94
        :param executions: iterable object containing executionss
95
        :type executions: iterable
96
        :return: a mapping between statistics target and its value
97
        :rtype: dict
98
        """
99
        idle_count = 0
100
        complete_count = 0
101
        for case_run in executions:
102
            if case_run.status.name in TestExecutionStatus.idle_status_names:
103
                idle_count += 1
104
            elif case_run.status.name in TestExecutionStatus.complete_status_names:
105
                complete_count += 1
106
107
        return {'idle': idle_count, 'complete': complete_count}
108