Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from unittest import TestCase, main
- from mock import patch, MagicMock
- from caching import CacheManager
- class TestCacheManager(TestCase):
- def test___init__(self):
- # initiate the manager object
- test = CacheManager()
- # change an attribute so a new instance wont have the same params
- test.worker = "test"
- # create a new instance of the object
- test_two = CacheManager()
- # check to see if the memory location is the same, if so they're the same object
- self.assertEqual(id(test), id(test_two))
- def test_cache_path(self):
- # initiate the manager object
- test = CacheManager()
- # make sure the cache_path is None
- self.assertEqual(None, test.cache_path)
- # assign the worker attribute as a MagicMock
- test.worker = MagicMock()
- # ensure that the property test.cache_path is the same as the current worker's base_dir
- self.assertEqual(test.worker.base_dir, test.cache_path)
- @patch("caching.Worker")
- def test_create_cache(self, mock_worker):
- # initiate the manager object
- test = CacheManager()
- # fire the function being tested
- test.create_cache()
- # ensure that the worker attribute is the worker object imported
- self.assertEqual(mock_worker.return_value, test.worker)
- def test_wipe_cache(self):
- # initiate the manager object
- test = CacheManager()
- # assign the worker attribute to anything that isn't None
- test.worker = "testing"
- # fire the function being tested
- test.wipe_cache()
- # ensure that the worker attribute is now None
- self.assertEqual(None, test.worker)
- @patch("caching.CacheManager.wipe_cache")
- @patch("caching.CacheManager.create_cache")
- def test___enter__(self, mock_create_cache, mock_wipe_cache):
- # initiate the manager object
- test = CacheManager()
- # run a session block
- with test:
- pass
- # ensure that a cache was created
- mock_create_cache.assert_called_once_with()
- # ensure that a cache was deleted
- mock_wipe_cache.assert_called_once_with()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement