summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Zielinski <tomasz.zielinski@pyconsultant.eu>2011-11-09 20:37:12 +0100
committerTomasz Zielinski <tomasz.zielinski@pyconsultant.eu>2011-11-09 20:37:12 +0100
commit30d8e5e3e7b4bdb7e6ef57ae059c0551ba37544e (patch)
tree8f9b01d48db7992bfab94823eeb77c647524727c
parentda93e5dde9b177bc361053c51caa20e15824d530 (diff)
downloadaskbot-30d8e5e3e7b4bdb7e6ef57ae059c0551ba37544e.tar.gz
askbot-30d8e5e3e7b4bdb7e6ef57ae059c0551ba37544e.tar.bz2
askbot-30d8e5e3e7b4bdb7e6ef57ae059c0551ba37544e.zip
Added test for the new wikipost models (only PostRevision at the moment)
-rw-r--r--askbot/models/post.py24
-rw-r--r--askbot/tests/__init__.py1
-rw-r--r--askbot/tests/post_model_tests.py55
3 files changed, 73 insertions, 7 deletions
diff --git a/askbot/models/post.py b/askbot/models/post.py
index c61e8877..d5c0c6df 100644
--- a/askbot/models/post.py
+++ b/askbot/models/post.py
@@ -1,4 +1,5 @@
from django.db import models
+from django.core.exceptions import ValidationError
from askbot.utils import markup
from askbot.utils.html import sanitize_html
@@ -7,13 +8,16 @@ from askbot.utils.html import sanitize_html
# pass
class PostRevisionManager(models.Manager):
+ def create(self, *kargs, **kwargs):
+ raise NotImplementedError # Prevent accidental creation of PostRevision instance without `revision_type` set
+
def create_question_revision(self, *kargs, **kwargs):
kwargs['revision_type'] = self.model.QUESTION_REVISION
- return self.create(*kargs, **kwargs)
+ return super(PostRevisionManager, self).create(*kargs, **kwargs)
def create_answer_revision(self, *kargs, **kwargs):
kwargs['revision_type'] = self.model.ANSWER_REVISION
- return self.create(*kargs, **kwargs)
+ return super(PostRevisionManager, self).create(*kargs, **kwargs)
def question_revisions(self):
return self.filter(revision_type=self.model.QUESTION_REVISION)
@@ -74,15 +78,21 @@ class PostRevision(models.Model):
elif self.is_answer_revision():
return self.answer
- def save(self, **kwargs):
- """Determines the revistion type and then looks up the next available revision number if not set."""
+ def clean(self):
+ "Internal cleaning method, called from self.save() by self.full_clean()"
+ if bool(self.question) == bool(self.answer): # one and only one has to be set (!xor)
+ raise ValidationError('One (and only one) of question/answer fields has to be set.')
+ if (self.question and not self.is_question_revision()) or (self.answer and not self.is_answer_revision()):
+ raise ValidationError('Revision_type doesn`t match values in question/answer fields.')
+ def save(self, **kwargs):
+ # Determine the revision number, if not set
if not self.revision:
- # TODO: Maybe use Max() aggregation?
- # TODO: Handle IntegrityError if revision id is already occupied?
+ # TODO: Maybe use Max() aggregation? Or `revisions.count() + 1`
self.revision = self.parent().revisions.values_list('revision', flat=True)[0] + 1
- self.full_clean() # Make sure that everything is ok, in particular that `revision_type` and `revision` are set to valid values
+ # Make sure that everything is ok, in particular that `revision_type` and `revision` are set to valid values
+ self.full_clean()
super(PostRevision, self).save(**kwargs)
diff --git a/askbot/tests/__init__.py b/askbot/tests/__init__.py
index 76e32e41..a9fa761b 100644
--- a/askbot/tests/__init__.py
+++ b/askbot/tests/__init__.py
@@ -12,3 +12,4 @@ from askbot.tests.follow_tests import *
from askbot.tests.templatefilter_tests import *
from askbot.tests.markup_test import *
from askbot.tests.misc_tests import *
+from askbot.tests.post_model_tests import *
diff --git a/askbot/tests/post_model_tests.py b/askbot/tests/post_model_tests.py
new file mode 100644
index 00000000..7ea8621a
--- /dev/null
+++ b/askbot/tests/post_model_tests.py
@@ -0,0 +1,55 @@
+import datetime
+
+from django.core.exceptions import ValidationError
+from askbot.tests.utils import AskbotTestCase
+from askbot.models import PostRevision
+
+
+class PostModelTests(AskbotTestCase):
+
+ def setUp(self):
+ self.u1 = self.create_user(username='user1')
+ self.u2 = self.create_user(username='user2')
+ self.u3 = self.create_user(username='user3')
+
+ def test_model_validation(self):
+ with self.assertRaises(NotImplementedError):
+ PostRevision.objects.create(text='blah', author=self.u1, revised_at=datetime.datetime.now(), revision_type=PostRevision.QUESTION_REVISION)
+
+ with self.assertRaisesRegexp(AttributeError, r"'NoneType' object has no attribute 'revisions'"):
+ # cannot set `revision` without a parent
+ PostRevision.objects.create_answer_revision(text='blah', author=self.u1, revised_at=datetime.datetime.now())
+
+ with self.assertRaisesRegexp(ValidationError, r"{'__all__': \[u'One \(and only one\) of question/answer fields has to be set.'\], 'revision_type': \[u'Value 4 is not a valid choice.'\]}"):
+ # revision_type not in (1,2)
+ PostRevision(text='blah', author=self.u1, revised_at=datetime.datetime.now(), revision=1, revision_type=4).save()
+
+ question = self.post_question(user=self.u1)
+
+ rev2 = PostRevision(question=question, text='blah', author=self.u1, revised_at=datetime.datetime.now(), revision=2, revision_type=PostRevision.QUESTION_REVISION)
+ rev2.save()
+ self.assertIsNotNone(rev2.id)
+
+ with self.assertRaisesRegexp(ValidationError, r"{'__all__': \[u'Revision_type doesn`t match values in question/answer fields.', u'Post revision with this Question and Revision already exists.'\]}"):
+ PostRevision(question=question, text='blah', author=self.u1, revised_at=datetime.datetime.now(), revision=2, revision_type=PostRevision.ANSWER_REVISION).save()
+
+ with self.assertRaisesRegexp(ValidationError, r"{'__all__': \[u'Revision_type doesn`t match values in question/answer fields.'\]}"):
+ PostRevision(question=question, text='blah', author=self.u1, revised_at=datetime.datetime.now(), revision=3, revision_type=PostRevision.ANSWER_REVISION).save()
+
+ rev3 = PostRevision.objects.create_question_revision(question=question, text='blah', author=self.u1, revised_at=datetime.datetime.now(), revision_type=123) # revision_type
+ self.assertIsNotNone(rev3.id)
+ self.assertEqual(3, rev3.revision) # By the way: let's test the auto-increase of revision number
+ self.assertEqual(PostRevision.QUESTION_REVISION, rev3.revision_type)
+
+ def test_post_revision_autoincrease(self):
+ question = self.post_question(user=self.u1)
+ self.assertEqual(1, question.revisions.all()[0].revision)
+ self.assertEqual(1, question.revisions.count())
+
+ question.apply_edit(edited_by=self.u1, text="blah2", comment="blahc2")
+ self.assertEqual(2, question.revisions.all()[0].revision)
+ self.assertEqual(2, question.revisions.count())
+
+ question.apply_edit(edited_by=self.u1, text="blah3", comment="blahc3")
+ self.assertEqual(3, question.revisions.all()[0].revision)
+ self.assertEqual(3, question.revisions.count())