test_expirelinks.py 1.71 KB
Newer Older
David Haynes's avatar
David Haynes committed
1 2 3 4
"""
go/commands/test_expirelinks.py
"""

5
# Future Imports
David Haynes's avatar
David Haynes committed
6 7
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
8

9 10 11
# Python stdlib Imports
from datetime import timedelta

12
# Django Imports
13 14
from django.contrib.auth.models import User
from django.core.management import call_command
David Haynes's avatar
David Haynes committed
15 16
from django.test import TestCase
from django.utils import timezone
17 18

# App Imports
19
from go.models import URL, RegisteredUser
20 21 22

class ExpireLinksTest(TestCase):
    """
23
    Test cases for the functions in expirelinks
24
    """
25

26 27 28 29 30 31 32 33 34 35 36 37
    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')
38

39 40 41 42 43 44 45 46 47 48 49 50
        # 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
        current_url.save()
David Haynes's avatar
David Haynes committed
51
        second_url.save()
52 53

    def test_expirelinks(self):
54
        """
55
        Test that the expirelinks django admin command functions as intentioned.
56 57
        """

58 59 60
        call_command('expirelinks')

        self.assertTrue(len(URL.objects.all()) == 1)