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
|
"""utility functions used by Askbot test cases
"""
from django.test import TestCase
from functools import wraps
from askbot import models
def create_user(
username = None,
email = None,
notification_schedule = None,
date_joined = None,
status = 'a',
reputation = 1
):
"""Creates a user and sets default update subscription
settings
``notification_schedule`` is a dictionary with keys
the same as in keys in
:attr:`~askbot.models.EmailFeedSetting.FEED_TYPES`:
* 'q_ask' - questions that user asks
* 'q_all' - enture forum, tag filtered
* 'q_ans' - questions that user answers
* 'q_sel' - questions that user decides to follow
* 'm_and_c' - comments and mentions of user anywhere
and values as keys in
:attr:`~askbot.models.EmailFeedSetting.FEED_TYPES`:
* 'i' - instantly
* 'd' - daily
* 'w' - weekly
* 'n' - never
"""
user = models.User.objects.create_user(username, email)
user.reputation = reputation
if date_joined is not None:
user.date_joined = date_joined
user.save()
user.set_status(status)
if notification_schedule == None:
notification_schedule = models.EmailFeedSetting.NO_EMAIL_SCHEDULE
#a hack, we need to delete these, that will be created automatically
#because just below we will be replacing them with the new values
user.notification_subscriptions.all().delete()
for feed_type, frequency in notification_schedule.items():
feed = models.EmailFeedSetting(
feed_type = feed_type,
frequency = frequency,
subscriber = user
)
feed.save()
return user
class AskbotTestCase(TestCase):
"""adds some askbot-specific methods
to django TestCase class
"""
def _fixture_setup(self):
# HACK: Create askbot_post database VIEW for the purpose of performing tests
import os.path
from django.conf import settings
from django.db import connection
sql = open(os.path.join(settings.PROJECT_ROOT, 'askbot', 'models', 'post_view.sql'), 'rt').read()
cursor = connection.cursor()
cursor.execute(sql)
super(AskbotTestCase, self)._fixture_setup()
def _fixture_teardown(self):
super(AskbotTestCase, self)._fixture_teardown()
from django.db import connection
cursor = connection.cursor()
cursor.execute('DROP VIEW IF EXISTS askbot_post')
def create_user(
self,
username = 'user',
email = None,
notification_schedule = None,
date_joined = None,
status = 'a'
):
"""creates user with username, etc and
makes the result accessible as
self.<username>
newly created user object is also returned
"""
assert(username is not None)
assert(not hasattr(self, username))
if email is None:
email = username + '@example.com'
user_object = create_user(
username = username,
email = email,
notification_schedule = notification_schedule,
date_joined = date_joined,
status = status
)
setattr(self, username, user_object)
return user_object
def assertRaisesRegexp(self, *args, **kwargs):
"""a shim for python < 2.7"""
try:
#run assertRaisesRegex, if available
super(AskbotTestCase, self).assertRaisesRegexp(*args, **kwargs)
except AttributeError:
#in this case lose testing for the error text
#second argument is the regex that is supposed
#to match the error text
args_list = list(args)#conv tuple to list
args_list.pop(1)#so we can remove an item
self.assertRaises(*args_list, **kwargs)
def post_question(
self,
user = None,
title = 'test question title',
body_text = 'test question body text',
tags = 'test',
wiki = False,
is_anonymous = False,
follow = False,
timestamp = None
):
"""posts and returns question on behalf
of user. If user is not given, it will be self.user
``tags`` is a string with tagnames
if follow is True, question is followed by the poster
"""
if user is None:
user = self.user
question = user.post_question(
title = title,
body_text = body_text,
tags = tags,
wiki = wiki,
is_anonymous = is_anonymous,
timestamp = timestamp
)
if follow:
user.follow_question(question)
return question
def reload_object(self, obj):
"""reloads model object from the database
"""
return obj.__class__.objects.get(id = obj.id)
def post_answer(
self,
user = None,
question = None,
body_text = 'test answer text',
follow = False,
wiki = False,
timestamp = None
):
if user is None:
user = self.user
return user.post_answer(
question = question,
body_text = body_text,
follow = follow,
wiki = wiki,
timestamp = timestamp
)
def post_comment(
self,
user = None,
parent_post = None,
body_text = 'test comment text',
timestamp = None
):
"""posts and returns a comment to parent post, uses
now timestamp if not given, dummy body_text
author is required
"""
if user is None:
user = self.user
comment = user.post_comment(
parent_post = parent_post,
body_text = body_text,
timestamp = timestamp,
)
return comment
"""
Some test decorators, taken from Django-1.3
"""
class SkipTest(Exception):
"""
Raise this exception in a test to skip it.
Usually you can use TestResult.skip() or one of the skipping decorators
instead of raising this directly.
"""
def _id(obj):
return obj
def skip(reason):
"""
Unconditionally skip a test.
"""
def decorator(test_item):
if not (isinstance(test_item, type) and issubclass(test_item, TestCase)):
@wraps(test_item)
def skip_wrapper(*args, **kwargs):
raise SkipTest(reason)
test_item = skip_wrapper
test_item.__unittest_skip__ = True
test_item.__unittest_skip_why__ = reason
return test_item
return decorator
def skipIf(condition, reason):
"""
Skip a test if the condition is true.
"""
if condition:
return skip(reason)
return _id
|