[FIX] (website_)sale: make test non-reliant on currency rate demo

Without this fix, the test does not work at the start of the year, because it is
reliant on a date that is hard-coded on the currency rate demo.

Instead of changing the demo, this commit will make the test stronger by not
depending on it.

PR: #29850
This commit is contained in:
Sébastien Theys
2019-01-02 12:03:26 +00:00
parent 76e46b02a1
commit 8ad348bf96
2 changed files with 64 additions and 22 deletions
@@ -1,24 +1,70 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields
from odoo.addons.product.tests.test_product_attribute_value_config import TestProductAttributeValueSetup
from odoo.tests import tagged
class TestSaleProductAttributeValueSetup(TestProductAttributeValueSetup):
def _setup_currency(self, currency_ratio=2):
"""Get or create a currency. This makes the test non-reliant on demo.
With an easy currency rate, for a simple 2 ratio in the following tests.
"""
from_currency = self.computer.currency_id
self._set_or_create_rate_today(from_currency, rate=1)
to_currency = self._get_or_create_currency("my currency", "C")
self._set_or_create_rate_today(to_currency, currency_ratio)
return to_currency
def _set_or_create_rate_today(self, currency, rate):
"""Get or create a currency rate for today. This makes the test
non-reliant on demo data."""
name = fields.Date.today()
currency_id = currency.id
company_id = self.env.user.company_id.id
CurrencyRate = self.env['res.currency.rate']
currency_rate = CurrencyRate.search([
('company_id', '=', company_id),
('currency_id', '=', currency_id),
('name', '=', name),
])
if currency_rate:
currency_rate.rate = rate
else:
CurrencyRate.create({
'company_id': company_id,
'currency_id': currency_id,
'name': name,
'rate': rate,
})
def _get_or_create_currency(self, name, symbol):
"""Get or create a currency based on name. This makes the test
non-reliant on demo data."""
currency = self.env['res.currency'].search([('name', '=', name)])
return currency or currency.create({
'name': name,
'symbol': symbol,
})
@tagged('post_install', '-at_install')
class TestSaleProductAttributeValueConfig(TestProductAttributeValueSetup):
def _setup_pricelist(self):
# easy currency rate, for a simple 2 ratio in the following tests
self.computer.currency_id.rate_ids[0].rate = 1
currency_ratio = 2
currency = self.env['res.currency'].browse(1 if self.computer.currency_id.id != 1 else 2)
currency.rate_ids[0].rate = currency_ratio
class TestSaleProductAttributeValueConfig(TestSaleProductAttributeValueSetup):
def _setup_pricelist(self, currency_ratio=2):
to_currency = self._setup_currency(currency_ratio)
discount = 10
pricelist = self.env['product.pricelist'].create({
'name': 'test pl',
'currency_id': currency.id,
'currency_id': to_currency.id,
'company_id': self.computer.company_id.id,
})
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.product.tests.test_product_attribute_value_config import TestProductAttributeValueSetup
from odoo.addons.sale.tests.test_sale_product_attribute_value_config import TestSaleProductAttributeValueSetup
from odoo.tests import tagged
@tagged('post_install', '-at_install')
class TestWebsiteSaleProductAttributeValueConfig(TestProductAttributeValueSetup):
class TestWebsiteSaleProductAttributeValueConfig(TestSaleProductAttributeValueSetup):
def test_get_combination_info(self):
current_website = self.env['website'].get_current_website()
@@ -27,12 +27,8 @@ class TestWebsiteSaleProductAttributeValueConfig(TestProductAttributeValueSetup)
self.computer.taxes_id = tax
tax_ratio = (100 + tax.amount) / 100
# easy currency rate, for a simple 2 ratio in the following tests
self.computer.currency_id.rate_ids[0].rate = 1
ratio = 2
currency = self.env['res.currency'].browse(1 if self.computer.currency_id.id != 1 else 2)
currency.rate_ids[0].rate = ratio
pricelist.currency_id = currency
currency_ratio = 2
pricelist.currency_id = self._setup_currency(currency_ratio)
# ensure pricelist is set to with_discount
pricelist.discount_policy = 'with_discount'
@@ -43,8 +39,8 @@ class TestWebsiteSaleProductAttributeValueConfig(TestProductAttributeValueSetup)
self.env.ref('account.group_show_line_subtotals_tax_included').users -= self.env.user
combination_info = self.computer._get_combination_info()
self.assertEqual(combination_info['price'], 2222 * discount_rate * ratio)
self.assertEqual(combination_info['list_price'], 2222 * discount_rate * ratio)
self.assertEqual(combination_info['price'], 2222 * discount_rate * currency_ratio)
self.assertEqual(combination_info['list_price'], 2222 * discount_rate * currency_ratio)
self.assertEqual(combination_info['has_discounted_price'], False)
# CASE: B2C setting
@@ -52,8 +48,8 @@ class TestWebsiteSaleProductAttributeValueConfig(TestProductAttributeValueSetup)
self.env.ref('account.group_show_line_subtotals_tax_excluded').users -= self.env.user
combination_info = self.computer._get_combination_info()
self.assertEqual(combination_info['price'], 2222 * discount_rate * ratio * tax_ratio)
self.assertEqual(combination_info['list_price'], 2222 * discount_rate * ratio * tax_ratio)
self.assertEqual(combination_info['price'], 2222 * discount_rate * currency_ratio * tax_ratio)
self.assertEqual(combination_info['list_price'], 2222 * discount_rate * currency_ratio * tax_ratio)
self.assertEqual(combination_info['has_discounted_price'], False)
# CASE: pricelist 'without_discount'
@@ -63,6 +59,6 @@ class TestWebsiteSaleProductAttributeValueConfig(TestProductAttributeValueSetup)
# ideally we would need to use compare_amounts everywhere, but this is
# the only rounding where it fails without it
combination_info = self.computer._get_combination_info()
self.assertEqual(pricelist.currency_id.compare_amounts(combination_info['price'], 2222 * discount_rate * ratio * tax_ratio), 0)
self.assertEqual(pricelist.currency_id.compare_amounts(combination_info['list_price'], 2222 * ratio * tax_ratio), 0)
self.assertEqual(pricelist.currency_id.compare_amounts(combination_info['price'], 2222 * discount_rate * currency_ratio * tax_ratio), 0)
self.assertEqual(pricelist.currency_id.compare_amounts(combination_info['list_price'], 2222 * currency_ratio * tax_ratio), 0)
self.assertEqual(combination_info['has_discounted_price'], True)