Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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))
Ejemplo n.º 3
0
 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)))
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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()
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
 def test_typecast_to_str(self):
     a = Hashid(1)
     self.assertEqual(str(a), a.hashid)
Ejemplo n.º 10
0
 def test_typecast_to_long(self):
     a = Hashid(1)
     self.assertEqual(long(a), 1)
Ejemplo n.º 11
0
 def test_typecast_to_int(self):
     a = Hashid(1)
     self.assertEqual(int(a), 1)
Ejemplo n.º 12
0
 def test_force_text_with_prefix(self):
     h = Hashid(2923, prefix="prefix_")
     t = force_str(h)
     self.assertEqual(t, "prefix_" + h.hashid)
Ejemplo n.º 13
0
 def test_pickle(self):
     a = Hashid(123)
     pickled = pickle.loads(pickle.dumps(a))
     self.assertTrue(a == pickled)
Ejemplo n.º 14
0
 def test_long_compare(self):
     a = Hashid(1)
     self.assertTrue(long(a) == a)
Ejemplo n.º 15
0
 def test_invalid_hashid(self):
     with self.assertRaises(Exception):
         h = Hashid('asdfqwer')
Ejemplo n.º 16
0
 def test_hashid_encoded_zero_integer(self):
     h = Hashid(0)
     z = Hashid(h.hashid)
     self.assertEqual(h, z)
Ejemplo n.º 17
0
 def test_negative_integer(self):
     with self.assertRaises(Exception):
         h = Hashid(-5)
Ejemplo n.º 18
0
 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)
Ejemplo n.º 19
0
 def test_integer(self):
     h = Hashid(5)
     self.assertIsInstance(h, Hashid)
     self.assertEqual(h.id, 5)
     self.assertEqual(h.hashid, h.hashids.encode(5))
Ejemplo n.º 20
0
 def test_hashable(self):
     h = Hashid(987)
     d = {h: "some value"}
     self.assertEqual(d[h], "some value")
Ejemplo n.º 21
0
 def test_str_compare(self):
     a = Hashid(1)
     self.assertTrue(str(a) == a)
Ejemplo n.º 22
0
 def test_hashid_encoded_zero_integer_with_prefix(self):
     h = Hashid(0, prefix="zero_")
     z = Hashid(str(h), prefix="zero_")
     self.assertEqual(h, z)
Ejemplo n.º 23
0
 def test_int_compare(self):
     a = Hashid(1)
     self.assertTrue(int(a) == a)
Ejemplo n.º 24
0
 def test_none_value(self):
     with self.assertRaises(Exception):
         h = Hashid(None)
Ejemplo n.º 25
0
 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))
Ejemplo n.º 26
0
 def test_force_text(self):
     h = Hashid(2923)
     t = force_text(h)
     self.assertEqual(t, h.hashid)
Ejemplo n.º 27
0
def default_hashid():
    return Hashid(int(timezone.now().timestamp()))
Ejemplo n.º 28
0
 def test_sorting(self):
     a = Hashid(1)
     b = Hashid(2)
     c = Hashid(3)
     arr = [b, a, c]
     self.assertEqual(sorted(arr), [a, b, c])
Ejemplo n.º 29
0
 def test_min_length(self):
     h = Hashid(456, min_length=10)
     self.assertEqual(len(h.hashid), 10)
     self.assertEqual(len(h), 10)
Ejemplo n.º 30
0
 def test_invalid_prefix(self):
     h = Hashid(678, prefix="inv_")
     with self.assertRaises(ValueError):
         Hashid("wrong_" + h.hashid, prefix="inv_")