def test_remove_not_added_course_from_s_list(course_factory, saved_for_later): SavedForLaterToolkit.remove_course_from_saved_for_later( s_list=saved_for_later, course_slug=course_factory().slug ) assert saved_for_later.courses.count() == 0
def test_add_course_with_wrong_slug_to_s_list(saved_for_later, course_factory): with pytest.raises(Course.DoesNotExist): SavedForLaterToolkit.add_course_to_saved_for_later( s_list=saved_for_later, course_slug="somerandomslug") saved_for_later.refresh_from_db() assert saved_for_later.courses.count() == 0
def test_add_courses_to_cart(courses_count, saved_for_later, course_factory): for i in range(courses_count): SavedForLaterToolkit.add_course_to_saved_for_later( s_list=saved_for_later, course_slug=course_factory().slug) saved_for_later.refresh_from_db() assert saved_for_later.courses.count() == courses_count
def test_remove_course_with_wrong_slug_from_s_list(saved_for_later): with pytest.raises(Course.DoesNotExist): SavedForLaterToolkit.remove_course_from_saved_for_later( s_list=saved_for_later, course_slug="somerandomslug" ) assert saved_for_later.courses.count() == 0
def test_add_course_to_s_list(saved_for_later, course_factory): course = course_factory() SavedForLaterToolkit.add_course_to_saved_for_later( s_list=saved_for_later, course_slug=course.slug, ) saved_for_later.refresh_from_db() assert saved_for_later.courses.count() == 1 assert saved_for_later.courses.first() == course
def test_loading_s_list_without_user(mocker, saved_for_later): create_new_s_list = mocker.patch( "carts.services.SavedForLaterToolkit._create_new_saved_for_later") test_s_list = SavedForLaterToolkit.load_saved_for_later( saved_for_later_id=saved_for_later.id) create_new_s_list.assert_called_once()
def test_loading_s_list_with_user_and_is_for_same_s_list(saved_for_later): test_s_list = SavedForLaterToolkit.load_saved_for_later( user=saved_for_later.user, saved_for_later_id=saved_for_later.id, ) assert test_s_list == saved_for_later
def test_remove_course_from_s_list(course_factory, saved_for_later, course_count): courses = [] for i in range(course_count): course = course_factory() saved_for_later.courses.add(course) courses.append(course) for i in range(course_count): SavedForLaterToolkit.remove_course_from_saved_for_later( s_list=saved_for_later, course_slug=courses[-1].slug ) assert courses[-1] not in saved_for_later.courses.all() assert saved_for_later.courses.count() == course_count - i - 1 courses.pop(-1)
def test_loading_s_list_with_user_and_different_id(saved_for_later, another_saved_for_later): test_s_list = SavedForLaterToolkit.load_saved_for_later( user=saved_for_later.user, saved_for_later_id=another_saved_for_later.id, ) assert test_s_list == saved_for_later
def remove_course_from_saved_for_later(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: SavedForLaterToolkit.remove_course_from_saved_for_later(s_list=cart_lists["saved_for_later"], course_slug=course_slug) except Course.DoesNotExist: return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400) return Response({}, status=200)
def test_loading_s_list_with_user_and_different_id_with_courses( s_list_with_course, s_list_without_user_with_course): course = s_list_without_user_with_course.courses.first() test_s_list = SavedForLaterToolkit.load_saved_for_later( user=s_list_with_course.user, saved_for_later_id=s_list_without_user_with_course.id, ) assert test_s_list == s_list_with_course assert not SavedForLater.objects.filter( id=s_list_without_user_with_course.id).exists() assert s_list_with_course.courses.count() == 2 assert course in s_list_with_course.courses.all()
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, }
def add_course_to_saved_for_later_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: saved_for_later = SavedForLaterToolkit.add_course_to_saved_for_later(s_list=cart_lists["saved_for_later"], 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=saved_for_later, **cart_lists) serializer = CartOnlyInfoSerializer(instance=cart_lists["cart"]) return Response(serializer.data, status=200)
def test_loading_s_list_with_user(saved_for_later): test_s_list = SavedForLaterToolkit.load_saved_for_later( user=saved_for_later.user) assert test_s_list == saved_for_later