async def test_filtering_with_limit_and_offset(app: FastAPI,
                                               authorized_client: Client,
                                               test_user: UserInDB,
                                               pool: Pool) -> None:
    async with pool.acquire() as connection:
        articles_repo = ArticlesRepository(connection)

        for i in range(5, 10):
            await articles_repo.create_article(
                slug=f"slug-{i}",
                title="tmp",
                description="tmp",
                body="tmp",
                author=test_user,
            )

    full_response = await authorized_client.get(
        app.url_path_for("articles:list-articles"))
    full_articles = ListOfArticlesInResponse(**full_response.json())

    response = await authorized_client.get(
        app.url_path_for("articles:list-articles"),
        params={
            "limit": 2,
            "offset": 3
        })

    articles_from_response = ListOfArticlesInResponse(**response.json())
    assert full_articles.articles[3:] == articles_from_response.articles
async def test_empty_feed_if_user_has_not_followings(
    app: FastAPI,
    authorized_client: Client,
    test_article: Article,
    test_user: UserInDB,
    pool: Pool,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        articles_repo = ArticlesRepository(connection)

        for i in range(5):
            user = await users_repo.create_user(username=f"user-{i}",
                                                email=f"user-{i}@email.com",
                                                password="******")
            for j in range(5):
                await articles_repo.create_article(
                    slug=f"slug-{i}-{j}",
                    title="tmp",
                    description="tmp",
                    body="tmp",
                    author=user,
                    tags=[f"tag-{i}-{j}"],
                )

    response = await authorized_client.get(
        app.url_path_for("articles:get-user-feed-articles"))

    articles = ListOfArticlesInResponse(**response.json())
    assert articles.articles == []
async def test_user_receiving_feed_with_limit_and_offset(
    app: FastAPI,
    authorized_client: Client,
    test_article: Article,
    test_user: UserInDB,
    pool: Pool,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        profiles_repo = ProfilesRepository(connection)
        articles_repo = ArticlesRepository(connection)

        for i in range(5):
            user = await users_repo.create_user(username=f"user-{i}",
                                                email=f"user-{i}@email.com",
                                                password="******")
            if i == 2:
                await profiles_repo.add_user_into_followers(
                    target_user=user, requested_user=test_user)

            for j in range(5):
                await articles_repo.create_article(
                    slug=f"slug-{i}-{j}",
                    title="tmp",
                    description="tmp",
                    body="tmp",
                    author=user,
                    tags=[f"tag-{i}-{j}"],
                )

    full_response = await authorized_client.get(
        app.url_path_for("articles:get-user-feed-articles"))
    full_articles = ListOfArticlesInResponse(**full_response.json())

    response = await authorized_client.get(
        app.url_path_for("articles:get-user-feed-articles"),
        params={
            "limit": 2,
            "offset": 3
        },
    )

    articles_from_response = ListOfArticlesInResponse(**response.json())
    assert full_articles.articles[3:] == articles_from_response.articles
Esempio n. 4
0
async def get_articles_for_user_feed(
    limit: int = Query(DEFAULT_ARTICLES_LIMIT, ge=1),
    offset: int = Query(DEFAULT_ARTICLES_OFFSET, ge=0),
    user: User = Depends(get_current_user_authorizer()),
    articles_repo: ArticlesRepository = Depends(
        get_repository(ArticlesRepository)),
) -> ListOfArticlesInResponse:
    articles = await articles_repo.get_articles_for_user_feed(user=user,
                                                              limit=limit,
                                                              offset=offset)
    return ListOfArticlesInResponse(articles=articles,
                                    articles_count=len(articles))
async def test_filtering_by_favorited(
    app: FastAPI,
    authorized_client: Client,
    test_user: UserInDB,
    pool: Pool,
    favorited: str,
    result: int,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        articles_repo = ArticlesRepository(connection)

        fan1 = await users_repo.create_user(username="******",
                                            email="*****@*****.**",
                                            password="******")
        fan2 = await users_repo.create_user(username="******",
                                            email="*****@*****.**",
                                            password="******")

        article1 = await articles_repo.create_article(slug=f"slug-1",
                                                      title="tmp",
                                                      description="tmp",
                                                      body="tmp",
                                                      author=test_user)
        article2 = await articles_repo.create_article(slug=f"slug-2",
                                                      title="tmp",
                                                      description="tmp",
                                                      body="tmp",
                                                      author=test_user)

        await articles_repo.add_article_into_favorites(article=article1,
                                                       user=fan1)
        await articles_repo.add_article_into_favorites(article=article1,
                                                       user=fan2)
        await articles_repo.add_article_into_favorites(article=article2,
                                                       user=fan2)

        for i in range(5, 10):
            await articles_repo.create_article(
                slug=f"slug-{i}",
                title="tmp",
                description="tmp",
                body="tmp",
                author=test_user,
            )

    response = await authorized_client.get(
        app.url_path_for("articles:list-articles"),
        params={"favorited": favorited})
    articles = ListOfArticlesInResponse(**response.json())
    assert articles.articles_count == result
async def list_articles(
    articles_filters: ArticlesFilters = Depends(get_articles_filters),
    user: Optional[User] = Depends(get_current_user_authorizer(required=False)),
    articles_repo: ArticlesRepository = Depends(get_repository(ArticlesRepository)),
) -> ListOfArticlesInResponse:
    articles = await articles_repo.filter_articles(
        tag=articles_filters.tag,
        author=articles_filters.author,
        favorited=articles_filters.favorited,
        limit=articles_filters.limit,
        offset=articles_filters.offset,
        requested_user=user,
    )
    return ListOfArticlesInResponse(articles=articles, articles_count=len(articles))
Esempio n. 7
0
async def test_filtering_by_authors(
    app: FastAPI,
    authorized_client: AsyncClient,
    test_user: UserInDB,
    pool: Pool,
    author: str,
    result: int,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        articles_repo = ArticlesRepository(connection)

        author1 = await users_repo.create_user(username="******",
                                               email="*****@*****.**",
                                               password="******")
        author2 = await users_repo.create_user(username="******",
                                               email="*****@*****.**",
                                               password="******")

        await articles_repo.create_article(slug=f"slug-1",
                                           title="tmp",
                                           description="tmp",
                                           body="tmp",
                                           author=author1)
        await articles_repo.create_article(slug=f"slug-2-1",
                                           title="tmp",
                                           description="tmp",
                                           body="tmp",
                                           author=author2)
        await articles_repo.create_article(slug=f"slug-2-2",
                                           title="tmp",
                                           description="tmp",
                                           body="tmp",
                                           author=author2)

        for i in range(5, 10):
            await articles_repo.create_article(
                slug=f"slug-{i}",
                title="tmp",
                description="tmp",
                body="tmp",
                author=test_user,
            )

    response = await authorized_client.get(
        app.url_path_for("articles:list-articles"), params={"author": author})
    articles = ListOfArticlesInResponse(**response.json())
    assert articles.articles_count == result
async def test_user_will_receive_only_following_articles(
    app: FastAPI,
    authorized_client: AsyncClient,
    test_article: Article,
    test_user: UserInDB,
    pool: Pool,
) -> None:
    following_author_username = "******"
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        profiles_repo = ProfilesRepository(connection)
        articles_repo = ArticlesRepository(connection)

        for i in range(5):
            user = await users_repo.create_user(
                username=f"user-{i}", email=f"user-{i}@email.com", password="******"
            )
            if i == 2:
                await profiles_repo.add_user_into_followers(
                    target_user=user, requested_user=test_user
                )

            for j in range(5):
                await articles_repo.create_article(
                    slug=f"slug-{i}-{j}",
                    title="tmp",
                    description="tmp",
                    body="tmp",
                    author=user,
                    tags=[f"tag-{i}-{j}"],
                )

    response = await authorized_client.get(
        app.url_path_for("articles:get-user-feed-articles")
    )

    articles_from_response = ListOfArticlesInResponse(**response.json())
    assert len(articles_from_response.articles) == 5

    all_from_following = (
        article.author.username == following_author_username
        for article in articles_from_response.articles
    )
    assert all(all_from_following)
async def test_filtering_by_tags(
    app: FastAPI,
    authorized_client: AsyncClient,
    test_user: UserInDB,
    pool: Pool,
    tag: str,
    result: int,
) -> None:
    async with pool.acquire() as connection:
        articles_repo = ArticlesRepository(connection)

        await articles_repo.create_article(
            slug=f"slug-1",
            title="tmp",
            description="tmp",
            body="tmp",
            author=test_user,
            tags=["tag1", "tag2"],
        )
        await articles_repo.create_article(
            slug=f"slug-2",
            title="tmp",
            description="tmp",
            body="tmp",
            author=test_user,
            tags=["tag2"],
        )

        for i in range(5, 10):
            await articles_repo.create_article(
                slug=f"slug-{i}",
                title="tmp",
                description="tmp",
                body="tmp",
                author=test_user,
                tags=[f"tag-{i}"],
            )

    response = await authorized_client.get(
        app.url_path_for("articles:list-articles"), params={"tag": tag}
    )
    articles = ListOfArticlesInResponse(**response.json())
    assert articles.articles_count == result
Esempio n. 10
0
async def list_articles(
        author: str = Query(None, max_length=16),
        favorited: str = Query(None, max_length=16),
        limit: int = Query(20),
        offset: int = Query(0),
) -> ListOfArticlesInResponse:
    query = Article.all().prefetch_related("author")

    if author is not None:
        query = query.filter(author__username=author)
    if favorited is not None:
        query = query.filter(favorited__username=favorited)

    articles: List[Article] = await query.offset(offset).limit(limit)
    articles_count = await query.count()

    return ListOfArticlesInResponse(
        articles=[ArticleBase.from_entity(a, False) for a in articles],
        articles_count=articles_count)