def test_edit_post_tags(self): user = self.create_user(commit=False) post = self.create_post(user, commit=False) post.tags = [Tag('yellow'), Tag('brown')] db.session.commit() self.edit_post(post=post, tags='yellow') self.assertEqual(len(post.tags), 1) self.edit_post(post=post, tags='') self.assertEqual(len(post.tags), 0)
def test_tag_posts(self): tag = Tag('blue') db.session.add(tag) post = self.create_post(self.create_user(), tags=[tag], status=PostStatus.published) self.assert_page(url_for('blog.tag_posts', slug=tag.slug), 'blog/tag_posts.html') posts = self.get_context_variable('posts').items self.assertEqual(len(posts), 1) self.assertEqual(posts[0], post)
def test_tag_list(self): """ Also tests that tags without published posts are not included. """ tag = Tag('blue') db.session.add(tag) self.create_post(self.create_user(), tags=[tag], status=PostStatus.published) url = url_for('blog.tag_list') self.assert_page(url, 'blog/tag_list.html') tags = self.get_context_variable('tags').items self.assertEqual(len(tags), 1) self.assertEqual(tags[0], tag)
def test_comma_separated_tag_value_serialized_to_tag_objects(self): original_green_tag = Tag('green') db.session.add(original_green_tag) db.session.commit() self.form_data['tags'] = 'green, red, red, blue' form = PostForm(self.form_data) self.assertTrue(form.validate()) self.assertEqual(len(form.tags.data), 3) tags = form.tags.data self.assertEqual(tags[0], original_green_tag) self.assertEqual(tags[1].id, None) self.assertEqual(tags[2].id, None)
def process_formdata(self, valuelist): if valuelist and valuelist[0]: # Order is preserved here but ``Post.tags`` orders by tag name seen = set() tag_names = [] for value in valuelist[0].split(','): stripped_value = value.strip() if stripped_value and stripped_value not in seen: tag_names.append(stripped_value) seen.add(stripped_value) existing_tags = Tag.query.filter(Tag.name.in_(tag_names)) existing_tag_names = {t.name: t for t in existing_tags} tags = [] for tag_name in tag_names: try: tags.append(existing_tag_names[tag_name]) except KeyError: tags.append(Tag(tag_name)) self.data = tags else: self.data = []
def test_slug_generated_from_name(self): tag = Tag('train spotting') self.assertEqual(tag.slug, 'train-spotting')
def test_tags(self): post = self.create_post(tags=[Tag('boeing'), Tag('airbus')]) self.assertEqual(len(post.tags), 2) self.assertEqual(Tag.query.count(), 2)
def test_url(self): tag = Tag('train spotting') self.assertEqual(tag.url, '/blog/tags/train-spotting')
def test_post_tags_converted_to_comma_separated_string(self): post = Post(tags=[Tag('green'), Tag('blue')]) form = PostForm(obj=post) self.assertEqual(form.tags._value(), 'green, blue')
def test_tag_posts_for_tag_without_posts_404s(self): tag = Tag('blue') db.session.add(tag) db.session.commit() url = url_for('blog.tag_posts', slug=tag.slug) self.assert_404(self.client.get(url))