Skip to content

Saleor Core: the high performance, composable, headless commerce API.

fix _get_the_cheapest_line when there is no lines_info (#15506) (#15541)

Arture73066ac209d
diff --git a/saleor/checkout/fetch.py b/saleor/checkout/fetch.py
index 72a2c42177..3c880e35a9 100644
--- a/saleor/checkout/fetch.py
+++ b/saleor/checkout/fetch.py
@@ -410,8 +410,10 @@ def apply_voucher_to_checkout_line(
def _get_the_cheapest_line(
- lines_info: Iterable[CheckoutLineInfo],
-):
+ lines_info: Optional[Iterable[CheckoutLineInfo]],
+) -> Optional[CheckoutLineInfo]:
+ if not lines_info:
+ return None
return min(
lines_info, key=lambda line_info: line_info.channel_listing.discounted_price
)
diff --git a/saleor/graphql/checkout/tests/mutations/test_utils.py b/saleor/graphql/checkout/tests/mutations/test_utils.py
index 24ee36efad..751bdb4876 100644
--- a/saleor/graphql/checkout/tests/mutations/test_utils.py
+++ b/saleor/graphql/checkout/tests/mutations/test_utils.py
@@ -1,6 +1,10 @@
import graphene
-from .....checkout.fetch import fetch_checkout_lines
+from .....checkout.fetch import (
+ CheckoutLineInfo,
+ _get_the_cheapest_line,
+ fetch_checkout_lines,
+)
from ...mutations.utils import (
CheckoutLineData,
group_lines_input_data_on_update,
@@ -179,3 +183,32 @@ def test_group_on_update_when_one_line_and_mixed_parameters_provided(
assert expected == group_lines_input_data_on_update(
lines_data, existing_checkout_lines
)
+
+
+def test_get_the_cheapest_line_no_lines_provided():
+ # when
+ line_info = _get_the_cheapest_line(None)
+ # then
+ assert line_info is None
+
+
+def test_get_the_cheapest_line(checkout_with_items, channel_USD):
+ # given
+ lines = [
+ CheckoutLineInfo(
+ line=line,
+ channel_listing=line.variant.channel_listings.first(),
+ collections=[],
+ product=line.variant.product,
+ variant=line.variant,
+ discounts=list(line.discounts.all()),
+ rules_info=[],
+ product_type=line.variant.product.product_type,
+ channel=channel_USD,
+ )
+ for line in checkout_with_items.lines.all()
+ ]
+ # when
+ line_info = _get_the_cheapest_line(lines)
+ # then
+ assert line_info == lines[0]