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

4 5
Test that the function to expire Go links actually works.
"""
6
# Future Imports
David Haynes's avatar
David Haynes committed
7 8
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
9

10 11 12
# Python stdlib Imports
from datetime import timedelta

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

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

class ExpireLinksTest(TestCase):
23 24
    def setUp(self):
        """
25
        Set up any variables such as dummy objects that will be utilized in
26 27 28 29 30 31 32 33
        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')
34

35 36 37 38 39 40 41 42 43 44 45 46
        # 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
47
        second_url.save()
48 49

    def test_expirelinks(self):
50
        """
51 52
        Make a call to expire Go links and assert that the number of links has
        been reduced.
53
        """
54 55
        call_command('expirelinks')
        self.assertTrue(len(URL.objects.all()) == 1)