def test_close_stale_onetime_attempts(self): """ The close_stale_onetime_attempts routine should close all expired one-time attempts, set them as requiring notification, and return the number that were closed. """ task = TaskFactory.create(repeatable=False) user = UserFactory.create() recent_attempt, expired_attempt_1, expired_attempt_2 = TaskAttemptFactory.create_batch( 3, user=user, state=TaskAttempt.STARTED, task=task) recent_attempt.created = aware_datetime(2014, 1, 29) recent_attempt.save() expired_attempt_1.created = aware_datetime(2014, 1, 1) expired_attempt_1.save() expired_attempt_2.created = aware_datetime(2014, 1, 1) expired_attempt_2.save() eq_(task.taskattempt_set.filter(state=TaskAttempt.STARTED).count(), 3) with patch('oneanddone.tasks.models.timezone.now') as now: now.return_value = aware_datetime(2014, 1, 31) eq_(TaskAttempt.close_stale_onetime_attempts(), 2) eq_( TaskAttempt.objects.filter(task=task, state=TaskAttempt.STARTED).count(), 1) eq_( TaskAttempt.objects.filter(task=task, state=TaskAttempt.CLOSED, requires_notification=True).count(), 2)
def test_close_stale_onetime_attempts(self): """ The close_stale_onetime_attempts routine should close all expired one-time attempts, set them as requiring notification, and return the number that were closed. """ task = TaskFactory.create(repeatable=False) user = UserFactory.create() recent_attempt, expired_attempt_1, expired_attempt_2 = TaskAttemptFactory.create_batch( 3, user=user, state=TaskAttempt.STARTED, task=task) recent_attempt.created = aware_datetime(2014, 1, 29) recent_attempt.save() expired_attempt_1.created = aware_datetime(2014, 1, 1) expired_attempt_1.save() expired_attempt_2.created = aware_datetime(2014, 1, 1) expired_attempt_2.save() eq_(task.taskattempt_set.filter(state=TaskAttempt.STARTED).count(), 3) with patch('oneanddone.tasks.models.timezone.now') as now: now.return_value = aware_datetime(2014, 1, 31) eq_(TaskAttempt.close_stale_onetime_attempts(), 2) eq_(TaskAttempt.objects.filter(task=task, state=TaskAttempt.STARTED).count(), 1) eq_(TaskAttempt.objects.filter(task=task, state=TaskAttempt.CLOSED, requires_notification=True).count(), 2)
def handle(self, *args, **options): invalidated = Task.invalidate_tasks() self.stdout.write('%s: %s tasks were invalidated via bug data\n' % (datetime.now().isoformat(), invalidated)) closed = TaskAttempt.close_stale_onetime_attempts() self.stdout.write('%s: %s stale one-time attempts were closed\n' % (datetime.now().isoformat(), closed)) closed = TaskAttempt.close_expired_task_attempts() self.stdout.write('%s: %s attempts for expired tasks were closed\n' % (datetime.now().isoformat(), closed))