Skip to content

Commit 26cb336

Browse files
authored
PTHMINT-78: Avoid duplicate taxes when checkout options is autogenerated (#29)
1 parent 14f1825 commit 26cb336

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,6 @@ dmypy.json
130130

131131
# Mac files
132132
.DS_Store
133+
134+
# VS Code settings
135+
.vscode/

src/multisafepay/api/paths/orders/request/components/checkout_options.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,18 @@ def generate_from_shopping_cart(
110110
)
111111
]
112112

113+
# reduce the array of items to unique tax tables
114+
unique_tax_tables = {
115+
item.tax_table_selector
116+
for item in items_with_tax_table_selector
117+
}
118+
113119
tax_rules = [
114120
TaxRule(
115-
name=str(item.tax_table_selector),
116-
rules=[TaxRate(rate=item.tax_table_selector)],
121+
name=str(tax_table_selector),
122+
rules=[TaxRate(rate=tax_table_selector)],
117123
)
118-
for item in items_with_tax_table_selector
124+
for tax_table_selector in unique_tax_tables
119125
]
120126
return CheckoutOptions(
121127
tax_tables=CheckoutOptionsApiModel(

tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,23 @@ def test_generate_from_shopping_cart():
6060
generated_checkout_options = CheckoutOptions.generate_from_shopping_cart(
6161
shopping_cart,
6262
)
63+
# Sort both lists before comparing
64+
generated_checkout_options.tax_tables.alternate = sorted(
65+
generated_checkout_options.tax_tables.alternate,
66+
key=lambda x: x.name,
67+
)
68+
expected_alternate = sorted(
69+
[
70+
TaxRule(name="0.21", rules=[TaxRate(rate=0.21)]),
71+
TaxRule(name="0.09", rules=[TaxRate(rate=0.09)]),
72+
TaxRule(name="0", rules=[TaxRate(rate=0.0)]),
73+
],
74+
key=lambda x: x.name,
75+
)
76+
6377
test_checkout_options = CheckoutOptions(
6478
tax_tables=CheckoutOptionsApiModel(
65-
alternate=[
66-
TaxRule(
67-
name="0.21",
68-
rules=[TaxRate(rate=0.21)],
69-
),
70-
TaxRule(
71-
name="0.09",
72-
rules=[TaxRate(rate=0.09)],
73-
),
74-
TaxRule(
75-
name="0",
76-
rules=[TaxRate(rate=0)],
77-
),
78-
],
79+
alternate=expected_alternate,
7980
),
8081
)
8182

@@ -149,3 +150,50 @@ def test_generate_from_shopping_cart_with_no_tax_table_selector():
149150
tax_tables=CheckoutOptionsApiModel(default=None, alternate=[]),
150151
validate_cart=None,
151152
)
153+
154+
155+
def test_generate_from_shopping_cart_with_items_with_same_tax_table_selector():
156+
"""
157+
Test the generate_from_shopping_cart method of CheckoutOptions with a shopping cart that has items with the same tax_table_selector.
158+
159+
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.
160+
161+
"""
162+
shopping_cart = ShoppingCart(
163+
items=[
164+
CartItem(
165+
name="Geometric Candle Holders",
166+
description="Geometric Candle Holders description",
167+
unit_price=90,
168+
quantity=3,
169+
merchant_item_id="1111",
170+
tax_table_selector=0.21,
171+
weight=Weight(value=1.0, unit="kg"),
172+
),
173+
CartItem(
174+
name="Geometric Candle Holders",
175+
description="Geometric Candle Holders description",
176+
unit_price=90,
177+
quantity=3,
178+
merchant_item_id="1111",
179+
tax_table_selector=0.21,
180+
weight=Weight(value=1.0, unit="kg"),
181+
),
182+
],
183+
)
184+
generated_checkout_options = CheckoutOptions.generate_from_shopping_cart(
185+
shopping_cart,
186+
)
187+
188+
test_checkout_options = CheckoutOptions(
189+
tax_tables=CheckoutOptionsApiModel(
190+
alternate=[
191+
TaxRule(
192+
name="0.21",
193+
rules=[TaxRate(rate=0.21)],
194+
),
195+
],
196+
),
197+
)
198+
199+
assert generated_checkout_options == test_checkout_options

0 commit comments

Comments
 (0)