Exemple #1
0
def test_save_commented_article(empty_session):
    # Create Article User objects.
    article = make_article()
    user = make_user()

    # Create a new Comment that is bidirectionally linked with the User and Article.
    comment_text = "Some comment text."
    comment = make_comment(comment_text, user, article)

    # Save the new Article.
    empty_session.add(article)
    empty_session.commit()

    # Test test_saving_of_article() checks for insertion into the articles table.
    rows = list(empty_session.execute('SELECT id FROM articles'))
    article_key = rows[0][0]

    # Test test_saving_of_users() checks for insertion into the users table.
    rows = list(empty_session.execute('SELECT id FROM users'))
    user_key = rows[0][0]

    # Check that the comments table has a new record that links to the articles and users
    # tables.
    rows = list(
        empty_session.execute(
            'SELECT user_id, article_id, comment FROM comments'))
    assert rows == [(user_key, article_key, comment_text)]
Exemple #2
0
def load_comments(data_path: str, repo: MemoryRepository, users):
    for data_row in read_csv_file(os.path.join(data_path, 'comments.csv')):
        comment = make_comment(comment_text=data_row[3],
                               user=users[data_row[1]],
                               article=repo.get_article(int(data_row[2])),
                               timestamp=datetime.fromisoformat(data_row[4]))
        repo.add_comment(comment)
Exemple #3
0
def test_repository_can_add_a_comment(in_memory_repo):
    user = in_memory_repo.get_user('thorke')
    article = in_memory_repo.get_article(2)
    comment = make_comment("Trump's onto it!", user, article)

    in_memory_repo.add_comment(comment)

    assert comment in in_memory_repo.get_comments()
Exemple #4
0
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()
Exemple #5
0
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
Exemple #6
0
def add_comment(article_id: int, comment_text: str, username: str, repo: AbstractRepository):
    # Check that the article exists.
    article = repo.get_article(article_id)
    if article is None:
        raise NonExistentArticleException

    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    # Create comment.
    comment = make_comment(comment_text, user, article)

    # Update the repository.
    repo.add_comment(comment)
Exemple #7
0
def test_make_comment_establishes_relationships(article, user):
    comment_text = 'COVID-19 in the USA!'
    comment = make_comment(comment_text, user, article)

    # Check that the User object knows about the Comment.
    assert comment in user.comments

    # Check that the Comment knows about the User.
    assert comment.user is user

    # Check that Article knows about the Comment.
    assert comment in article.comments

    # Check that the Comment knows about the Article.
    assert comment.article is article
Exemple #8
0
def add_comment(article_id: int, comment_text: str, username: str,
                uow: unit_of_work.AbstractUnitOfWork):
    with uow:
        # Check that the article exists.
        article = uow.repo.get_article(article_id)
        if article is None:
            raise NonExistentArticleException

        user = uow.repo.get_user(username)
        if user is None:
            raise UnknownUserException

        # Create comment.
        comment = make_comment(comment_text, user, article)

        # Update the repository.
        uow.repo.add_comment(comment)
        uow.commit()
def test_uow_can_retrieve_an_article_and_add_a_comment_to_it(session_factory):
    uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)
    with uow:
        # Fetch Article and User.
        article = uow.repo.get_article(5)
        author = uow.repo.get_user('thorke')

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

        # Commit the changes.
        uow.commit()

    # Check that a Comment has been added that links to the Article and User.
    uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)
    with uow:
        # Fetch Article and User.
        article = uow.repo.get_article(5)
        author = uow.repo.get_user('thorke')

        assert comment in article.comments
        assert comment in author.comments
Exemple #10
0
def test_saving_of_comment(empty_session):
    article_key = insert_article(empty_session)
    user_key = insert_user(empty_session, ("Andrew", "1234"))

    rows = empty_session.query(Article).all()
    article = rows[0]
    user = empty_session.query(User).filter(User._username == "Andrew").one()

    # Create a new Comment that is bidirectionally linked with the User and Article.
    comment_text = "Some comment text."
    comment = make_comment(comment_text, user, article)

    # Note: if the bidirectional links between the new Comment and the User and
    # Article objects hadn't been established in memory, they would exist following
    # committing the addition of the Comment to the database.
    empty_session.add(comment)
    empty_session.commit()

    rows = list(
        empty_session.execute(
            'SELECT user_id, article_id, comment FROM comments'))

    assert rows == [(user_key, article_key, comment_text)]
Exemple #11
0
def test_make_comment_establishes_relationships():
    comment_text = 'COVID-19 in the USA!'
    user = User('bspringsteen', '0987654321')
    article = Article(
        date.fromisoformat('2020-03-15'),
        'Coronavirus travel restrictions: Self-isolation deadline pushed back to give airlines breathing room',
        'The self-isolation deadline has been pushed back',
        'https://www.nzherald.co.nz/business/news/article.cfm?c_id=3&objectid=12316800',
        'https://th.bing.com/th/id/OIP.0lCxLKfDnOyswQCF9rcv7AHaCz?w=344&h=132&c=7&o=5&pid=1.7'
    )

    comment = make_comment(comment_text, user, article)

    # Check that the User object knows about the Comment.
    assert comment in user.comments

    # Check that the Comment knows about the User.
    assert comment.user is user

    # Check that Article knows about the Comment.
    assert comment in article.comments

    # Check that the Comment knows about the Article.
    assert comment.article is article