diff options
author | Laurent Peuch <cortex@worlddomination.be> | 2020-04-01 00:11:10 +0200 |
---|---|---|
committer | Laurent Peuch <cortex@worlddomination.be> | 2020-04-01 00:11:10 +0200 |
commit | b8899451fa861b04568e2a0bb4e3fe4acc0daee3 (patch) | |
tree | 1446c809e19b5571b31b1999246aa0e50b19f5c8 /test/test_cache.py | |
parent | 32cd73810056594f55eff0ffafebbdeb50c7a860 (diff) | |
download | logilab-common-b8899451fa861b04568e2a0bb4e3fe4acc0daee3.tar.gz |
Black the whole code base
Diffstat (limited to 'test/test_cache.py')
-rw-r--r-- | test/test_cache.py | 125 |
1 files changed, 64 insertions, 61 deletions
diff --git a/test/test_cache.py b/test/test_cache.py index 459f172..8e169c4 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -20,109 +20,112 @@ from logilab.common.testlib import TestCase, unittest_main, TestSuite from logilab.common.cache import Cache -class CacheTestCase(TestCase): +class CacheTestCase(TestCase): def setUp(self): self.cache = Cache(5) self.testdict = {} def test_setitem1(self): """Checks that the setitem method works""" - self.cache[1] = 'foo' - self.assertEqual(self.cache[1], 'foo', "1:foo is not in cache") + self.cache[1] = "foo" + self.assertEqual(self.cache[1], "foo", "1:foo is not in cache") self.assertEqual(len(self.cache._usage), 1) - self.assertEqual(self.cache._usage[-1], 1, - '1 is not the most recently used key') - self.assertCountEqual(self.cache._usage, - self.cache.keys(), - "usage list and data keys are different") + self.assertEqual(self.cache._usage[-1], 1, "1 is not the most recently used key") + self.assertCountEqual( + self.cache._usage, self.cache.keys(), "usage list and data keys are different" + ) def test_setitem2(self): """Checks that the setitem method works for multiple items""" - self.cache[1] = 'foo' - self.cache[2] = 'bar' - self.assertEqual(self.cache[2], 'bar', - "2 : 'bar' is not in cache.data") - self.assertEqual(len(self.cache._usage), 2, - "lenght of usage list is not 2") - self.assertEqual(self.cache._usage[-1], 2, - '1 is not the most recently used key') - self.assertCountEqual(self.cache._usage, - self.cache.keys())# usage list and data keys are different + self.cache[1] = "foo" + self.cache[2] = "bar" + self.assertEqual(self.cache[2], "bar", "2 : 'bar' is not in cache.data") + self.assertEqual(len(self.cache._usage), 2, "lenght of usage list is not 2") + self.assertEqual(self.cache._usage[-1], 2, "1 is not the most recently used key") + self.assertCountEqual( + self.cache._usage, self.cache.keys() + ) # usage list and data keys are different def test_setitem3(self): """Checks that the setitem method works when replacing an element in the cache""" - self.cache[1] = 'foo' - self.cache[1] = 'bar' - self.assertEqual(self.cache[1], 'bar', "1 : 'bar' is not in cache.data") + self.cache[1] = "foo" + self.cache[1] = "bar" + self.assertEqual(self.cache[1], "bar", "1 : 'bar' is not in cache.data") self.assertEqual(len(self.cache._usage), 1, "lenght of usage list is not 1") - self.assertEqual(self.cache._usage[-1], 1, '1 is not the most recently used key') - self.assertCountEqual(self.cache._usage, - self.cache.keys())# usage list and data keys are different + self.assertEqual(self.cache._usage[-1], 1, "1 is not the most recently used key") + self.assertCountEqual( + self.cache._usage, self.cache.keys() + ) # usage list and data keys are different def test_recycling1(self): """Checks the removal of old elements""" - self.cache[1] = 'foo' - self.cache[2] = 'bar' - self.cache[3] = 'baz' - self.cache[4] = 'foz' - self.cache[5] = 'fuz' - self.cache[6] = 'spam' - self.assertTrue(1 not in self.cache, - 'key 1 has not been suppressed from the cache dictionnary') - self.assertTrue(1 not in self.cache._usage, - 'key 1 has not been suppressed from the cache LRU list') + self.cache[1] = "foo" + self.cache[2] = "bar" + self.cache[3] = "baz" + self.cache[4] = "foz" + self.cache[5] = "fuz" + self.cache[6] = "spam" + self.assertTrue( + 1 not in self.cache, "key 1 has not been suppressed from the cache dictionnary" + ) + self.assertTrue( + 1 not in self.cache._usage, "key 1 has not been suppressed from the cache LRU list" + ) self.assertEqual(len(self.cache._usage), 5, "lenght of usage list is not 5") - self.assertEqual(self.cache._usage[-1], 6, '6 is not the most recently used key') - self.assertCountEqual(self.cache._usage, - self.cache.keys())# usage list and data keys are different + self.assertEqual(self.cache._usage[-1], 6, "6 is not the most recently used key") + self.assertCountEqual( + self.cache._usage, self.cache.keys() + ) # usage list and data keys are different def test_recycling2(self): """Checks that accessed elements get in the front of the list""" - self.cache[1] = 'foo' - self.cache[2] = 'bar' - self.cache[3] = 'baz' - self.cache[4] = 'foz' + self.cache[1] = "foo" + self.cache[2] = "bar" + self.cache[3] = "baz" + self.cache[4] = "foz" a = self.cache[1] - self.assertEqual(a, 'foo') - self.assertEqual(self.cache._usage[-1], 1, '1 is not the most recently used key') - self.assertCountEqual(self.cache._usage, - self.cache.keys())# usage list and data keys are different + self.assertEqual(a, "foo") + self.assertEqual(self.cache._usage[-1], 1, "1 is not the most recently used key") + self.assertCountEqual( + self.cache._usage, self.cache.keys() + ) # usage list and data keys are different def test_delitem(self): """Checks that elements are removed from both element dict and element list. """ - self.cache['foo'] = 'bar' - del self.cache['foo'] - self.assertTrue('foo' not in self.cache.keys(), "Element 'foo' was not removed cache dictionnary") - self.assertTrue('foo' not in self.cache._usage, "Element 'foo' was not removed usage list") - self.assertCountEqual(self.cache._usage, - self.cache.keys())# usage list and data keys are different - + self.cache["foo"] = "bar" + del self.cache["foo"] + self.assertTrue( + "foo" not in self.cache.keys(), "Element 'foo' was not removed cache dictionnary" + ) + self.assertTrue("foo" not in self.cache._usage, "Element 'foo' was not removed usage list") + self.assertCountEqual( + self.cache._usage, self.cache.keys() + ) # usage list and data keys are different def test_nullsize(self): """Checks that a 'NULL' size cache doesn't store anything """ null_cache = Cache(0) - null_cache['foo'] = 'bar' - self.assertEqual(null_cache.size, 0, 'Cache size should be O, not %d' % \ - null_cache.size) - self.assertEqual(len(null_cache), 0, 'Cache should be empty !') + null_cache["foo"] = "bar" + self.assertEqual(null_cache.size, 0, "Cache size should be O, not %d" % null_cache.size) + self.assertEqual(len(null_cache), 0, "Cache should be empty !") # Assert null_cache['foo'] raises a KeyError - self.assertRaises(KeyError, null_cache.__getitem__, 'foo') + self.assertRaises(KeyError, null_cache.__getitem__, "foo") # Deleting element raises a KeyError - self.assertRaises(KeyError, null_cache.__delitem__, 'foo') + self.assertRaises(KeyError, null_cache.__delitem__, "foo") def test_getitem(self): """ Checks that getitem doest not modify the _usage attribute """ try: - self.cache['toto'] + self.cache["toto"] except KeyError: - self.assertTrue('toto' not in self.cache._usage) + self.assertTrue("toto" not in self.cache._usage) else: - self.fail('excepted KeyError') + self.fail("excepted KeyError") if __name__ == "__main__": |