From 2af74e76e185af9bed7884a2226efc123f4b0214 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Date: Tue, 30 May 2017 17:33:17 +0200 Subject: [PATCH] add test cases --- stock_demand_estimate/tests/__init__.py | 6 + .../tests/test_stock_demand_estimate.py | 124 ++++++++++++++++++ .../wizards/stock_demand_estimate_wizard.py | 2 +- 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 stock_demand_estimate/tests/__init__.py create mode 100644 stock_demand_estimate/tests/test_stock_demand_estimate.py diff --git a/stock_demand_estimate/tests/__init__.py b/stock_demand_estimate/tests/__init__.py new file mode 100644 index 000000000..311ebdf8c --- /dev/null +++ b/stock_demand_estimate/tests/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Eficent Business and IT Consulting Services S.L. +# (http://www.eficent.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import test_stock_demand_estimate diff --git a/stock_demand_estimate/tests/test_stock_demand_estimate.py b/stock_demand_estimate/tests/test_stock_demand_estimate.py new file mode 100644 index 000000000..b8d3b4c01 --- /dev/null +++ b/stock_demand_estimate/tests/test_stock_demand_estimate.py @@ -0,0 +1,124 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Eficent Business and IT Consulting Services S.L. +# (http://www.eficent.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from openerp.tests import common +from dateutil.rrule import MONTHLY + + +class TestStockDemandEstimate(common.TransactionCase): + + def setUp(self): + super(TestStockDemandEstimate, self).setUp() + self.res_users_model = self.env['res.users'] + self.product_model = self.env['product.product'] + self.stock_location_model = self.env['stock.location'] + + self.g_stock_manager = self.env.ref('stock.group_stock_manager') + self.g_stock_user = self.env.ref('stock.group_stock_user') + self.company = self.env.ref('base.main_company') + + # Create users: + self.manager = self._create_user( + 'user_1', [self.g_stock_manager], self.company).id + self.user = self._create_user( + 'user_2', [self.g_stock_user], self.company).id + self.drt_monthly = self.env['date.range.type'].create( + {'name': 'Month', + 'allow_overlap': False}) + + generator = self.env['date.range.generator'] + generator = generator.create({ + 'date_start': '1943-01-01', + 'name_prefix': '1943-', + 'type_id': self.drt_monthly.id, + 'duration_count': 1, + 'unit_of_time': MONTHLY, + 'count': 12}) + generator.action_apply() + + # Create a product: + self.product1 = self.product_model.create({ + 'name': 'Test Product 1', + 'type': 'product', + 'default_code': 'PROD1', + }) + # Create a location: + self.location = self.stock_location_model.create({ + 'name': 'Place', + 'usage': 'production' + }) + + def _create_user(self, login, groups, company): + group_ids = [group.id for group in groups] + user = self.res_users_model.create({ + 'name': login, + 'login': login, + 'password': 'demo', + 'email': 'example@yourcompany.com', + 'company_id': company.id, + 'company_ids': [(4, company.id)], + 'groups_id': [(6, 0, group_ids)] + }) + return user + + def test_demand_estimate(self): + """Tests creation of demand estimates.""" + sheets = self.env['stock.demand.estimate.sheet'].search([]) + for sheet in sheets: + sheet.unlink() + wiz = self.env['stock.demand.estimate.wizard'] + wiz = wiz.create({ + 'date_start': '1943-01-01', + 'date_end': '1943-12-31', + 'location_id': self.location.id, + 'date_range_type_id': self.drt_monthly.id, + 'product_ids': [(6, 0, [self.product1.id])]}) + wiz.create_sheet() + sheets = self.env['stock.demand.estimate.sheet'].search([]) + for sheet in sheets: + + self.assertEquals(len(sheet.line_ids), 12, 'There should be 12 ' + 'lines.') + self.assertEquals(sheet.date_start, '1943-01-01', 'The date start ' + 'should be 1943-01-01') + self.assertEquals(sheet.date_end, '1943-12-31', 'The date end ' + 'should be ' + '1943-12-31') + self.assertEquals(sheet.location_id.id, self.location.id, + 'Wrong location') + self.assertEquals(sheet.product_ids.ids, [self.product1.id], + 'Wrong products') + for line in sheet.line_ids: + line.product_uom_qty = 1 + self.assertEquals(line.product_id.id, self.product1.id, + 'The product does not match in the line') + self.assertEquals(line.location_id.id, self.location.id, + 'The product does not match in the line') + sheet.button_validate() + estimates = self.env['stock.demand.estimate'].search([( + 'date_range_type_id', '=', self.drt_monthly)]) + self.assertEquals(len(estimates), 12, 'There should be 12 ' + 'estimate records.') + for estimate in estimates: + self.assertEquals(estimate.product_id.id, self.product1.id, + 'The product does not match in the estimate') + self.assertEquals(estimate.location_id.id, self.location.id, + 'The product does not match in the estimate') + + sheets = self.env['stock.demand.estimate.sheet'].search([]) + for sheet in sheets: + sheet.unlink() + wiz = self.env['stock.demand.estimate.wizard'] + wiz = wiz.create({ + 'date_start': '1943-01-01', + 'date_end': '1943-12-31', + 'location_id': self.location.id, + 'date_range_type_id': self.drt_monthly.id, + 'product_ids': [(6, 0, [self.product1.id])]}) + wiz.create_sheet() + sheets = self.env['stock.demand.estimate.sheet'].search([]) + for sheet in sheets: + for line in sheet.line_ids: + self.assertEquals(line.product_uom_qty,1, + 'The quantity should be 1') diff --git a/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py b/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py index dc4c8f405..b7f00a60e 100644 --- a/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py +++ b/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py @@ -136,7 +136,7 @@ class StockDemandEstimateSheetLine(models.TransientModel): estimate_id = fields.Many2one(comodel_name='stock.demand.estimate') date_range_id = fields.Many2one( - comodel_name='stock.demand.estimate.period', + comodel_name='date.range', string='Period') location_id = fields.Many2one(comodel_name='stock.location', string="Stock Location")