Пример #1
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)))
Пример #2
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)))
    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)
Пример #4
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)
    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))
Пример #6
0
    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)

        self.patcher_shared_preferences_get = patch(
            'src.utils.shared_preferences.SharedPreferences.get')
        self.mock_shared_preferences_get = self.patcher_shared_preferences_get.start(
        )
        self.mock_shared_preferences_get.side_effect = lambda date_shift=0: DateTimeHelper.get_fixed_date(
            date_shift)
        self.addCleanup(self.patcher_shared_preferences_get.stop)

        self.patcher_shared_preferences_put = patch(
            'src.utils.shared_preferences.SharedPreferences.put')
        self.mock_shared_preferences_put = self.patcher_shared_preferences_put.start(
        )
        self.mock_shared_preferences_put.side_effect = lambda date_shift=0: DateTimeHelper.get_fixed_date(
            date_shift)
        self.addCleanup(self.patcher_shared_preferences_put.stop)

        self.patcher_shared_preferences_clear = patch(
            'src.utils.shared_preferences.SharedPreferences.clear_starting_pattern'
        )
        self.mock_shared_preferences_clear = self.patcher_shared_preferences_clear.start(
        )
        self.mock_shared_preferences_clear.side_effect = lambda date_shift=0: DateTimeHelper.get_fixed_date(
            date_shift)
        self.addCleanup(self.patcher_shared_preferences_clear.stop)
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)