forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_tests.py
176 lines (138 loc) · 6.17 KB
/
comment_tests.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
from . import OBSLocal
from osclib.comments import CommentAPI
import random
import re
import unittest
COMMENT = 'short comment'
COMMENT_INFO = {'foo': 'bar', 'distro': 'openSUSE'}
PROJECT = 'openSUSE:Factory:Staging'
class TestComment(unittest.TestCase):
def setUp(self):
self.api = CommentAPI('bogus')
self.bot = type(self).__name__
self.comments = {
1: {'comment': f'<!-- {self.bot} -->\n\nshort comment'},
2: {'comment': f'<!-- {self.bot} foo=bar distro=openSUSE -->\n\nshort comment'}
}
def test_truncate(self):
comment = 'string of text'
for i in range(len(comment) + 1):
truncated = self.api.truncate(comment, length=i)
print(truncated)
self.assertEqual(len(truncated), i)
def test_truncate_pre(self):
comment = """
Some text.
<pre>
bar
mar
car
</pre>
## section 2
<pre>
more
lines
than
you
can
handle
</pre>
""".strip()
for i in range(len(comment) + len('...\n</pre>')):
truncated = self.api.truncate(comment, length=i)
print('=' * 80)
print(truncated)
self.assertTrue(len(truncated) <= i, f'{len(truncated)} <= {i}')
self.assertEqual(truncated.count('<pre>'), truncated.count('</pre>'))
self.assertFalse(len(re.findall(r'</?\w+[^\w>]', truncated)))
tag_count = truncated.count('<pre>') + truncated.count('</pre>')
self.assertEqual(tag_count, truncated.count('<'))
self.assertEqual(tag_count, truncated.count('>'))
def test_add_marker(self):
comment_marked = self.api.add_marker(COMMENT, self.bot)
self.assertEqual(comment_marked, self.comments[1]['comment'])
comment_marked = self.api.add_marker(COMMENT, self.bot, COMMENT_INFO)
self.assertEqual(comment_marked, self.comments[2]['comment'])
def test_remove_marker(self):
comment = self.api.remove_marker(COMMENT)
self.assertEqual(comment, COMMENT)
comment = self.api.remove_marker(self.comments[1]['comment'])
self.assertEqual(comment, COMMENT)
comment = self.api.remove_marker(self.comments[2]['comment'])
self.assertEqual(comment, COMMENT)
def test_comment_find(self):
comment, info = self.api.comment_find(self.comments, self.bot)
self.assertEqual(comment, self.comments[1])
comment, info = self.api.comment_find(self.comments, self.bot, COMMENT_INFO)
self.assertEqual(comment, self.comments[2])
self.assertEqual(info, COMMENT_INFO)
info_partial = dict(COMMENT_INFO)
del info_partial['foo']
comment, info = self.api.comment_find(self.comments, self.bot, info_partial)
self.assertEqual(comment, self.comments[2])
self.assertEqual(info, COMMENT_INFO)
class TestCommentOBS(OBSLocal.TestCase):
def setUp(self):
super(TestCommentOBS, self).setUp()
self.wf = OBSLocal.FactoryWorkflow()
self.wf.create_user('factory-auto')
self.wf.create_user('repo-checker')
self.wf.create_user('staging-bot')
self.wf.create_group('factory-staging', ['staging-bot'])
self.wf.create_project(PROJECT, maintainer={'groups': ['factory-staging']})
self.api = CommentAPI(self.apiurl)
# Ensure different test runs operate in unique namespace.
self.bot = '::'.join([type(self).__name__, str(random.getrandbits(8))])
def tearDown(self):
self.osc_user('Admin')
del self.wf
def test_basic(self):
self.osc_user('staging-bot')
self.assertFalse(self.comments_filtered(self.bot)[0])
self.assertTrue(self.api.add_comment(
project_name=PROJECT, comment=self.api.add_marker(COMMENT, self.bot)))
comment, _ = self.comments_filtered(self.bot)
self.assertTrue(comment)
self.assertTrue(self.api.delete(comment['id']))
self.assertFalse(self.comments_filtered(self.bot)[0])
def test_delete_nested(self):
self.osc_user('staging-bot')
comment_marked = self.api.add_marker(COMMENT, self.bot)
# Allow for existing comments by basing assertion on delta from initial count.
comment_count = len(self.api.get_comments(project_name=PROJECT))
self.assertFalse(self.comments_filtered(self.bot)[0])
self.assertTrue(self.api.add_comment(project_name=PROJECT, comment=comment_marked))
comment, _ = self.comments_filtered(self.bot)
self.assertTrue(comment)
for i in range(0, 3):
self.assertTrue(self.api.add_comment(
project_name=PROJECT, comment=comment_marked, parent_id=comment['id']))
comments = self.api.get_comments(project_name=PROJECT)
parented_count = 0
for comment in comments.values():
if comment['parent']:
parented_count += 1
self.assertEqual(parented_count, 3)
self.assertTrue(len(comments) == comment_count + 4)
self.api.delete_from(project_name=PROJECT)
self.assertFalse(len(self.api.get_comments(project_name=PROJECT)))
def test_delete_batch(self):
users = ['factory-auto', 'repo-checker', 'staging-bot']
for user in users:
self.osc_user(user)
print('logged in as ', user)
bot = '::'.join([self.bot, user])
comment = self.api.add_marker(COMMENT, bot)
self.assertFalse(self.comments_filtered(bot)[0])
self.assertTrue(self.api.add_comment(project_name=PROJECT, comment=comment))
self.assertTrue(self.comments_filtered(bot)[0])
# Allow for existing comments by basing assertion on delta from initial count.
comment_count = len(self.api.get_comments(project_name=PROJECT))
self.assertTrue(comment_count >= len(users))
self.api.delete_from_where_user(users[0], project_name=PROJECT)
self.assertTrue(len(self.api.get_comments(project_name=PROJECT)) == comment_count - 1)
self.api.delete_from(project_name=PROJECT)
self.assertFalse(len(self.api.get_comments(project_name=PROJECT)))
def comments_filtered(self, bot):
comments = self.api.get_comments(project_name=PROJECT)
return self.api.comment_find(comments, bot)