From 54d7b6fa58a7e5254910a9c4dec370b459f6ca42 Mon Sep 17 00:00:00 2001 From: tolstislon <34726608+tolstislon@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:01:29 +0300 Subject: [PATCH] Added support for Variables API methods (#104) --- testrail_api/_category.py | 42 +++++++++++++++++++ testrail_api/_testrail_api.py | 8 ++++ tests/test_tests.py | 4 +- tests/test_variables.py | 77 +++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 tests/test_variables.py diff --git a/testrail_api/_category.py b/testrail_api/_category.py index efe91a9..4ff9fdb 100644 --- a/testrail_api/_category.py +++ b/testrail_api/_category.py @@ -2268,3 +2268,45 @@ def delete_group(self, group_id: int) -> None: The ID of the group """ return self.s.post(f"delete_group/{group_id}") + + +class Variables(_MetaCategory): + """https://support.testrail.com/hc/en-us/articles/7077979742868-Variables""" + + def get_variables(self, project_id: int) -> dict: + """ + Retrieves the requested variables. + :param project_id: int + The ID of the project from which to retrieve variables. + """ + return self.s.get(endpoint=f"get_variables/{project_id}") + + def add_variable(self, project_id: int, id: int, name: str) -> dict: + """ + Creates a new variable. + :param project_id: int + The ID of the project to which the variable should be added. + :param id: int + The ID of the newly added variable + :param name: str + Name of the newly added variable + """ + return self.s.post(endpoint=f"add_variable/{project_id}", json={"name": name, "id": id}) + + def update_variable(self, variable_id: int, name: str) -> dict: + """ + Updates an existing variable. + :param variable_id: int + The ID of the variable to update. + :param name: str + Name of the variable to update + """ + return self.s.post(endpoint=f"update_variable/{variable_id}", json={"name": name}) + + def delete_variable(self, variable_id: int) -> None: + """ + Deletes an existing variable. + :param variable_id: str + The ID of the variable to be deleted. + """ + return self.s.post(endpoint=f"delete_variable/{variable_id}") diff --git a/testrail_api/_testrail_api.py b/testrail_api/_testrail_api.py index b275e34..dbcf579 100644 --- a/testrail_api/_testrail_api.py +++ b/testrail_api/_testrail_api.py @@ -198,3 +198,11 @@ def groups(self) -> _category.Groups: Use the following API methods to request details about groups. """ return _category.Groups(self) + + @property + def variables(self) -> _category.Variables: + """ + https://support.testrail.com/hc/en-us/articles/7077979742868-Variables + Use the following API methods to upload, retrieve, update, and delete variables that exist in datasets. + """ + return _category.Variables(self) diff --git a/tests/test_tests.py b/tests/test_tests.py index afcbbe3..08e45c8 100644 --- a/tests/test_tests.py +++ b/tests/test_tests.py @@ -1,5 +1,4 @@ import json -from re import A import pytest import responses @@ -13,7 +12,7 @@ def get_tests(r): "limit": 250, "size": len(resp), "tests": resp - } + } ) @@ -38,6 +37,7 @@ def test_get_tests(api, mock, url, status_id): assert resp[0]['status_id'] == 1 assert resp[1]['status_id'] == 5 + @pytest.mark.parametrize('status_id', ('1,5', [1, 5])) def test_get_tests_bulk(api, mock, url, status_id): mock.add_callback( diff --git a/tests/test_variables.py b/tests/test_variables.py new file mode 100644 index 0000000..d34bb87 --- /dev/null +++ b/tests/test_variables.py @@ -0,0 +1,77 @@ +import json +import uuid +import random + +import responses +from requests import PreparedRequest + + +def _add_variables(r: PreparedRequest): + req = json.loads(r.body) + assert "id" in req and "name" in req + return 200, {}, json.dumps(req) + + +def _update_variable(r: PreparedRequest): + req = json.loads(r.body) + v = r.url.split('/')[-1] + return 200, {}, json.dumps({"id": int(v), "name": req["name"]}) + + +def test_get_variables(api, mock, url): + project_id = random.randint(1, 10000) + mock.add_callback( + responses.GET, + url(f'get_variables/{project_id}'), + lambda x: ( + 200, {}, + json.dumps({ + "offset": 0, + "limit": 250, + "size": 2, + "_links": { + "next": None, + "prev": None + }, + "variables": [ + { + "id": 611, + "name": "d" + }, + { + "id": 612, + "name": "e" + } + ] + }) + ), + ) + response = api.variables.get_variables(project_id) + assert response['size'] == 2 + for variable in response['variables']: + assert tuple(variable) == ('id', 'name') + + +def test_add_variables(api, mock, url): + project_id = random.randint(1, 10000) + _id, _name = random.randint(1, 10000), uuid.uuid4().hex + mock.add_callback(responses.POST, url(f'add_variable/{project_id}'), _add_variables) + response = api.variables.add_variable(project_id, _id, _name) + assert response['id'] == _id + assert response['name'] == _name + + +def test_update_variable(api, mock, url): + variable_id = random.randint(1, 10000) + _name = uuid.uuid4().hex + mock.add_callback(responses.POST, url(f'update_variable/{variable_id}'), _update_variable) + response = api.variables.update_variable(variable_id=variable_id, name=_name) + assert response['id'] == variable_id + assert response['name'] == _name + + +def test_delete_variable(api, mock, url): + variable_id = random.randint(1, 10000) + mock.add_callback(responses.POST, url(f'delete_variable/{variable_id}'), lambda x: (200, {}, None)) + response = api.variables.delete_variable(variable_id) + assert response is None