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