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
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()
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
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
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
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
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)