salt/tests/unit/utils/cache_test.py

51 lines
1.2 KiB
Python
Raw Normal View History

2014-03-06 02:12:20 +00:00
# -*- coding: utf-8 -*-
'''
tests.unit.utils.cache_test
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test the salt cache objects
'''
# Import Salt Testing libs
from salttesting import TestCase
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import salt libs
from salt.utils import cache
import time
class CacheDictTestCase(TestCase):
def test_sanity(self):
'''
Make sure you can instantiate etc.
'''
cd = cache.CacheDict(5)
self.assertIsInstance(cd, cache.CacheDict)
2014-03-06 02:12:20 +00:00
# do some tests to make sure it looks like a dict
self.assertNotIn('foo', cd)
2014-03-06 02:12:20 +00:00
cd['foo'] = 'bar'
self.assertEqual(cd['foo'], 'bar')
2014-03-06 02:12:20 +00:00
del cd['foo']
self.assertNotIn('foo', cd)
2014-03-06 02:12:20 +00:00
def test_ttl(self):
cd = cache.CacheDict(0.1)
cd['foo'] = 'bar'
self.assertIn('foo', cd)
self.assertEqual(cd['foo'], 'bar')
2014-03-06 02:12:20 +00:00
time.sleep(0.1)
self.assertNotIn('foo', cd)
2014-03-06 02:12:20 +00:00
# make sure that a get would get a regular old key error
self.assertRaises(KeyError, cd.__getitem__, 'foo')
if __name__ == '__main__':
from integration import run_tests
run_tests(CacheDictTestCase, needs_daemon=False)