|
1 # -*- coding: utf-8 -*- |
|
2 from django.test import TestCase |
|
3 from BeautifulSoup import BeautifulSoup |
|
4 from cm.models import * |
|
5 |
|
6 # python manage.py test |
|
7 # |
|
8 # python manage.py test cm.CommentPositioningTest |
|
9 |
|
10 |
|
11 def create_comment(start_wrapper=0, end_wrapper=0, start_offset=0, end_offset=0, reply_to=None, user=None, state='pending'): |
|
12 version = Text.objects.all()[0].get_latest_version() |
|
13 co = Comment.objects.create(text_version=version, |
|
14 title="tt", |
|
15 content="tt", |
|
16 start_wrapper=start_wrapper, |
|
17 end_wrapper=end_wrapper, |
|
18 start_offset=start_offset, |
|
19 end_offset=end_offset, |
|
20 reply_to=reply_to, |
|
21 state=state, |
|
22 user=user) |
|
23 return co |
|
24 |
|
25 |
|
26 class CommentPositioningTest(TestCase): |
|
27 |
|
28 |
|
29 def assert_comment(self, old_comment_id, start_wrapper, end_wrapper, start_offset, end_offset): |
|
30 comment = Comment.objects.get(id=old_comment_id) |
|
31 #print comment.start_wrapper, comment.end_wrapper, comment.start_offset, comment.end_offset |
|
32 #print start_wrapper, end_wrapper, start_offset, end_offset |
|
33 self.assertEqual(comment.start_wrapper, start_wrapper) |
|
34 self.assertEqual(comment.end_wrapper, end_wrapper) |
|
35 self.assertEqual(comment.start_offset, start_offset) |
|
36 self.assertEqual(comment.end_offset, end_offset) |
|
37 |
|
38 def preserve_comment_pos(self, content, new_content, comment_pos_list): |
|
39 text = Text.objects.create_text("text", "html", content, "", "", "", None) |
|
40 version = Text.objects.all()[0].get_latest_version() |
|
41 |
|
42 res = {} |
|
43 for old, new in comment_pos_list: |
|
44 x, y, z, k = old |
|
45 comment = create_comment(x, y, z, k) |
|
46 res[comment.id] = new |
|
47 |
|
48 version.edit("text", "html", new_content, keep_comments = True) |
|
49 |
|
50 for id, new in res.items(): |
|
51 if not new: |
|
52 self.assertFalse(Comment.objects.filter(id=id)) |
|
53 else: |
|
54 x, y , z, k = new |
|
55 self.assert_comment(id, x, y, z, k) |
|
56 |
|
57 def test_wrapper_shifted(self): |
|
58 content = """<html><body>This is a <b>test</b> text</body></html>""" |
|
59 new_content = """<html><body>This is a <b>te</b>e<b>est</b> text</body></html>""" |
|
60 self.preserve_comment_pos(content, new_content, [([2,2,2,4],[4,4,2,4]),]) |
|
61 |
|
62 def test_comment_removed(self): |
|
63 content = """<html><body>This is a <b>test</b> text</body></html>""" |
|
64 new_content = """<html><body>This is a <b>test</b> txt</body></html>""" |
|
65 self.preserve_comment_pos(content, new_content, [([2,2,2,4],None),]) |
|
66 |
|
67 def test_offset_shifted(self): |
|
68 content = """<html><body>This is a <b>test</b> text</body></html>""" |
|
69 new_content = """<html><body>a <b>teXXXst</b>a text</body></html>""" |
|
70 self.preserve_comment_pos(content, new_content, [([2,2,2,4],[2,2,3,5]),]) |
|
71 |
|
72 def test_insert_wrapper(self): |
|
73 content = """<html><body>This is a <b>test</b> text</body></html>""" |
|
74 new_content = """<html><body>This is a <b>test</b> te<b>x</b>t</body></html>""" |
|
75 self.preserve_comment_pos(content, new_content, [([2,2,2,5],[2,4,2,1]),]) |
|
76 |
|
77 def test_multiwrapper(self): |
|
78 content = """<html><body>This is a <b>test</b> text</body></html>""" |
|
79 new_content = """<html><body>This is a <b>testXXX<b>X</b>XXXXXXX</b>X text</body></html>""" |
|
80 self.preserve_comment_pos(content, new_content, [([0,2,2,4],None),]) |
|
81 |
|
82 def test_insert_wrapper(self): |
|
83 content = """<html><body>aa<b>test</b>bb</body></html>""" |
|
84 new_content = """<html><body>aXa<b>test</b>bXb</body></html>""" |
|
85 self.preserve_comment_pos(content, new_content, [([0,2,1,1],[0,2,2,1]),]) |
|
86 |