-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·226 lines (205 loc) · 7.16 KB
/
test.py
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
#!/usr/bin/env python3
import json
import wsgi
import unittest
class FakeBugzilla(object):
comments = {
'bugs': {
'999999': {
'comments': [{
'count': 0,
'author': '[email protected]',
'text': 'Description of problem:',
'creator': '[email protected]',
'bug_id': 999999,
'creator_id': 210279,
'id': 8749185,
'is_private': False
}, {
'count': 1,
'author': '[email protected]',
'text': """
https://github.com/atodorov/bztest/commit/8ee2da3e225008f17bc89439f72bd6652111942a
Branch: master
Author: Alexander Todorov <[email protected]>
Date: 2015-10-23 13:19:13+03:00
2nd commit (#123456)
Resolves: rhbz#999999
""",
'creator': '[email protected]',
'bug_id': 999999,
'creator_id': 210279,
'id': 8749241,
'is_private': True
}]
},
'123456': {
'comments': [{
'count': 0,
'author': '[email protected]',
'text': 'Description of problem:',
'creator': '[email protected]',
'bug_id': 123456,
'creator_id': 210279,
'id': 8749185,
'is_private': False
}]
},
},
'comments': {}
}
def get_comments(self, bug_id):
return self.comments
def update_bugs(self, bug_id, update):
pass
class TestGHBH_TestCase(unittest.TestCase):
data = """
{
"ref":"refs/heads/master",
"repository": {
"html_url": "https://github.com/Codertocat"
},
"commits":[
{
"id":"d3faddfaddb9eac64f00a09df73cbb783c6ad855",
"distinct":true,
"message":"First commit bug 123456",
"timestamp":"2015-10-23T13:18:47+03:00",
"url":"https://github.com/atodorov/bztest/commit/d3faddfaddb9eac64f00a09df73cbb783c6ad855",
"author":
{
"name":"Alexander Todorov",
"email":"[email protected]",
"username":"atodorov"
},
"committer":
{
"name":"Alexander Todorov",
"email":"[email protected]",
"username":"atodorov"
},
"added":[],
"removed":[],
"modified":["README"]
},
{
"id":"8ee2da3e225008f17bc89439f72bd6652111942a",
"distinct":true,
"message":"2nd commit issue 123456\\n\\nResolves: 999999",
"timestamp":"2015-10-23T13:19:13+03:00",
"url":"https://github.com/atodorov/bztest/commit/8ee2da3e225008f17bc89439f72bd6652111942a",
"author":
{
"name":"Alexander Todorov",
"email":"[email protected]",
"username":"atodorov"
},
"committer":
{
"name":"Alexander Todorov",
"email":"[email protected]",
"username":"atodorov"
},
"added":[],
"removed":[],
"modified":["README"]
}
]
}
"""
data = json.loads(data)
def test_get_bugs(self):
bugs = wsgi.get_bugs(self.data)
bzs = bugs.keys()
self.assertEqual(len(bzs), 2)
self.assertTrue('123456' in bzs)
self.assertTrue('999999' in bzs)
# assert number of commits per bug is as expected
self.assertEqual(len(bugs['123456']), 2)
self.assertEqual(len(bugs['999999']), 1)
def test_get_comments(self):
comments = wsgi.get_comments(self.data)
bzs = comments.keys()
self.assertEqual(len(bzs), 2)
self.assertTrue('123456' in bzs)
self.assertTrue('999999' in bzs)
comment_1 = comments['123456']
# bug 123456 is referenced in two commits
self.assertTrue(comment_1.find("d3faddfaddb9eac64f00a09df73cbb783c6ad855") > -1)
self.assertTrue(comment_1.find("8ee2da3e225008f17bc89439f72bd6652111942a") > -1)
comment_2 = comments['999999']
# bug 999999 is referenced only in the second commit
self.assertEqual(comment_2.find("d3faddfaddb9eac64f00a09df73cbb783c6ad855"), -1)
self.assertTrue(comment_2.find("8ee2da3e225008f17bc89439f72bd6652111942a") > -1)
def test_post_to_bugzilla(self):
"""
- Bug 999999 already has a comment on it so don't post a new one,
- Bug 123456 doesn't have any comments from us so update it
"""
posts = wsgi.post_to_bugzilla(FakeBugzilla(), self.data)
# comment posted only for bug 123456
self.assertEqual(posts, 1)
def test_post_to_bugzilla_2_branches_same_bug(self):
"""
If a commit on another branch references the same bug we
have to add a comment for it. Bug 999999 already has a comment
for the master branch.
"""
data = """
{
"ref":"refs/heads/new_branch",
"repository": {
"html_url": "https://github.com/Codertocat"
},
"commits":[
{
"id":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"url":"https://github.com/atodorov/bztest/commit/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"message":"Commit on another branch\\n\\nResolves: 999999",
"timestamp":"2015-10-23T13:18:47+03:00",
"author":
{
"name":"Alexander Todorov",
"email":"[email protected]",
"username":"atodorov"
}
}
]
}
"""
data = json.loads(data)
posts = wsgi.post_to_bugzilla(FakeBugzilla(), data)
self.assertEqual(posts, 1)
def test_post_to_bugzilla_same_branch_separate_commits(self):
"""
If separate commits (pushed later) on the branch reference
the same bug we are not going to add a comment for it.
Bug 999999 already has a comment for the master branch.
"""
data = """
{
"ref":"refs/heads/master",
"repository": {
"html_url": "https://github.com/Codertocat"
},
"commits":[
{
"id":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"url":"https://github.com/atodorov/bztest/commit/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"message":"Commit on another branch\\n\\nResolves: rhbz#999999",
"timestamp":"2015-10-23T13:18:47+03:00",
"author":
{
"name":"Alexander Todorov",
"email":"[email protected]",
"username":"atodorov"
}
}
]
}
"""
data = json.loads(data)
posts = wsgi.post_to_bugzilla(FakeBugzilla(), data)
self.assertEqual(posts, 0)
if __name__ == '__main__':
unittest.main()