diff options
| author | Jacob Kaplan-Moss | 2009-04-12 16:25:05 -0500 | 
|---|---|---|
| committer | Jacob Kaplan-Moss | 2009-04-12 16:25:05 -0500 | 
| commit | b5ee253b28673fcfad49f09d9d2687e86ed520b7 (patch) | |
| tree | 32f3f5939bf8d69503f443573414ea80567d095e /src/shorturls/tests | |
| parent | 4391a39ee4cde31ff9c9a56cd8d452b934136b4e (diff) | |
| download | django-shorturls-b5ee253b28673fcfad49f09d9d2687e86ed520b7.tar.bz2 | |
Added a working shorturl redirect view, with tests.
Diffstat (limited to 'src/shorturls/tests')
| -rw-r--r-- | src/shorturls/tests/__init__.py | 3 | ||||
| -rw-r--r-- | src/shorturls/tests/models.py | 37 | ||||
| -rw-r--r-- | src/shorturls/tests/templates/404.html | 27 | ||||
| -rw-r--r-- | src/shorturls/tests/test_views.py | 61 | 
4 files changed, 124 insertions, 4 deletions
| diff --git a/src/shorturls/tests/__init__.py b/src/shorturls/tests/__init__.py index d983d15..6b7a7a3 100644 --- a/src/shorturls/tests/__init__.py +++ b/src/shorturls/tests/__init__.py @@ -1 +1,2 @@ -from test_views import *
\ No newline at end of file +from shorturls.tests.models import * +from shorturls.tests.test_views import *
\ No newline at end of file diff --git a/src/shorturls/tests/models.py b/src/shorturls/tests/models.py index f521e56..5416b17 100644 --- a/src/shorturls/tests/models.py +++ b/src/shorturls/tests/models.py @@ -1,3 +1,38 @@  """  A handful of test modules to test out resolving redirects. -"""
\ No newline at end of file +""" + +from django.db import models + +class Animal(models.Model): +    name = models.CharField(max_length=100) + +    class Meta: +        app_label = 'shorturls' + +    def __unicode__(self): +        return self.name +         +    def get_absolute_url(self): +        return '/animal/%s/' % self.id +         +class Vegetable(models.Model): +    name = models.CharField(max_length=100) + +    class Meta: +        app_label = 'shorturls' + +    def __unicode__(self): +        return self.name +         +    def get_absolute_url(self): +        return 'http://example.net/veggies/%s' % self.id +     +class Mineral(models.Model): +    name = models.CharField(max_length=100) + +    class Meta: +        app_label = 'shorturls' + +    def __unicode__(self): +        return self.name
\ No newline at end of file diff --git a/src/shorturls/tests/templates/404.html b/src/shorturls/tests/templates/404.html new file mode 100644 index 0000000..e63d741 --- /dev/null +++ b/src/shorturls/tests/templates/404.html @@ -0,0 +1,27 @@ +     ## +     #### +     ### ## +     ###   ## +#    ###      ## +################## +#################### +#    ### + +       ####### +   ############### +  ################## + #                 ## +#                   # + #                 ## + ####           #### +   ###############  +       ####### +      +     ## +     #### +     ### ## +     ###   ## +#    ###      ## +################## +#################### +#    ###
\ No newline at end of file diff --git a/src/shorturls/tests/test_views.py b/src/shorturls/tests/test_views.py index 8fa11a4..a2813f2 100644 --- a/src/shorturls/tests/test_views.py +++ b/src/shorturls/tests/test_views.py @@ -1,5 +1,62 @@ +from django.conf import settings +from django.http import Http404  from django.test import TestCase +from shorturls.baseconv import base62  class RedirectViewTestCase(TestCase): -    def test_this_is_working(self): -        self.assertEquals(1, 1)
\ No newline at end of file +    urls = 'shorturls.urls' +    fixtures = ['shorturls-test-data.json'] +     +    def setUp(self): +        self.old_shorten = getattr(settings, 'SHORTEN_MODELS', None) +        self.old_base = getattr(settings, 'SHORTEN_FULL_BASE_URL', None) +        settings.SHORTEN_MODELS = { +            'A': 'shorturls.animal', +            'V': 'shorturls.vegetable', +            'M': 'shorturls.mineral', +            'bad': 'not.amodel', +            'bad2': 'not.even.valid', +        } +        settings.SHORTEN_FULL_BASE_URL = 'http://example.com' +         +    def tearDown(self): +        if self.old_shorten is not None: +            settings.SHORTEN_MODELS = self.old_shorten +        if self.old_base is not None: +            settings.SHORTEN_FULL_BASE_URL = self.old_base +     +    def test_redirect(self): +        """ +        Test the basic operation of a working redirect. +        """ +        response = self.client.get('/A%s' % enc(12345)) +        self.assertEqual(response.status_code, 301) +        self.assertEqual(response['Location'], 'http://example.com/animal/12345/') +         +    def test_redirect_from_request(self): +        """ +        Test a relative redirect when the Sites app isn't installed. +        """ +        settings.SHORTEN_FULL_BASE_URL = None +        response = self.client.get('/A%s' % enc(54321), HTTP_HOST='example.org') +        self.assertEqual(response.status_code, 301) +        self.assertEqual(response['Location'], 'http://example.org/animal/54321/') +         +    def test_redirect_complete_url(self): +        """ +        Test a redirect when the object returns a complete URL. +        """ +        response = self.client.get('/V%s' % enc(785)) +        self.assertEqual(response.status_code, 301) +        self.assertEqual(response['Location'], 'http://example.net/veggies/785') +         +    def test_bad_short_urls(self): +        self.assertEqual(404, self.client.get('/badabcd').status_code) +        self.assertEqual(404, self.client.get('/bad2abcd').status_code) +        self.assertEqual(404, self.client.get('/Vssssss').status_code) + +    def test_model_without_get_absolute_url(self): +        self.assertEqual(404, self.client.get('/M%s' % enc(10101)).status_code) +         +def enc(id): +    return base62.from_decimal(id) | 
