|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import pytest |
| 4 | +from crowdin_api.api_resources.enums import PatchOperation |
| 5 | +from crowdin_api.api_resources.task_comments.enums import TaskCommentPatchPath |
| 6 | +from crowdin_api.api_resources.task_comments.resource import TaskCommentsResource |
| 7 | +from crowdin_api.requester import APIRequester |
| 8 | +from crowdin_api.sorting import Sorting |
| 9 | + |
| 10 | + |
| 11 | +class TestTaskCommentsResource: |
| 12 | + resource_class = TaskCommentsResource |
| 13 | + |
| 14 | + def get_resource(self, base_absolut_url): |
| 15 | + return self.resource_class(requester=APIRequester(base_url=base_absolut_url)) |
| 16 | + |
| 17 | + def test_resource_with_id(self, base_absolut_url): |
| 18 | + project_id = 1 |
| 19 | + resource = self.resource_class( |
| 20 | + requester=APIRequester(base_url=base_absolut_url), project_id=project_id |
| 21 | + ) |
| 22 | + assert resource.get_project_id() == project_id |
| 23 | + |
| 24 | + @pytest.mark.parametrize( |
| 25 | + "incoming_data, path", |
| 26 | + ( |
| 27 | + ({"projectId": 1, "taskId": 2}, "projects/1/tasks/2/comments"), |
| 28 | + ( |
| 29 | + {"projectId": 1, "taskId": 2, "taskCommentId": 3}, |
| 30 | + "projects/1/tasks/2/comments/3", |
| 31 | + ), |
| 32 | + ), |
| 33 | + ) |
| 34 | + def test_get_task_comments_path(self, incoming_data, path, base_absolut_url): |
| 35 | + resource = self.get_resource(base_absolut_url) |
| 36 | + assert resource.get_task_comments_path(**incoming_data) == path |
| 37 | + |
| 38 | + @pytest.mark.parametrize( |
| 39 | + "incoming_data, request_params", |
| 40 | + ( |
| 41 | + ({}, {"orderBy": None, "offset": 0, "limit": 25}), |
| 42 | + ({"orderBy": Sorting([])}, {"orderBy": Sorting([]), "offset": 0, "limit": 25}), |
| 43 | + ), |
| 44 | + ) |
| 45 | + @mock.patch("crowdin_api.requester.APIRequester.request") |
| 46 | + def test_list_task_comments(self, m_request, incoming_data, request_params, base_absolut_url): |
| 47 | + m_request.return_value = "response" |
| 48 | + |
| 49 | + resource = self.get_resource(base_absolut_url) |
| 50 | + assert resource.list_task_comments(projectId=1, taskId=2, **incoming_data) == "response" |
| 51 | + m_request.assert_called_once_with( |
| 52 | + method="get", |
| 53 | + params=request_params, |
| 54 | + path=resource.get_task_comments_path(projectId=1, taskId=2), |
| 55 | + ) |
| 56 | + |
| 57 | + @mock.patch("crowdin_api.requester.APIRequester.request") |
| 58 | + def test_add_task_comment(self, m_request, base_absolut_url): |
| 59 | + m_request.return_value = "response" |
| 60 | + |
| 61 | + resource = self.get_resource(base_absolut_url) |
| 62 | + assert resource.add_task_comment(projectId=1, taskId=2, text="hello") == "response" |
| 63 | + m_request.assert_called_once_with( |
| 64 | + method="post", |
| 65 | + path=resource.get_task_comments_path(projectId=1, taskId=2), |
| 66 | + request_data={"text": "hello"}, |
| 67 | + ) |
| 68 | + |
| 69 | + @mock.patch("crowdin_api.requester.APIRequester.request") |
| 70 | + def test_get_task_comment(self, m_request, base_absolut_url): |
| 71 | + m_request.return_value = "response" |
| 72 | + |
| 73 | + resource = self.get_resource(base_absolut_url) |
| 74 | + assert resource.get_task_comment(projectId=1, taskId=2, taskCommentId=3) == "response" |
| 75 | + m_request.assert_called_once_with( |
| 76 | + method="get", |
| 77 | + path=resource.get_task_comments_path(projectId=1, taskId=2, taskCommentId=3), |
| 78 | + ) |
| 79 | + |
| 80 | + @mock.patch("crowdin_api.requester.APIRequester.request") |
| 81 | + def test_delete_task_comment(self, m_request, base_absolut_url): |
| 82 | + m_request.return_value = "response" |
| 83 | + |
| 84 | + resource = self.get_resource(base_absolut_url) |
| 85 | + assert resource.delete_task_comment(projectId=1, taskId=2, taskCommentId=3) == "response" |
| 86 | + m_request.assert_called_once_with( |
| 87 | + method="delete", |
| 88 | + path=resource.get_task_comments_path(projectId=1, taskId=2, taskCommentId=3), |
| 89 | + ) |
| 90 | + |
| 91 | + @mock.patch("crowdin_api.requester.APIRequester.request") |
| 92 | + def test_edit_task_comment(self, m_request, base_absolut_url): |
| 93 | + m_request.return_value = "response" |
| 94 | + |
| 95 | + data = [ |
| 96 | + { |
| 97 | + "value": "new text", |
| 98 | + "op": PatchOperation.REPLACE, |
| 99 | + "path": TaskCommentPatchPath.TEXT, |
| 100 | + } |
| 101 | + ] |
| 102 | + |
| 103 | + resource = self.get_resource(base_absolut_url) |
| 104 | + assert ( |
| 105 | + resource.edit_task_comment(projectId=1, taskId=2, taskCommentId=3, data=data) |
| 106 | + == "response" |
| 107 | + ) |
| 108 | + m_request.assert_called_once_with( |
| 109 | + method="patch", |
| 110 | + path=resource.get_task_comments_path(projectId=1, taskId=2, taskCommentId=3), |
| 111 | + request_data=data, |
| 112 | + ) |
| 113 | + |
| 114 | + |
0 commit comments