test_product_product.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Antiun Ingenieria S.L. - Javier Iniesta
  3. # © 2015 Pedro M. Baeza - Serv. Tecnol. Avanzados
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp.tests.common import TransactionCase
  6. class TestProductProduct(TransactionCase):
  7. def setUp(self):
  8. super(TestProductProduct, self).setUp()
  9. self.template_model = self.env['product.template']
  10. self.product_model = self.env['product.product']
  11. self.attribute = self.env['product.attribute'].create(
  12. {'name': 'Test attribute'})
  13. self.value1 = self.env['product.attribute.value'].create(
  14. {'name': 'Test value 1',
  15. 'attribute_id': self.attribute.id})
  16. self.value2 = self.env['product.attribute.value'].create(
  17. {'name': 'Test value 2',
  18. 'attribute_id': self.attribute.id})
  19. self.product = self.product_model.create(
  20. {'name': 'Product',
  21. 'standard_price': 15})
  22. self.template_single = self.template_model.create(
  23. {'name': 'Template',
  24. 'attribute_line_ids': [
  25. [0, 0, {'attribute_id': self.attribute.id,
  26. 'value_ids': [[6, 0, [self.value1.id]]]}]]})
  27. self.product_single = self.template_single.product_variant_ids[0]
  28. self.template_multi = self.template_model.create(
  29. {'name': 'Template multi',
  30. 'standard_price': 5})
  31. self.product_multi_1 = self.product_model.create(
  32. {'name': 'Product 01',
  33. 'product_tmpl_id': self.template_multi.id,
  34. 'standard_price': 10})
  35. self.product_multi_2 = self.product_model.create(
  36. {'name': 'Product 02',
  37. 'product_tmpl_id': self.template_multi.id,
  38. 'standard_price': 20})
  39. self.product_multi_3 = self.product_model.create(
  40. {'name': 'Product 03',
  41. 'product_tmpl_id': self.template_multi.id,
  42. 'standard_price': 30})
  43. def test_default_cost_method(self):
  44. self.assertEqual(
  45. self.product.cost_method, self.template_single.cost_method)
  46. def test_propagate_cost(self):
  47. # From product to template
  48. self.product_single.standard_price = 100
  49. self.assertEqual(self.template_single.standard_price, 100)
  50. prev_price = self.template_multi.standard_price
  51. self.product_multi_1.standard_price = 100
  52. self.assertEqual(self.template_multi.standard_price, prev_price)
  53. # From template to product
  54. self.template_multi.standard_price = 100
  55. def test_product_price_history(self):
  56. price_history_obj = self.env['product.price.history.product']
  57. history = price_history_obj.search(
  58. [('product_id', '=', self.product.id)])
  59. self.assertEqual(self.product.standard_price, history.cost)
  60. self.product.standard_price = 25.00
  61. history = price_history_obj.search(
  62. [('product_id', '=', self.product.id)])
  63. self.assertEqual(len(history), 2)
  64. def test_product_template_price_history(self):
  65. price_history_obj = self.env['product.price.history']
  66. variant_history_obj = self.env['product.price.history.product']
  67. history = price_history_obj.search(
  68. [('product_template_id', '=', self.template_single.id)])
  69. variant_history = variant_history_obj.search(
  70. [('product_id', '=', self.product_single.id)])
  71. self.assertEqual(len(history), 1)
  72. self.assertEqual(len(variant_history), 1)
  73. self.assertEqual(self.template_single.standard_price, history.cost)
  74. self.template_single.standard_price = 20.00
  75. history = price_history_obj.search(
  76. [('product_template_id', '=', self.template_single.id)])
  77. variant_history = variant_history_obj.search(
  78. [('product_id', '=', self.product_single.id)])
  79. self.assertEqual(self.product_single.standard_price,
  80. self.template_single.standard_price)
  81. self.assertEqual(len(history), 2)
  82. self.assertEqual(len(variant_history), 2)
  83. def test_product_variant_cost_prices(self):
  84. self.assertEqual(self.template_multi.standard_price, 5)
  85. self.assertEqual(self.product_multi_1.standard_price, 10)
  86. self.assertEqual(self.product_multi_2.standard_price, 20)
  87. self.assertEqual(self.product_multi_3.standard_price, 30)
  88. def test_new_variant_creation_prices(self):
  89. price_history_obj = self.env['product.price.history']
  90. variant_history_obj = self.env['product.price.history.product']
  91. self.template_single.write(
  92. {'attribute_line_ids': [
  93. [1, self.template_single.attribute_line_ids[:1].id,
  94. {'value_ids': [[6, 0, [self.value1.id, self.value2.id]]]}]]})
  95. history = price_history_obj.search(
  96. [('product_template_id', '=', self.template_single.id)])
  97. self.assertEqual(len(history), 1)
  98. for product in self.template_single.product_variant_ids:
  99. variant_history = variant_history_obj.search(
  100. [('product_id', '=', product.id)])
  101. self.assertEqual(len(variant_history), 1)
  102. self.assertEqual(self.template_single.standard_price,
  103. product.standard_price)
  104. def test_synchro_cost_method_single(self):
  105. self.template_single.cost_method = 'real'
  106. self.assertEqual(
  107. self.product_single.cost_method, self.template_single.cost_method,
  108. 'Not synchronized from template.')
  109. self.product_single.cost_method = 'average'
  110. self.assertEqual(
  111. self.template_single.cost_method, self.product_single.cost_method,
  112. 'Not synchronized from variant')
  113. def test_synchro_cost_method_multi(self):
  114. self.template_multi.cost_method = 'average'
  115. self.product_multi_1.cost_method = 'real'
  116. self.assertEqual(
  117. self.template_multi.cost_method, 'average',
  118. 'Bad synchronize when multiple variants for template.')
  119. self.template_multi.cost_method = 'standard'
  120. self.assertEqual(
  121. self.product_multi_1.cost_method, 'real',
  122. 'Bad synchronize when multiple variants for template.')