You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.8 KiB

  1. # -*- coding: utf-8 -*-
  2. from collections import namedtuple
  3. from openerp.tests.common import TransactionCase
  4. from openerp.addons.stock.product import product_template as ProductTemplate
  5. from openerp.addons.stock.product import product_product as ProductProduct
  6. import datetime as dt
  7. # fixme setup tests based on demo data, test on a clean database
  8. _datetimes = map(
  9. lambda d: d.strftime('%Y-%m-%d %H:%M:%S'),
  10. (dt.datetime.now() - dt.timedelta(days=d) for d in range(0, 24, 2)))
  11. _quantities = [0.64, 6.45, 9.65, 1.76, 9.14, 3.99,
  12. 6.92, 2.25, 6.91, 1.44, 6.52, 1.44]
  13. class TestProductTemplate(TransactionCase):
  14. def setUp(self, *args, **kwargs):
  15. result = super(TestProductTemplate, self).setUp(*args, **kwargs)
  16. test_product_template = (
  17. self.env['product.template']
  18. .create({'name': 'test product template',
  19. 'calculation_range': 14,
  20. 'consumption_calculation_method': 'sales_history',
  21. 'product_template_id': 0,
  22. })
  23. )
  24. pid = (
  25. self.env['product.product']
  26. .search([('product_tmpl_id', '=', test_product_template.id)])
  27. .ids
  28. ).pop()
  29. for date, qty in zip(_datetimes, _quantities):
  30. (self.env['pos.order.line']
  31. .create({'create_date': date,
  32. 'qty': qty,
  33. 'product_id': pid,
  34. })
  35. )
  36. def _product_available(*args, **kwargs):
  37. products = (
  38. self.env['product.product']
  39. .search([
  40. ('product_tmpl_id', '=', test_product_template.id)])
  41. )
  42. mock_data = {
  43. 'qty_available': 53.2,
  44. 'incoming_qty': 14,
  45. 'outgoing_qty': 4.1,
  46. 'virtual_available': 53.2 + 14 - 4.1,
  47. }
  48. return {pid: mock_data for pid in products.ids}
  49. # mock area
  50. # ProductTemplate._product_available = _product_available
  51. # ProductProduct._product_available = _product_available
  52. # Order = namedtuple('Order', ['id', 'state'])
  53. # PosOrderLine.order_id = Order('1', 'done')
  54. test_product_template._compute_total_consumption()
  55. self.product_template_id = test_product_template.id
  56. return result
  57. def test_create(self):
  58. """Create a simple product template"""
  59. Template = self.env['product.template']
  60. product = Template.create({'name': 'Test create product'})
  61. self.assertEqual(product.name, 'Test create product')
  62. def test_compute_average_daily_consumption(self):
  63. """Test computed field average_daily_consumption"""
  64. ProductTemplate = self.env['product.template']
  65. product_template = ProductTemplate.browse(self.product_template_id)
  66. computed_value = product_template.average_consumption
  67. expected_value = 4.08
  68. self.assertAlmostEqual(computed_value, expected_value, 7)
  69. def test_compute_total_consumption(self):
  70. """Test total consumption was computed in setup"""
  71. ProductTemplate = self.env['product.template']
  72. product_template = ProductTemplate.browse(self.product_template_id)
  73. computed_value = product_template.total_consumption
  74. expected_value = 57.11
  75. self.assertAlmostEqual(computed_value, expected_value)
  76. # def test_compute_estimated_stock_coverage(self):
  77. # """Test computed field estimated_stock_coverage"""
  78. # ProductTemplate = self.env['product.template']
  79. # product_template = ProductTemplate.browse(self.product_template_id)
  80. # computed_value = product_template.estimated_stock_coverage
  81. # expected_value = 13.04
  82. # self.assertAlmostEqual(computed_value, expected_value)