Exemplo n.º 1
0
def test_get_blog_by_id(tmp_path):
    """
    Test that get_blog_by_id() returns False when it should, and properly
    returns a blog when there are 1 or 2 blogs in the database.
    """

    db = BlogPost(build_db_path(tmp_path))

    assert db.get_blog_by_id(1) is False
    assert db.insert_blog('title', 'Sub', 'non_existent', 'content') is False

    assert db.sign_up_entry('KHANDOKAR', 'PASSWORD')

    blog_inserted = db.insert_blog('STITLE', 'SSUBTITLE', 'KHANDOKAR',
                                   'SCONTENT')
    blog = db.get_blog_by_id(1)

    assert blog_inserted == blog

    assert db.get_blog_by_id(23) is False

    blog_inserted = db.insert_blog('title2', 'subtitle2', 'Khandokar2',
                                   'content2')
    blog = db.get_blog_by_id(2)

    assert blog_inserted == blog
Exemplo n.º 2
0
def test_get_all_posts(tmp_path):
    """
    Test that get_all_posts() properly returns an empty list when no blogs
    have been inserted, and returns a correct list of blogs when there are one
    or two blogs in the database.
    """

    db = BlogPost(build_db_path(tmp_path))

    assert db.get_all_posts() == []
    assert db.sign_up_entry('KHANDOKAR', 'PASSWORD')
    blog_1 = db.insert_blog('STITLE', 'SSUBTITLE', 'KHANDOKAR',
                                   'SCONTENT')

    assert blog_1['title'] == 'STITLE'
    assert blog_1['subtitle'] == 'SSUBTITLE'
    assert blog_1['content'] == 'SCONTENT'
    assert blog_1['author_id'] == db.get_author_by_name('KHANDOKAR')[
        'author_id']

    blogs = db.get_all_posts()

    assert len(blogs) == 1

    assert db.insert_blog('title', 'Sub', 'non_existent', 'content') is False

    assert db.sign_up_entry('khan', 'haunter')
    assert db.insert_blog('title', 'sub', 'khan', 'content')

    blogs = db.get_all_posts()
    assert len(blogs) == 2
Exemplo n.º 3
0
def test_insert_blog(tmp_path):
    """
    Test that insert_blog() runs without raising exceptions, and correctly
    returns a dictionary representing the new blog.
    Otherwise returns False

    :param tmp_path: a Path object representing the path to the temporary
     directory created via the pytest tmp_path fixture
    """
    db = BlogPost(build_db_path(tmp_path))

    assert db.sign_up_entry('Giraffe', 'animal')
    blog = db.insert_blog('stitle', 'ssubtitle', 'Giraffe', 'scontent')

    assert blog['title'] == 'stitle'
    assert blog['subtitle'] == 'ssubtitle'
    assert blog['author_id'] == db.get_author_by_name('Giraffe')['author_id']
    assert blog['content'] == 'scontent'
    assert db.insert_blog('title_2', 'subtitle_2', 'Giraffe', 'new_content')

    assert db.insert_blog('title', 'sub', 'fake_user', 'content') is False