Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ dmypy.json

# Mac files
.DS_Store

# VS Code settings
.vscode/
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ def generate_from_shopping_cart(
)
]

# reduce the array of items to unique tax tables
unique_tax_tables = {
item.tax_table_selector
for item in items_with_tax_table_selector
}

tax_rules = [
TaxRule(
name=str(item.tax_table_selector),
rules=[TaxRate(rate=item.tax_table_selector)],
name=str(tax_table_selector),
rules=[TaxRate(rate=tax_table_selector)],
)
for item in items_with_tax_table_selector
for tax_table_selector in unique_tax_tables
]
return CheckoutOptions(
tax_tables=CheckoutOptionsApiModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,23 @@ def test_generate_from_shopping_cart():
generated_checkout_options = CheckoutOptions.generate_from_shopping_cart(
shopping_cart,
)
# Sort both lists before comparing
generated_checkout_options.tax_tables.alternate = sorted(
generated_checkout_options.tax_tables.alternate,
key=lambda x: x.name,
)
expected_alternate = sorted(
[
TaxRule(name="0.21", rules=[TaxRate(rate=0.21)]),
TaxRule(name="0.09", rules=[TaxRate(rate=0.09)]),
TaxRule(name="0", rules=[TaxRate(rate=0.0)]),
],
key=lambda x: x.name,
)

test_checkout_options = CheckoutOptions(
tax_tables=CheckoutOptionsApiModel(
alternate=[
TaxRule(
name="0.21",
rules=[TaxRate(rate=0.21)],
),
TaxRule(
name="0.09",
rules=[TaxRate(rate=0.09)],
),
TaxRule(
name="0",
rules=[TaxRate(rate=0)],
),
],
alternate=expected_alternate,
),
)

Expand Down Expand Up @@ -149,3 +150,50 @@ def test_generate_from_shopping_cart_with_no_tax_table_selector():
tax_tables=CheckoutOptionsApiModel(default=None, alternate=[]),
validate_cart=None,
)


def test_generate_from_shopping_cart_with_items_with_same_tax_table_selector():
"""
Test the generate_from_shopping_cart method of CheckoutOptions with a shopping cart that has items with the same tax_table_selector.

This test creates a ShoppingCart with items that all have the same tax_table_selector (0.21) and checks if the generated CheckoutOptions matches the expected output.

"""
shopping_cart = ShoppingCart(
items=[
CartItem(
name="Geometric Candle Holders",
description="Geometric Candle Holders description",
unit_price=90,
quantity=3,
merchant_item_id="1111",
tax_table_selector=0.21,
weight=Weight(value=1.0, unit="kg"),
),
CartItem(
name="Geometric Candle Holders",
description="Geometric Candle Holders description",
unit_price=90,
quantity=3,
merchant_item_id="1111",
tax_table_selector=0.21,
weight=Weight(value=1.0, unit="kg"),
),
],
)
generated_checkout_options = CheckoutOptions.generate_from_shopping_cart(
shopping_cart,
)

test_checkout_options = CheckoutOptions(
tax_tables=CheckoutOptionsApiModel(
alternate=[
TaxRule(
name="0.21",
rules=[TaxRate(rate=0.21)],
),
],
),
)

assert generated_checkout_options == test_checkout_options