PageRenderTime 88ms CodeModel.GetById 58ms RepoModel.GetById 1ms app.codeStats 0ms

/askbot/tests/utils.py

https://github.com/ragsagar/askbot-devel
Python | 236 lines | 226 code | 1 blank | 9 comment | 0 complexity | 1bb6f3c670631e5f2a5a5c59680c5b8c MD5 | raw file
  1. """utility functions used by Askbot test cases
  2. """
  3. from django.test import TestCase
  4. from functools import wraps
  5. from askbot import models
  6. def create_user(
  7. username = None,
  8. email = None,
  9. notification_schedule = None,
  10. date_joined = None,
  11. status = 'a',
  12. reputation = 1
  13. ):
  14. """Creates a user and sets default update subscription
  15. settings
  16. ``notification_schedule`` is a dictionary with keys
  17. the same as in keys in
  18. :attr:`~askbot.models.EmailFeedSetting.FEED_TYPES`:
  19. * 'q_ask' - questions that user asks
  20. * 'q_all' - enture forum, tag filtered
  21. * 'q_ans' - questions that user answers
  22. * 'q_sel' - questions that user decides to follow
  23. * 'm_and_c' - comments and mentions of user anywhere
  24. and values as keys in
  25. :attr:`~askbot.models.EmailFeedSetting.FEED_TYPES`:
  26. * 'i' - instantly
  27. * 'd' - daily
  28. * 'w' - weekly
  29. * 'n' - never
  30. """
  31. user = models.User.objects.create_user(username, email)
  32. user.reputation = reputation
  33. if date_joined is not None:
  34. user.date_joined = date_joined
  35. user.save()
  36. user.set_status(status)
  37. if notification_schedule == None:
  38. notification_schedule = models.EmailFeedSetting.NO_EMAIL_SCHEDULE
  39. #a hack, we need to delete these, that will be created automatically
  40. #because just below we will be replacing them with the new values
  41. user.notification_subscriptions.all().delete()
  42. for feed_type, frequency in notification_schedule.items():
  43. feed = models.EmailFeedSetting(
  44. feed_type = feed_type,
  45. frequency = frequency,
  46. subscriber = user
  47. )
  48. feed.save()
  49. return user
  50. class AskbotTestCase(TestCase):
  51. """adds some askbot-specific methods
  52. to django TestCase class
  53. """
  54. def create_user(
  55. self,
  56. username = 'user',
  57. email = None,
  58. notification_schedule = None,
  59. date_joined = None,
  60. status = 'a'
  61. ):
  62. """creates user with username, etc and
  63. makes the result accessible as
  64. self.<username>
  65. newly created user object is also returned
  66. """
  67. assert(username is not None)
  68. assert(not hasattr(self, username))
  69. if email is None:
  70. email = username + '@example.com'
  71. user_object = create_user(
  72. username = username,
  73. email = email,
  74. notification_schedule = notification_schedule,
  75. date_joined = date_joined,
  76. status = status
  77. )
  78. setattr(self, username, user_object)
  79. return user_object
  80. def assertRaisesRegexp(self, *args, **kwargs):
  81. """a shim for python < 2.7"""
  82. try:
  83. #run assertRaisesRegex, if available
  84. super(AskbotTestCase, self).assertRaisesRegexp(*args, **kwargs)
  85. except AttributeError:
  86. #in this case lose testing for the error text
  87. #second argument is the regex that is supposed
  88. #to match the error text
  89. args_list = list(args)#conv tuple to list
  90. args_list.pop(1)#so we can remove an item
  91. self.assertRaises(*args_list, **kwargs)
  92. def post_question(
  93. self,
  94. user = None,
  95. title = 'test question title',
  96. body_text = 'test question body text',
  97. tags = 'test',
  98. wiki = False,
  99. is_anonymous = False,
  100. follow = False,
  101. timestamp = None
  102. ):
  103. """posts and returns question on behalf
  104. of user. If user is not given, it will be self.user
  105. ``tags`` is a string with tagnames
  106. if follow is True, question is followed by the poster
  107. """
  108. if user is None:
  109. user = self.user
  110. question = user.post_question(
  111. title = title,
  112. body_text = body_text,
  113. tags = tags,
  114. wiki = wiki,
  115. is_anonymous = is_anonymous,
  116. timestamp = timestamp
  117. )
  118. if follow:
  119. user.follow_question(question)
  120. return question
  121. def reload_object(self, obj):
  122. """reloads model object from the database
  123. """
  124. return obj.__class__.objects.get(id = obj.id)
  125. def post_answer(
  126. self,
  127. user = None,
  128. question = None,
  129. body_text = 'test answer text',
  130. follow = False,
  131. wiki = False,
  132. timestamp = None
  133. ):
  134. if user is None:
  135. user = self.user
  136. return user.post_answer(
  137. question = question,
  138. body_text = body_text,
  139. follow = follow,
  140. wiki = wiki,
  141. timestamp = timestamp
  142. )
  143. def post_comment(
  144. self,
  145. user = None,
  146. parent_post = None,
  147. body_text = 'test comment text',
  148. timestamp = None
  149. ):
  150. """posts and returns a comment to parent post, uses
  151. now timestamp if not given, dummy body_text
  152. author is required
  153. """
  154. if user is None:
  155. user = self.user
  156. comment = user.post_comment(
  157. parent_post = parent_post,
  158. body_text = body_text,
  159. timestamp = timestamp,
  160. )
  161. return comment
  162. """
  163. Some test decorators, taken from Django-1.3
  164. """
  165. class SkipTest(Exception):
  166. """
  167. Raise this exception in a test to skip it.
  168. Usually you can use TestResult.skip() or one of the skipping decorators
  169. instead of raising this directly.
  170. """
  171. def _id(obj):
  172. return obj
  173. def skip(reason):
  174. """
  175. Unconditionally skip a test.
  176. """
  177. def decorator(test_item):
  178. if not (isinstance(test_item, type) and issubclass(test_item, TestCase)):
  179. @wraps(test_item)
  180. def skip_wrapper(*args, **kwargs):
  181. raise SkipTest(reason)
  182. test_item = skip_wrapper
  183. test_item.__unittest_skip__ = True
  184. test_item.__unittest_skip_why__ = reason
  185. return test_item
  186. return decorator
  187. def skipIf(condition, reason):
  188. """
  189. Skip a test if the condition is true.
  190. """
  191. if condition:
  192. return skip(reason)
  193. return _id