summaryrefslogtreecommitdiff
path: root/addons/product_margin/tests/test_product_margin.py
diff options
context:
space:
mode:
authorMehul Patel <mep@odoo.com>2017-04-17 18:53:29 +0530
committerYannick Tivisse <yti@odoo.com>2017-11-03 14:53:53 +0100
commit35182f45eb586b5805aeb01f4e9071a45d370d1b (patch)
treef6217086d12cbb5416569db2eef9e9a7701a1701 /addons/product_margin/tests/test_product_margin.py
parent20ab577531e3ed65843e3788b4e3756be3c827d2 (diff)
[IMP] product_margin: Replace yml test by a meaningful python test
Purpose ======= The original yml doesn't test anything except a read on the related fields. Specification ============= Replace the test by a python test that compares the values with the expected ones.
Diffstat (limited to 'addons/product_margin/tests/test_product_margin.py')
-rw-r--r--addons/product_margin/tests/test_product_margin.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/addons/product_margin/tests/test_product_margin.py b/addons/product_margin/tests/test_product_margin.py
new file mode 100644
index 00000000000..c67ffd5eb2f
--- /dev/null
+++ b/addons/product_margin/tests/test_product_margin.py
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+from odoo import tools
+from odoo.tests import common
+from odoo.modules.module import get_resource_path
+
+
+class TestProductMargin(common.TransactionCase):
+
+ def create_account_invoice(self, invoice_type, partner, product, quantity=0.0, price_unit=0.0):
+ """ Create an invoice as in a view by triggering its onchange methods"""
+ default_get_vals = self.env['account.invoice'].default_get(list(self.env['account.invoice'].fields_get()))
+ account_invoice = self.env['account.invoice'].new(default_get_vals)
+ account_invoice.type = invoice_type
+ account_invoice._onchange_journal_id()
+ account_invoice.partner_id = partner.id
+ account_invoice._onchange_partner_id()
+ account_invoice.invoice_line_ids = [(0, 0, {
+ 'quantity': quantity,
+ 'product_id': product.id,
+ 'uom_id': self.ref('product.product_uom_unit'),
+ 'invoice_id': account_invoice, # This value is required for '_onchange_product_id' to work correctly
+ })]
+ account_invoice.invoice_line_ids._onchange_product_id()
+ account_invoice.invoice_line_ids.price_unit = price_unit
+ account_invoice._onchange_invoice_line_ids()
+ vals = account_invoice._convert_to_write(account_invoice._cache)
+ account_invoice = self.env['account.invoice'].create(vals)
+ account_invoice.action_invoice_open()
+
+ def test_product_margin(self):
+ ''' In order to test the product_margin module '''
+
+ # load account_minimal_test.xml file for chart of account in configuration
+ tools.convert_file(self.cr, 'product_margin',
+ get_resource_path('account', 'test', 'account_minimal_test.xml'),
+ {}, 'init', False, 'test', self.registry._assertion_report)
+
+ supplier = self.env['res.partner'].create({'name': 'Supplier', 'supplier': True})
+ customer = self.env['res.partner'].create({'name': 'Customer', 'customer': True})
+ ipad = self.env.ref("product.product_product_4")
+
+ # Create supplier invoice and customer invoice to test product margin.
+ # Define supplier invoices
+ self.create_account_invoice('in_invoice', supplier, ipad, 10.0, 300.00)
+ self.create_account_invoice('in_invoice', supplier, ipad, 4.0, 450.00)
+ # Define Customer Invoices
+ self.create_account_invoice('out_invoice', customer, ipad, 20.0, 750.00)
+ self.create_account_invoice('out_invoice', customer, ipad, 10.0, 550.00)
+
+ result = ipad._compute_product_margin_fields_values()
+
+ # Sale turnover ( Quantity * Price Subtotal / Quantity)
+ sale_turnover = ((20.0 * 750.00) + (10.0 * 550.00))
+
+ # Expected sale (Total quantity * Sale price)
+ sale_expected = (750.00 * 30.0)
+
+ # Purchase total cost (Quantity * Unit price)
+ purchase_total_cost = ((10.0 * 300.00) + (4.0 * 450.00))
+
+ # Purchase normal cost ( Total quantity * Cost price)
+ purchase_normal_cost = (14.0 * 500.00)
+
+ total_margin = sale_turnover - purchase_total_cost
+ expected_margin = sale_expected - purchase_normal_cost
+
+ # Check total margin
+ self.assertEqual(result[ipad.id]['total_margin'], total_margin, "Wrong Total Margin.")
+
+ # Check expected margin
+ self.assertEqual(result[ipad.id]['expected_margin'], expected_margin, "Wrong Expected Margin.")