def test_post_creation(self): """Test if posts are created and saved properly to the database""" self.clear_db() title = "My post" markup = "# Title" tags = ['django','franz-und-bertha','vil-bil'] expected_slug = "my-post" expected_html = "<h1>Title</h1>" expected_date = datetime.date.today() post = Post(title=title, markup=markup) post.tags = tags db.session.add(post) db.session.commit() assert_equal(post.title, title) assert_equal(post.markup, markup) assert_true(post.comments_allowed) assert_true(post.visible) assert_equal(post.slug, expected_slug) assert expected_html in post.html assert_equal(post.datetime.date(), expected_date) assert_equal(sorted(tag.name for tag in post.tags), sorted(tags)) assert_equal([], post.categories) # Add another post db.session.add(Post(title=title, markup=markup, comments_allowed=False, visible=False)) db.session.commit() assert_false(Post.query.get(2).comments_allowed) assert_false(Post.query.get(2).visible) assert_equal(Post.query.count(), 2)
def test_post_creation(self): """Test if posts are created and saved properly to the database""" self.clear_db() title = "My post" markup = "# Title" tags = ['django', 'franz-und-bertha', 'vil-bil'] expected_slug = "my-post" expected_html = "<h1>Title</h1>" expected_date = datetime.date.today() post = Post(title=title, markup=markup) post.tags = tags db.session.add(post) db.session.commit() assert_equal(post.title, title) assert_equal(post.markup, markup) assert_true(post.comments_allowed) assert_true(post.visible) assert_equal(post.slug, expected_slug) assert expected_html in post.html assert_equal(post.datetime.date(), expected_date) assert_equal(sorted(tag.name for tag in post.tags), sorted(tags)) assert_equal([], post.categories) # Add another post db.session.add( Post(title=title, markup=markup, comments_allowed=False, visible=False)) db.session.commit() assert_false(Post.query.get(2).comments_allowed) assert_false(Post.query.get(2).visible) assert_equal(Post.query.count(), 2)
def test_view(self): """Test the displaying of the tag view""" self.clear_db() self.register_and_login('barney', 'abc') tag = Tag('drdre') db.session.add(tag) db.session.commit() post = Post('the chronic 2001', visible=True) post2 = Post('the chronic 2002', visible=False) post._tags = [tag] post2._tags = [tag] db.session.add(post) db.session.add(post2) db.session.commit() rv = self.client.get('/tag/drdre/') self.assert_200(rv) assert 'the chronic 2001' in rv.data rv = self.client.get('/tag/bobbybrown/') self.assert_404(rv) self.logout() rv = self.client.get('/tag/drdre/') self.assert_200(rv) assert 'the chronic 2001' in rv.data assert 'the chronic 2002' not in rv.data
def test_view(self): """Test the displaying of the category view""" self.clear_db() self.register_and_login('barney', 'abc') category = Category('drdre') db.session.add(category) db.session.commit() post = Post('the chronic', visible=True) post2 = Post('the chrinoc', visible=False) post._categories = [category] post2._categories = [category] db.session.add(post) db.session.add(post2) db.session.commit() rv = self.client.get('/category/drdre/') self.assert_200(rv) assert 'the chronic' in rv.data rv = self.client.get('/category/sugeknight/') self.assert_404(rv) self.logout() rv = self.client.get('/category/drdre/') self.assert_200(rv) assert 'the chronic' in rv.data assert 'the chrinoc' not in rv.data rv = self.client.get('/uncategorized/') self.assert_200(rv) assert 'Uncategorized posts' in rv.data post2 = Post('dancing in the moonlight') db.session.add(post2) db.session.commit() rv = self.client.get('/uncategorized/') self.assert_200(rv) assert 'dancing in the moonlight' in rv.data
def test_same_slug_after_updating(self): """Test if updating a post without changing the title does not result in a different slug (regression)""" self.clear_db() post = Post(title='t', markup='') db.session.add(post) db.session.commit() post.title = 't' db.session.commit() assert_equal(post.slug, 't')
def create_post(slug): """Create a new or edit an existing blog post""" next = request.values.get('next', '') post = None if slug: post = Post.query.filter_by(slug=slug).first() if slug and not post: flash('Invalid slug', 'error') return redirect(next) if request.method == 'GET': return render_template('admin/compose.html', post=post, categories=Category.query.all()) if request.method == 'POST': if request.form['action'] == 'Cancel': return redirect(next) title = request.form['title'] markup = request.form['markup'] tags = normalize_tags(request.form['tags']) comments_allowed = bool(request.values.get('comments_allowed', False)) visible = bool(request.values.get('visible', False)) #: Contains the ids of the categories categories = [] for name, id in request.form.iteritems(): if 'category-' in name: categories.append(id) if title == '': flash('You must provide a title', 'error') return render_template('admin/compose.html') elif request.form['action'] == 'Publish': post = Post(title, markup, comments_allowed, visible) post.tags = tags post.categories = categories db.session.add(post) db.session.commit() signals.post_created.send(post) flash('New post was successfully posted') return redirect(url_for('main.show_posts')) elif request.form['action'] == 'Update': post.title = title post.markup = markup post.comments_allowed = comments_allowed post.visible = visible post.tags = tags post.categories = categories db.session.commit() signals.post_updated.send(post) flash('Post was successfully updated') return redirect(url_for('main.show_post', slug=post.slug))
def test_tag_tidying(self): """Test if tags are automatically deleted when a post is deleted and there are no tag associations after that""" self.clear_db() db.session.add(Tag('cool')) db.session.add(Tag('cooler')) db.session.commit() post1 = Post(title='t', markup='') post1.tags = ['cool'] post2 = Post(title='t2', markup='') post2.tags = ['cool', 'cooler'] db.session.add(post1) db.session.add(post2) db.session.commit() db.session.delete(post2) db.session.commit() signals.post_deleted.send(post2) assert_equal(Tag.query.count(), 1) assert_equal(Tag.query.first().name, 'cool')
def test_month_view(self): """Test the displaying of the month view""" self.clear_db() self.register_and_login('barney', 'abc') post = Post('the chronic 2001', visible=False) post.datetime = datetime.datetime(1999, 11, 16) db.session.add(post) db.session.commit() rv = self.client.get('/1999/11/') self.assert_200(rv) assert 'the chronic 2001' in rv.data rv = self.client.get('/7777/12/') assert 'No entries here so far' in rv.data rv = self.client.get('/1999/14/') self.assert_404(rv) self.logout() rv = self.client.get('/1999/11/') self.assert_200(rv) assert 'No entries here so far' in rv.data
def test_slug_uniqueness(self): """Test if posts with the same title result in different slugs""" self.clear_db() for i in range(3): post = Post(title='t', markup='') db.session.add(post) db.session.commit() posts = Post.query.all() assert_equal(posts[0].slug, 't') assert_equal(posts[1].slug, 't-2') assert_equal(posts[2].slug, 't-2-2')
def test_tag_associations(self): """Test if tags and posts are correctly associated with each other""" self.clear_db() db.session.add(Tag('cool')) db.session.add(Tag('cooler')) db.session.commit() post1 = Post(title='t', markup='') post1.tags = ['cool'] post2 = Post(title='t2', markup='') post2.tags = ['cool', 'cooler'] db.session.add(post1) db.session.add(post2) db.session.commit() assert_equal(Tag.query.get(1).posts.count(), 2) # cool assert_equal(Tag.query.get(2).posts.count(), 1) # cooler assert_equal(len(post1.tags), 1) assert_equal(post1.tags[0].name, 'cool') assert_equal(len(post2.tags), 2) assert_equal(post2.tags[0].name, 'cool') assert_equal(post2.tags[1].name, 'cooler')
def test_months_view(self): """Test the month objects for the archive view""" self.clear_db() datetimes = [ datetime.datetime(2000, 1, 1), datetime.datetime(2000, 1, 2), datetime.datetime(2000, 2, 3), datetime.datetime(2002, 2, 4), ] for date in datetimes: post = Post(title='t', markup='') post.datetime = date db.session.add(post) db.session.commit() months = Post.query.get_months() assert_equal(len(months), 3) assert_equal(months[0]['year'], 2002) assert_equal(months[0]['index'], 2) assert_equal(months[0]['count'], 1) assert_equal(months[2]['year'], 2000) assert_equal(months[2]['index'], 1) assert_equal(months[2]['count'], 2)