/usr/share/pyshared/threadedcomments/tests.py is in python-django-threadedcomments 0.9.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | from unittest import TestCase
from django.test import TransactionTestCase
from django.contrib import comments
from django.contrib.sites.models import Site
from django.template import loader, TemplateSyntaxError
from django.conf import settings
from threadedcomments.util import annotate_tree_properties
from threadedcomments.templatetags import threadedcomments_tags as tags
PATH_SEPARATOR = getattr(settings, 'COMMENT_PATH_SEPARATOR', '/')
PATH_DIGITS = getattr(settings, 'COMMENT_PATH_DIGITS', 10)
def sanitize_html(html):
return '\n'.join((i.strip() for i in html.split('\n') if i.strip() != ''))
class SanityTests(TransactionTestCase):
BASE_DATA = {
'name': u'Eric Florenzano',
'email': u'floguy@gmail.com',
'comment': u'This is my favorite Django app ever!',
}
def _post_comment(self, data=None, parent=None):
Comment = comments.get_model()
body = self.BASE_DATA.copy()
if data:
body.update(data)
url = comments.get_form_target()
args = [Site.objects.all()[0]]
kwargs = {}
if parent is not None:
kwargs['parent'] = unicode(parent.pk)
body['parent'] = unicode(parent.pk)
form = comments.get_form()(*args, **kwargs)
body.update(form.generate_security_data())
self.client.post(url, body, follow=True)
return Comment.objects.order_by('-id')[0]
def test_post_comment(self):
Comment = comments.get_model()
self.assertEqual(Comment.objects.count(), 0)
comment = self._post_comment()
self.assertEqual(comment.tree_path, str(comment.pk).zfill(PATH_DIGITS))
self.assertEqual(Comment.objects.count(), 1)
self.assertEqual(comment.last_child, None)
def test_post_comment_child(self):
Comment = comments.get_model()
comment = self._post_comment()
self.assertEqual(comment.tree_path, str(comment.pk).zfill(PATH_DIGITS))
child_comment = self._post_comment(data={'name': 'ericflo'}, parent=comment)
comment_pk = str(comment.pk).zfill(PATH_DIGITS)
child_comment_pk = str(child_comment.pk).zfill(PATH_DIGITS)
self.assertEqual(child_comment.tree_path, PATH_SEPARATOR.join((comment.tree_path, child_comment_pk)))
self.assertEqual(comment.pk, child_comment.parent.pk)
comment = comments.get_model().objects.get(pk=comment.pk)
self.assertEqual(comment.last_child, child_comment)
class HierarchyTest(TransactionTestCase):
fixtures = ['simple_tree']
EXPECTED_HTML_PARTIAL = sanitize_html('''
<ul>
<li>
0000000001 ADDED
<ul>
<li class="last">
0000000001/0000000004 ADDED
<ul>
<li class="last">
0000000001/0000000004/0000000006
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>
0000000007
</li>
</ul>
''')
EXPECTED_HTML_FULL = sanitize_html('''
<ul>
<li>
0000000001
<ul>
<li>
0000000001/0000000002
<ul>
<li>
0000000001/0000000002/0000000003
</li>
<li class="last">
0000000001/0000000002/0000000005
</li>
</ul>
</li>
<li class="last">
0000000001/0000000004
<ul>
<li class="last">
0000000001/0000000004/0000000006
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>
0000000007
</li>
</ul>
''')
def test_root_path_returns_empty_for_root_comments(self):
c = comments.get_model().objects.get(pk=7)
self.assertEqual([], [x.pk for x in c.root_path])
def test_root_path_returns_only_correct_nodes(self):
c = comments.get_model().objects.get(pk=6)
self.assertEqual([1, 4], [x.pk for x in c.root_path])
def test_root_id_returns_self_for_root_comments(self):
c = comments.get_model().objects.get(pk=7)
self.assertEqual(c.pk, c.root_id)
def test_root_id_returns_root_for_replies(self):
c = comments.get_model().objects.get(pk=6)
self.assertEqual(1, c.root_id)
def test_root_has_depth_1(self):
c = comments.get_model().objects.get(pk=7)
self.assertEqual(1, c.depth)
def test_open_and_close_match(self):
depth = 0
for x in annotate_tree_properties(comments.get_model().objects.all()):
depth += getattr(x, 'open', 0)
self.assertEqual(x.depth, depth)
depth -= len(getattr(x, 'close', []))
self.assertEqual(0, depth)
def test_last_flags_set_correctly_only_on_last_sibling(self):
# construct the tree
nodes = {}
for x in comments.get_model().objects.all():
nodes[x.pk] = (x, [])
if x.parent_id:
nodes[x.parent_id][1].append(x.pk)
# check all the comments
for x in annotate_tree_properties(comments.get_model().objects.all()):
if getattr(x, 'last', False):
# last comments have a parent
self.assertTrue(x.parent_id)
par, siblings = nodes[x.parent_id]
# and ar last in their child list
self.assertTrue(x.pk in siblings)
self.assertEqual(len(siblings) - 1, siblings.index(x.pk))
def test_rendering_of_partial_tree(self):
output = loader.render_to_string('sample_tree.html', {'comment_list': comments.get_model().objects.all()[5:]})
self.assertEqual(self.EXPECTED_HTML_PARTIAL, sanitize_html(output))
def test_rendering_of_full_tree(self):
output = loader.render_to_string('sample_tree.html', {'comment_list': comments.get_model().objects.all()})
self.assertEqual(self.EXPECTED_HTML_FULL, sanitize_html(output))
def test_last_child_properly_created(self):
Comment = comments.get_model()
new_child_comment = Comment(comment="Comment 8", site_id=1, content_type_id=7, object_pk=1, parent_id=1)
new_child_comment.save()
comment = Comment.objects.get(pk=1)
self.assertEqual(comment.last_child, new_child_comment)
def test_last_child_doesnt_delete_parent(self):
Comment = comments.get_model()
comment = Comment.objects.get(pk=1)
new_child_comment = Comment(comment="Comment 9", site_id=1, content_type_id=7, object_pk=1, parent_id=comment.id)
new_child_comment.save()
new_child_comment.delete()
comment = Comment.objects.get(pk=1)
def test_last_child_repointed_correctly_on_delete(self):
Comment = comments.get_model()
comment = Comment.objects.get(pk=1)
last_child = comment.last_child
new_child_comment = Comment(comment="Comment 9", site_id=1, content_type_id=7, object_pk=1, parent_id=comment.id)
new_child_comment.save()
comment = Comment.objects.get(pk=1)
self.assertEqual(comment.last_child, new_child_comment)
new_child_comment.delete()
comment = Comment.objects.get(pk=1)
self.assertEqual(last_child, comment.last_child)
# Templatetags tests
##############################################################################
class MockParser(object):
"Mock parser object for handle_token()"
def compile_filter(self, var):
return var
mock_parser = MockParser()
class MockToken(object):
"Mock token object for handle_token()"
def __init__(self, bits):
self.contents = self
self.bits = bits
def split(self):
return self.bits
class TestCommentListNode(TestCase):
"""
{% get_comment_list for [object] as [varname] %}
{% get_comment_list for [app].[model] [object_id] as [varname] %}
"""
correct_ct_pk_params = ['get_comment_list', 'for', 'sites.site', '1', 'as', 'var']
correct_var_params = ['get_comment_list', 'for', 'var', 'as', 'var']
def test_parsing_fails_for_empty_token(self):
self.assertRaises(TemplateSyntaxError, tags.get_comment_list, mock_parser, MockToken(['get_comment_list']))
def test_parsing_fails_if_model_not_exists(self):
params = self.correct_ct_pk_params[:]
params[2] = 'not_app.not_model'
self.assertRaises(TemplateSyntaxError, tags.get_comment_list, mock_parser, MockToken(params))
def test_parsing_fails_if_object_not_exists(self):
params = self.correct_ct_pk_params[:]
params[2] = '1000'
self.assertRaises(TemplateSyntaxError, tags.get_comment_list, mock_parser, MockToken(params))
def test_parsing_works_for_ct_pk_pair(self):
node = tags.get_comment_list(mock_parser, MockToken(self.correct_ct_pk_params))
self.assertTrue(isinstance(node, tags.CommentListNode))
def test_parsing_works_for_var(self):
node = tags.get_comment_list(mock_parser, MockToken(self.correct_var_params))
self.assertTrue(isinstance(node, tags.CommentListNode))
def test_flat_parameter_is_passed_into_the_node_for_ct_pk_pair(self):
params = self.correct_ct_pk_params[:]
params.append(u'flat')
node = tags.get_comment_list(mock_parser, MockToken(params))
self.assertTrue(isinstance(node, tags.CommentListNode))
self.assertTrue(node.flat)
def test_flat_parameter_is_passed_into_the_node_for_var(self):
params = self.correct_var_params[:]
params.append(u'flat')
node = tags.get_comment_list(mock_parser, MockToken(params))
self.assertTrue(isinstance(node, tags.CommentListNode))
self.assertTrue(node.flat)
def test_root_only_parameter_is_passed_into_the_node_for_var(self):
params = self.correct_var_params[:]
params.append(u'root_only')
node = tags.get_comment_list(mock_parser, MockToken(params))
self.assertTrue(isinstance(node, tags.CommentListNode))
self.assertTrue(node.root_only)
def test_root_only_parameter_is_passed_into_the_node_for_ct_pk_pair(self):
params = self.correct_ct_pk_params[:]
params.append(u'root_only')
node = tags.get_comment_list(mock_parser, MockToken(params))
self.assertTrue(isinstance(node, tags.CommentListNode))
self.assertTrue(node.root_only)
|