def test_create_placeholder_on_comment_deleted_is_correct(self): token = create_sample_usertoken(username="******") target = create_sample_usertoken() created_on = get_now().replace(year=2019, month=2, day=25, hour=15, minute=32, second=1, microsecond=0) now = get_now() comment = create_sample_comment( target=target, user=token, text="Hello my name is fred", created_on=created_on, modified_on=created_on, ) comment.delete() placeholder_comment: Comment = Comment.objects.first() self.assertIsNone(placeholder_comment.user) self.assertEqual(placeholder_comment.text, "") self.assertEqual(placeholder_comment.target, target) # Ensure modified_on has been set to current timestamp when the comment was deleted self.assertNotEqual(created_on, now) self.assertEqual(placeholder_comment.created_on, created_on) self.assertEqual(placeholder_comment.modified_on.date(), now.date())
def test_delete_pending(self): DeletionPendingMixin.DELETION_PENDING_PERIOD_HOURS = 1 now = get_now() target = create_sample_usertoken() token = create_sample_usertoken() pending_comment = create_sample_comment(target, token, "hello pending") pending_comment.mark_pending_deletion() pending_comment.save() expired_comment = create_sample_comment(target, token, "hello expired") expired_comment.mark_pending_deletion() expired_comment.deletion_requested_at = now - timedelta(hours=3) expired_comment.save() self.assertLengthEquals(Comment.objects.all(), 2) delete_expired_models() Comment.objects.get(text="hello pending") # Original comment should no longer exist, but an empty placeholder should have been created self.assertLengthEquals(Comment.objects.all(), 2) # Original has gone self.assertRaises( Comment.DoesNotExist, Comment.objects.get, text="hello expired", ) # Placeholder exists Comment.objects.get(user=None, text="", **get_target_kwargs(target))
def mark_as_complete(self): if self.finished: return self.complete = True self.finished_at = get_now() self.save()
def _save_current(): with open(TEST_HISTORY_FILE, "w", encoding="utf-8") as f: json.dump( { "tests_run": tests_run, "timestamp": get_now().strftime("%y-%m-%d"), }, f, )
def mark_as_failed(self, err=None): if err: self.content = ( self.content or "" ) + f"\n{err}\n\n{traceback.format_exc(limit=20)}" log.error(f"Task `{self.title} {self.pk}` failed: {err}") self.failed = True self.finished_at = get_now() self.save()
def test_cache_time_to_live_is_correct(self): url = "https://parliament.uk/example/request/data.json" data = {"a": 5, "b": 17} now = get_now() live_cache_timestamp = now + datetime.timedelta(minutes=55) expired_timestamp = now + datetime.timedelta(minutes=65) ttl = int(datetime.timedelta(minutes=60).total_seconds()) cache = create_json_cache(self.cache_name, ttl_seconds=ttl, now=now) cache.remember(url, data) cache.finish(now=now) live_cache = create_json_cache(self.cache_name, ttl_seconds=ttl, now=live_cache_timestamp) self.assertDictEqual(live_cache.get_json(url), data) # Cache should be flushed as previous timestamp is more than [ttl] seconds in the past. expired_cache = create_json_cache(self.cache_name, ttl_seconds=ttl, now=expired_timestamp) self.assertIsNone(expired_cache.get_json(url))
def is_expired(self): now = get_now() return self.expires_at() < now
def hours_until_expired(self): now = get_now() return (self.expires_at() - now) / datetime.timedelta(hours=1)
def mark_pending_deletion(self): self.pending_deletion = True self.deletion_requested_at = get_now()