From fe27ddd9684ca6f7f2ecf0140003b950f424bb92 Mon Sep 17 00:00:00 2001 From: Gabriel Davini <gabrielfranciscodavini@gmail.com> Date: Fri, 5 Jun 2020 17:39:49 -0300 Subject: [PATCH 1/2] [MOD] Allow to edit season_id field only 'draft' and 'sent' states --- .gitignore | 1 + views/sale_order_line.xml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 951a5b0..e42ce7b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ __pycache__ *.jasper .vscode setup.cfg +coverage/ diff --git a/views/sale_order_line.xml b/views/sale_order_line.xml index 73c57c3..85b69f7 100644 --- a/views/sale_order_line.xml +++ b/views/sale_order_line.xml @@ -35,7 +35,7 @@ </xpath> <xpath expr="/form/sheet/group/group/field[@name='validity_date']" position="after"> - <field name="season_id" domain="[('state','!=','closed')]"/> + <field name="season_id" domain="[('state','!=','closed')]" attrs="{'readonly': [('state', 'not in', ('draft', 'sent'))]}"/> </xpath> </field> -- GitLab From 0cec90c3df2c3d4799cb2240b9f3048d5bd8dd79 Mon Sep 17 00:00:00 2001 From: Gabriel Davini <gabrielfranciscodavini@gmail.com> Date: Thu, 11 Jun 2020 16:42:20 -0300 Subject: [PATCH 2/2] [ADD] Adds some tests for sale.order validity_date change and product.season state workflow and other minor changes. --- models/product_season.py | 14 ++++++------ tests/test_product_season.py | 29 +++++++++++++++++++------ tests/test_sale_order.py | 41 +++++++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/models/product_season.py b/models/product_season.py index 6568e8e..fcac4c0 100644 --- a/models/product_season.py +++ b/models/product_season.py @@ -8,22 +8,22 @@ class ProductImplantationLocation(models.Model): name = fields.Char(string="Name", required="True") starting_date = fields.Date(string="Starting Date", required="True") ending_date = fields.Date(string="Ending Date", required="True") - season_line_ids = fields.One2many("season.product.location.lines", "product_season_id", string='Season Lines') + season_line_ids = fields.One2many("season.product.location.lines", "product_season_id", + string='Season Lines') state = fields.Selection([ ('in_progress', 'In Progress'), ('closed', 'Closed'), - ], required=True, default='in_progress') + ], required=True, default='in_progress') @api.constrains('ending_date', 'starting_date') def date_constrains(self): for rec in self: if rec.ending_date < rec.starting_date: - raise exceptions.ValidationError(_('Sorry, End Date Must be greater Than Start Date')) + raise exceptions.ValidationError( + _('Sorry, End Date Must be greater Than Start Date')) def button_set_close(self): - for rec in self: - rec.write({'state': 'closed'}) + return self.write({'state': 'closed'}) def button_set_in_progress(self): - for rec in self: - rec.write({'state': 'in_progress'}) + return self.write({'state': 'in_progress'}) diff --git a/tests/test_product_season.py b/tests/test_product_season.py index 0d07a0b..4355943 100644 --- a/tests/test_product_season.py +++ b/tests/test_product_season.py @@ -1,30 +1,47 @@ from datetime import date, timedelta -import pytest +from freezegun import freeze_time +import pytest from odoo.exceptions import ValidationError from odoo.tests.common import TransactionCase -class TestModel(TransactionCase): +class TestProductSeason(TransactionCase): + @freeze_time("2018-01-01") def setUp(self): - super(TestModel, self).setUp() + super(TestProductSeason, self).setUp() self._season = self.env['product.season'].create({ - 'name': 'Season', + 'name': 'Test Season', 'starting_date': date.today(), 'ending_date': date.today() + timedelta(days=10) }) - def _modify_season_ending_date(self): + @freeze_time("2018-01-01") + def _modify_season_ending_date(self, days=-1): self._season.write({ - 'ending_date': date.today() - timedelta(days=1) + 'ending_date': date.today() + timedelta(days=days) }) def test_new_season(self): self.assertEqual(len(self._season), 1) def test_date_exception(self): + with pytest.raises(ValidationError, match='Must be greater'): + self._modify_season_ending_date(days=-10) + with pytest.raises(ValidationError, match='Must be greater'): self._modify_season_ending_date() + + self._modify_season_ending_date(days=0) + self._modify_season_ending_date(days=30) + + def test_button_set_close(self): + self._season.button_set_close() + assert self._season.state == "closed", "Season was not closed!" + + def test_button_set_in_progress(self): + self._season.button_set_in_progress() + assert self._season.state == "in_progress", "Season was not set to 'In Progress'!" diff --git a/tests/test_sale_order.py b/tests/test_sale_order.py index a5f3030..f2e593f 100644 --- a/tests/test_sale_order.py +++ b/tests/test_sale_order.py @@ -1,15 +1,17 @@ -from datetime import datetime +from datetime import datetime, timedelta +from freezegun import freeze_time import pytest +from odoo.fields import Date from odoo.exceptions import ValidationError from odoo.tests.common import TransactionCase -class TestModel(TransactionCase): +class TestSaleOrder(TransactionCase): def setUp(self): - super(TestModel, self).setUp() + super(TestSaleOrder, self).setUp() self.partner = self.env['res.partner'].create({'name': 'Partner'}) self.sale_order_line = self.env['sale.order.line'] @@ -108,3 +110,36 @@ class TestModel(TransactionCase): to_remove_order_line = self.new_order[0].order_line[0] to_remove_order_line.unlink() self.assertEqual(len(self.new_order[0].order_line), total_order_lines - 1) + + @freeze_time("2018-01-01") + def test_season_change(self): + season = self.env['product.season'].create({ + 'name': 'Test Season', + 'starting_date': Date.today(), + 'ending_date': Date.today() + timedelta(days=10) + }) + + # Test we find the current season + self.new_order.validity_date = Date.today() + self.new_order.onchange_date_order() + assert self.new_order.season_id == season, "Season does not match!" + + # 2 days ahead + self.new_order.validity_date = Date.today() + timedelta(days=2) + self.new_order.onchange_date_order() + assert self.new_order.season_id == season, "Season does not match!" + + # 10 days ahead + self.new_order.validity_date = Date.today() + timedelta(days=10) + self.new_order.onchange_date_order() + assert self.new_order.season_id == season, "Season does not match!" + + # 11 days ahead + self.new_order.validity_date = Date.today() + timedelta(days=11) + self.new_order.onchange_date_order() + assert self.new_order.season_id != season, "Season should be different!" + + # -1 days ahead + self.new_order.validity_date = Date.today() + timedelta(days=-1) + self.new_order.onchange_date_order() + assert not self.new_order.season_id, "Season should be different!" -- GitLab