Unverified Commit 46c5a592 authored by David Haynes's avatar David Haynes
Browse files

Test for expirelinks django-admin command

- add two links, make sure the old one is removed
- another one bites the dust :fiestaparrot:
parent da9d9ec7
Pipeline #1104 passed with stage
in 1 minute and 27 seconds
......@@ -8,17 +8,20 @@ from django.utils import timezone
# App Imports
from go.models import URL
# Define a new custom django-admin command
class Command(BaseCommand):
Remove expired links from the database
Define a new custom django-admin command to remove expired links from the
# Define help text for this command
help = 'Removes expired links from the database'
# The handle function handles the main component of the django-admin command
def handle(self, *args, **options):
The handle function handles the main component of the django-admin command.
# Loop through a list of all URL objects that have expired
# (expires field is less than or equal to today's date)
for toexpire in URL.objects.filter(expires__lte=timezone.now()):
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Python stdlib Imports
from datetime import timedelta
# Django Imports
from django.test import TestCase
from django.contrib.auth.models import User
from django.utils import timezone
from django.core.management import call_command
# App Imports
from .expirelinks import *
from .expirelinks import Command
from go.models import URL, RegisteredUser
class ExpireLinksTest(TestCase):
Test cases for the functions in expirelinks
Test cases for the functions in expirelinks
def setUp(self):
Set up any variables such as dummy objects that will be utilised in
testing methods
# Setup a blank URL object with an owner
User.objects.create(username='dhaynes', password='password')
get_user = User.objects.get(username='dhaynes')
get_registered_user = RegisteredUser.objects.get(user=get_user)
URL.objects.create(owner=get_registered_user, short='test')
URL.objects.create(owner=get_registered_user, short='test-2')
def test_Django_Test(self):
# Get some dates
yesterday = timezone.now() - timedelta(days=1)
tomorrow = timezone.now() + timedelta(days=1)
# Get the URL to apply it to
current_url = URL.objects.get(short='test')
second_url = URL.objects.get(short='test-2')
# Apply the dates
current_url.expires = yesterday
second_url.expires = tomorrow
def test_expirelinks(self):
Default test case, does not actually test anything
Test that the expirelinks django admin command functions as intentioned.
self.assertEqual("Hello World!", "Hello World!")
self.assertTrue(len(URL.objects.all()) == 1)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment