2024-02-24 14:36:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# This file is part of the cashbook-planner from m-ds for Tryton.
|
|
|
|
# The COPYRIGHT file at the top level of this repository contains the
|
|
|
|
# full copyright notices and license terms.
|
|
|
|
|
|
|
|
from trytond.tests.test_tryton import with_transaction
|
|
|
|
from trytond.pool import Pool
|
|
|
|
from trytond.transaction import Transaction
|
|
|
|
from trytond.exceptions import UserError
|
2024-02-25 22:10:40 +00:00
|
|
|
|
2024-02-24 14:36:15 +00:00
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
|
|
|
|
class PlannerTestCase(object):
|
|
|
|
""" test planner
|
|
|
|
"""
|
2024-02-25 22:10:40 +00:00
|
|
|
def prep_create_job(self, name='Job 1'):
|
|
|
|
pool = Pool()
|
|
|
|
Book = pool.get('cashbook.book')
|
|
|
|
Planner = pool.get('cashbook.planner')
|
|
|
|
|
|
|
|
types = self.prep_type()
|
|
|
|
company = self.prep_company()
|
|
|
|
job = None
|
|
|
|
with Transaction().set_context({
|
|
|
|
'company': company.id,
|
2024-02-29 22:20:19 +00:00
|
|
|
'nextrun_querydate': date(2022, 5, 1)}):
|
2024-02-25 22:10:40 +00:00
|
|
|
book, = Book.create([{
|
|
|
|
'name': 'Book 1',
|
|
|
|
'btype': types.id,
|
|
|
|
'company': company.id,
|
|
|
|
'currency': company.currency.id,
|
|
|
|
'number_sequ': self.prep_sequence().id,
|
|
|
|
'start_date': date(2022, 5, 1),
|
|
|
|
}])
|
|
|
|
self.assertEqual(book.rec_name, 'Book 1 | 0.00 usd | Open')
|
|
|
|
|
|
|
|
job, = Planner.create([{
|
|
|
|
'cashbook': book.id,
|
|
|
|
'name': name,
|
|
|
|
'start_date': date(2022, 5, 1)}])
|
|
|
|
# check applied defaults
|
|
|
|
self.assertEqual(job.rec_name, 'Job 1')
|
|
|
|
self.assertEqual(job.start_date, date(2022, 5, 1))
|
|
|
|
self.assertEqual(job.end_date, None)
|
|
|
|
self.assertEqual(job.frequ, 'month')
|
|
|
|
self.assertEqual(job.weekday, '99')
|
|
|
|
self.assertEqual(job.monthday, 1)
|
|
|
|
self.assertEqual(job.interval, 1)
|
|
|
|
self.assertEqual(job.setpos, None)
|
|
|
|
self.assertEqual(
|
|
|
|
job.nextdates,
|
|
|
|
'05/01/2022 | 06/01/2022 | 07/01/2022 | 08/01/2022 |' +
|
|
|
|
' 09/01/2022')
|
|
|
|
return job
|
|
|
|
|
|
|
|
@with_transaction()
|
|
|
|
def test_planner_create_job(self):
|
2024-02-26 21:46:20 +00:00
|
|
|
""" create job, check rule + constraints
|
2024-02-25 22:10:40 +00:00
|
|
|
"""
|
|
|
|
Planner = Pool().get('cashbook.planner')
|
|
|
|
|
|
|
|
job = self.prep_create_job()
|
|
|
|
self.assertEqual(
|
|
|
|
job._compute_dates_by_rrule(
|
2024-02-29 22:20:19 +00:00
|
|
|
query_date=date(2022, 5, 1), count=5), [
|
2024-02-25 22:10:40 +00:00
|
|
|
date(2022, 5, 1), date(2022, 6, 1),
|
|
|
|
date(2022, 7, 1), date(2022, 8, 1),
|
|
|
|
date(2022, 9, 1)])
|
|
|
|
|
|
|
|
self.assertRaisesRegex(
|
|
|
|
UserError,
|
|
|
|
r'The value "2022-05-01" for field "End Date" in "Job 1" of ' +
|
|
|
|
r'"Scheduled Booking" is not valid according to its domain\.',
|
|
|
|
Planner.write,
|
|
|
|
*[[job], {'end_date': date(2022, 5, 1)}])
|
|
|
|
|
|
|
|
Planner.write(*[[job], {
|
|
|
|
'end_date': date(2022, 9, 15), 'monthday': 3}])
|
|
|
|
self.assertEqual(
|
2024-02-29 22:20:19 +00:00
|
|
|
job._compute_dates_by_rrule(query_date=date(2022, 5, 1)), [
|
2024-02-25 22:10:40 +00:00
|
|
|
date(2022, 5, 3), date(2022, 6, 3),
|
|
|
|
date(2022, 7, 3), date(2022, 8, 3),
|
|
|
|
date(2022, 9, 3)])
|
|
|
|
|
|
|
|
Planner.write(*[[job], {
|
|
|
|
'end_date': date(2022, 9, 15), 'monthday': 3, 'interval': 2}])
|
|
|
|
self.assertEqual(
|
2024-02-29 22:20:19 +00:00
|
|
|
job._compute_dates_by_rrule(query_date=date(2022, 5, 1)), [
|
2024-02-25 22:10:40 +00:00
|
|
|
date(2022, 5, 3), date(2022, 7, 3),
|
|
|
|
date(2022, 9, 3)])
|
|
|
|
|
|
|
|
# 3rd of each 2nd month
|
|
|
|
Planner.write(*[[job], {
|
|
|
|
'end_date': None, 'monthday': 1, 'interval': 1}])
|
|
|
|
self.assertEqual(
|
|
|
|
job._compute_dates_by_rrule(
|
2024-02-29 22:20:19 +00:00
|
|
|
query_date=date(2022, 5, 1),
|
2024-02-25 22:10:40 +00:00
|
|
|
params={
|
|
|
|
'end_date': date(2022, 9, 15), 'monthday': 3,
|
|
|
|
'interval': 2}),
|
|
|
|
[date(2022, 5, 3), date(2022, 7, 3), date(2022, 9, 3)])
|
|
|
|
|
|
|
|
# 1st wednesday of each 2nd month
|
|
|
|
self.assertEqual(
|
|
|
|
job._compute_dates_by_rrule(
|
2024-02-29 22:20:19 +00:00
|
|
|
query_date=date(2022, 5, 1),
|
2024-02-25 22:10:40 +00:00
|
|
|
params={
|
|
|
|
'end_date': date(2022, 9, 15), 'weekday': '2',
|
2024-02-26 21:46:20 +00:00
|
|
|
'interval': 2, 'setpos': 1, 'monthday': None}),
|
2024-02-25 22:10:40 +00:00
|
|
|
[date(2022, 5, 4), date(2022, 7, 6), date(2022, 9, 7)])
|
2024-02-24 14:36:15 +00:00
|
|
|
|
2024-02-26 21:46:20 +00:00
|
|
|
# 2nd wednesday of each 2nd month
|
|
|
|
self.assertEqual(
|
|
|
|
job._compute_dates_by_rrule(
|
2024-02-29 22:20:19 +00:00
|
|
|
query_date=date(2022, 5, 1),
|
2024-02-26 21:46:20 +00:00
|
|
|
params={
|
|
|
|
'end_date': date(2022, 9, 15), 'weekday': '2',
|
|
|
|
'interval': 2, 'setpos': 2, 'monthday': None}),
|
|
|
|
[date(2022, 5, 11), date(2022, 7, 13), date(2022, 9, 14)])
|
|
|
|
|
|
|
|
# 2nd wednesday of each month, 6x occurences
|
|
|
|
self.assertEqual(
|
|
|
|
job._compute_dates_by_rrule(
|
2024-02-29 22:20:19 +00:00
|
|
|
query_date=date(2022, 5, 1), count=6,
|
2024-02-26 21:46:20 +00:00
|
|
|
params={
|
|
|
|
'weekday': '2', 'end_date': None,
|
|
|
|
'interval': 1, 'setpos': 2, 'monthday': None}),
|
|
|
|
[date(2022, 5, 11), date(2022, 6, 8), date(2022, 7, 13),
|
|
|
|
date(2022, 8, 10), date(2022, 9, 14), date(2022, 10, 12)])
|
|
|
|
|
|
|
|
Planner.write(*[[job], {
|
|
|
|
'frequ': 'year', 'start_date': date(2022, 5, 1),
|
|
|
|
'setpos': None, 'monthday': None, 'interval': 1,
|
|
|
|
'weekday': '99'}])
|
|
|
|
|
|
|
|
# invalid end_date
|
|
|
|
self.assertRaisesRegex(
|
|
|
|
UserError,
|
|
|
|
'The value "2022-04-30" for field "End Date" in "Job 1" of ' +
|
|
|
|
'"Scheduled Booking" is not valid according to its domain.',
|
|
|
|
Planner.write,
|
|
|
|
*[[job], {
|
|
|
|
'frequ': 'year', 'start_date': date(2022, 5, 1),
|
|
|
|
'end_date': date(2022, 4, 30)}])
|
|
|
|
|
|
|
|
# monthday and weekday used together
|
|
|
|
self.assertRaisesRegex(
|
|
|
|
UserError,
|
|
|
|
'The value "2" for field "Day of month" in "Job 1" of ' +
|
|
|
|
'"Scheduled Booking" is not valid according to its domain.',
|
|
|
|
Planner.write,
|
|
|
|
*[[job], {
|
|
|
|
'frequ': 'month', 'start_date': date(2022, 5, 1),
|
|
|
|
'monthday': 2, 'weekday': '1', 'end_date': None}])
|
|
|
|
|
|
|
|
# monthday out of range 1
|
|
|
|
self.assertRaisesRegex(
|
|
|
|
UserError,
|
|
|
|
'The value "0" for field "Day of month" in "Job 1" of ' +
|
|
|
|
'"Scheduled Booking" is not valid according to its domain.',
|
|
|
|
Planner.write,
|
|
|
|
*[[job], {
|
|
|
|
'frequ': 'year', 'start_date': date(2022, 5, 1),
|
|
|
|
'monthday': 0, 'weekday': '99', 'end_date': None}])
|
|
|
|
|
|
|
|
# monthday out of range 2
|
|
|
|
self.assertRaisesRegex(
|
|
|
|
UserError,
|
|
|
|
'The value "32" for field "Day of month" in "Job 1" of ' +
|
|
|
|
'"Scheduled Booking" is not valid according to its domain.',
|
|
|
|
Planner.write,
|
|
|
|
*[[job], {
|
|
|
|
'frequ': 'year', 'start_date': date(2022, 5, 1),
|
|
|
|
'monthday': 32, 'weekday': '99', 'end_date': None}])
|
|
|
|
|
|
|
|
# invalid usage of setpos
|
|
|
|
self.assertRaisesRegex(
|
|
|
|
UserError,
|
|
|
|
'The value "1" for field "Occurrence" in "Job 1" of ' +
|
|
|
|
'"Scheduled Booking" is not valid according to its domain.',
|
|
|
|
Planner.write,
|
|
|
|
*[[job], {
|
|
|
|
'frequ': 'year', 'start_date': date(2022, 5, 1),
|
|
|
|
'setpos': 1, 'monthday': None, 'weekday': '99',
|
|
|
|
'end_date': None}])
|
|
|
|
|
|
|
|
# setpos out of range 1
|
|
|
|
self.assertRaisesRegex(
|
|
|
|
UserError,
|
|
|
|
'The value "0" for field "Occurrence" in "Job 1" of ' +
|
|
|
|
'"Scheduled Booking" is not valid according to its domain.',
|
|
|
|
Planner.write,
|
|
|
|
*[[job], {
|
|
|
|
'frequ': 'month', 'start_date': date(2022, 5, 1),
|
|
|
|
'setpos': 0, 'monthday': None, 'weekday': '2',
|
|
|
|
'end_date': None}])
|
|
|
|
|
|
|
|
# setpos out of range 2
|
|
|
|
self.assertRaisesRegex(
|
|
|
|
UserError,
|
|
|
|
'The value "5" for field "Occurrence" in "Job 1" of ' +
|
|
|
|
'"Scheduled Booking" is not valid according to its domain.',
|
|
|
|
Planner.write,
|
|
|
|
*[[job], {
|
|
|
|
'frequ': 'month', 'start_date': date(2022, 5, 1),
|
|
|
|
'setpos': 5, 'monthday': None, 'weekday': '2',
|
|
|
|
'end_date': None}])
|
|
|
|
|
2024-02-27 21:40:59 +00:00
|
|
|
@with_transaction()
|
|
|
|
def test_planner_create_update_nextrun(self):
|
|
|
|
""" create job, check nextrun-record
|
|
|
|
"""
|
|
|
|
Planner = Pool().get('cashbook.planner')
|
|
|
|
|
|
|
|
job = self.prep_create_job()
|
|
|
|
self.assertEqual(
|
|
|
|
job._compute_dates_by_rrule(
|
2024-02-29 22:20:19 +00:00
|
|
|
count=1, query_date=date(2022, 5, 1)), [
|
2024-02-27 21:40:59 +00:00
|
|
|
date(2022, 5, 1)])
|
|
|
|
|
2024-02-29 22:20:19 +00:00
|
|
|
with Transaction().set_context({
|
|
|
|
'nextrun_querydate': date(2022, 5, 25)}):
|
|
|
|
Planner.update_next_occurence([job])
|
|
|
|
self.assertEqual(len(job.nextrun), 1)
|
|
|
|
self.assertEqual(job.nextrun[0].date, date(2022, 6, 1))
|
|
|
|
|
|
|
|
with Transaction().set_context({
|
|
|
|
'nextrun_querydate': date(2022, 5, 30)}):
|
|
|
|
Planner.update_next_occurence([job])
|
|
|
|
self.assertEqual(len(job.nextrun), 1)
|
|
|
|
self.assertEqual(job.nextrun[0].date, date(2022, 6, 1))
|
|
|
|
|
|
|
|
with Transaction().set_context({
|
|
|
|
'nextrun_querydate': date(2022, 6, 1)}):
|
|
|
|
Planner.update_next_occurence([job])
|
|
|
|
self.assertEqual(len(job.nextrun), 1)
|
|
|
|
self.assertEqual(job.nextrun[0].date, date(2022, 6, 1))
|
|
|
|
|
|
|
|
with Transaction().set_context({
|
|
|
|
'nextrun_querydate': date(2022, 6, 2)}):
|
|
|
|
Planner.update_next_occurence([job])
|
|
|
|
self.assertEqual(len(job.nextrun), 1)
|
|
|
|
self.assertEqual(job.nextrun[0].date, date(2022, 7, 1))
|
|
|
|
|
|
|
|
with Transaction().set_context({
|
|
|
|
'nextrun_querydate': date(2022, 6, 2)}):
|
|
|
|
# set end-date to check delete of futore runs
|
|
|
|
Planner.write(*[[job], {'end_date': date(2022, 6, 20)}])
|
|
|
|
# write to planner-record updates nextrun-records too
|
|
|
|
self.assertEqual(len(job.nextrun), 0)
|
2024-02-27 21:40:59 +00:00
|
|
|
|
2024-02-24 14:36:15 +00:00
|
|
|
# end PlannerTestCase
|