def test_can_retrieve_an_article_and_add_a_comment_to_it(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    # Fetch Article and User.
    article = repo.get_article(5)
    author = repo.get_user('thorke')

    # Create a new Comment, connecting it to the Article and User.
    comment = make_comment('First death in Australia', author, article)

    article_fetched = repo.get_article(5)
    author_fetched = repo.get_user('thorke')

    assert comment in article_fetched.comments
    assert comment in author_fetched.comments
def test_repository_returns_date_of_next_article(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    article = repo.get_article(3)
    next_date = repo.get_date_of_next_article(article)

    assert next_date.isoformat() == '2020-03-05'
def test_repository_returns_date_of_previous_article(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    article = repo.get_article(6)
    previous_date = repo.get_date_of_previous_article(article)

    assert previous_date.isoformat() == '2020-03-01'
def test_repository_does_not_add_a_comment_without_a_user(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    article = repo.get_article(2)
    comment = Comment(None, article, "Trump's onto it!", datetime.today())

    with pytest.raises(RepositoryException):
        repo.add_comment(comment)
def test_repository_returns_none_when_there_are_no_subsequent_articles(
        session_factory):
    repo = SqlAlchemyRepository(session_factory)

    article = repo.get_article(177)
    next_date = repo.get_date_of_next_article(article)

    assert next_date is None
def test_repository_returns_none_when_there_are_no_previous_articles(
        session_factory):
    repo = SqlAlchemyRepository(session_factory)

    article = repo.get_article(1)
    previous_date = repo.get_date_of_previous_article(article)

    assert previous_date is None
def test_repository_can_add_a_comment(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    user = repo.get_user('thorke')
    article = repo.get_article(2)
    comment = make_comment("Trump's onto it!", user, article)

    repo.add_comment(comment)

    assert comment in repo.get_comments()
def test_repository_can_add_article(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    number_of_articles = repo.get_number_of_articles()

    new_article_id = number_of_articles + 1

    article = Article(
        2014,
        'James Gunn',
        ['Chris Pratt', 'Vin Diesel', 'Bradley Cooper', 'Zoe Saldana'],
        121,
        8.1,
        'Guardians of the Galaxy',
        'A group of intergalactic criminals are forced to work together to stop a fanatical warrior from taking control of the universe.',
    )
    repo.add_article(article)

    assert repo.get_article(new_article_id) == article
def test_repository_can_retrieve_article(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    article = repo.get_article(1)

    # Check that the Article has the expected title.
    assert article.title == 'Coronavirus: First case of virus in New Zealand'

    # Check that the Article is commented as expected.
    comment_one = [
        comment for comment in article.comments
        if comment.comment == 'Oh no, COVID-19 has hit New Zealand'
    ][0]
    comment_two = [
        comment for comment in article.comments
        if comment.comment == 'Yeah Freddie, bad news'
    ][0]

    assert comment_one.user.username == 'fmercury'
    assert comment_two.user.username == "thorke"

    # Check that the Article is tagged as expected.
    assert article.is_tagged_by(Tag('Health'))
    assert article.is_tagged_by(Tag('New Zealand'))
def test_repository_does_not_retrieve_a_non_existent_article(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    article = repo.get_article(201)
    assert article is None