Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. @patch("caching.worker.os")
  2. @patch("caching.worker.Worker._delete_directory")
  3. @patch("caching.worker.Worker.__init__")
  4. def test__generate_directory(self, mock_init, mock_delete_directory, mock_os):
  5. # setup return values for function to pass
  6. mock_init.return_value = None
  7. mock_os.path.isdir.return_value = False
  8.  
  9. # initiate the worker and run the function
  10. test = Worker()
  11. test.base_dir = "test dir"
  12. test._generate_directory()
  13.  
  14. # check that the checking of path has been done once with the base dir
  15. mock_os.path.isdir.assert_called_once_with(test.base_dir)
  16.  
  17. # check that the mkdir function is called once with the base dir
  18. mock_os.mkdir.assert_called_once_with(test.base_dir)
  19.  
  20. # setup return values for the directory to already exist
  21. mock_os.path.isdir.return_value = True
  22.  
  23. # check that it raises an exception because of this
  24. with self.assertRaises(Exception):
  25. test._generate_directory()
  26.  
  27. # check that the mkdir has still only been called once
  28. mock_os.mkdir.assert_called_once_with(test.base_dir)
  29.  
  30. # check that the isdir function has been called twice
  31. self.assertEqual(2, len(mock_os.path.isdir.call_args_list))
  32.  
  33. del test
  34.  
  35. mock_delete_directory.assert_called_once_with()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement