Beispiel #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, 5)

    # 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, review_text FROM comments'))
    assert rows == [(user_key, article_key, comment_text)]
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])),
                               rating=data_row[4])
        repo.add_comment(comment)
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("Boo", user, article, 0)

    repo.add_comment(comment)

    assert comment in repo.get_comments()
Beispiel #4
0
def add_comment(article_id: int, comment_text: str, username: str, rating: 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, rating)
    # Update the repository.
    repo.add_comment(comment)
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('ehhh', author, article, 2)

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

    assert comment.user_id == 1
    assert comment.article_id == 5
def test_make_comment_establishes_relationships(article, user):
    comment_text = 'csflix in the USA!'
    rating = 3
    comment = make_comment(comment_text, user, article, rating)

    # 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.movie is article
Beispiel #7
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, 6)

    # 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, review_text FROM comments'))

    assert rows == [(user_key, article_key, comment_text)]