....... [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):
"""Compute the discounted total for a single line item.
Per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
Returns the discounted line total (price * qty with discount applied).
"""
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 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):
"""Compute the discounted total for a single line item.
Discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
"""
total = price * qty
if category == "clearance":
total *= 0.75
elif qty >= 10:
total *= 0.90
return total
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 = sum(line_total(item["price"], item["qty"], item["category"]) for item in items)
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 = sum(line_total(item["price"], item["qty"], item["category"]) for item in items)
return round(total, 2)
"""Pricing helpers."""
def line_total(price, qty, category):
"""Return the discounted total for a single line item.
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 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.
Per-line discount rules:
* ``clearance`` category: 25% off
* otherwise, quantity 10 or more: 10% off (bulk)
* otherwise: no discount
Returns the discounted line total (price * qty, with discount applied).
"""
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
from shop.pricing import line_total
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 a single line item.
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