def test_if_total_posts_of_search_posts_when_provide_username_is_less_than_total_posts_exists_in_database( conn): username1 = 'cool_name_1' email1 = '*****@*****.**' username2 = 'cool_name_2' email2 = '*****@*****.**' password = '******' password_repeat = '123' profile_text = 'this is some random profile text' user.create(conn, username1, email1, password, password_repeat, profile_text) user.create(conn, username2, email2, password, password_repeat, profile_text) user1 = user.get_profile(conn, username1) user2 = user.get_profile(conn, username2) random_title1 = 'title 1' random_title2 = 'title 2' random_title3 = 'title 3' random_content = 'content' post1 = post_service.create(conn, user1.id, random_title1, random_content) post2 = post_service.create(conn, user1.id, random_title2, random_content) post3 = post_service.create(conn, user2.id, random_title3, random_content) posts = post_service.get_posts_by_author(conn, username1) with conn.cursor(cursor_factory=DictCursor) as curs: query = "SELECT * FROM posts" curs.execute(query) rows = curs.fetchall() assert len(rows) > len(posts)
def test_get_authors_and_posts(conn): user1 = user.User(0, "user1", "*****@*****.**", "") user2 = user.User(0, "user2", "*****@*****.**", "") try: user.create(conn, user1.username, user1.email, "password", "") user.create(conn, user2.username, user2.email, "password", "") user1 = user.get_by_email(conn, user1.email) user2 = user.get_by_email(conn, user2.email) post_service.create(conn, user2.id, "first post", "content") post_service.create(conn, user2.id, "second post", "content") subscription.subscribe(conn, user1.id, user2.id) conn.commit() subscriptions = subscription.get_authors_and_posts(conn, user1.id) except DataError as e: pytest.fail(e) except DatabaseError as e: pytest.fail(e) for sub in subscriptions: assert sub.author == user2.username for p in sub.posts: assert p.author == user2.username assert p.title in ["first post", "second post"] assert p.content == "content"
def test_search_posts_when_provide_username_and_all_return_posts_should_be_related_to_requested_author( conn): username1 = 'cool_name_1' email1 = '*****@*****.**' username2 = 'cool_name_2' email2 = '*****@*****.**' password = '******' password_repeat = '123' profile_text = 'this is some random profile text' user.create(conn, username1, email1, password, password_repeat, profile_text) user.create(conn, username2, email2, password, password_repeat, profile_text) user1 = user.get_profile(conn, username1) user2 = user.get_profile(conn, username2) random_title1 = 'title 1' random_title2 = 'title 2' random_title3 = 'title 3' random_content = 'content' post1 = post_service.create(conn, user1.id, random_title1, random_content) post2 = post_service.create(conn, user1.id, random_title2, random_content) post3 = post_service.create(conn, user2.id, random_title3, random_content) posts = post_service.get_posts_by_author(conn, username1) for post in posts: assert post.author == username1
def test_user_search_for_post_by_specifc_tag_then_return_posts_with_this_specifc_tag( conn): # Create two users username1 = 'cool_name_1' email1 = '*****@*****.**' username2 = 'cool_name_2' email2 = '*****@*****.**' password = '******' password_repeat = '123' profile_text = 'this is some random profile text' user.create(conn, username1, email1, password, password_repeat, profile_text) # Get the two created users user1 = user.get_profile(conn, username1) # Create 2 posts random_title1 = 'title 1' random_title2 = 'title 2' random_content = 'content' post_service.create(conn, user1.id, random_title1, random_content) post_service.create(conn, user1.id, random_title2, random_content) # Create 2 tags with conn.cursor(cursor_factory=DictCursor) as cur: tag1 = 'ble-ble' tag2 = 'ble-bl1' query = "INSERT INTO tags (tag) VALUES (%s)" cur.execute(query, (tag1, )) cur.execute(query, (tag2, )) # Create 2 posts_tags with conn.cursor(cursor_factory=DictCursor) as cur: tag_id_1 = 1 post_id_1 = 1 tag_id_2 = 2 post_id_2 = 2 query = "INSERT INTO posts_tags (tag_id, post_id) VALUES (%s, %s)" cur.execute(query, (tag_id_1, post_id_1)) cur.execute(query, (tag_id_2, post_id_2)) tag_searching_for = 'ble-ble' posts = post_service.get_posts_by_tag(conn, tag_searching_for) assert len(posts) == 1 for post in posts: assert post.title == random_title1
def test_get_post_by_slug(conn): title = "How create a programing language" slug = post_service.generate_slug(title) username = '******' content = "BLA BLA BLA" user.create(conn, username, "*****@*****.**", "123456", "123456", "profile text") author = user.get_profile(conn, username) post_service.create(conn, author.id, title, content) post = post_service.get_post_by_slug(conn, username, slug) assert post.slug == slug assert post.author == username assert post.content == content assert post.title == title
def test_create_post(conn): author = 1 title = "Pytest test" slug = "pytest-test" content = "foo" username = "******" user.create(conn, username, "*****@*****.**", "123456", "123456", "profile") post_service.create(conn, author, title, content) post = post_service.get_post_by_slug(conn, username, slug) assert post.title == title assert post.slug == slug assert post.author == username assert post.content == content
def test_update_post(conn): author = 1 old_title = "Pytest test" old_slug = "pytest-test" old_content = "foo" new_title = "Pytest test 2" new_slug = "pytest-test-2" new_content = "bar" username = "******" user.create(conn, username, "*****@*****.**", "123456", "123456", "profile") post_service.create(conn, author, old_title, old_content) old_post = post_service.get_post_by_slug(conn, username, old_slug) post_service.create(conn, author, new_title, new_content) new_post = post_service.get_post_by_slug(conn, username, new_slug) assert not new_post.content == old_post.content assert not new_post.title == old_post.title assert not new_post.content == old_post.content