Skip to content

Commit

Permalink
Added support for Variables API methods (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolstislon authored Jan 10, 2024
1 parent 03fd04c commit 54d7b6f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
42 changes: 42 additions & 0 deletions testrail_api/_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
8 changes: 8 additions & 0 deletions testrail_api/_testrail_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tests/test_tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from re import A

import pytest
import responses
Expand All @@ -13,7 +12,7 @@ def get_tests(r):
"limit": 250,
"size": len(resp),
"tests": resp
}
}
)


Expand All @@ -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(
Expand Down
77 changes: 77 additions & 0 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 54d7b6f

Please sign in to comment.