aboutsummaryrefslogtreecommitdiffstats
path: root/pygithub3/tests/services/test_issues.py
blob: 8d34b71bf4d65817e5fb680b8cf2a0655ac6e94e (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import requests
from mock import patch, Mock

from pygithub3.exceptions import ValidationError
from pygithub3.tests.utils.core import TestCase
from pygithub3.resources.base import json
from pygithub3.services.issues import (Issue, Comments, Events, Labels,
                                       Milestones)
from pygithub3.tests.utils.base import (mock_response, mock_response_result,
                                        mock_json)
from pygithub3.tests.utils.services import _

json.dumps = Mock(side_effect=mock_json)
json.loads = Mock(side_effect=mock_json)


@patch.object(requests.sessions.Session, 'request')
class TestIssuesService(TestCase):

    def setUp(self):
        self.isu = Issue(user='octocat', repo='Hello-World')

    def test_LIST_without_user(self, request_method):
        request_method.return_value = mock_response_result()
        self.isu.list().all()
        self.assertEqual(request_method.call_args[0], ('get', _('issues')))

    def test_LIST_by_repo(self, request_method):
        request_method.return_value = mock_response_result()
        self.isu.list_by_repo().all()
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/issues')))

    def test_GET(self, request_method):
        request_method.return_value = mock_response()
        self.isu.get(1)
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/issues/1')))

    def test_CREATE(self, request_method):
        request_method.return_value = mock_response('post')
        self.isu.create(dict(title='My issue', body='Fix this issue'))
        self.assertEqual(request_method.call_args[0],
            ('post', _('repos/octocat/Hello-World/issues')))

    def test_UPDATE(self, request_method):
        request_method.return_value = mock_response('patch')
        self.isu.update(1, {'body': 'edited'})
        self.assertEqual(request_method.call_args[0],
            ('patch', _('repos/octocat/Hello-World/issues/1')))


@patch.object(requests.sessions.Session, 'request')
class TestCommentService(TestCase):

    def setUp(self):
        self.cs = Comments(user='octocat', repo='Hello-World')

    def test_LIST(self, request_method):
        request_method.return_value = mock_response_result()
        self.cs.list(1).all()
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/issues/1/comments')))

    def test_GET(self, request_method):
        request_method.return_value = mock_response()
        self.cs.get(1)
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/issues/comments/1')))

    def test_CREATE(self, request_method):
        request_method.return_value = mock_response('post')
        self.cs.create(1, 'comment')
        self.assertEqual(request_method.call_args[0],
            ('post', _('repos/octocat/Hello-World/issues/1/comments')))

    def test_UPDATE(self, request_method):
        request_method.return_value = mock_response('patch')
        self.cs.update(1, 'new comment')
        self.assertEqual(request_method.call_args[0],
            ('patch', _('repos/octocat/Hello-World/issues/comments/1')))

    def test_DELETE(self, request_method):
        request_method.return_value = mock_response('delete')
        self.cs.delete(1)
        self.assertEqual(request_method.call_args[0],
            ('delete', _('repos/octocat/Hello-World/issues/comments/1')))


@patch.object(requests.sessions.Session, 'request')
class TestEventsService(TestCase):

    def setUp(self):
        self.ev = Events(user='octocat', repo='Hello-World')

    def test_LIST_by_issue(self, request_method):
        request_method.return_value = mock_response_result()
        self.ev.list_by_issue(1).all()
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/issues/1/events')))

    def test_LIST_by_repo(self, request_method):
        request_method.return_value = mock_response_result()
        self.ev.list_by_repo().all()
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/issues/events')))

    def test_GET(self, request_method):
        request_method.return_value = mock_response()
        self.ev.get(1)
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/issues/events/1')))


@patch.object(requests.sessions.Session, 'request')
class TestLabelsService(TestCase):

    def setUp(self):
        self.lb = Labels(user='octocat', repo='Hello-World')

    def test_GET(self, request_method):
        request_method.return_value = mock_response()
        self.lb.get('bug')
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/labels/bug')))

    def test_CREATE(self, request_method):
        request_method.return_value = mock_response('post')
        self.lb.create(dict(name='bug', color='FF0000'))
        self.assertEqual(request_method.call_args[0],
            ('post', _('repos/octocat/Hello-World/labels')))

    def test_CREATE_with_invalid_color(self, request_method):
        request_method.return_value = mock_response('post')
        # invalid color
        with self.assertRaises(ValidationError):
            args = {'name': 'bug', 'color': 'FF00'}
            self.lb.create(args)

    def test_UPDATE(self, request_method):
        request_method.return_value = mock_response('patch')
        self.lb.update('bug', dict(name='critical', color='FF0000'))
        self.assertEqual(request_method.call_args[0],
            ('patch', _('repos/octocat/Hello-World/labels/bug')))

    def test_UPDATE_with_invalid_color(self, request_method):
        request_method.return_value = mock_response('post')
        # invalid color
        with self.assertRaises(ValidationError):
            args = {'name': 'critical', 'color': 'FF00'}
            self.lb.update('bug', args)

    def test_DELETE(self, request_method):
        request_method.return_value = mock_response('delete')
        self.lb.delete('bug')
        self.assertEqual(request_method.call_args[0],
            ('delete', _('repos/octocat/Hello-World/labels/bug')))

    def test_LIST_by_issue(self, request_method):
        request_method.return_value = mock_response()
        self.lb.list_by_issue(1)
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/issues/1/labels')))

    def test_ADD_to_issue(self, request_method):
        request_method.return_value = mock_response('post')
        self.lb.add_to_issue(1, ('bug', 'critical'))
        self.assertEqual(request_method.call_args[0],
            ('post', _('repos/octocat/Hello-World/issues/1/labels')))

    def test_REMOVE_from_issue(self, request_method):
        request_method.return_value = mock_response('delete')
        self.lb.remove_from_issue(1, 'bug')
        self.assertEqual(request_method.call_args[0],
            ('delete', _('repos/octocat/Hello-World/issues/1/labels/bug')))

    def test_REPLACE_all(self, request_method):
        self.lb.replace_all(1, ['bug', 'critical'])
        self.assertEqual(request_method.call_args[0],
            ('put', _('repos/octocat/Hello-World/issues/1/labels')))

    def test_REMOVE_all(self, request_method):
        request_method.return_value = mock_response('delete')
        self.lb.remove_all(1)
        self.assertEqual(request_method.call_args[0],
            ('delete', _('repos/octocat/Hello-World/issues/1/labels')))

    def test_LIST_by_milestone(self, request_method):
        request_method.return_value = mock_response_result()
        self.lb.list_by_milestone(1).all()
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/milestones/1/labels')))


@patch.object(requests.sessions.Session, 'request')
class TestMilestonesService(TestCase):

    def setUp(self):
        self.mi = Milestones(user='octocat', repo='Hello-World')

    def test_LIST_by_repo(self, request_method):
        request_method.return_value = mock_response_result()
        self.mi.list().all()
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/milestones')))

    def test_GET(self, request_method):
        request_method.return_value = mock_response()
        self.mi.get(1)
        self.assertEqual(request_method.call_args[0],
            ('get', _('repos/octocat/Hello-World/milestones/1')))

    def test_CREATE(self, request_method):
        request_method.return_value = mock_response('post')
        self.mi.create(dict(title='test'))
        self.assertEqual(request_method.call_args[0],
            ('post', _('repos/octocat/Hello-World/milestones')))

    def test_UPDATE(self, request_method):
        request_method.return_value = mock_response('patch')
        self.mi.update(1, dict(title='test'))
        self.assertEqual(request_method.call_args[0],
            ('patch', _('repos/octocat/Hello-World/milestones/1')))

    def test_DELETE(self, request_method):
        request_method.return_value = mock_response('delete')
        self.mi.delete(1)
        self.assertEqual(request_method.call_args[0],
            ('delete', _('repos/octocat/Hello-World/milestones/1')))