test_utils.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from botocore.exceptions import ClientError
  2. from mock import patch, MagicMock, call
  3. import pytest
  4. from backend.ecs_tasks.delete_files.utils import retry_wrapper, remove_none
  5. pytestmark = [pytest.mark.unit, pytest.mark.ecs_tasks]
  6. def get_list_object_versions_error():
  7. return ClientError(
  8. {
  9. "Error": {
  10. "Code": "InvalidArgument",
  11. "Message": "Invalid version id specified",
  12. }
  13. },
  14. "ListObjectVersions",
  15. )
  16. @patch("time.sleep")
  17. def test_it_doesnt_retry_success_fn(sleep_mock):
  18. fn = MagicMock()
  19. fn.side_effect = [31, 32]
  20. result = retry_wrapper(fn, retry_wait_seconds=1, retry_factor=3)(25)
  21. assert result == 31
  22. assert fn.call_args_list == [call(25)]
  23. assert not sleep_mock.called
  24. @patch("time.sleep")
  25. def test_it_retries_retriable_fn(sleep_mock):
  26. fn = MagicMock()
  27. e = get_list_object_versions_error()
  28. fn.side_effect = [e, e, 32]
  29. result = retry_wrapper(fn, retry_wait_seconds=1, retry_factor=3)(22)
  30. assert result == 32
  31. assert fn.call_args_list == [call(22), call(22), call(22)]
  32. assert sleep_mock.call_args_list == [call(1), call(3)]
  33. @patch("time.sleep")
  34. def test_it_doesnt_retry_non_retriable_fn(sleep_mock):
  35. fn = MagicMock()
  36. fn.side_effect = NameError("fail!")
  37. with pytest.raises(NameError) as e:
  38. result = retry_wrapper(fn, retry_wait_seconds=1, retry_factor=3)(22)
  39. assert e.value.args[0] == "fail!"
  40. assert fn.call_args_list == [call(22)]
  41. assert not sleep_mock.called
  42. @patch("time.sleep")
  43. def test_it_retries_and_gives_up_fn(sleep_mock):
  44. fn = MagicMock()
  45. fn.side_effect = get_list_object_versions_error()
  46. with pytest.raises(ClientError) as e:
  47. result = retry_wrapper(fn, max_retries=3)(22)
  48. assert (
  49. e.value.args[0]
  50. == "An error occurred (InvalidArgument) when calling the ListObjectVersions operation: Invalid version id specified"
  51. )
  52. assert fn.call_args_list == [call(22), call(22), call(22), call(22)]
  53. assert sleep_mock.call_args_list == [call(2), call(4), call(8)]
  54. def test_it_removes_empty_keys():
  55. assert {"test": "value"} == remove_none({"test": "value", "none": None})