예제 #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
예제 #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()
예제 #3
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
예제 #4
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
예제 #5
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
예제 #6
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
예제 #7
0
def add_course_to_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:
        cart = CartToolkit.add_course_to_cart(cart=cart_lists["cart"], course_slug=course_slug)
    except Course.DoesNotExist:
        return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400)

    CartListsToolkit.delete_duplicates_excluding_instance(course_slug=course_slug, instance=cart, **cart_lists)

    serializer = CartOnlyInfoSerializer(instance=cart)
    return Response(serializer.data, status=200)