diff --git a/modules/account_tax_rule_country/tests/test_module.py b/modules/account_tax_rule_country/tests/test_module.py index ca6a7decc9..c22ea50efe 100644 --- a/modules/account_tax_rule_country/tests/test_module.py +++ b/modules/account_tax_rule_country/tests/test_module.py @@ -1,6 +1,8 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +from unittest.mock import Mock + from trytond.modules.account.tests import create_chart from trytond.modules.company.tests import ( CompanyTestMixin, create_company, set_company) @@ -166,5 +168,210 @@ class AccountTaxRuleCountryTestCase(CompanyTestMixin, ModuleTestCase): self.assertListEqual(tax_rule.apply(tax, pattern), [tax.id]) + @with_transaction() + def test_check_tax_rule_pattern_no_origin(self): + "Check tax rule pattern without origin" + pool = Pool() + InvoiceLine = pool.get('account.invoice.line') + invoice_line = InvoiceLine(origin=None, invoice=None) + + pattern = invoice_line._get_tax_rule_pattern() + + self.assertEqual(pattern, pattern | { + 'from_country': None, + 'from_subdivision': None, + 'to_country': None, + 'to_subdivision': None, + }) + + @with_transaction() + def test_check_tax_rule_pattern_origin_sale_line(self): + "Check tax rule pattern with sale line origin" + pool = Pool() + Address = pool.get('party.address') + InvoiceLine = pool.get('account.invoice.line') + Location = pool.get('stock.location') + Sale = pool.get('sale.sale') + SaleLine = pool.get('sale.line') + + country1, country2 = self._create_countries() + subdivision1 = country1.subdivisions[0] + subdivision2 = country2.subdivisions[0] + warehouse = Mock(spec=Location, address=Mock( + spec=Address, country=country1, subdivision=subdivision1)) + invoice_line = InvoiceLine(origin=None, invoice=None) + invoice_line.origin = Mock( + spec=SaleLine, id=1, warehouse=warehouse, + sale=Mock(spec=Sale, shipment_address=Mock( + spec=Address, country=country2, subdivision=subdivision2))) + + for level in range(3): + with self.subTest(level=level): + pattern = invoice_line._get_tax_rule_pattern() + + self.assertEqual(pattern, pattern | { + 'from_country': country1.id, + 'from_subdivision': subdivision1.id, + 'to_country': country2.id, + 'to_subdivision': subdivision2.id, + }) + + invoice_line.origin = Mock( + spec=InvoiceLine, id=1, origin=invoice_line.origin) + + @with_transaction() + def test_check_tax_rule_pattern_origin_purchase_line(self): + "Check tax rule pattern with purchase line origin" + pool = Pool() + Address = pool.get('party.address') + InvoiceLine = pool.get('account.invoice.line') + Location = pool.get('stock.location') + Purchase = pool.get('purchase.purchase') + PurchaseLine = pool.get('purchase.line') + + country1, country2 = self._create_countries() + subdivision1 = country1.subdivisions[0] + subdivision2 = country2.subdivisions[0] + warehouse = Mock(spec=Location, address=Mock( + spec=Address, country=country1, subdivision=subdivision1)) + invoice_line = InvoiceLine(origin=None, invoice=None) + invoice_line.origin = Mock( + spec=PurchaseLine, id=1, warehouse=warehouse, + purchase=Mock(spec=Purchase, invoice_address=Mock( + spec=Address, country=country2, subdivision=subdivision2))) + for level in range(3): + with self.subTest(level=level): + pattern = invoice_line._get_tax_rule_pattern() + + self.assertEqual(pattern, pattern | { + 'from_country': country2.id, + 'from_subdivision': subdivision2.id, + 'to_country': country1.id, + 'to_subdivision': subdivision1.id, + }) + invoice_line.origin = Mock( + spec=InvoiceLine, id=1, origin=invoice_line.origin) + + @with_transaction() + def test_check_tax_rule_pattern_origin_stock_move_from_location(self): + "Check tax rule pattern with stock move from location origin" + pool = Pool() + Address = pool.get('party.address') + InvoiceLine = pool.get('account.invoice.line') + Location = pool.get('stock.location') + Move = pool.get('stock.move') + + country, _ = self._create_countries() + subdivision = country.subdivisions[0] + warehouse = Mock(spec=Location, address=Mock( + spec=Address, country=country, subdivision=subdivision)) + invoice_line = InvoiceLine(origin=None, invoice=None) + invoice_line.origin = Mock( + spec=Move, id=1, + from_location=Mock(spec=Location, warehouse=warehouse)) + for level in range(3): + with self.subTest(level=level): + pattern = invoice_line._get_tax_rule_pattern() + + self.assertEqual(pattern, pattern | { + 'from_country': country.id, + 'from_subdivision': subdivision.id, + }) + + invoice_line.origin = Mock( + spec=InvoiceLine, id=1, origin=invoice_line.origin) + + @with_transaction() + def test_check_tax_rule_pattern_origin_shipment_out_return(self): + "Check tax rule pattern with shipment out return origin" + pool = Pool() + Address = pool.get('party.address') + InvoiceLine = pool.get('account.invoice.line') + Location = pool.get('stock.location') + Move = pool.get('stock.move') + ShipmentOutReturn = pool.get('stock.shipment.out.return') + + country, _ = self._create_countries() + subdivision = country.subdivisions[0] + invoice_line = InvoiceLine(origin=None, invoice=None) + invoice_line.origin = Mock( + spec=Move, id=1, + from_location=Mock(spec=Location, warehouse=None), + origin=Mock(spec=ShipmentOutReturn, delivery_address=Mock( + spec=Address, country=country, subdivision=subdivision))) + + for level in range(3): + with self.subTest(level=level): + pattern = invoice_line._get_tax_rule_pattern() + + self.assertEqual(pattern, pattern | { + 'from_country': country.id, + 'from_subdivision': subdivision.id, + }) + + invoice_line.origin = Mock( + spec=InvoiceLine, id=1, origin=invoice_line.origin) + + @with_transaction() + def test_check_tax_rule_pattern_origin_stock_move_to_location(self): + "Check tax rule pattern with stock move to location origin" + pool = Pool() + Address = pool.get('party.address') + InvoiceLine = pool.get('account.invoice.line') + Location = pool.get('stock.location') + Move = pool.get('stock.move') + + country, _ = self._create_countries() + subdivision = country.subdivisions[0] + warehouse = Mock(spec=Location, address=Mock( + spec=Address, country=country, subdivision=subdivision)) + invoice_line = InvoiceLine(origin=None, invoice=None) + invoice_line.origin = Mock( + spec=Move, id=1, + to_location=Mock(spec=Location, warehouse=warehouse)) + + for level in range(3): + with self.subTest(level=level): + pattern = invoice_line._get_tax_rule_pattern() + + self.assertEqual(pattern, pattern | { + 'to_country': country.id, + 'to_subdivision': subdivision.id, + }) + + invoice_line.origin = Mock( + spec=InvoiceLine, id=1, origin=invoice_line.origin) + + @with_transaction() + def test_check_tax_rule_pattern_origin_shipment_out(self): + "Check tax rule pattern with shipment out origin" + pool = Pool() + Address = pool.get('party.address') + InvoiceLine = pool.get('account.invoice.line') + Location = pool.get('stock.location') + Move = pool.get('stock.move') + ShipmentOut = pool.get('stock.shipment.out') + + country, _ = self._create_countries() + subdivision = country.subdivisions[0] + invoice_line = InvoiceLine(origin=None, invoice=None) + invoice_line.origin = Mock( + spec=Move, id=1, + to_location=Mock(spec=Location, warehouse=None), + origin=Mock(spec=ShipmentOut, delivery_address=Mock( + spec=Address, country=country, subdivision=subdivision))) + + for level in range(3): + with self.subTest(level=level): + pattern = invoice_line._get_tax_rule_pattern() + + self.assertEqual(pattern, pattern | { + 'to_country': country.id, + 'to_subdivision': subdivision.id, + }) + + invoice_line.origin = Mock( + spec=InvoiceLine, id=1, origin=invoice_line.origin) + del ModuleTestCase