def create_topic(author, forum, title, subtitle, text, key): """create topic in forum""" (tags, title_only) = get_tag_by_title(title[:80]) # Creating the thread n_topic = Topic() n_topic.forum = forum n_topic.title = title_only n_topic.subtitle = subtitle n_topic.pubdate = datetime.now() n_topic.author = author n_topic.key = key n_topic.save() n_topic.add_tags(tags) n_topic.save() # Add the first message post = Post() post.topic = n_topic post.author = author post.text = text post.text_html = emarkdown(text) post.pubdate = datetime.now() post.position = 1 post.save() n_topic.last_message = post n_topic.save() follow(n_topic, user=author) return n_topic
def test_add_tag(self): TagCSharp = TagFactory(title="C#") TagC = TagFactory(title="C") self.assertEqual(TagCSharp.slug, TagC.slug) self.assertNotEqual(TagCSharp.title, TagC.title) #post a topic with a tag result = self.client.post(reverse( 'zds.forum.views.new' ) + '?forum={0}'.format(self.forum12.pk), { 'title': u'[C#]Un autre sujet', 'subtitle': u'Encore ces lombards en plein ete', 'text': u'C\'est tout simplement l\'histoire de la ville de Paris que je voudrais vous conter ' }, follow=False) self.assertEqual(result.status_code, 302) #test the topic is added to the good tag self.assertEqual( Topic.objects.filter(tags__in=[TagCSharp]).order_by( "-last_message__pubdate").prefetch_related("tags").count(), 1) self.assertEqual( Topic.objects.filter(tags__in=[TagC]).order_by( "-last_message__pubdate").prefetch_related("tags").count(), 0) topicWithConflictTags = TopicFactory(forum=self.forum11, author=self.user) topicWithConflictTags.title = u"[C][c][ c][C ]name" (tags, title) = get_tag_by_title(topicWithConflictTags.title) topicWithConflictTags.add_tags(tags) self.assertEqual(topicWithConflictTags.tags.all().count(), 1) topicWithConflictTags = TopicFactory(forum=self.forum11, author=self.user) topicWithConflictTags.title = u"[][ ][ ]name" (tags, title) = get_tag_by_title(topicWithConflictTags.title) topicWithConflictTags.add_tags(tags) self.assertEqual(topicWithConflictTags.tags.all().count(), 0)
def test_tag_parsing(self): """test the tag parsing in nominal, limit and borns cases""" (tags, title) = get_tag_by_title("[tag]title") self.assertEqual(len(tags), 1) self.assertEqual(title, "title") (tags, title) = get_tag_by_title("[[tag1][tag2]]title") self.assertEqual(len(tags), 1) self.assertEqual(tags[0], "[tag1][tag2]") self.assertEqual(title, "title") (tags, title) = get_tag_by_title("[tag1][tag2]title") self.assertEqual(len(tags), 2) self.assertEqual(tags[0], "tag1") self.assertEqual(title, "title") (tags, title) = get_tag_by_title("[tag1] [tag2]title") self.assertEqual(len(tags), 2) self.assertEqual(tags[0], "tag1") self.assertEqual(title, "title") (tags, title) = get_tag_by_title("[tag1][tag2]title[tag3]") self.assertEqual(len(tags), 2) self.assertEqual(tags[0], "tag1") self.assertEqual(title, "title[tag3]") (tags, title) = get_tag_by_title("[tag1[][tag2]title") self.assertEqual(len(tags), 0) self.assertEqual(title, "[tag1[][tag2]title")
def test_tag_parsing(self): """test the tag parsing in nominal, limit and borns cases""" (tags, title) = get_tag_by_title("[tag]title"); self.assertEqual(len(tags),1) self.assertEqual(title,"title") (tags,title) = get_tag_by_title("[[tag1][tag2]]title") self.assertEqual(len(tags),1) self.assertEqual(tags[0],"[tag1][tag2]") self.assertEqual(title,"title") (tags,title) = get_tag_by_title("[tag1][tag2]title") self.assertEqual(len(tags),2) self.assertEqual(tags[0],"tag1") self.assertEqual(title,"title") (tags,title) = get_tag_by_title("[tag1] [tag2]title") self.assertEqual(len(tags),2) self.assertEqual(tags[0],"tag1") self.assertEqual(title,"title") (tags,title) = get_tag_by_title("[tag1][tag2]title[tag3]") self.assertEqual(len(tags),2) self.assertEqual(tags[0],"tag1") self.assertEqual(title,"title[tag3]") (tags,title) = get_tag_by_title("[tag1[][tag2]title") self.assertEqual(len(tags),0) self.assertEqual(title,"[tag1[][tag2]title")
def test_add_tag(self): TagCSharp = TagFactory(title="C#") TagC = TagFactory(title="C") self.assertEqual(TagCSharp.slug, TagC.slug) self.assertNotEqual(TagCSharp.title, TagC.title) #post a topic with a tag result = self.client.post( reverse('zds.forum.views.new') + '?forum={0}' .format(self.forum12.pk), {'title': u'[C#]Un autre sujet', 'subtitle': u'Encore ces lombards en plein ete', 'text': u'C\'est tout simplement l\'histoire de la ville de Paris que je voudrais vous conter ' }, follow=False) self.assertEqual(result.status_code, 302) #test the topic is added to the good tag self.assertEqual( Topic.objects.filter( tags__in=[TagCSharp]) .order_by("-last_message__pubdate").prefetch_related( "tags").count(), 1) self.assertEqual( Topic.objects.filter(tags__in=[TagC]) .order_by("-last_message__pubdate").prefetch_related( "tags").count(), 0) topicWithConflictTags = TopicFactory( forum=self.forum11, author=self.user) topicWithConflictTags.title = u"[C][c][ c][C ]name" (tags, title) = get_tag_by_title(topicWithConflictTags.title) topicWithConflictTags.add_tags(tags) self.assertEqual(topicWithConflictTags.tags.all().count(), 1) topicWithConflictTags = TopicFactory( forum=self.forum11, author=self.user) topicWithConflictTags.title = u"[][ ][ ]name" (tags, title) = get_tag_by_title(topicWithConflictTags.title) topicWithConflictTags.add_tags(tags) self.assertEqual(topicWithConflictTags.tags.all().count(), 0)