Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. from unittest import TestCase, main
  2. from mock import patch, MagicMock
  3. from caching import CacheManager
  4.  
  5.  
  6. class TestCacheManager(TestCase):
  7.  
  8. def test___init__(self):
  9. # initiate the manager object
  10. test = CacheManager()
  11.  
  12. # change an attribute so a new instance wont have the same params
  13. test.worker = "test"
  14.  
  15. # create a new instance of the object
  16. test_two = CacheManager()
  17.  
  18. # check to see if the memory location is the same, if so they're the same object
  19. self.assertEqual(id(test), id(test_two))
  20.  
  21. def test_cache_path(self):
  22. # initiate the manager object
  23. test = CacheManager()
  24.  
  25. # make sure the cache_path is None
  26. self.assertEqual(None, test.cache_path)
  27.  
  28. # assign the worker attribute as a MagicMock
  29. test.worker = MagicMock()
  30.  
  31. # ensure that the property test.cache_path is the same as the current worker's base_dir
  32. self.assertEqual(test.worker.base_dir, test.cache_path)
  33.  
  34. @patch("caching.Worker")
  35. def test_create_cache(self, mock_worker):
  36. # initiate the manager object
  37. test = CacheManager()
  38.  
  39. # fire the function being tested
  40. test.create_cache()
  41.  
  42. # ensure that the worker attribute is the worker object imported
  43. self.assertEqual(mock_worker.return_value, test.worker)
  44.  
  45. def test_wipe_cache(self):
  46. # initiate the manager object
  47. test = CacheManager()
  48.  
  49. # assign the worker attribute to anything that isn't None
  50. test.worker = "testing"
  51.  
  52. # fire the function being tested
  53. test.wipe_cache()
  54.  
  55. # ensure that the worker attribute is now None
  56. self.assertEqual(None, test.worker)
  57.  
  58. @patch("caching.CacheManager.wipe_cache")
  59. @patch("caching.CacheManager.create_cache")
  60. def test___enter__(self, mock_create_cache, mock_wipe_cache):
  61. # initiate the manager object
  62. test = CacheManager()
  63.  
  64. # run a session block
  65. with test:
  66. pass
  67.  
  68. # ensure that a cache was created
  69. mock_create_cache.assert_called_once_with()
  70.  
  71. # ensure that a cache was deleted
  72. mock_wipe_cache.assert_called_once_with()
  73.  
  74.  
  75. if __name__ == "__main__":
  76. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement