optimize call of run_booking() + tests
This commit is contained in:
parent
95f06e8e26
commit
d800b17561
2 changed files with 38 additions and 21 deletions
11
planner.py
11
planner.py
|
@ -359,10 +359,12 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
|
|||
super(ScheduledBooking, cls).write(*args)
|
||||
cls.update_next_occurence(records)
|
||||
|
||||
def run_booking(self):
|
||||
@classmethod
|
||||
def run_booking(cls, records):
|
||||
""" do prepared booking
|
||||
"""
|
||||
print('-- booking:', self.rec_name)
|
||||
for record in records:
|
||||
print('-- booking:', record.rec_name)
|
||||
|
||||
@classmethod
|
||||
def cronjob(cls):
|
||||
|
@ -376,9 +378,8 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
|
|||
('active', '=', True),
|
||||
('nextrun.date', '<=', query_date)])
|
||||
|
||||
for record in records:
|
||||
record.run_booking()
|
||||
|
||||
if records:
|
||||
cls.run_booking(records)
|
||||
cls.update_next_occurence(
|
||||
records,
|
||||
query_date=query_date + timedelta(days=1))
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
# The COPYRIGHT file at the top level of this repository contains the
|
||||
# full copyright notices and license terms.
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
from trytond.tests.test_tryton import with_transaction
|
||||
from trytond.pool import Pool
|
||||
from trytond.transaction import Transaction
|
||||
|
@ -247,7 +248,9 @@ class PlannerTestCase(object):
|
|||
def test_planner_run_cronjobs(self):
|
||||
""" create job, check cron
|
||||
"""
|
||||
Planner = Pool().get('cashbook.planner')
|
||||
pool = Pool()
|
||||
Planner = pool.get('cashbook.planner')
|
||||
IrDate = pool.get('ir.date')
|
||||
|
||||
job = self.prep_create_job()
|
||||
self.assertEqual(
|
||||
|
@ -255,18 +258,31 @@ class PlannerTestCase(object):
|
|||
count=1, query_date=date(2022, 5, 1)), [
|
||||
date(2022, 5, 1)])
|
||||
|
||||
with Transaction().set_context({
|
||||
'nextrun_crondate': date(2022, 5, 24)}):
|
||||
# job was not yet run after configure
|
||||
IrDate.today = MagicMock(return_value=date(2022, 5, 24))
|
||||
Planner.run_booking = MagicMock()
|
||||
job, = Planner.search([])
|
||||
self.assertEqual(job.nextrun[0].date, date(2022, 5, 1))
|
||||
Planner.cronjob()
|
||||
self.assertEqual(job.nextrun[0].date, date(2022, 6, 1))
|
||||
Planner.run_booking.assert_called_with([job])
|
||||
|
||||
with Transaction().set_context({
|
||||
'nextrun_crondate': date(2022, 6, 1)}):
|
||||
job, = Planner.search([])
|
||||
# next call before due date - nothing should happen
|
||||
IrDate.today = MagicMock(return_value=date(2022, 5, 30))
|
||||
Planner.run_booking = MagicMock() # restart mock
|
||||
self.assertEqual(job.nextrun[0].date, date(2022, 6, 1))
|
||||
Planner.cronjob()
|
||||
self.assertEqual(job.nextrun[0].date, date(2022, 6, 1))
|
||||
Planner.run_booking.assert_not_called()
|
||||
|
||||
# next call at due date - calls booking and set due date
|
||||
IrDate.today = MagicMock(return_value=date(2022, 6, 1))
|
||||
Planner.run_booking = MagicMock() # restart mock
|
||||
self.assertEqual(job.nextrun[0].date, date(2022, 6, 1))
|
||||
Planner.cronjob()
|
||||
self.assertEqual(job.nextrun[0].date, date(2022, 7, 1))
|
||||
Planner.run_booking.assert_called_with([job])
|
||||
|
||||
IrDate.today = MagicMock(return_value=date.today())
|
||||
|
||||
# end PlannerTestCase
|
||||
|
|
Loading…
Reference in a new issue