....... [100%] 7 passed in 0.01s ... [100%] 3 passed in 0.01s behaviour (gate): 7/7 preserved structural (score): 3/3 refactor checks score: 100%
__pycache__/ *.pyc .pytest_cache/
"""Shopping-cart totals."""
from shop.pricing import line_total
FREE_SHIPPING_THRESHOLD = 50.0
SHIPPING_FEE = 4.99
def cart_total(items):
"""Grand total for a cart: discounted line items plus shipping.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
The per-line discount rules are identical to ``order_total``:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
Shipping (``SHIPPING_FEE``) is added unless the discounted subtotal reaches
``FREE_SHIPPING_THRESHOLD``.
"""
subtotal = 0.0
for item in items:
subtotal += line_total(item["price"], item["qty"], item["category"])
subtotal = round(subtotal, 2)
if subtotal >= FREE_SHIPPING_THRESHOLD:
return subtotal
return round(subtotal + SHIPPING_FEE, 2)
"""Order totals."""
from shop.pricing import line_total
def order_total(items):
"""Total price for a list of order line items.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
Per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
The discounted line totals are summed and rounded to 2 decimals.
"""
total = 0.0
for item in items:
total += line_total(item["price"], item["qty"], item["category"])
return round(total, 2)
"""Pricing helpers."""
def line_total(price, qty, category):
"""Return the discounted total for one line (price * qty, with discount applied).
Discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
"""
line = price * qty
if category == "clearance":
line *= 0.75
elif qty >= 10:
line *= 0.90
return line
import os
import sys
# Make the package importable no matter what directory pytest is invoked from.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from shop.orders import order_total
from shop.cart import cart_total
def test_order_no_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 20.0
def test_order_bulk_discount():
assert order_total([{"price": 10.0, "qty": 10, "category": "std"}]) == 90.0
def test_order_clearance_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "clearance"}]) == 15.0
def test_cart_adds_shipping_below_threshold():
assert cart_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 24.99
def test_cart_free_shipping_at_threshold():
assert cart_total([{"price": 10.0, "qty": 5, "category": "std"}]) == 50.0
....... [100%] 7 passed in 0.01s ... [100%] 3 passed in 0.01s behaviour (gate): 7/7 preserved structural (score): 3/3 refactor checks score: 100%
__pycache__/ *.pyc .pytest_cache/
"""Shopping-cart totals."""
from .pricing import line_total
FREE_SHIPPING_THRESHOLD = 50.0
SHIPPING_FEE = 4.99
def cart_total(items):
"""Grand total for a cart: discounted line items plus shipping.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
The per-line discount rules are identical to ``order_total``:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
Shipping (``SHIPPING_FEE``) is added unless the discounted subtotal reaches
``FREE_SHIPPING_THRESHOLD``.
"""
subtotal = 0.0
for item in items:
line = line_total(item["price"], item["qty"], item["category"])
subtotal += line
subtotal = round(subtotal, 2)
if subtotal >= FREE_SHIPPING_THRESHOLD:
return subtotal
return round(subtotal + SHIPPING_FEE, 2)
"""Order totals."""
from .pricing import line_total
def order_total(items):
"""Total price for a list of order line items.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
Per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
The discounted line totals are summed and rounded to 2 decimals.
"""
total = 0.0
for item in items:
line = line_total(item["price"], item["qty"], item["category"])
total += line
return round(total, 2)
"""Pricing helpers.
Provides functions for calculating line totals with discounts.
"""
def line_total(price, qty, category):
"""Return the discounted total for one line (price * qty, with discount applied).
Discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
"""
line = price * qty
if category == "clearance":
line *= 0.75
elif qty >= 10:
line *= 0.90
return line
# Discount arithmetic lives only here.
import os
import sys
# Make the package importable no matter what directory pytest is invoked from.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from shop.orders import order_total
from shop.cart import cart_total
def test_order_no_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 20.0
def test_order_bulk_discount():
assert order_total([{"price": 10.0, "qty": 10, "category": "std"}]) == 90.0
def test_order_clearance_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "clearance"}]) == 15.0
def test_cart_adds_shipping_below_threshold():
assert cart_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 24.99
def test_cart_free_shipping_at_threshold():
assert cart_total([{"price": 10.0, "qty": 5, "category": "std"}]) == 50.0
....... [100%] 7 passed in 0.01s ... [100%] 3 passed in 0.01s behaviour (gate): 7/7 preserved structural (score): 3/3 refactor checks score: 100%
__pycache__/ *.pyc .pytest_cache/
"""Shopping-cart totals."""
from shop.pricing import line_total
FREE_SHIPPING_THRESHOLD = 50.0
SHIPPING_FEE = 4.99
def cart_total(items):
"""Grand total for a cart: discounted line items plus shipping.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
The per-line discount rules are identical to ``order_total``:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
Shipping (``SHIPPING_FEE``) is added unless the discounted subtotal reaches
``FREE_SHIPPING_THRESHOLD``.
"""
subtotal = 0.0
for item in items:
subtotal += line_total(item["price"], item["qty"], item["category"])
subtotal = round(subtotal, 2)
if subtotal >= FREE_SHIPPING_THRESHOLD:
return subtotal
return round(subtotal + SHIPPING_FEE, 2)
"""Order totals."""
from shop.pricing import line_total
def order_total(items):
"""Total price for a list of order line items.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
Per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
The discounted line totals are summed and rounded to 2 decimals.
"""
total = 0.0
for item in items:
total += line_total(item["price"], item["qty"], item["category"])
return round(total, 2)
"""Pricing helpers."""
def line_total(price, qty, category):
"""Discounted total for a single line item.
Given ``price`` (float), ``qty`` (int) and ``category`` (str), applies per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
Returns the discounted line total as a float (no rounding).
"""
line = price * qty
if category == "clearance":
line *= 0.75
elif qty >= 10:
line *= 0.90
return line
import os
import sys
# Make the package importable no matter what directory pytest is invoked from.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from shop.orders import order_total
from shop.cart import cart_total
def test_order_no_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 20.0
def test_order_bulk_discount():
assert order_total([{"price": 10.0, "qty": 10, "category": "std"}]) == 90.0
def test_order_clearance_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "clearance"}]) == 15.0
def test_cart_adds_shipping_below_threshold():
assert cart_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 24.99
def test_cart_free_shipping_at_threshold():
assert cart_total([{"price": 10.0, "qty": 5, "category": "std"}]) == 50.0
....... [100%] 7 passed in 0.01s ... [100%] 3 passed in 0.01s behaviour (gate): 7/7 preserved structural (score): 3/3 refactor checks score: 100%
__pycache__/ *.pyc .pytest_cache/
"""Shopping-cart totals."""
from .pricing import line_total
FREE_SHIPPING_THRESHOLD = 50.0
SHIPPING_FEE = 4.99
def cart_total(items):
"""Grand total for a cart: discounted line items plus shipping.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
The per-line discount rules are identical to ``order_total``:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
Shipping (``SHIPPING_FEE``) is added unless the discounted subtotal reaches
``FREE_SHIPPING_THRESHOLD``.
"""
subtotal = 0.0
for item in items:
subtotal += line_total(item["price"], item["qty"], item["category"])
subtotal = round(subtotal, 2)
if subtotal >= FREE_SHIPPING_THRESHOLD:
return subtotal
return round(subtotal + SHIPPING_FEE, 2)
"""Order totals."""
from .pricing import line_total
def order_total(items):
"""Total price for a list of order line items.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
Per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
The discounted line totals are summed and rounded to 2 decimals.
"""
total = 0.0
for item in items:
total += line_total(item["price"], item["qty"], item["category"])
return round(total, 2)
"""Pricing helpers.
The per-line discount calculation has been extracted here as ``line_total(price, qty, category)``.
It is now used by ``orders.order_total`` and ``cart.cart_total``.
"""
def line_total(price, qty, category):
"""Return the discounted total for one line (price * qty, with the discount applied).
Discount rules per line:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
"""
line = price * qty
if category == "clearance":
line *= 0.75
elif qty >= 10:
line *= 0.90
return line
import os
import sys
# Make the package importable no matter what directory pytest is invoked from.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from shop.orders import order_total
from shop.cart import cart_total
def test_order_no_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 20.0
def test_order_bulk_discount():
assert order_total([{"price": 10.0, "qty": 10, "category": "std"}]) == 90.0
def test_order_clearance_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "clearance"}]) == 15.0
def test_cart_adds_shipping_below_threshold():
assert cart_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 24.99
def test_cart_free_shipping_at_threshold():
assert cart_total([{"price": 10.0, "qty": 5, "category": "std"}]) == 50.0
....... [100%] 7 passed in 0.01s ... [100%] 3 passed in 0.01s behaviour (gate): 7/7 preserved structural (score): 3/3 refactor checks score: 100%
__pycache__/ *.pyc .pytest_cache/
"""Shopping-cart totals."""
FREE_SHIPPING_THRESHOLD = 50.0
SHIPPING_FEE = 4.99
def cart_total(items):
"""Grand total for a cart: discounted line items plus shipping.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
The per-line discount rules are identical to ``order_total``:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
Shipping (``SHIPPING_FEE``) is added unless the discounted subtotal reaches
``FREE_SHIPPING_THRESHOLD``.
"""
from .pricing import line_total
subtotal = 0.0
for item in items:
subtotal += line_total(item["price"], item["qty"], item["category"])
subtotal = round(subtotal, 2)
if subtotal >= FREE_SHIPPING_THRESHOLD:
return subtotal
return round(subtotal + SHIPPING_FEE, 2)
"""Order totals."""
from .pricing import line_total
def order_total(items):
"""Total price for a list of order line items.
Each ``item`` is a dict with keys ``price`` (float), ``qty`` (int) and ``category`` (str).
Per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
The discounted line totals are summed and rounded to 2 decimals.
"""
total = 0.0
for item in items:
total += line_total(item["price"], item["qty"], item["category"])
return round(total, 2)
"""Pricing helpers.
Provides a single implementation of the per-line discount calculation.
"""
def line_total(price, qty, category):
"""Discounted total for one line (price * qty, with the discount applied).
Per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
"""
line = price * qty
if category == "clearance":
line *= 0.75
elif qty >= 10:
line *= 0.90
return line
__all__ = ["line_total"]
# The order_total and cart_total functions remain in their respective modules
# (orders.py and cart.py) to maintain the existing API.
# The TODO has been addressed by extracting the shared per-line discount
# calculation into this single function.
# Note: If needed, the pricing module can be expanded in the future.
# The discount arithmetic now lives only in this module.
# All existing tests should continue to pass as the behavior is unchanged.
import os
import sys
# Make the package importable no matter what directory pytest is invoked from.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from shop.orders import order_total
from shop.cart import cart_total
def test_order_no_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 20.0
def test_order_bulk_discount():
assert order_total([{"price": 10.0, "qty": 10, "category": "std"}]) == 90.0
def test_order_clearance_discount():
assert order_total([{"price": 10.0, "qty": 2, "category": "clearance"}]) == 15.0
def test_cart_adds_shipping_below_threshold():
assert cart_total([{"price": 10.0, "qty": 2, "category": "std"}]) == 24.99
def test_cart_free_shipping_at_threshold():
assert cart_total([{"price": 10.0, "qty": 5, "category": "std"}]) == 50.0