Esempio n. 1
0
 def test_create_with_custom_id(self):
     """
     Create a shortened URL with non-default id specified
     """
     id = 5000
     link = Link.objects.create(id=id, url='http://www.python.org')
     self.assertEqual(link.to_base62(), base62.from_decimal(id))
Esempio n. 2
0
 def test_create_with_custom_id(self):
     """
     Link.base_62() is derived from custom Link.id
     """
     id = 5000
     link = Link.objects.create(id=id, url='http://www.python.org')
     self.assertEqual(link.to_base62(), base62.from_decimal(id))
Esempio n. 3
0
 def test_create_with_custom_id(self):
     """
     Link.base_62() is derived from custom Link.id
     """
     id = 5000
     link = Link.objects.create(id=id, url='http://www.python.org')
     self.assertEqual(link.to_base62(), base62.from_decimal(id))
Esempio n. 4
0
 def test_symmetry_positive_int(self):
     """
     symmetry for encoding/decoding values
     """
     for x in range(1000):
         random_int = random.randint(0, sys.maxsize)
         encoded_int = base62.from_decimal(random_int)
         self.assertEqual(random_int, base62.to_decimal(encoded_int))
Esempio n. 5
0
 def test_symmetry_negative_int(self):
     """
     symmetry for negative numbers
     """
     for x in xrange(1000):
         random_int = random.randint(-1 * sys.maxint - 1, 0)
         encoded_int = base62.from_decimal(random_int)
         self.assertEqual(random_int, base62.to_decimal(encoded_int))
Esempio n. 6
0
 def test_symmetry_positive_int(self):
     """
     symmetry for encoding/decoding values
     """
     for x in xrange(1000):
         random_int = random.randint(0, sys.maxint)
         encoded_int = base62.from_decimal(random_int)
         self.assertEqual(random_int, base62.to_decimal(encoded_int))
Esempio n. 7
0
 def test_symmetry_negative_int(self):
     """
     symmetry for negative numbers
     """
     for x in range(1000):
         random_int = random.randint(-1 * sys.maxsize - 1, 0)
         encoded_int = base62.from_decimal(random_int)
         self.assertEqual(random_int, base62.to_decimal(encoded_int))
Esempio n. 8
0
 def test_submit(self):
     url = u'http://www.python.org/'
     response = self.client.post(reverse('submit'), {'url': url,})
     self.assertEqual(response.status_code, 200)
     self.assertTemplateUsed(response, 'shortener/submit_success.html')
     self.assertIn('link', response.context)
     link = response.context['link']
     self.assertIsInstance(link, Link)
     self.assertEqual(url, link.url)
     self.assertEqual(link.usage_count, 0)
     self.assertEqual(base62.from_decimal(link.id), link.to_base62())
Esempio n. 9
0
 def test_submit(self):
     """
     submit view with auto-generated short url
     """
     url = u'http://www.python.org/'
     response = self.client.post(reverse('submit'), {'url': url})
     self.assertEqual(response.status_code, 200)
     self.assertTemplateUsed(response, 'shortener/submit_success.html')
     self.assertIn('link', response.context)
     link = response.context['link']
     self.assertIsInstance(link, Link)
     self.assertEqual(url, link.url)
     self.assertEqual(link.usage_count, 0)
     self.assertEqual(base62.from_decimal(link.id), link.to_base62())
Esempio n. 10
0
    def test_follow(self):
        url = u'http://www.python.org/'
        response = self.client.post(reverse('submit'), {'url': url,})
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'shortener/submit_success.html')

        link = response.context['link']
        self.assertIsInstance(link, Link)
        self.assertEqual(url, link.url)
        self.assertEqual(base62.from_decimal(link.id), link.to_base62())
        self.assertEqual(link.usage_count, 0)

        # follow the short url and get a redirect
        response = self.client.get(reverse('follow', kwargs={
            'base62_id': link.to_base62()}))
        self.assertRedirects(response, url, 301)

        # re-fetch link so that we can make sure that usage_count incremented
        link = Link.objects.get(id=link.id)
        self.assertEqual(link.usage_count, 1)
Esempio n. 11
0
 def to_base62(self):
     return base62.from_decimal(self.id)
Esempio n. 12
0
 def test_create(self):
     """
     Link.base_62() is derived from auto-generated Link.id
     """
     link = Link.objects.create(url='http://www.python.org')
     self.assertEqual(link.to_base62(), base62.from_decimal(link.id))
Esempio n. 13
0
 def test_create(self):
     """
     Link.base_62() is derived from auto-generated Link.id
     """
     link = Link.objects.create(url='http://www.python.org')
     self.assertEqual(link.to_base62(), base62.from_decimal(link.id))
Esempio n. 14
0
 def test_encoding_non_int_fails(self):
     try:
         encoding = base62.from_decimal(string.letters)
     except EncodingError, e:
         err = e
Esempio n. 15
0
 def test_symmetry_int(self):
     random_int = random.randint(0, sys.maxint)
     encoded_int = base62.from_decimal(random_int)
     self.assertEqual(random_int, base62.to_decimal(encoded_int))
Esempio n. 16
0
 def to_base62(self):
     return unicode(base62.from_decimal(self.id))
Esempio n. 17
0
 def to_base62(self):
     return base62.from_decimal(self.key().id())