test_file.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Test file utils."""
  18. import shutil
  19. from pathlib import Path
  20. import pytest
  21. from pydolphinscheduler.utils import file
  22. from tests.testing.file import delete_file, get_file_content
  23. content = "test_content"
  24. file_path = "/tmp/test/file/test_file_write.txt"
  25. @pytest.fixture
  26. def teardown_del_file():
  27. """Teardown about delete file."""
  28. yield
  29. delete_file(file_path)
  30. @pytest.fixture
  31. def setup_crt_first():
  32. """Set up and teardown about create file first and then delete it."""
  33. file.write(content=content, to_path=file_path)
  34. yield
  35. delete_file(file_path)
  36. def test_write_content(teardown_del_file):
  37. """Test function :func:`write` on write behavior with correct content."""
  38. assert not Path(file_path).exists()
  39. file.write(content=content, to_path=file_path)
  40. assert Path(file_path).exists()
  41. assert content == get_file_content(file_path)
  42. def test_write_not_create_parent(teardown_del_file):
  43. """Test function :func:`write` with parent not exists and do not create path."""
  44. file_test_dir = Path(file_path).parent
  45. if file_test_dir.exists():
  46. shutil.rmtree(str(file_test_dir))
  47. assert not file_test_dir.exists()
  48. with pytest.raises(
  49. ValueError,
  50. match="Parent directory do not exists and set param `create` to `False`",
  51. ):
  52. file.write(content=content, to_path=file_path, create=False)
  53. def test_write_overwrite(setup_crt_first):
  54. """Test success with file exists but set ``True`` to overwrite."""
  55. assert Path(file_path).exists()
  56. new_content = f"new_{content}"
  57. file.write(content=new_content, to_path=file_path, overwrite=True)
  58. assert new_content == get_file_content(file_path)
  59. def test_write_overwrite_error(setup_crt_first):
  60. """Test error with file exists but set ``False`` to overwrite."""
  61. assert Path(file_path).exists()
  62. new_content = f"new_{content}"
  63. with pytest.raises(
  64. FileExistsError, match=".*already exists and you choose not overwrite mode\\."
  65. ):
  66. file.write(content=new_content, to_path=file_path)