ref: Use pathlib in tests (#4159)

Use pathlib in tests
This commit is contained in:
Christophe Bornet 2024-10-15 19:51:50 +02:00 • committed by GitHub
commit eb53f66641
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 59 additions and 87 deletions

View file

@ -1,4 +1,3 @@
import os
import tempfile
from pathlib import Path
from unittest.mock import Mock, patch, ANY
@ -128,7 +127,7 @@ def test_directory_component_build_with_multithreading(
):
# Arrange
directory_component = data.DirectoryComponent()
path = os.path.dirname(os.path.abspath(__file__))
path = Path(__file__).resolve().parent
depth = 1
max_concurrency = 2
load_hidden = False
@ -136,16 +135,15 @@ def test_directory_component_build_with_multithreading(
silent_errors = False
use_multithreading = True
mock_resolve_path.return_value = path
mock_retrieve_file_paths.return_value = [
os.path.join(path, file) for file in os.listdir(path) if file.endswith(".py")
]
mock_resolve_path.return_value = str(path)
mock_retrieve_file_paths.return_value = [str(p) for p in path.iterdir() if p.suffix == ".py"]
mock_parallel_load_data.return_value = [Mock()]
# Act
directory_component.set_attributes(
{
"path": path,
"path": str(path),
"depth": depth,
"max_concurrency": max_concurrency,
"load_hidden": load_hidden,
@ -157,9 +155,9 @@ def test_directory_component_build_with_multithreading(
directory_component.load_directory()
# Assert
mock_resolve_path.assert_called_once_with(path)
mock_resolve_path.assert_called_once_with(str(path))
mock_retrieve_file_paths.assert_called_once_with(
path, load_hidden=load_hidden, recursive=recursive, depth=depth, types=ANY
str(path), load_hidden=load_hidden, recursive=recursive, depth=depth, types=ANY
)
mock_parallel_load_data.assert_called_once_with(
mock_retrieve_file_paths.return_value, silent_errors=silent_errors, max_concurrency=max_concurrency
@ -170,11 +168,9 @@ def test_directory_without_mocks():
directory_component = data.DirectoryComponent()
with tempfile.TemporaryDirectory() as temp_dir:
with open(temp_dir + "/test.txt", "w") as f:
f.write("test")
(Path(temp_dir) / "test.txt").write_text("test")
# also add a json file
with open(temp_dir + "/test.json", "w") as f:
f.write('{"test": "test"}')
(Path(temp_dir) / "test.json").write_text('{"test": "test"}')
directory_component.set_attributes({"path": str(temp_dir), "use_multithreading": False})
results = directory_component.load_directory()