WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c8324c8

Browse files
committed
Add TestExecutionStatus.create() API method
1 parent 2b4b446 commit c8324c8

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

tcms/rpc/api/forms/testrun.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
from tcms.core.forms.fields import UserField
55
from tcms.management.models import Build
66
from tcms.rpc.api.forms import DateTimeField, UpdateModelFormMixin
7-
from tcms.testruns.models import Environment, TestExecution, TestRun
7+
from tcms.testruns.models import (
8+
Environment,
9+
TestExecution,
10+
TestExecutionStatus,
11+
TestRun,
12+
)
813

914
User = get_user_model() # pylint: disable=invalid-name
1015

@@ -46,3 +51,9 @@ class EnvironmentForm(forms.ModelForm):
4651
class Meta:
4752
model = Environment
4853
fields = "__all__"
54+
55+
56+
class TestExecutionStatusForm(forms.ModelForm):
57+
class Meta:
58+
model = TestExecutionStatus
59+
fields = "__all__"

tcms/rpc/api/testexecutionstatus.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# Copyright (c) 2019,2021 Alexander Todorov <[email protected]>
1+
# Copyright (c) 2019-2025 Alexander Todorov <[email protected]>
22

33
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
44

5+
from django.forms.models import model_to_dict
56
from modernrpc.core import rpc_method
67

8+
from tcms.rpc.api.forms.testrun import TestExecutionStatusForm
79
from tcms.rpc.decorators import permissions_required
810
from tcms.testruns.models import TestExecutionStatus
911

@@ -26,3 +28,29 @@ def filter(query): # pylint: disable=redefined-builtin
2628
.values("id", "name", "weight", "icon", "color")
2729
.distinct()
2830
)
31+
32+
33+
@permissions_required("testruns.add_testexecutionstatus")
34+
@rpc_method(name="TestExecutionStatus.create")
35+
def create(values):
36+
"""
37+
.. function:: RPC TestExecutionStatus.create(values)
38+
39+
Create a new TestExecutionStatus object and store it in the database.
40+
41+
:param values: Field values for :class:`tcms.testruns.models.TestExecutionStatus`
42+
:type values: dict
43+
:return: Serialized :class:`tcms.testruns.models.TestExecutionStatus` object
44+
:rtype: dict
45+
:raises ValueError: if input values don't validate
46+
:raises PermissionDenied: if missing *testruns.add_testexecutionstatus* permission
47+
48+
.. versionadded:: 15.2
49+
"""
50+
form = TestExecutionStatusForm(values)
51+
52+
if form.is_valid():
53+
status = form.save()
54+
return model_to_dict(status)
55+
56+
raise ValueError(list(form.errors.items()))

tcms/rpc/tests/test_testexecutionstatus.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
from tcms.rpc.tests.utils import APIPermissionsTestCase, APITestCase
6+
from tcms.testruns.models import TestExecutionStatus
67
from tcms.xmlrpc_wrapper import XmlRPCFault
78

89

@@ -40,3 +41,45 @@ def verify_api_without_permission(self):
4041
'Authentication failed when calling "TestExecutionStatus.filter"',
4142
):
4243
self.rpc_client.TestExecutionStatus.filter({"weight__lt": 0})
44+
45+
46+
class TestExecutionStatusCreate(APIPermissionsTestCase):
47+
permission_label = "testruns.add_testexecutionstatus"
48+
49+
def verify_api_with_permission(self):
50+
result = self.rpc_client.TestExecutionStatus.create(
51+
{
52+
"name": "EMBARGOED",
53+
"weight": -50,
54+
"icon": "fa fa-ban",
55+
"color": "#ff0f1f",
56+
}
57+
)
58+
59+
# verify the serialized result
60+
self.assertIn("id", result)
61+
self.assertEqual(result["name"], "EMBARGOED")
62+
self.assertEqual(result["weight"], -50)
63+
self.assertEqual(result["icon"], "fa fa-ban")
64+
self.assertEqual(result["color"], "#ff0f1f")
65+
66+
# verify the object from the DB
67+
status = TestExecutionStatus.objects.get(pk=result["id"])
68+
self.assertEqual(status.name, result["name"])
69+
self.assertEqual(status.weight, result["weight"])
70+
self.assertEqual(status.icon, result["icon"])
71+
self.assertEqual(status.color, result["color"])
72+
73+
def verify_api_without_permission(self):
74+
with self.assertRaisesRegex(
75+
XmlRPCFault,
76+
'Authentication failed when calling "TestExecutionStatus.create"',
77+
):
78+
self.rpc_client.TestExecutionStatus.create(
79+
{
80+
"name": "NOT GOOD",
81+
"weight": 0,
82+
"icon": "fa fa-meh-o",
83+
"color": "#e3dada",
84+
}
85+
)

0 commit comments

Comments
 (0)