def test_create_myurl(self): """Test creation of myurl""" # create a MyURL m = MyUrl(to_url=self.test_url, user_id='1', utm_campaign='1', utm_term='narf', utm_medium='test', utm_source='clouds', utm_content='stringsandstrings', append_text='gianttrex') # save it m.save() # check that it worked self.assertEqual(m.to_url, self.test_url) # calculate basex for pk s = BaseX(m.pk) url = Site.objects.get(pk=1).domain url = u'%s%s/%s' % (settings.MYURLS_DEFAULT_SCHEME, url, s.encoded) # Test short URL self.assertEqual(m.short_url, url) # Test appended stuff self.assertEqual( m.redirect_url, u'http://directemployersfoundation.org?utm_campaign=1&utm_term=narf&utm_content=stringsandstrings&utm_medium=test&utm_source=clouds&gianttrex' )
def save(self, *args, **kwargs): """Custom save method that saves short URL to database on save()""" # If there is no PK, save and let the DB create one. if self.pk is None: super(MyUrl, self).save(*args, **kwargs) # create a short url if one isn't there if self.short_path == None: # create the short path path = BaseX(number=self.pk) # Here be the magic self.short_path = path.encoded # populate the full short URL for later use self.short_url = u'%s%s/%s' % (settings.MYURLS_DEFAULT_SCHEME, self.site.domain, path) # Populate the redirect URL self._create_redirect_url() # finally, save the model, this time with the short URL. super(MyUrl, self).save(*args, **kwargs)
def test_basex_outputs(self): """Test basex default outputs""" s = BaseX(number=100) self.assertEqual('1C', s.__str__()) self.assertEqual(u'1C', s.__unicode__()) self.assertEqual(100, s.__int__())
def test_basex_alternate_decoding(self): """Test basex decoding with alternate character set""" s = BaseX(encoded='G5b5baZ', character_set="5AGZab") self.assertEqual(99999, s.number) self.assertEqual('G5b5baZ', s.encoded) self.assertEqual('5AGZab', s.character_set)
def test_basex_decoding(self): """Test basex with encoded string""" s = BaseX(encoded='2Jv') self.assertEqual(10509, s.number) self.assertEqual('2Jv', s.encoded)
def test_basex_encoding(self): """Test basex with an integer""" s = BaseX(number=100) self.assertEqual('1C', s.encoded) self.assertEqual(100, s.number)