def test_hashid_with_prefix(self): h = Hashid(123, prefix="hid_") b = Hashid(str(h), prefix="hid_") self.assertEqual(h, b) self.assertEqual(h.id, b.id) self.assertEqual(h.hashid, b.hashid) self.assertEqual(h.prefix, b.prefix)
def test_hashid_equality(self): a = Hashid(123) b = Hashid(123) c = Hashid(123, salt="asdfqwer") self.assertTrue(a == b) self.assertTrue(hash(a) == hash(b)) self.assertTrue(hash(a) == hash(str(b))) self.assertFalse(a == c) self.assertFalse(hash(a) == hash(c))
def test_hashable(self): h = Hashid(987) d = {h: "some value"} self.assertEqual(d[h], "some value") self.assertTrue(h in d) self.assertTrue(str(h) in d) self.assertTrue(hash(h) == hash(str(h))) self.assertFalse(hash(int(h) == hash(h)))
def test_hashid_override_with_minimum_alphabet(self): alphabet = "0123456789abcdef" h = hashids.Hashids(alphabet=alphabet) a = Hashid(5, alphabet=alphabet, hashids=h) self.assertIsInstance(a, Hashid) self.assertEqual(a.id, 5) self.assertEqual(a.hashid, h.encode(5)) self.assertEqual(a._alphabet, alphabet)
def test_set_valid_hashid_string(self): t = TestClass() h = Hashid(456, salt=salt, min_length=min_length, alphabet=alphabet) t.a = h.hashid self.assertIsInstance(t.a, Hashid) self.assertEqual(t.a.id, 456) t.b = h.hashid self.assertIsInstance(t.b, str) self.assertEqual(t.b, h.hashid)
def add_like(self, obj): id = Hashid(obj.id).id ct = obj._ct() qs = Like.objects.filter(user=self, content_type=ct, object_id=id) if qs.count() > 0: qs.delete() else: like = Like(user=self, content_type=ct, object_id=id) like.save()
def test_set_hashid(self): t = TestClass() h = Hashid(234) t.a = h self.assertIsInstance(t.a, Hashid) self.assertEqual(t.a.id, 234) t.b = h self.assertIsInstance(t.b, str) self.assertEqual(t.b, h.hashid)
def test_non_existing_post_view(self): from hashid_field import Hashid non_existing_post_id = 99 hash_id = Hashid(non_existing_post_id).hashid # Ensure that Post with ID 99 does not exist with self.assertRaises(Post.DoesNotExist): Post.objects.get(hash_id=hash_id) # Create url for non-existing post post_view_url = reverse('post_detail', kwargs={'hash_id': hash_id}) # Issue a GET request response = self.client.get(post_view_url) # Check that the response is 404 self.assertEqual(response.status_code, 404)
def test_typecast_to_str(self): a = Hashid(1) self.assertEqual(str(a), a.hashid)
def test_typecast_to_long(self): a = Hashid(1) self.assertEqual(long(a), 1)
def test_typecast_to_int(self): a = Hashid(1) self.assertEqual(int(a), 1)
def test_force_text_with_prefix(self): h = Hashid(2923, prefix="prefix_") t = force_str(h) self.assertEqual(t, "prefix_" + h.hashid)
def test_pickle(self): a = Hashid(123) pickled = pickle.loads(pickle.dumps(a)) self.assertTrue(a == pickled)
def test_long_compare(self): a = Hashid(1) self.assertTrue(long(a) == a)
def test_invalid_hashid(self): with self.assertRaises(Exception): h = Hashid('asdfqwer')
def test_hashid_encoded_zero_integer(self): h = Hashid(0) z = Hashid(h.hashid) self.assertEqual(h, z)
def test_negative_integer(self): with self.assertRaises(Exception): h = Hashid(-5)
def test_hashid(self): h = Hashid(123) b = Hashid(h.hashid) self.assertEqual(h, b) self.assertEqual(h.id, b.id) self.assertEqual(h.hashid, b.hashid)
def test_integer(self): h = Hashid(5) self.assertIsInstance(h, Hashid) self.assertEqual(h.id, 5) self.assertEqual(h.hashid, h.hashids.encode(5))
def test_hashable(self): h = Hashid(987) d = {h: "some value"} self.assertEqual(d[h], "some value")
def test_str_compare(self): a = Hashid(1) self.assertTrue(str(a) == a)
def test_hashid_encoded_zero_integer_with_prefix(self): h = Hashid(0, prefix="zero_") z = Hashid(str(h), prefix="zero_") self.assertEqual(h, z)
def test_int_compare(self): a = Hashid(1) self.assertTrue(int(a) == a)
def test_none_value(self): with self.assertRaises(Exception): h = Hashid(None)
def test_integer_with_prefix(self): h = Hashid(10, prefix="num_") self.assertIsInstance(h, Hashid) self.assertEqual(h.id, 10) self.assertEqual(h.hashid, h.hashids.encode(10)) self.assertEqual(str(h), "num_" + h.hashids.encode(10))
def test_force_text(self): h = Hashid(2923) t = force_text(h) self.assertEqual(t, h.hashid)
def default_hashid(): return Hashid(int(timezone.now().timestamp()))
def test_sorting(self): a = Hashid(1) b = Hashid(2) c = Hashid(3) arr = [b, a, c] self.assertEqual(sorted(arr), [a, b, c])
def test_min_length(self): h = Hashid(456, min_length=10) self.assertEqual(len(h.hashid), 10) self.assertEqual(len(h), 10)
def test_invalid_prefix(self): h = Hashid(678, prefix="inv_") with self.assertRaises(ValueError): Hashid("wrong_" + h.hashid, prefix="inv_")