Exemplo n.º 1
0
def test_add_course_with_wrong_slug_to_cart(cart):
    with pytest.raises(Course.DoesNotExist):
        CartToolkit.add_course_to_cart(cart=cart, course_slug="somerandomslug")

    cart.refresh_from_db()

    assert cart.courses.count() == 0
Exemplo n.º 2
0
def test_updating_totals_after_adding_course_to_cart(mocker, course_factory,
                                                     cart):
    update_totals = mocker.patch("carts.models.Cart.update_totals")
    CartToolkit.add_course_to_cart(cart=cart,
                                   course_slug=course_factory().slug)

    update_totals.assert_called_once()
Exemplo n.º 3
0
def test_updating_cart_totals_without_discounts(
        cart_with_one_course_with_zero_totals, course_factory):
    cart = cart_with_one_course_with_zero_totals
    CartToolkit.update_cart_totals(cart=cart)

    cart.refresh_from_db()

    assert cart.subtotal == cart.total == cart.courses.first().price
Exemplo n.º 4
0
def test_add_unpublished_course_to_cart(cart, unpublished_course):
    with pytest.raises(Course.DoesNotExist):
        CartToolkit.add_course_to_cart(cart=cart,
                                       course_slug=unpublished_course.slug)

    cart.refresh_from_db()

    assert cart.courses.count() == 0
Exemplo n.º 5
0
def test_removing_course_from_cart_updating_totals(mocker,
                                                   cart_with_one_course,
                                                   course):
    update_totals = mocker.patch("carts.models.Cart.update_totals")
    CartToolkit.remove_course_from_cart(cart=cart_with_one_course,
                                        course_slug=course.slug)

    update_totals.assert_called_once()
Exemplo n.º 6
0
def test_add_courses_to_cart(courses_count, cart, course_factory):
    for i in range(courses_count):
        CartToolkit.add_course_to_cart(cart=cart,
                                       course_slug=course_factory().slug)

    cart.refresh_from_db()

    assert cart.courses.count() == courses_count
Exemplo n.º 7
0
def test_updating_cart_totals_with_multiple_courses_without_discounts(
        cart_with_two_courses_with_zero_totals):
    cart = cart_with_two_courses_with_zero_totals
    CartToolkit.update_cart_totals(cart=cart)

    cart.refresh_from_db()

    total = cart.courses.first().price + cart.courses.last().price
    assert cart.subtotal == cart.total == total
Exemplo n.º 8
0
def test_add_already_added_course_to_cart(course_factory, cart):
    course = course_factory()

    cart.courses.add(course)
    CartToolkit.add_course_to_cart(cart=cart, course_slug=course.slug)

    cart.refresh_from_db()

    assert cart.courses.count() == 1
Exemplo n.º 9
0
def test_add_course_to_cart(course_factory, cart):
    assert cart.courses.count() == 0
    course = course_factory()

    CartToolkit.add_course_to_cart(cart=cart, course_slug=course.slug)

    cart.refresh_from_db()
    test_course = cart.courses.first()

    assert test_course == course
Exemplo n.º 10
0
def test_updating_cart_totals_with_discount(
        mocker, cart_with_one_course_with_zero_totals, discount):
    cart = cart_with_one_course_with_zero_totals
    mocker.patch(
        "discounts.services.DiscountSelector.get_discount_for_course_or_nothing",
        return_value=discount)
    CartToolkit.update_cart_totals(cart=cart)

    cart.refresh_from_db()

    assert cart.subtotal == cart.courses.first().price
    assert cart.total == discount.new_price
Exemplo n.º 11
0
def test_remove_courses_from_cart(course_factory, cart, course_count):
    courses = []
    for i in range(course_count):
        course = course_factory()
        cart.courses.add(course)
        courses.append(course)

    for i in range(course_count):
        CartToolkit.remove_course_from_cart(cart=cart,
                                            course_slug=courses[-1].slug)

        assert courses[-1] not in cart.courses.all()
        assert cart.courses.count() == course_count - i - 1

        courses.pop(-1)
Exemplo n.º 12
0
def test_loading_inactive_cart(mocker, inactive_cart):
    create_cart = mocker.patch("carts.services.CartToolkit._create_new_cart")
    test_cart = CartToolkit.load_cart(user=inactive_cart.user,
                                      cart_id=inactive_cart.id)

    create_cart.assert_called_once()
    assert test_cart != inactive_cart
Exemplo n.º 13
0
def check_on_course_already_in_cart_api(request, *args, **kwargs):
    try:
        data = request.GET
        cart_id = int(data.get("cart_id")) if data.get("cart_id") is not None else None
        course_slug = data["course_slug"]
    except KeyError:
        logger.error("Request object doesn't have a slug field")
        return Response({"error": CartErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400)
    
    user = request.user
    cart = CartToolkit.load_cart(user=user, cart_id=cart_id)
    try:
        course_already_in_cart = CartToolkit.check_on_course_in_cart(cart=cart, course_slug=course_slug)
    except Course.DoesNotExist:
        return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400)
    return Response({"course_already_in_cart": course_already_in_cart}, status=200)
Exemplo n.º 14
0
def remove_course_from_cart_api(request, *args, **kwargs):
    try:
        course_slug = request.POST["course_slug"]
    except KeyError:
        logger.error("Request object doesn't have a slug field")
        return Response({"error": CartErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400)
    
    user = request.user
    ids = CartListsToolkit.get_cart_lists_ids_from_request(request)
    cart_lists = CartListsSelector.get_cart_lists_by_user_and_ids(user=request.user, ids=ids)

    try:
        CartToolkit.remove_course_from_cart(cart=cart_lists["cart"], course_slug=course_slug)
    except Course.DoesNotExist:
        return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400)

    serializer = CartOnlyInfoSerializer(instance=cart_lists["cart"])
    return Response(serializer.data, status=200)
Exemplo n.º 15
0
def get_cart_courses_count_api(request, *args, **kwargs):
    data = request.GET
    cart_id = int(data.get("cart_id")) if data.get("cart_id") is not None else None
    user = request.user
    cart = CartToolkit.load_cart(user=user, cart_id=cart_id)
    return Response({
        "cart_courses_count": cart.get_courses_count(), 
        "cart_id": cart.id,
    }, status=200)
Exemplo n.º 16
0
 def get_cart_lists_by_user_and_ids(cls, user: User, ids: dict, *args,
                                    **kwargs) -> dict:
     cart = CartToolkit.load_cart(user=user, cart_id=ids["cart_id"])
     saved_for_later = SavedForLaterToolkit.load_saved_for_later(
         user=user, saved_for_later_id=ids["saved_for_later_id"])
     if user.is_authenticated:
         wishlist = WishlistToolkit.load_wishlist(user=user)
     else:
         wishlist = None
     return {
         "cart": cart,
         "saved_for_later": saved_for_later,
         "wishlist": wishlist,
     }
Exemplo n.º 17
0
def test_cart_loading_updating_totals(mocker, cart):
    cart_update_totals = mocker.patch("carts.models.Cart.update_totals")
    test_cart = CartToolkit.load_cart(user=cart.user)

    cart_update_totals.assert_called_once()
Exemplo n.º 18
0
def test_cart_loading_without_arguments(mocker):
    create_cart = mocker.patch("carts.services.CartToolkit._create_new_cart")
    test_cart = CartToolkit.load_cart()

    create_cart.assert_called_once()
Exemplo n.º 19
0
def test_cart_loading_for_user_without_existing_cart(mocker, user):
    create_cart = mocker.patch("carts.services.CartToolkit._create_new_cart")
    test_cart = CartToolkit.load_cart(user=user)

    create_cart.assert_called_once()
Exemplo n.º 20
0
def test_cart_loading_with_user_and_different_cart_id(cart, another_cart):
    test_cart = CartToolkit.load_cart(user=cart.user, cart_id=another_cart.id)

    assert test_cart == cart
Exemplo n.º 21
0
def test_cart_loading_with_id(cart):
    test_cart = CartToolkit.load_cart(cart_id=cart.id)

    assert test_cart == cart
Exemplo n.º 22
0
def test_remove_course_with_wrong_slug_from_cart(cart):
    with pytest.raises(Course.DoesNotExist):
        CartToolkit.remove_course_from_cart(cart=cart,
                                            course_slug="somerandomslug")

    assert cart.courses.count() == 0
Exemplo n.º 23
0
def get_cart_info_api(request, *args, **kwargs):
    user = request.user
    ids = CartListsToolkit.get_cart_lists_ids_from_request(request)
    cart = CartToolkit.load_cart(user=user, cart_id=ids["cart_id"])
    serializer = CartOnlyInfoSerializer(instance=cart)
    return Response(serializer.data, status=200)
Exemplo n.º 24
0
def test_remove_not_added_course_from_cart(course_factory, cart):
    CartToolkit.remove_course_from_cart(cart=cart,
                                        course_slug=course_factory().slug)

    assert cart.courses.count() == 0
Exemplo n.º 25
0
 def update_totals(self) -> 'carts.Cart':
     """Updates cart totals. This method made to avoid circular imports"""
     from carts.services import CartToolkit
     CartToolkit.update_cart_totals(cart=self)
     return self
Exemplo n.º 26
0
def test_loading_active_and_inactive_cart_with_same_user_with_proper_id(
        cart, inactive_cart):
    test_cart = CartToolkit.load_cart(user=inactive_cart.user, cart_id=cart.id)

    assert test_cart == cart
Exemplo n.º 27
0
def test_cart_loading_with_user(cart):
    test_cart = CartToolkit.load_cart(user=cart.user)

    assert test_cart == cart
Exemplo n.º 28
0
def test_cart_loading_with_user_and_id_for_same_cart(cart):
    test_cart = CartToolkit.load_cart(user=cart.user, cart_id=cart.id)

    assert test_cart == cart
Exemplo n.º 29
0
def test_updating_cart_totals_for_empty_cart(cart):
    CartToolkit.update_cart_totals(cart=cart)

    cart.refresh_from_db()

    assert cart.subtotal == cart.total == 0