Skip to content

Commit

Permalink
Fix #302: Improve cachetools.keys unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Aug 18, 2024
1 parent ea158fc commit e960781
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ def test_hashkey(self, key=cachetools.keys.hashkey):
self.assertEqual(key(1, 2, 3), key(1.0, 2.0, 3.0))
self.assertEqual(hash(key(1, 2, 3)), hash(key(1.0, 2.0, 3.0)))

def methodkey(self, key=cachetools.keys.methodkey):
# similar to hashkey(), but ignores its first positional argument
self.assertEqual(key("x"), key("y"))
self.assertEqual(hash(key("x")), hash(key("y")))
self.assertEqual(key("x", 1, 2, 3), key("y", 1, 2, 3))
self.assertEqual(hash(key("x", 1, 2, 3)), hash(key("y", 1, 2, 3)))
self.assertEqual(key("x", 1, 2, 3, x=0), key("y", 1, 2, 3, x=0))
self.assertEqual(hash(key("x", 1, 2, 3, x=0)), hash(key("y", 1, 2, 3, x=0)))
self.assertNotEqual(key("x", 1, 2, 3), key("x", 3, 2, 1))
self.assertNotEqual(key("x", 1, 2, 3), key("x", 1, 2, 3, x=None))
self.assertNotEqual(key("x", 1, 2, 3, x=0), key("x", 1, 2, 3, x=None))
self.assertNotEqual(key("x", 1, 2, 3, x=0), key("x", 1, 2, 3, y=0))
with self.assertRaises(TypeError):
hash("x", key({}))
# untyped keys compare equal
self.assertEqual(key("x", 1, 2, 3), key("y", 1.0, 2.0, 3.0))
self.assertEqual(hash(key("x", 1, 2, 3)), hash(key("y", 1.0, 2.0, 3.0)))

def test_typedkey(self, key=cachetools.keys.typedkey):
self.assertEqual(key(), key())
self.assertEqual(hash(key()), hash(key()))
Expand All @@ -37,6 +55,23 @@ def test_typedkey(self, key=cachetools.keys.typedkey):
# typed keys compare unequal
self.assertNotEqual(key(1, 2, 3), key(1.0, 2.0, 3.0))

def test_typedmethodkey(self, key=cachetools.keys.typedmethodkey):
# similar to typedkey(), but ignores its first positional argument
self.assertEqual(key("x"), key("y"))
self.assertEqual(hash(key("x")), hash(key("y")))
self.assertEqual(key("x", 1, 2, 3), key("y", 1, 2, 3))
self.assertEqual(hash(key("x", 1, 2, 3)), hash(key("y", 1, 2, 3)))
self.assertEqual(key("x", 1, 2, 3, x=0), key("y", 1, 2, 3, x=0))
self.assertEqual(hash(key("x", 1, 2, 3, x=0)), hash(key("y", 1, 2, 3, x=0)))
self.assertNotEqual(key("x", 1, 2, 3), key("x", 3, 2, 1))
self.assertNotEqual(key("x", 1, 2, 3), key("x", 1, 2, 3, x=None))
self.assertNotEqual(key("x", 1, 2, 3, x=0), key("x", 1, 2, 3, x=None))
self.assertNotEqual(key("x", 1, 2, 3, x=0), key("x", 1, 2, 3, y=0))
with self.assertRaises(TypeError):
hash(key("x", {}))
# typed keys compare unequal
self.assertNotEqual(key("x", 1, 2, 3), key("x", 1.0, 2.0, 3.0))

def test_addkeys(self, key=cachetools.keys.hashkey):
self.assertIsInstance(key(), tuple)
self.assertIsInstance(key(1, 2, 3) + key(4, 5, 6), type(key()))
Expand Down

0 comments on commit e960781

Please sign in to comment.