diff --git a/.gitignore b/.gitignore index 401214c..6f4cad7 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,6 @@ dmypy.json # Mac files .DS_Store + +# VS Code settings +.vscode/ \ No newline at end of file diff --git a/src/multisafepay/api/paths/orders/request/components/checkout_options.py b/src/multisafepay/api/paths/orders/request/components/checkout_options.py index 15bd773..b893d08 100644 --- a/src/multisafepay/api/paths/orders/request/components/checkout_options.py +++ b/src/multisafepay/api/paths/orders/request/components/checkout_options.py @@ -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( diff --git a/tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py b/tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py index e86319a..e9f21d7 100644 --- a/tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py +++ b/tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py @@ -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, ), ) @@ -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