File: test_labels.py

package info (click to toggle)
pontos 25.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,744 kB
  • sloc: python: 44,602; makefile: 21; sh: 10; xml: 3
file content (80 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# SPDX-FileCopyrightText: 2022-2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

# pylint: disable=redefined-builtin

from unittest.mock import MagicMock

import httpx

from pontos.github.api.labels import GitHubAsyncRESTLabels
from tests import AsyncIteratorMock, aiter, anext
from tests.github.api import GitHubAsyncRESTTestCase, create_response


class GitHubAsyncRESTLabelsTestCase(GitHubAsyncRESTTestCase):
    api_cls = GitHubAsyncRESTLabels

    async def test_get_all(self):
        response1 = create_response()
        response1.json.return_value = [{"id": 1, "name": "a"}]
        response2 = create_response()
        response2.json.return_value = [
            {"id": 2, "name": "b"},
            {"id": 3, "name": "c"},
        ]

        self.client.get_all.return_value = AsyncIteratorMock(
            [response1, response2]
        )

        async_it = aiter(self.api.get_all("foo/bar", 123))
        label = await anext(async_it)
        self.assertEqual(label, "a")
        label = await anext(async_it)
        self.assertEqual(label, "b")
        label = await anext(async_it)
        self.assertEqual(label, "c")

        with self.assertRaises(StopAsyncIteration):
            await anext(async_it)

        self.client.get_all.assert_called_once_with(
            "/repos/foo/bar/issues/123/labels",
            params={"per_page": "100"},
        )

    async def test_delete_all(self):
        response = create_response()
        self.client.delete.return_value = response

        await self.api.delete_all("foo/bar", 123)

        self.client.delete.assert_awaited_once_with(
            "/repos/foo/bar/issues/123/labels"
        )

    async def test_set_all(self):
        response = create_response()
        self.client.post.return_value = response

        await self.api.set_all("foo/bar", 123, ["a", "b"])

        self.client.post.assert_awaited_once_with(
            "/repos/foo/bar/issues/123/labels", data={"labels": ["a", "b"]}
        )

    async def test_set_labels_failure(self):
        response = create_response()
        self.client.post.side_effect = httpx.HTTPStatusError(
            "404", request=MagicMock(), response=response
        )

        with self.assertRaises(httpx.HTTPStatusError):
            await self.api.set_all("foo/bar", 123, ["a", "b"])

        self.client.post.assert_awaited_once_with(
            "/repos/foo/bar/issues/123/labels", data={"labels": ["a", "b"]}
        )