def test_modified_date_changes(post_user): author = get_user_by_username('post_user') post = Post(title='Testing', body='Test Body', author=author) db.session.add(post) db.session.commit() assert post.modified_on is not None old_value = post.modified_on # update the post post.title = 'Published' db.session.commit() timedelta = post.modified_on - old_value assert timedelta.microseconds > 0
def test_author_field_required(post_user): failing_post = Post(title='Testing', body='Testing') db.session.add(failing_post) with pytest.raises(IntegrityError) as excinfo: db.session.commit() assert 'NOT NULL constraint failed: post.user_id' in str(excinfo.value)
def test_body_field_required(post_user): author = get_user_by_username('post_user') failing_post = Post(title='Testing', author=author) db.session.add(failing_post) with pytest.raises(IntegrityError) as excinfo: db.session.commit() assert 'NOT NULL constraint failed: post.body' in str(excinfo.value)
def test_default_values(post_user): author = get_user_by_username('post_user') post = Post(title='Testing', body='Test Body', author=author) db.session.add(post) db.session.commit() new_post = Post.query.filter_by(title='Testing').first() assert new_post.status == Post.DRAFT assert new_post.created_on is not None assert new_post.modified_on is not None
def test_cascade_delete(post_user): author = get_user_by_username('post_user') post = Post(title='Testing', body='Test Body', author=author) db.session.add(post) db.session.commit() db.session.delete(author) db.session.commit() new_post = Post.query.filter_by(title='Testing').first() assert new_post is None
def test_status_can_have_choices_only(post_user): author = get_user_by_username('post_user') with pytest.raises(KeyError) as excinfo: post = Post( title='Testing', body='Test Body', status='ABC', author=author ) assert 'ABC' in str(excinfo.value)
def test_post_created_with_minimum_fields(post_user): title = 'Test' body = 'Testing Post' author = get_user_by_username('post_user') post = Post(title=title, body=body, author=author) db.session.add(post) db.session.commit() new_post = Post.query.filter_by(title='Test').first() assert new_post.title == title assert new_post.body == body assert new_post.author == author
def users_with_posts(init_database): user1_data = dict(username='******', password='******') user2_data = dict(username='******', password='******') user1 = create_user(**user1_data) user2 = create_user(**user2_data) token1 = Token(auth_token='post_user1_token', user=user1) token2 = Token(auth_token='post_user2_token', user=user2) user1_post1 = Post(title='Draft Post', body='User 1', status=Post.DRAFT, author=user1, created_on=random_time()) user1_post2 = Post(title='Published Post', body='User 1', status=Post.PUBLISHED, author=user1, created_on=random_time()) user1_post3 = Post(title='Unpublished Post', body='User 1', status=Post.UNPUBLISHED, author=user1, created_on=random_time()) user2_post1 = Post(title='Draft Post', body='User 2', status=Post.DRAFT, author=user2, created_on=random_time()) user2_post2 = Post(title='Published Post', body='User 2', status=Post.PUBLISHED, author=user2, created_on=random_time()) user2_post3 = Post(title='Unpublished Post', body='User 2', status=Post.UNPUBLISHED, author=user2, created_on=random_time()) db.session.add(token1) db.session.add(token2) db.session.add(user1_post1) db.session.add(user1_post2) db.session.add(user1_post3) db.session.add(user2_post1) db.session.add(user2_post2) db.session.add(user2_post3) db.session.commit() return user1