/
level2_unit.py
175 lines (122 loc) · 5.6 KB
/
level2_unit.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
'''
@author: Steve Cassidy
'''
import unittest
import interface
from database import COMP249Db
class LevelAUnitTests(unittest.TestCase):
def setUp(self):
# open an in-memory database for testing
self.db = COMP249Db(test=True)
self.db.create_tables()
self.db.sample_data(random=False)
self.posts = self.db.posts
def test_post_list(self):
"""Test that post_list returns all posts"""
# first get all posts
posts = interface.post_list(self.db)
print(posts)
self.assertEqual(len(self.posts), len(posts))
print(posts)
# now restrict to one user
posts = interface.post_list(self.db, usernick='Mandible')
print(posts)
self.assertEqual(3, len(posts))
# try a user with no posts
posts = interface.post_list(self.db, usernick='jb@up.com')
print(posts)
self.assertEqual(0, len(posts))
# try a user who doesn't exist
posts = interface.post_list(self.db, usernick='not@real.com')
print(posts)
self.assertEqual(0, len(posts))
# now look at the limit argument
posts = interface.post_list(self.db, limit=3)
self.assertEqual(3, len(posts))
posts = interface.post_list(self.db, limit=1)
self.assertEqual(1, len(posts))
# limit higher than number of posts
posts = interface.post_list(self.db, limit=100)
self.assertEqual(10, len(posts))
def test_post_list_mentions(self):
"""Test getting a list of posts mentioning a user"""
user1 = 'Contrary'
user2 = 'Bobalooba'
user3 = 'Mandible'
posts = interface.post_list_mentions(self.db, usernick=user1)
# should include only posts with @Contrary
print(posts)
self.assertEqual(2, len(posts))
self.assertListEqual([2, 5], [p[0] for p in posts])
def test_post_html(self):
"""Test conversion of posts to HTML"""
# plain text doesn't change
text = "Hello World!"
self.assertEqual(text, interface.post_to_html(text))
# quoting HTML
text = "<p>Hello World</p>"
html = interface.post_to_html(text)
self.assertEqual("<p>Hello World</p>", html)
# quoting HTML with attributes
text = "<p class='foo'>Hello World</p>"
html = interface.post_to_html(text)
self.assertEqual("<p class='foo'>Hello World</p>", html)
# quoting HTML entities
text = "Fish & Chips"
html = interface.post_to_html(text)
self.assertEqual("Fish & Chips", html)
# links
text = "<p>Hello World http://example.org/ is it</p>"
html = interface.post_to_html(text)
self.assertEqual("<p>Hello World <a href='http://example.org/'>http://example.org/</a> is it</p>", html)
# links with stuff
text = "<p>Hello World http://example.org/home_page.html is it</p>"
html = interface.post_to_html(text)
self.assertEqual("<p>Hello World <a href='http://example.org/home_page.html'>http://example.org/home_page.html</a> is it</p>", html)
def test_post_html_mentions(self):
"""Test conversion of @mentions in posts to HTML"""
# mentions
text = "<p>Hello World @Bobolooba</p>"
html = interface.post_to_html(text)
self.assertEqual("<p>Hello World <a href='/users/Bobolooba'>@Bobolooba</a></p>", html)
# word internal period is allowed
text = "<p>Hello World @steve.cassidy</p>"
html = interface.post_to_html(text)
self.assertEqual("<p>Hello World <a href='/users/steve.cassidy'>@steve.cassidy</a></p>", html)
# word final period is not part of the name
text = "Hello World @Cat."
html = interface.post_to_html(text)
self.assertEqual("Hello World <a href='/users/Cat'>@Cat</a>.", html)
# word final period followed by space is not part of the name
text = "Hello World @Cat. Hi!"
html = interface.post_to_html(text)
self.assertEqual("Hello World <a href='/users/Cat'>@Cat</a>. Hi!", html)
def test_post_html_hashtag(self):
"""Test conversion of #hashtags in posts to HTML"""
text = "Hello World #whatever"
html = interface.post_to_html(text)
self.assertEqual("Hello World <strong class='hashtag'>#whatever</strong>", html)
text = "#whatever you #want"
html = interface.post_to_html(text)
self.assertEqual("<strong class='hashtag'>#whatever</strong> you <strong class='hashtag'>#want</strong>", html)
def test_post_add(self):
"""Test adding new posts"""
usernick = 'Bean'
message = "one two three"
postid = interface.post_add(self.db, usernick, message)
# result should be an integer
self.assertEqual(int, type(postid))
# get all posts for this user (should be just one)
posts = interface.post_list(self.db, usernick=usernick)
self.assertEquals(1, len(posts))
# check it's the same
self.assertEquals(postid, posts[0][0])
def test_post_add_length(self):
"""Posting a long message should fail"""
usernick = 'Bean'
longmessage = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
result = interface.post_add(self.db, usernick, longmessage)
self.assertEqual(None, result)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()