• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

testit-tms / adapters-python / 17091453566

20 Aug 2025 07:19AM UTC coverage: 36.848% (+0.4%) from 36.441%
17091453566

push

github

web-flow
fix: fix updating autotest links to workitems. (#199)

* fix: fix updating autotest links to workitems.

* fix: fix the models.

---------

Co-authored-by: pavel.butuzov <pavel.butuzov@testit.software>

53 of 147 new or added lines in 3 files covered. (36.05%)

1 existing line in 1 file now uncovered.

1237 of 3357 relevant lines covered (36.85%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.54
/testit-python-commons/src/testit_python_commons/client/helpers/bulk_autotest_helper.py
1
import logging
2✔
2
import typing
2✔
3

4
from testit_api_client.apis import AutoTestsApi, TestRunsApi
2✔
5
from testit_api_client.models import (
2✔
6
    AutoTestPostModel,
7
    AutoTestPutModel,
8
    AutoTestResultsForTestRunModel,
9
    LinkAutoTestToWorkItemRequest,
10
    WorkItemIdentifierModel,
11
)
12

13
from testit_python_commons.client.client_configuration import ClientConfiguration
2✔
14
from testit_python_commons.services.logger import adapter_logger
2✔
15
from testit_python_commons.services.retry import retry
2✔
16
from testit_python_commons.utils.html_escape_utils import HtmlEscapeUtils
2✔
17

18

19
class BulkAutotestHelper:
2✔
20
    __max_tests_for_import = 100
2✔
21

22
    def __init__(
2✔
23
            self,
24
            autotests_api: AutoTestsApi,
25
            test_runs_api: TestRunsApi,
26
            config: ClientConfiguration):
NEW
27
        self.__autotests_api = autotests_api
×
NEW
28
        self.__test_runs_api = test_runs_api
×
NEW
29
        self.__test_run_id = config.get_test_run_id()
×
NEW
30
        self.__automatic_updation_links_to_test_cases = config.get_automatic_updation_links_to_test_cases()
×
NEW
31
        self.__autotests_for_create = []
×
NEW
32
        self.__autotests_for_update = []
×
NEW
33
        self.__autotest_links_to_wi_for_update = {}
×
NEW
34
        self.__results_for_autotests_being_created = []
×
NEW
35
        self.__results_for_autotests_being_updated = []
×
36

37
    @adapter_logger
2✔
38
    def add_for_create(
2✔
39
            self,
40
            create_model: AutoTestPostModel,
41
            result_model: AutoTestResultsForTestRunModel):
NEW
42
        self.__autotests_for_create.append(create_model)
×
NEW
43
        self.__results_for_autotests_being_created.append(result_model)
×
44

NEW
45
        if len(self.__autotests_for_create) >= self.__max_tests_for_import:
×
NEW
46
            self.__bulk_create()
×
47

48
    @adapter_logger
2✔
49
    def add_for_update(
2✔
50
            self,
51
            update_model: AutoTestPutModel,
52
            result_model: AutoTestResultsForTestRunModel,
53
            autotest_links_to_wi_for_update: dict):
NEW
54
        self.__autotests_for_update.append(update_model)
×
NEW
55
        self.__results_for_autotests_being_updated.append(result_model)
×
NEW
56
        self.__autotest_links_to_wi_for_update.update(autotest_links_to_wi_for_update)
×
57

NEW
58
        if len(self.__autotests_for_create) >= self.__max_tests_for_import:
×
NEW
59
            self.__bulk_update()
×
60

61
    @adapter_logger
2✔
62
    def teardown(self):
2✔
NEW
63
        if len(self.__autotests_for_create) > 0:
×
NEW
64
            self.__bulk_create()
×
65

NEW
66
        if len(self.__autotests_for_update) > 0:
×
NEW
67
            self.__bulk_update()
×
68

69
    @adapter_logger
2✔
70
    def __bulk_create(self):
2✔
NEW
71
        self.__create_tests(self.__autotests_for_create)
×
NEW
72
        self.__load_test_results(self.__results_for_autotests_being_created)
×
73

NEW
74
        self.__autotests_for_create.clear()
×
NEW
75
        self.__results_for_autotests_being_created.clear()
×
76

77
    @adapter_logger
2✔
78
    def __bulk_update(self):
2✔
NEW
79
        self.__update_tests(self.__autotests_for_update)
×
NEW
80
        self.__load_test_results(self.__results_for_autotests_being_updated)
×
81

NEW
82
        for autotest_id, work_item_ids in self.__autotest_links_to_wi_for_update.items():
×
NEW
83
            self.__update_autotest_link_from_work_items(autotest_id, work_item_ids)
×
84

NEW
85
        self.__autotests_for_update.clear()
×
NEW
86
        self.__results_for_autotests_being_updated.clear()
×
NEW
87
        self.__autotest_links_to_wi_for_update.clear()
×
88

89
    @adapter_logger
2✔
90
    def __create_tests(self, autotests_for_create: typing.List[AutoTestPostModel]):
2✔
NEW
91
        logging.debug(f'Creating autotests: "{autotests_for_create}')
×
92

NEW
93
        autotests_for_create = HtmlEscapeUtils.escape_html_in_object(autotests_for_create)
×
NEW
94
        self.__autotests_api.create_multiple(auto_test_post_model=autotests_for_create)
×
95

NEW
96
        logging.debug(f'Autotests were created')
×
97

98
    @adapter_logger
2✔
99
    def __update_tests(self, autotests_for_update: typing.List[AutoTestPutModel]):
2✔
NEW
100
        logging.debug(f'Updating autotests: {autotests_for_update}')
×
101

NEW
102
        autotests_for_update = HtmlEscapeUtils.escape_html_in_object(autotests_for_update)
×
NEW
103
        self.__autotests_api.update_multiple(auto_test_put_model=autotests_for_update)
×
104

NEW
105
        logging.debug(f'Autotests were updated')
×
106

107
    @adapter_logger
2✔
108
    def __load_test_results(self, test_results: typing.List[AutoTestResultsForTestRunModel]):
2✔
NEW
109
        logging.debug(f'Loading test results: {test_results}')
×
110

NEW
111
        test_results = HtmlEscapeUtils.escape_html_in_object(test_results)
×
NEW
112
        self.__test_runs_api.set_auto_test_results_for_test_run(
×
113
            id=self.__test_run_id,
114
            auto_test_results_for_test_run_model=test_results)
115

116
    # TODO: delete after fix PUT/api/v2/autoTests
117
    @adapter_logger
2✔
118
    def __get_work_items_linked_to_autotest(self, autotest_global_id: str) -> typing.List[WorkItemIdentifierModel]:
2✔
NEW
119
        return self.__autotests_api.get_work_items_linked_to_auto_test(id=autotest_global_id)
×
120

121
    # TODO: delete after fix PUT/api/v2/autoTests
122
    @adapter_logger
2✔
123
    @retry
2✔
124
    def __unlink_test_to_work_item(self, autotest_global_id: str, work_item_id: str):
2✔
NEW
125
        self.__autotests_api.delete_auto_test_link_from_work_item(
×
126
            id=autotest_global_id,
127
            work_item_id=work_item_id)
128

NEW
129
        logging.debug(f'Autotest was unlinked with workItem "{work_item_id}" by global id "{autotest_global_id}')
×
130

131
    # TODO: delete after fix PUT/api/v2/autoTests
132
    @adapter_logger
2✔
133
    @retry
2✔
134
    def __link_test_to_work_item(self, autotest_global_id: str, work_item_id: str):
2✔
NEW
135
        self.__autotests_api.link_auto_test_to_work_item(
×
136
            autotest_global_id,
137
            link_auto_test_to_work_item_request=LinkAutoTestToWorkItemRequest(id=work_item_id))
138

NEW
139
        logging.debug(f'Autotest was linked with workItem "{work_item_id}" by global id "{autotest_global_id}')
×
140

141
    # TODO: delete after fix PUT/api/v2/autoTests
142
    @adapter_logger
2✔
143
    def __update_autotest_link_from_work_items(self, autotest_global_id: str, work_item_ids: list):
2✔
NEW
144
        linked_work_items = self.__get_work_items_linked_to_autotest(autotest_global_id)
×
145

NEW
146
        for linked_work_item in linked_work_items:
×
NEW
147
            linked_work_item_id = str(linked_work_item.global_id)
×
148

NEW
149
            if linked_work_item_id in work_item_ids:
×
NEW
150
                work_item_ids.remove(linked_work_item_id)
×
151

NEW
152
                continue
×
153

NEW
154
            if self.__automatic_updation_links_to_test_cases != 'false':
×
NEW
155
                self.__unlink_test_to_work_item(autotest_global_id, linked_work_item_id)
×
156

NEW
157
        for work_item_id in work_item_ids:
×
NEW
158
            self.__link_test_to_work_item(autotest_global_id, work_item_id)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc