Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. from unittest import TestCase, main
  2. from mock import patch
  3. from caching.worker import Worker
  4.  
  5.  
  6. class TestWorker(TestCase):
  7.  
  8. @patch("caching.worker.Worker._delete_directory") # mock the _delete_directory function in relation to the Worker object
  9. @patch("caching.worker.Worker._generate_directory") # mock the _generate_directory function in relation to the Worker object
  10. @patch("caching.worker.uuid.uuid1") # mock the uuid.uuid function that's imported in the worker.py file
  11. def test___init__(self, mock_uuid, mock_generate_directory, mock_delete_directory):
  12. # define the return value for the mocked uuid
  13. mock_uuid.return_value = "test"
  14.  
  15. # run the test by initialising the worker
  16. test = Worker()
  17.  
  18. # check that the test.id is the same as the return value from the uuid module
  19. self.assertEqual(mock_uuid.return_value, test.id)
  20.  
  21. # delete the test object now instead of waiting for the end of the program to do it
  22. del test
  23.  
  24. # check to see if the generate directory function is called once with nothing
  25. mock_generate_directory.assert_called_once_with()
  26.  
  27. # check to see if the delete directory function is called once with nothing
  28. mock_delete_directory.assert_called_once_with()
  29.  
  30. @patch("caching.worker.os")
  31. @patch("caching.worker.Worker._delete_directory")
  32. @patch("caching.worker.Worker.__init__")
  33. def test__generate_directory(self, mock_init, mock_delete_directory, mock_os):
  34. # setup return values for function to pass
  35. mock_init.return_value = None
  36. mock_os.path.isdir.return_value = False
  37.  
  38. # initiate the worker and run the function
  39. test = Worker()
  40. test.base_dir = "test dir"
  41. test._generate_directory()
  42.  
  43. # check that the checking of path has been done once with the base dir
  44. mock_os.path.isdir.assert_called_once_with(test.base_dir)
  45.  
  46. # check that the mkdir function is called once with the base dir
  47. mock_os.mkdir.assert_called_once_with(test.base_dir)
  48.  
  49. # setup return values for the directory to already exist
  50. mock_os.path.isdir.return_value = True
  51.  
  52. # check that it raises an exception because of this
  53. with self.assertRaises(Exception):
  54. test._generate_directory()
  55.  
  56. # check that the mkdir has still only been called once
  57. mock_os.mkdir.assert_called_once_with(test.base_dir)
  58.  
  59. # check that the isdir function has been called twice
  60. self.assertEqual(2, len(mock_os.path.isdir.call_args_list))
  61.  
  62. del test
  63.  
  64. mock_delete_directory.assert_called_once_with()
  65.  
  66. @patch("caching.worker.shutil")
  67. @patch("caching.worker.os")
  68. @patch("caching.worker.Worker.__init__")
  69. def test__delete_directory(self, mock_init, mock_os, mock_shutil):
  70. # setup return values for function to pass
  71. mock_init.return_value = None
  72. mock_os.path.isdir.return_value = True
  73.  
  74. # initiate the worker and run the function
  75. test = Worker()
  76. test.base_dir = "test dir"
  77. test._delete_directory()
  78.  
  79. # check that the checking of path has been done once with the base dir
  80. mock_os.path.isdir.assert_called_once_with(test.base_dir)
  81.  
  82. # check that the rmtree function is called once with the base dir
  83. mock_shutil.rmtree.assert_called_once_with(test.base_dir)
  84.  
  85. # setup return values for the directory to not exist
  86. mock_os.path.isdir.return_value = False
  87.  
  88. # check that it raises an exception because of this
  89. with self.assertRaises(Exception):
  90. test._delete_directory()
  91.  
  92. # check that the rmtree has still only been called once
  93. mock_shutil.rmtree.assert_called_once_with(test.base_dir)
  94.  
  95. # check that the isdir function has been called twice
  96. self.assertEqual(2, len(mock_os.path.isdir.call_args_list))
  97.  
  98. # set to true before exiting to avoid error raising when exiting program
  99. mock_os.path.isdir.return_value = True
  100.  
  101.  
  102. if __name__ == "__main__":
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement