Esempio n. 1
0
 def __get_tasks_periodic(self):
     tasks = []
     file = Path(self.periodic_file)
     if file.is_file():
         with open(self.periodic_file, 'r') as my_file:
             data = my_file.read()
         d = xmltodict.parse(data)
         for t in d['tasks']['task']:
             name = t['name']
             occurrence_type = PeriodicTaskOccurrenceType.day
             if t['occurrence_type'] == 'week':
                 occurrence_type = PeriodicTaskOccurrenceType.week
             occurrence = t['occurrence']
             reminder = t['reminder']
             start = datetime.datetime.strptime(t['start'],
                                                '%Y-%m-%d').date()
             tasks.append(
                 PeriodicTaskFactory.get_periodic_task(
                     name=name,
                     occurrence_type=occurrence_type,
                     occurrence=occurrence,
                     reminder=reminder,
                     start=start,
                 ))
     return tasks
Esempio n. 2
0
 def __get_tasks_events(self):
     tasks = []
     file = Path(self.event_file)
     if file.is_file():
         with open(self.event_file, 'r') as file:
             data = file.read()
         d = xmltodict.parse(data)
         occurrence_type = PeriodicTaskOccurrenceType.year
         occurrence = 1
         reminder = self.event_reminder
         try:
             if len(d['events']['event']) == 1:
                 start = datetime.datetime.strptime(
                     str(datetime.datetime.now().year) + '-' +
                     d['events']['event']['date'], '%Y-%d-%m').date()
                 tasks.append(
                     PeriodicTaskFactory.get_periodic_task(
                         name=d['events']['event']['name'],
                         occurrence_type=occurrence_type,
                         occurrence=occurrence,
                         reminder=reminder,
                         start=start,
                         task_type=TaskType.event,
                     ))
             else:
                 for event in d['events']['event']:
                     start = datetime.datetime.strptime(
                         str(datetime.datetime.now().year) + '-' +
                         event['date'], '%Y-%d-%m').date()
                     tasks.append(
                         PeriodicTaskFactory.get_periodic_task(
                             name=event['name'],
                             occurrence_type=occurrence_type,
                             occurrence=occurrence,
                             reminder=reminder,
                             start=start,
                             task_type=TaskType.event,
                         ))
         except TypeError:
             # todo: investigate
             logging.error("Type error in events, please investigate")
             return tasks
     else:
         logging.error("Event file doesn't exist")
     return tasks
Esempio n. 3
0
 def test_is_to_be_reminded_week2_ok(self):
     task = PeriodicTaskFactory.get_periodic_task(
         name='task1',
         occurrence_type=PeriodicTaskOccurrenceType.week,
         occurrence=2,
         reminder=2,
         start=DateTimeHelper.get_fixed_date(-6),
     )
     self.assertTrue(
         task.is_to_be_reminded(DateTimeHelper.get_fixed_date(6)))
Esempio n. 4
0
 def test_is_to_be_reminded_day5_ok(self):
     task = PeriodicTaskFactory.get_periodic_task(
         name='task1',
         occurrence_type=PeriodicTaskOccurrenceType.day,
         occurrence=5,
         reminder=2,
         start=DateTimeHelper.get_fixed_date(-6),
     )
     self.assertFalse(
         task.is_to_be_reminded(DateTimeHelper.get_fixed_date(5)))
Esempio n. 5
0
 def test_get_next_occurrence_week3_ok(self):
     task = PeriodicTaskFactory.get_periodic_task(
         name='task1',
         occurrence_type=PeriodicTaskOccurrenceType.week,
         occurrence=2,
         reminder=2,
         start=DateTimeHelper.get_fixed_date(-6),
     )
     self.assertEqual(
         task.get_next_occurrence_in_days_as_int(
             DateTimeHelper.get_fixed_date(9)), 13)
class TestPeriodicTaskGenerator(TestCase):
    def __init__(self, *args, **kwargs):
        super(TestPeriodicTaskGenerator, self).__init__(*args, **kwargs)
        logging.getLogger().disabled = True

    def setUp(self):
        self.patcher_date_long = patch('src.utils.helpers.DateHelper.get_today_long')
        self.mock_date_long = self.patcher_date_long.start()
        self.mock_date_long.side_effect = lambda date_shift=0: DateTimeHelper.get_fixed_datetime(date_shift)
        self.addCleanup(self.patcher_date_long.stop)

        self.patcher_date_short = patch('src.utils.helpers.DateHelper.get_today_short')
        self.mock_date_short = self.patcher_date_short.start()
        self.mock_date_short.side_effect = lambda date_shift=0: DateTimeHelper.get_fixed_date(date_shift)
        self.addCleanup(self.patcher_date_short.stop)

    test_data = [
        PeriodicTaskFactory().get_periodic_task(
            name='task1',
            occurrence_type=PeriodicTaskOccurrenceType.day,
            occurrence=5,
            reminder=2,
            start=DateTimeHelper.get_fixed_date(-1),
        ),
        PeriodicTaskFactory().get_periodic_task(
            name='task2',
            occurrence_type=PeriodicTaskOccurrenceType.day,
            occurrence=11,
            reminder=1,
            start=DateTimeHelper.get_fixed_date(10),
        ),
        PeriodicTaskFactory().get_periodic_task(
            name='task3',
            occurrence_type=PeriodicTaskOccurrenceType.week,
            occurrence=2,
            reminder=1,
            start=DateTimeHelper.get_fixed_date(-5),
        ),
        PeriodicTaskFactory().get_periodic_task(
            name='task4',
            occurrence_type=PeriodicTaskOccurrenceType.week,
            occurrence=1,
            reminder=3,
            start=DateTimeHelper.get_fixed_date(8),
        ),
    ]

    test_data_events = [
        PeriodicTaskFactory().get_periodic_task(
            name='task1',
            occurrence_type=PeriodicTaskOccurrenceType.year,
            occurrence=1,
            reminder=3,
            start=DateTimeHelper.get_fixed_date(1),
        ),
    ]

    @patch.object(PeriodicTaskLoader, 'get', return_value=test_data)
    def test_get_list_next_occurrence_ok(self, mock_load):
        ptg = PeriodicTaskGenerator('test', 'test2')
        returned_tasks = ptg.get_list_next_occurrence()

        self.assertTrue(self.mock_date_short.called)
        self.assertTrue(self.mock_date_long.called)
        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(len(returned_tasks), 4)

        self.assertEqual(returned_tasks[0].date_to_finish, DateTimeHelper.get_fixed_date(4))
        self.assertEqual(returned_tasks[1].date_to_finish, DateTimeHelper.get_fixed_date(10))
        self.assertEqual(returned_tasks[2].date_to_finish, DateTimeHelper.get_fixed_date(9))
        self.assertEqual(returned_tasks[3].date_to_finish, DateTimeHelper.get_fixed_date(8))

    @patch.object(PeriodicTaskLoader, 'get', return_value=test_data)
    def test_get_tasks_without_shift_ok(self, mock_load):
        ptg = PeriodicTaskGenerator('test', 'test2')
        returned_tasks = ptg.get_tasks()

        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(len(returned_tasks), 0)

    @patch.object(PeriodicTaskLoader, 'get', return_value=test_data)
    def test_get_tasks_with_shift_7_ok(self, mock_load):
        ptg = PeriodicTaskGenerator('test', 'test2')
        returned_tasks = ptg.get_tasks(7)

        self.assertTrue(self.mock_date_short.called)
        self.assertTrue(self.mock_date_long.called)
        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(len(returned_tasks), 2)

        self.assertEqual(returned_tasks[0].name, 'task1')
        self.assertEqual(returned_tasks[1].name, 'task4')

    @patch.object(PeriodicTaskLoader, 'get', return_value=test_data)
    def test_get_tasks_with_shift_8_ok(self, mock_load):
        ptg = PeriodicTaskGenerator('test', 'test2')
        returned_tasks = ptg.get_tasks(8)

        self.assertTrue(self.mock_date_short.called)
        self.assertTrue(self.mock_date_long.called)
        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(len(returned_tasks), 3)

        self.assertEqual(returned_tasks[0].name, 'task1')
        self.assertEqual(returned_tasks[1].name, 'task3')
        self.assertEqual(returned_tasks[2].name, 'task4')

    @patch.object(PeriodicTaskLoader, 'get', return_value=test_data)
    def test_get_tasks_with_shift_9_ok(self, mock_load):
        ptg = PeriodicTaskGenerator('test', 'test2')
        returned_tasks = ptg.get_tasks(9)

        self.assertTrue(self.mock_date_short.called)
        self.assertTrue(self.mock_date_long.called)
        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(len(returned_tasks), 3)

        self.assertEqual(returned_tasks[0].name, 'task1')
        self.assertEqual(returned_tasks[1].name, 'task2')
        self.assertEqual(returned_tasks[2].name, 'task3')

    @patch.object(PeriodicTaskLoader, 'get', return_value=test_data)
    def test_get_tasks_with_shift_10_ok(self, mock_load):
        ptg = PeriodicTaskGenerator('test', 'test2')
        returned_tasks = ptg.get_tasks(10)

        self.assertTrue(self.mock_date_short.called)
        self.assertTrue(self.mock_date_long.called)
        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(len(returned_tasks), 1)

        self.assertEqual(returned_tasks[0].name, 'task2')

    @patch.object(PeriodicTaskLoader, 'get', return_value=test_data_events)
    def test_get_events_without_shift_ok(self, mock_load):
        ptg = PeriodicTaskGenerator('test', 'test2')
        returned_tasks = ptg.get_tasks(0)

        self.assertTrue(self.mock_date_short.called)
        self.assertTrue(self.mock_date_long.called)
        self.assertEqual(mock_load.call_count, 1)
        self.assertEqual(len(returned_tasks), 1)