def test_get_selling_post_list(
    post_count_result,
    input_user_id,
    session,
    normal_user_factory,
    create_categories,
    post_factory,
):
    """
    판매 목록 조회
    """
    user_list = normal_user_factory.build_batch(size=2,
                                                Region=True,
                                                UserProfile=True)
    session.add_all(user_list)
    session.commit()

    post_owner = user_list[0]

    categories = create_categories(PostCategoryEnum.get_dict())

    region_group_id = post_owner.region.region_group_id

    post1 = post_factory(
        Article=True,
        Categories=[categories[0]],
        region_group_id=region_group_id,
        user_id=post_owner.id,
        status=PostStatusEnum.SELLING.value,
    )
    post2 = post_factory(
        Article=True,
        Categories=[categories[0]],
        region_group_id=region_group_id,
        user_id=post_owner.id,
        status=PostStatusEnum.COMPLETED.value,
    )

    session.add_all([post1, post2])
    session.commit()

    selling_post_list = BoardRepository().get_selling_post_list(
        user_id=input_user_id)

    assert len(selling_post_list) == post_count_result
Esempio n. 2
0
def test_when_create_post_then_success(upload_mock, session,
                                       normal_user_factory, create_categories,
                                       add_and_commit):
    user = normal_user_factory(Region=True, UserProfile=True)
    add_and_commit([user])

    categories = create_categories(PostCategoryEnum.get_dict())

    # 실제 업로드 확인하려면 아래 경로에 이미지 첨부하고 patch 데코레이터 제거한 뒤 실행.
    file = FileStorage(
        stream=io.BytesIO(b"aaa"),
        filename="C:/project/rabbit/app/extensions/utils/a.jpg",
        content_type="multipart/form-data",
    )

    dto = CreatePostDto(
        user_id=user.id,
        title="떡볶이 나눠 먹어요",
        body="",
        region_group_id=1,
        type=PostTypeEnum.ATTACHMENT.value,
        is_comment_disabled=True,
        is_deleted=False,
        is_blocked=False,
        report_count=0,
        read_count=0,
        last_user_action="default",
        last_admin_action="default",
        amount=10,
        unit=PostUnitEnum.UNIT.value,
        price_per_unit=10000,
        status=PostStatusEnum.SELLING.value,
        category_ids=[categories[0].id],
        file_type=AttachmentEnum.PICTURE.value,
        files=[file],
    )

    post_entity = CreatePostUseCase().execute(dto=dto).value

    assert post_entity.title == dto.title
    assert post_entity.post_like_count == PostLikeCountEnum.DEFAULT.value
    assert isinstance(post_entity.attachments, list)
Esempio n. 3
0
def test_when_deleted_or_blocked_post_then_except(session, normal_user_factory,
                                                  post_factory,
                                                  create_categories,
                                                  add_and_commit):
    """
    post list 조회 시 삭제, 차단된 게시글 제외
    """
    user = normal_user_factory(Region=True, UserProfile=True)
    add_and_commit([user])

    categories = create_categories(PostCategoryEnum.get_dict())

    post = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user.region.region_group.id,
        user_id=user.id,
    )
    deleted_post = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user.region.region_group.id,
        user_id=user.id,
        is_deleted=True,
    )
    blocked_post = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user.region.region_group.id,
        user_id=user.id,
        is_blocked=True,
    )

    add_and_commit([post, deleted_post, blocked_post])

    dto = GetPostListDto(
        region_group_id=user.region.region_group.id,
        category_ids=[categories[0].id],
    )
    post_list = GetPostListUseCase().execute(dto=dto).value

    assert len(post_list) == 1
def test_get_post_list_by_status(
    post_status,
    input_status,
    result_count,
    session,
    normal_user_factory,
    post_factory,
    create_categories,
):
    """
    post list 조회 시 판매중, 거래완료 상태에 따라 응답
    """
    user = normal_user_factory.build(Region=True, UserProfile=True)
    session.add(user)
    session.commit()

    region_group_id = user.region.region_group_id

    categories = create_categories(PostCategoryEnum.get_dict())

    post1 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user.id,
    )
    post2 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user.id,
        status=post_status,
    )

    session.add_all([post1, post2])
    session.commit()

    post_list = BoardRepository().get_post_list(
        region_group_id=region_group_id, status=input_status)

    assert len(post_list) == result_count
def test_get_post_list_by_category(
    input_category_ids,
    result_count,
    session,
    normal_user_factory,
    post_factory,
    create_categories,
):
    """
    post category에 해당되는 post 응답
    """
    user = normal_user_factory.build(Region=True, UserProfile=True)
    session.add(user)
    session.commit()

    categories = create_categories(PostCategoryEnum.get_dict())

    region_group_id = user.region.region_group_id

    post1 = post_factory(
        Article=True,
        Categories=[categories[0], categories[1], categories[3]],
        region_group_id=region_group_id,
        user_id=user.id,
    )
    post2 = post_factory(
        Article=True,
        Categories=[categories[3]],
        region_group_id=region_group_id,
        user_id=user.id,
    )

    session.add_all([post1, post2])
    session.commit()

    post_list = BoardRepository().get_post_list(
        region_group_id=region_group_id, category_ids=input_category_ids)

    assert len(post_list) == result_count
Esempio n. 6
0
def test_when_update_post_then_success(
    s3_upload_mock,
    client,
    session,
    test_request_context,
    jwt_manager,
    make_header,
    normal_user_factory,
    article_factory,
    create_categories,
    attachment_factory,
    add_and_commit,
    make_authorization,
):
    user = normal_user_factory(Region=True, UserProfile=True, Post=True)
    add_and_commit([user])

    attachment = attachment_factory(post_id=user.post[0].id)
    add_and_commit([attachment])

    article = article_factory(post_id=user.post[0].id)
    add_and_commit([article])

    categories = create_categories(PostCategoryEnum.get_dict())

    # 실제 업로드 확인하려면 아래 경로에 이미지 첨부하고 patch 데코레이터 제거한 뒤 실행.
    file = FileStorage(
        stream=io.BytesIO(b"aaa"),
        filename="C:/project/rabbit/app/extensions/utils/a.jpg",
        content_type="multipart/form-data",
    )

    authorization = make_authorization(user_id=user.id)
    headers = make_header(authorization=authorization,
                          content_type="multipart/form-data",
                          accept="*/*")
    dct = dict(
        user_id=user.id,
        title="떡볶이 같이 먹어요",
        body="new body",
        region_group_id=1,
        type="article",
        is_comment_disabled=True,
        category_ids=json.dumps([categories[0].id, categories[1].id]),
        amount=10,
        unit=PostUnitEnum.UNIT.value,
        price_per_unit=10000,
        status=PostStatusEnum.SELLING.value,
        file_type=AttachmentEnum.PICTURE.value,
        files=[file],
    )

    with test_request_context:
        response = client.put(
            url_for("api/rabbit.update_post_view", post_id=user.post[0].id),
            data=dct,
            headers=headers,
        )

    assert response.status_code == 200
    data = response.get_json()["data"]
    assert data["post"]["user_id"] == user.id
    attachment = (session.query(AttachmentModel).filter_by(
        post_id=data["post"]["id"]).first())
    assert attachment.extension == ".jpg"
Esempio n. 7
0
def test_when_get_like_post_list_then_success(
    session,
    normal_user_factory,
    make_header,
    like_post,
    test_request_context,
    client,
    create_categories,
    post_factory,
    post_like_state_factory,
    add_and_commit,
    make_authorization,
):
    """
    내가 찜한 게시글, 찜 안한 게시글 생성 후 찜한 게시글만 응답  확인
    """
    user_list = normal_user_factory.build_batch(size=2,
                                                Region=True,
                                                UserProfile=True)
    add_and_commit(user_list)

    user1 = user_list[0]
    user2 = user_list[1]

    categories = create_categories(PostCategoryEnum.get_dict())

    region_group_id = user1.region.region_group_id

    liked_post = post_factory(
        Article=True,
        Categories=[categories[0]],
        region_group_id=region_group_id,
        user_id=user1.id,
        status=PostStatusEnum.SELLING.value,
    )
    post = post_factory(
        Article=True,
        Categories=[categories[0]],
        region_group_id=region_group_id,
        user_id=user1.id,
        status=PostStatusEnum.SELLING.value,
    )
    add_and_commit([liked_post])

    post_like_state = post_like_state_factory(post_id=liked_post.id,
                                              user_id=user2.id)

    add_and_commit([post, post_like_state])

    authorization = make_authorization(user_id=user1.id)
    headers = make_header(authorization=authorization)
    dct = dict(user_id=user2.id)

    with test_request_context:
        response = client.get(url_for("api/rabbit.get_like_post_list_view"),
                              json=dct,
                              headers=headers)

    assert response.status_code == 200
    data = response.get_json()["data"]
    assert len(data["post_list"]) == 1
Esempio n. 8
0
def test_when_get_post_list_order_by_desc_then_success(
    session,
    normal_user_factory,
    create_categories,
    post_factory,
    make_header,
    test_request_context,
    client,
    add_and_commit,
    make_authorization,
):
    """
    판매중, 거래완료 최신순으로 조회
    """
    user = normal_user_factory.build(Region=True, UserProfile=True)
    add_and_commit([user])

    categories = create_categories(PostCategoryEnum.get_dict())

    region_group_id = user.region.region_group_id

    post1 = post_factory(
        Article=True,
        Categories=[categories[0]],
        region_group_id=region_group_id,
        user_id=user.id,
        status=PostStatusEnum.SELLING.value,
    )
    post2 = post_factory(
        Article=True,
        Categories=[categories[0]],
        region_group_id=region_group_id,
        user_id=user.id,
        status=PostStatusEnum.COMPLETED.value,
    )
    post3 = post_factory(
        Article=True,
        Categories=[categories[0]],
        region_group_id=region_group_id,
        user_id=user.id,
        status=PostStatusEnum.COMPLETED.value,
    )

    add_and_commit([post1, post2, post3])

    authorization = make_authorization(user_id=user.id)
    headers = make_header(authorization=authorization)
    dct = dict(
        region_group_id=user.region.region_group.id,
        category_ids=[categories[0].id],
        status=PostStatusEnum.ALL.value,
    )

    with test_request_context:
        response = client.get(url_for("api/rabbit.get_post_list_view"),
                              json=dct,
                              headers=headers)

    assert response.status_code == 200
    data = response.get_json()["data"]
    assert len(data["post_list"]) == 3
    assert data["post_list"][0]["id"] == 3
    assert data["post_list"][1]["id"] == 2
    assert data["post_list"][2]["id"] == 1
Esempio n. 9
0
def test_when_get_post_list_then_include_like_count_and_exclude_like_state(
    session,
    normal_user_factory,
    make_header,
    post_factory,
    like_post,
    test_request_context,
    client,
    create_categories,
    add_and_commit,
    make_authorization,
):
    """
    post list 조회 시 찜 개수 포함, 찜 상태 제외
    user1 -> post1 찜
    user2 -> post1 찜
    user2 -> post2 찜
    """
    user1 = normal_user_factory(Region=True, UserProfile=True)
    user2 = normal_user_factory(Region=True, UserProfile=True)
    add_and_commit([user1, user2])

    categories = create_categories(PostCategoryEnum.get_dict())

    post1 = post_factory(
        Article=True,
        PostLikeCount=True,
        Categories=categories,
        region_group_id=user1.region.region_group.id,
        user_id=user1.id,
    )
    post2 = post_factory(
        Article=True,
        PostLikeCount=True,
        Categories=categories,
        region_group_id=user1.region.region_group.id,
        user_id=user1.id,
    )

    add_and_commit([post1, post2])

    # 찜하기
    like_post(user_id=user1.id, post_id=post1.id)
    like_post(user_id=user2.id, post_id=post1.id)
    like_post(user_id=user2.id, post_id=post2.id)

    authorization = make_authorization(user_id=user1.id)
    headers = make_header(authorization=authorization)
    dct = dict(
        region_group_id=user1.region.region_group.id,
        category_ids=[categories[0].id],
        status=PostStatusEnum.SELLING.value,
    )

    with test_request_context:
        response = client.get(url_for("api/rabbit.get_post_list_view"),
                              json=dct,
                              headers=headers)

    assert response.status_code == 200
    data = response.get_json()["data"]
    assert data["post_list"][0]["post_like_count"] == 1
    assert data["post_list"][1]["post_like_count"] == 2
Esempio n. 10
0
def test_when_get_post_list_then_success(
    client,
    session,
    test_request_context,
    make_header,
    normal_user_factory,
    post_factory,
    create_categories,
    add_and_commit,
    make_authorization,
):
    user = normal_user_factory(Region=True, UserProfile=True)
    add_and_commit([user])

    categories = create_categories(PostCategoryEnum.get_dict())

    post1 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user.region.region_group.id,
        user_id=user.id,
    )
    post2 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user.region.region_group.id,
        user_id=user.id,
    )

    user2 = normal_user_factory(Region=True, UserProfile=True)
    add_and_commit([user2])
    post3 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user2.region.region_group.id,
        user_id=user2.id,
    )
    post4 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user2.region.region_group.id,
        user_id=user2.id,
    )

    add_and_commit([post1, post2, post3, post4])

    authorization = make_authorization(user_id=user.id)
    headers = make_header(authorization=authorization)
    dct = dict(
        region_group_id=user.region.region_group.id,
        category_ids=[categories[0].id],
        status=PostStatusEnum.SELLING.value,
    )

    with test_request_context:
        response = client.get(url_for("api/rabbit.get_post_list_view"),
                              json=dct,
                              headers=headers)

    assert response.status_code == 200
    post_list = response.get_json()["data"]["post_list"]
    cursor = response.get_json()["meta"]["cursor"]
    assert post1.id == cursor["last_post_id"]
    assert post2.id == post_list[0]["id"]
    assert post1.id == post_list[1]["id"]
Esempio n. 11
0
def test_when_search_post_list_then_success(session, normal_user_factory,
                                            post_factory, create_categories,
                                            add_and_commit):
    """
    post 검색. user1 post 2개, user2 post 2개, user3 post 1개, 다른 지역 user4 post 1개
    총 5개 post 응답
    """
    user_list = normal_user_factory.build_batch(size=4,
                                                Region=True,
                                                UserProfile=True)
    add_and_commit(user_list)

    region_group_id = user_list[0].region.region_group_id

    categories = create_categories(PostCategoryEnum.get_dict())

    post1 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[0].id,
    )
    post2 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[0].id,
    )
    post3 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[1].id,
    )
    post4 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[1].id,
    )
    post5 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[2].id,
    )
    post6 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user_list[3].region.region_group_id,
        user_id=user_list[3].id,
    )

    add_and_commit([post1, post2, post3, post4, post5, post6])

    dto = GetPostListDto(
        region_group_id=region_group_id,
        title=post1.title[2:6],
        category_ids=[categories[0].id],
    )

    post_list = GetPostListUseCase().execute(dto=dto).value

    for post in post_list:
        assert post.region_group_id == region_group_id
        assert post1.title[2:6] in post.title
    assert len(post_list) == 5
Esempio n. 12
0
def test_create_notification_history(session, normal_user_factory,
                                     create_categories):
    user = normal_user_factory(Region=True, UserProfile=True)
    session.add(user)
    session.commit()

    categories = create_categories(PostCategoryEnum.get_dict())

    post_dto = CreatePostDto(
        user_id=user.id,
        title="떡볶이 나눠 먹어요",
        body="",
        region_group_id=1,
        type="article",
        is_comment_disabled=True,
        is_deleted=False,
        is_blocked=False,
        report_count=0,
        read_count=0,
        last_user_action="default",
        last_admin_action="default",
        amount=10,
        unit=PostUnitEnum.UNIT.value,
        price_per_unit=10000,
        status=PostStatusEnum.SELLING.value,
        category_ids=[categories[0].id],
    )

    post_entity = BoardRepository().create_post(dto=post_dto)

    user2 = normal_user_factory(Region=True, UserProfile=True)
    session.add(user2)
    session.commit()

    message_dto = MessageDto(
        post_id=post_entity.id,
        user_id=user2.id,
        category=CategoryEnum.KEYWORD.value,
        type=TypeEnum.ALL.value,
        title=TitleEnum.KEYWORD.value,
        body=BodyEnum.KEYWORD.value,
        token="asdfasdf",
    )

    notification_message = NotificationMessageConverter.to_dict(message_dto)

    notification_history_list = list()
    notification_history_list.append(
        NotificationHistoryDto(
            user_id=message_dto.user_id,
            status=StatusEnum.PENDING.value,
            type=message_dto.type,
            category=message_dto.category,
            message=notification_message,
        ))
    NotificationRepository().create_notification_history(
        notification_list=notification_history_list)
    notification_history = (session.query(NotificationHistoryModel).filter(
        NotificationHistoryModel.id == 1).first())

    assert notification_history.message == notification_message
Esempio n. 13
0
def test_search_post_list(session, normal_user_factory, post_factory,
                          create_categories):
    """
    post 검색. user1 post 2개, user2 post 2개, user3 post 1개, 다른 지역 user4 post 1개
    총 5개 post 응답
    """
    user_list = normal_user_factory.build_batch(size=4,
                                                Region=True,
                                                UserProfile=True)
    session.add_all(user_list)
    session.commit()

    region_group_id = user_list[0].region.region_group_id

    categories = create_categories(PostCategoryEnum.get_dict())

    post1 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[0].id,
    )
    post2 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[0].id,
    )
    post3 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[1].id,
    )
    post4 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[1].id,
    )
    post5 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=region_group_id,
        user_id=user_list[2].id,
    )
    post6 = post_factory(
        Article=True,
        Categories=categories,
        region_group_id=user_list[3].region.region_group_id,
        user_id=user_list[3].id,
    )

    session.add_all([post1, post2, post3, post4, post5, post6])
    session.commit()

    post_list = BoardRepository().get_post_list(
        region_group_id=region_group_id,
        title=post1.title[2:6],
        category_ids=[categories[0].id],
    )

    for post in post_list:
        assert post.region_group_id == region_group_id
        assert post1.title[2:6] in post.title
    assert len(post_list) == 5