[TMP] Test if date_start date_end is required and vice-versa AND unit test

This commit is contained in:
michel 2018-01-17 15:44:43 +01:00
parent 8cf4a1f07f
commit e7ba4b91f8
2 changed files with 26 additions and 29 deletions

View File

@ -42,12 +42,11 @@ class GolemSeason(models.Model):
def _onchange_season_dates(self):
""" Sets defaults dates according to membership type """
for season in self:
if season.membership_id:
if not season.date_start:
season.update({
'date_start': season.membership_id.membership_date_from,
'date_end': season.membership_id.membership_date_to
})
if season.membership_id and not season.date_start:
season.update({
'date_start': season.membership_id.membership_date_from,
'date_end': season.membership_id.membership_date_to
})
@api.constrains('date_start', 'date_end')
def _check_period(self):
@ -59,25 +58,24 @@ class GolemSeason(models.Model):
raise models.ValidationError(_('The date end is required'))
elif season.date_end and not season.date_start:
raise models.ValidationError(_('The date start is required'))
if season.date_start > season.date_end:
raise models.ValidationError(_('Start of the period cannot be '
'after end of the period.'))
else:
seasons = self.env['golem.season'].search([])
for eachs in seasons:
if eachs.date_start < season.date_start < eachs.date_end:
msg = _(u'Start of the period is in range of an '
'existing period.')
raise models.ValidationError(msg)
if eachs.date_start < season.date_end < eachs.date_end:
msg = _(u'End of the period is in range of an '
'existing period.')
raise models.ValidationError(msg)
if season.date_start < eachs.date_start < season.date_end:
msg = _(u'Current period cannot be included into '
'another existing period.')
raise models.ValidationError(msg)
if season.date_start > season.date_end:
raise models.ValidationError(_('Start of the period cannot be '
'after end of the period.'))
else:
seasons = self.env['golem.season'].search([])
for eachs in seasons:
if eachs.date_start < season.date_start < eachs.date_end:
msg = _(u'Start of the period is in range of an '
'existing period.')
raise models.ValidationError(msg)
if eachs.date_start < season.date_end < eachs.date_end:
msg = _(u'End of the period is in range of an '
'existing period.')
raise models.ValidationError(msg)
if season.date_start < eachs.date_start < season.date_end:
msg = _(u'Current period cannot be included into '
'another existing period.')
raise models.ValidationError(msg)
is_default = fields.Boolean('Default season for views?', readonly=True)

View File

@ -74,9 +74,8 @@ class TestGolemSeason(TransactionCase):
'membership_date_to': '2018-11-01'})
new_season = self.env['golem.season'].create({'name': 'Name',
'membership_id': membership.id})
new_season._onchange_season_dates()
'membership_id': membership.id,
'membership_date_from': '2017-11-01',
'membership_date_to': '2018-11-01'})
self.assertEqual(new_season.date_start, membership.membership_date_from)
self.assertEqual(new_season.date_end, membership.membership_date_to)