diff --git a/golem_resource/models/golem_resource.py b/golem_resource/models/golem_resource.py index 1afd6160..092a8129 100644 --- a/golem_resource/models/golem_resource.py +++ b/golem_resource/models/golem_resource.py @@ -19,6 +19,7 @@ """ GOLEM Resources management """ from odoo import models, fields, api +from odoo.exceptions import ValidationError class GolemResource(models.Model): @@ -30,7 +31,7 @@ class GolemResource(models.Model): name = fields.Char(required=True, index=True) active = fields.Boolean(default=True) - validation_required = fields.Boolean(default=True, + validation_required = fields.Boolean(default=False, string='Is validation required ?') type_id = fields.Many2one('golem.resource.type', index=True, string='Resource Type') @@ -53,3 +54,11 @@ class GolemResource(models.Model): """ Toggles active boolean """ for resource in self: resource.active = not resource.active + + @api.constrains('avaibility_start', 'avaibility_stop') + def _check_date_consistency(self): + """ Checks date consistency """ + for resource in self: + if resource.avaibility_stop <= resource.avaibility_start: + raise ValidationError(_('End availibility should be after than ' + 'start availibility')) diff --git a/golem_resource/tests/__init__.py b/golem_resource/tests/__init__.py new file mode 100644 index 00000000..4eb2e865 --- /dev/null +++ b/golem_resource/tests/__init__.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +# Copyright 2018 Youssef El Ouahby +# Copyright 2018 Fabien Bourgeois +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from . import test_golem_resource, test_golem_resource_timetable diff --git a/golem_resource/tests/test_golem_resource.py b/golem_resource/tests/test_golem_resource.py new file mode 100644 index 00000000..903b83d9 --- /dev/null +++ b/golem_resource/tests/test_golem_resource.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- + +# Copyright 2018 Youssef El Ouahby +# Copyright 2018 Fabien Bourgeois +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +""" GOLEM Resource testing """ + +from odoo.tests.common import TransactionCase +from odoo.exceptions import ValidationError + + +class TestGolemResource(TransactionCase): + """ GOLEM Resource testing """ + + def setUp(self, *args, **kwargs): + """ Bootstrap Resource """ + super(TestGolemResource, self).setUp(*args, **kwargs) + self.data = { + 'name': 'Resource', + 'avaibility_start': '2018-01-01', + 'avaibility_stop': '2020-01-01' + } + self.resource = self.env['golem.resource'] + + def test_resource_basic(self): + """ Test resource bases """ + resource = self.resource.create(self.data) + self.assertTrue(resource.active) + self.assertFalse(resource.validation_required) + self.assertEqual(resource.avaibility_start, '2018-01-01') + self.assertEqual(resource.avaibility_stop, '2020-01-01') + self.assertFalse(resource.supervisor_id) + self.assertFalse(resource.product_tmpl_id) + self.assertFalse(resource.timetable_ids) + self.assertFalse(resource.reservation_ids) + + def test_resource_active(self): + """ Test resource active """ + resource = self.resource.create(self.data) + self.assertTrue(resource.active) + resource.active_toggle() + self.assertFalse(resource.active) + resource.active_toggle() + self.assertTrue(resource.active) + + def test_resource_dates(self): + """ Test resource dates : stop can not be after start """ + self.data.update({'avaibility_stop': '2017-01-01'}) + with self.assertRaises(ValidationError): + self.resource.create(self.data) + + def test_resource_dates_equal(self): + """ Test resource dates : stop can not be equal to start """ + self.data.update({'avaibility_stop': self.data['avaibility_start']}) + with self.assertRaises(ValidationError): + self.resource.create(self.data)