/src/blogging/tests.py

https://github.com/nvbn/djang0byte · Python · 366 lines · 330 code · 20 blank · 16 comment · 19 complexity · d4078179a9fd432fed09305a4843e327 MD5 · raw file

  1. from django.test import TestCase
  2. from django.contrib.auth.models import User
  3. from tools.exceptions import (
  4. AlreadyRatedError, InvalidRateSignError,
  5. RateDisabledError, AlreadyRemovedError,
  6. NotRemovedError,
  7. )
  8. from blogging.models import (
  9. Blog, Post, Quiz, Answer, Comment,
  10. Section,
  11. )
  12. from blogging.exceptions import (
  13. AlreadySubscribedError, NotSubscribedError,
  14. AlreadyStarredError, NotStarredError,
  15. AlreadyAnsweredError, NotSeveralQuizError,
  16. AlreadyIgnoredError, VoteForIgnoredError,
  17. IgnoreForVotedError, NotQuestionError,
  18. SolutionAlreadyExistError, SoulutionDoesNotExistError,
  19. WrongSolutionHolderError,
  20. )
  21. from blogging.forms import (
  22. PostForm, PostOptionsForm, CommentForm,
  23. BlogForm,
  24. )
  25. from accounts.middleware import _thread_locals
  26. class ModelsTest(TestCase):
  27. def setUp(self):
  28. self.root = User.objects.create(
  29. username='root',
  30. is_staff=True,
  31. is_superuser=True,
  32. )
  33. def test_rating(self):
  34. """Check ratins"""
  35. blog = Blog.objects.create(
  36. name='blog', description='description',
  37. author=self.root,
  38. )
  39. self.assertEqual(blog.is_rated(self.root), False)
  40. with self.assertRaises(InvalidRateSignError):
  41. blog.set_rate(-15, self.root)
  42. blog.set_rate(+1, self.root)
  43. self.assertEqual(blog.is_rated(self.root), True)
  44. with self.assertRaises(AlreadyRatedError):
  45. blog.set_rate(-1, self.root)
  46. blog2 = Blog.objects.create(
  47. name='blog', description='description',
  48. author=self.root, is_rate_enabled=False,
  49. )
  50. with self.assertRaises(RateDisabledError):
  51. blog2.set_rate(-1, self.root)
  52. def test_removing(self):
  53. """Check removing"""
  54. post = Post.objects.create(
  55. title='asd', preview='fsd',
  56. content='esd', author=self.root,
  57. )
  58. self.assertEqual(post.is_removed, False)
  59. post.remove()
  60. self.assertEqual(post.is_removed, True)
  61. with self.assertRaises(AlreadyRemovedError):
  62. post.remove()
  63. def test_restore(self):
  64. """Check restoring"""
  65. post = Post.objects.create(
  66. title='asd', preview='fsd',
  67. content='esd', author=self.root,
  68. )
  69. with self.assertRaises(NotRemovedError):
  70. post.restore()
  71. post.remove()
  72. self.assertEqual(post.is_removed, True)
  73. post.restore()
  74. self.assertEqual(post.is_removed, False)
  75. def test_subscriptions(self):
  76. """Check subscriptions"""
  77. post = Post.objects.create(
  78. title='asd', preview='fsd',
  79. content='esd', author=self.root,
  80. )
  81. self.assertEqual(post.is_subscribed(self.root), False)
  82. post.subscribe(self.root)
  83. self.assertEqual(post.is_subscribed(self.root), True)
  84. with self.assertRaises(AlreadySubscribedError):
  85. post.subscribe(self.root)
  86. post.unsubscribe(self.root)
  87. self.assertEqual(post.is_subscribed(self.root), False)
  88. with self.assertRaises(NotSubscribedError):
  89. post.unsubscribe(self.root)
  90. def test_stars(self):
  91. """Check stars"""
  92. post = Post.objects.create(
  93. title='asd', preview='fsd',
  94. content='esd', author=self.root,
  95. )
  96. self.assertEqual(post.is_starred(self.root), False)
  97. post.star(self.root)
  98. self.assertEqual(post.is_starred(self.root), True)
  99. with self.assertRaises(AlreadyStarredError):
  100. post.star(self.root)
  101. post.unstar(self.root)
  102. self.assertEqual(post.is_starred(self.root), False)
  103. with self.assertRaises(NotStarredError):
  104. post.unstar(self.root)
  105. def test_vote_quiz(self):
  106. """Test vote for quiz"""
  107. post = Post.objects.create(
  108. title='asd', preview='fsd',
  109. content='esd', author=self.root,
  110. )
  111. quiz = Quiz.objects.create(
  112. name='qqqq', is_several=False,
  113. post=post,
  114. )
  115. answer = Answer.objects.create(
  116. name='answer', quiz=quiz,
  117. )
  118. answer1 = Answer.objects.create(
  119. name='answer1', quiz=quiz,
  120. )
  121. self.assertEqual(quiz.is_voted(self.root), False)
  122. with self.assertRaises(NotSeveralQuizError):
  123. quiz.vote(self.root, [answer, answer1])
  124. quiz.vote(self.root, answer)
  125. self.assertEqual(quiz.is_voted(self.root), True)
  126. with self.assertRaises(AlreadyAnsweredError):
  127. quiz.vote(self.root, answer1)
  128. with self.assertRaises(IgnoreForVotedError):
  129. quiz.ignore(self.root)
  130. def test_ignore_quiz(self):
  131. """Test ignore for quiz"""
  132. post = Post.objects.create(
  133. title='asd', preview='fsd',
  134. content='esd', author=self.root,
  135. )
  136. quiz = Quiz.objects.create(
  137. name='qqqq', is_several=False,
  138. post=post,
  139. )
  140. answer = Answer.objects.create(
  141. name='answer', quiz=quiz,
  142. )
  143. answer1 = Answer.objects.create(
  144. name='answer1', quiz=quiz,
  145. )
  146. self.assertEqual(quiz.is_ignored(self.root), False)
  147. quiz.ignore(self.root)
  148. self.assertEqual(quiz.is_ignored(self.root), True)
  149. with self.assertRaises(AlreadyIgnoredError):
  150. quiz.ignore(self.root)
  151. with self.assertRaises(VoteForIgnoredError):
  152. quiz.vote(self.root, answer)
  153. def test_solution(self):
  154. """Test question sulution"""
  155. post = Post.objects.create(
  156. title='asd', preview='fsd',
  157. content='esd', author=self.root,
  158. )
  159. comment = Comment.objects.create(
  160. post=post, author=self.root,
  161. content='asdfg',
  162. )
  163. with self.assertRaises(NotQuestionError):
  164. post.set_solution(comment)
  165. post.type = Post.TYPE_QUESTION
  166. with self.assertRaises(SoulutionDoesNotExistError):
  167. post.get_solution()
  168. self.assertEqual(post.has_solution(), False)
  169. post.set_solution(comment)
  170. self.assertEqual(post.has_solution(), True)
  171. self.assertEqual(post.get_solution(), comment)
  172. comment2 = Comment.objects.create(
  173. post=post, author=self.root,
  174. content='asdfg',
  175. )
  176. with self.assertRaises(SolutionAlreadyExistError):
  177. post.set_solution(comment2)
  178. post2 = Post.objects.create(
  179. title='asd', preview='fsd', author=self.root,
  180. content='esd', type=Post.TYPE_QUESTION,
  181. )
  182. with self.assertRaises(WrongSolutionHolderError):
  183. post2.set_solution(comment2)
  184. def test_remove_solution(self):
  185. """Test remove solution"""
  186. post = Post.objects.create(
  187. title='asd', preview='fsd', author=self.root,
  188. content='esd', type=Post.TYPE_QUESTION,
  189. )
  190. comment = Comment.objects.create(
  191. post=post, author=self.root,
  192. content='asdfg',
  193. )
  194. with self.assertRaises(SoulutionDoesNotExistError):
  195. post.unset_solution()
  196. post.set_solution(comment)
  197. self.assertEqual(post.has_solution(), True)
  198. post.unset_solution()
  199. self.assertEqual(post.has_solution(), False)
  200. def test_posts_from_section(self):
  201. """Test posts from section"""
  202. blogs = map(lambda i: Blog.objects.create(
  203. name=str(i), author=self.root,
  204. ), range(10))
  205. posts = map(lambda i: Post.objects.create(
  206. title=str(i), preview='fsd', author=self.root,
  207. content='esd', blog=blogs[i],
  208. ), range(10))
  209. section_1 = Section.objects.create(
  210. name='1',
  211. )
  212. section_1.included_blogs.add(blogs[0])
  213. section_1.included_blogs.add(blogs[1])
  214. self.assertEqual(
  215. list(section_1.get_posts()),
  216. [posts[1], posts[0]],
  217. )
  218. section_2 = Section.objects.create(
  219. name='2',
  220. )
  221. section_2.excluded_blogs.add(blogs[2])
  222. section_2.excluded_blogs.add(blogs[3])
  223. self.assertEqual(
  224. list(section_2.get_posts()),
  225. [
  226. posts[9], posts[8], posts[7], posts[6],
  227. posts[5], posts[4], posts[1], posts[0],
  228. ],
  229. )
  230. section_3 = Section.objects.create(
  231. name='3',
  232. )
  233. section_3.included_blogs.add(blogs[5])
  234. section_3.included_blogs.add(blogs[6])
  235. section_3.excluded_blogs.add(blogs[6])
  236. self.assertEqual(
  237. list(section_3.get_posts()),
  238. [posts[5]],
  239. )
  240. class FormsTest(TestCase):
  241. def setUp(self):
  242. self.root = User.objects.create(
  243. username='root',
  244. is_staff=True,
  245. is_superuser=True,
  246. )
  247. _thread_locals.user = self.root
  248. def test_post_creating(self):
  249. """Test post creating"""
  250. form = PostForm({
  251. 'title': 'ok',
  252. 'content': 'ok',
  253. 'type': Post.TYPE_POST,
  254. })
  255. self.assertEqual(form.is_valid(), True)
  256. post = form.save()
  257. self.assertEqual(post.title, 'ok')
  258. form = PostForm({
  259. 'title': 'ok',
  260. 'content': 'ok',
  261. 'type': Post.TYPE_LINK,
  262. })
  263. self.assertEqual(form.is_valid(), False)
  264. form = PostForm({
  265. 'title': 'ok',
  266. 'content': 'ok',
  267. 'type': Post.TYPE_LINK,
  268. 'related_url': 'http://welinux.ru/',
  269. })
  270. form.is_valid()
  271. self.assertEqual(form.is_valid(), True)
  272. def test_post_updating(self):
  273. """Test post updating"""
  274. post = Post.objects.create(
  275. title='asd', preview='fsd',
  276. content='esd', author=self.root,
  277. )
  278. form = PostForm({
  279. 'title': '12345',
  280. 'content': 'ok',
  281. 'type': Post.TYPE_POST,
  282. }, instance=post)
  283. form.is_valid()
  284. self.assertEqual(form.is_valid(), True)
  285. post = form.save()
  286. self.assertEqual(post.title, '12345')
  287. def test_comment_create(self):
  288. """Test comment creating"""
  289. post = Post.objects.create(
  290. title='asd', preview='fsd',
  291. content='esd', author=self.root,
  292. )
  293. form = CommentForm({
  294. 'content': 'okok',
  295. 'post': post.id,
  296. })
  297. self.assertEqual(form.is_valid(), True)
  298. comment = form.save()
  299. self.assertEqual(comment.content, 'okok')
  300. form = CommentForm({
  301. 'content': 'eee',
  302. 'parent': comment.id,
  303. 'post': post.id,
  304. })
  305. self.assertEqual(form.is_valid(), True)
  306. child = form.save()
  307. self.assertEqual(child.parent.id, comment.id)
  308. def test_comment_update(self):
  309. """Test comment updating"""
  310. post = Post.objects.create(
  311. title='asd', preview='fsd',
  312. content='esd', author=self.root,
  313. )
  314. comment = Comment.objects.create(
  315. content='okok', post=post,
  316. author=self.root,
  317. )
  318. form = CommentForm({
  319. 'content': '12345',
  320. 'post': post.id,
  321. }, instance=comment)
  322. self.assertEqual(form.is_valid(), True)
  323. comment = form.save()
  324. self.assertEqual(comment.content, '12345')
  325. def test_blog_create(self):
  326. """Test blog creating"""
  327. form = BlogForm({
  328. 'name': 'okok',
  329. 'description': 'yeee',
  330. })
  331. self.assertEqual(form.is_valid(), True)
  332. blog = form.save()
  333. self.assertEqual(blog.name, 'okok')
  334. def test_blog_updating(self):
  335. """Test blog updating"""
  336. blog = Blog.objects.create(
  337. author=self.root, name='okok',
  338. description='yeeee',
  339. )
  340. form = BlogForm({
  341. 'name': '12345',
  342. 'description': 'yeee',
  343. }, instance=blog)
  344. self.assertEqual(form.is_valid(), True)
  345. blog = form.save()
  346. self.assertEqual(blog.name, '12345')