Skip to content

Commit

Permalink
chore(tests): IMprove tests by usinng txt file and not .py files. Usi…
Browse files Browse the repository at this point in the history
…ng fixture for local file manager with clean up.
  • Loading branch information
Lasse-numerous committed May 13, 2024
1 parent 6652a35 commit 30fbefe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 82 deletions.
28 changes: 10 additions & 18 deletions tests/test_aws_filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

@pytest.fixture()
def test_file_create() -> Generator[Path, None, None]:

path_to_file = Path("test.txt")

with Path.open(path_to_file, "w") as f:
Expand All @@ -17,7 +16,6 @@ def test_file_create() -> Generator[Path, None, None]:

@pytest.fixture()
def file_manager() -> Generator[FileManager, None, None]:

file_manager = FileManager(bucket="numerous-files", base_prefix="tests")
yield file_manager

Expand All @@ -28,53 +26,47 @@ def file_manager() -> Generator[FileManager, None, None]:


def test_file_put(test_file_create: str, file_manager: FileManager) -> None:

file_manager.put(test_file_create, "tests/test_aws_filemanager.py")

def test_file_remove(test_file_create: str, file_manager: FileManager) -> None:

upload_path = "tests/test_aws_filemanager.py"
upload_path = "tests/test.txt"

file_manager.put(test_file_create, upload_path)
file_manager.remove(upload_path)

def test_file_list(test_file_create: str, file_manager: FileManager) -> None:

upload_path = "tests/test_aws_filemanager.py"
upload_path = "tests/test.txt"

file_manager.put(test_file_create, upload_path)
results = file_manager.list("tests/")

assert upload_path in results

def test_file_move(test_file_create: str, file_manager: FileManager) -> None:

upload_path = "tests/test_aws_filemanager.py"
upload_path = "tests/test.txt"

file_manager.put(test_file_create, upload_path)
file_manager.move(upload_path, "tests/test_aws_filemanager2.py")
file_manager.move(upload_path, "tests/test2.txt")

assert "tests/test_aws_filemanager2.py" in file_manager.list("tests/")
assert "tests/test2.txt" in file_manager.list("tests/")
assert upload_path not in file_manager.list("tests/")

def test_file_copy(test_file_create: str, file_manager: FileManager) -> None:

upload_path = "tests/test_aws_filemanager.py"

file_manager.put(test_file_create, upload_path)
file_manager.copy(upload_path, "tests/test_aws_filemanager2.py")
file_manager.copy(upload_path, "tests/text2.py")

assert "tests/test_aws_filemanager2.py" in file_manager.list("tests/")
assert "tests/test2.txt" in file_manager.list("tests/")
assert upload_path in file_manager.list("tests/")

def test_file_get(test_file_create: str, file_manager: FileManager) -> None:

upload_path = "tests/test_aws_filemanager.py"

file_manager.put(test_file_create, upload_path)
file_manager.get(upload_path, "test_aws_filemanager.py")
file_manager.get(upload_path, "test_download.txt")

with Path.open(Path("test_aws_filemanager.py")) as f:
with Path.open(Path("test_download.txt")) as f:
assert f.read() == "Hello World!"

Path.unlink(Path("test_aws_filemanager.py"))
Path.unlink(Path("test_download.txt"))
3 changes: 0 additions & 3 deletions tests/test_file_manager_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@


def test_file_manager_factory_default() -> None:
"""Test the file manager factory with the default file manager."""
file_manager = file_manager_factory()
assert file_manager is not None
assert isinstance(file_manager, Memory)

def test_file_manager_factory_memory() -> None:
"""Test the file manager factory with the memory file manager."""
os.environ["NUMEROUS_FILES_BACKEND"] = "INMEMORY"
file_manager = file_manager_factory()
assert file_manager is not None
Expand All @@ -23,7 +21,6 @@ def test_file_manager_factory_memory() -> None:
del os.environ["NUMEROUS_FILES_BACKEND"]

def test_file_manager_factory_aws_s3(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test the file manager factory with the aws s3 file manager."""
monkeypatch.setenv("NUMEROUS_FILES_BACKEND", "AWS_S3")
monkeypatch.setenv("NUMEROUS_FILES_BUCKET", "numerous-files")
monkeypatch.setenv("NUMEROUS_FILES_BASE_PREFIX", "tests")
Expand Down
1 change: 0 additions & 1 deletion tests/test_filemanager_from_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

@pytest.fixture()
def file_text() -> Generator[Path, None, None]:

path_to_file = Path("test.txt")

with Path.open(path_to_file, "w") as f:
Expand Down
66 changes: 27 additions & 39 deletions tests/test_local_filemanager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
from pathlib import Path
from typing import Generator

Expand All @@ -7,90 +8,77 @@

@pytest.fixture()
def test_file_create() -> Generator[Path, None, None]:

path_to_file = Path("test.txt")

with Path.open(path_to_file, "w") as f:
f.write("Hello World!")
yield path_to_file
Path.unlink(path_to_file)

@pytest.fixture()
def file_manager() -> Generator[FileManager, None, None]:
workfolder = Path("./tmp")

def test_file_manager_create() -> None:

file_manager = FileManager() # noqa: F841

def test_file_put(test_file_create: str) -> None:

file_manager = FileManager()
file_manager = FileManager(workfolder=workfolder)
yield file_manager

file_manager.put(test_file_create, "tests/test_memory_filemanager.py")
# Cleanup
shutil.rmtree(workfolder)

def test_file_remove(test_file_create: str) -> None:
def test_file_put(file_manager: FileManager, test_file_create: str) -> None:

file_manager = FileManager()
file_manager.put(test_file_create, "tests/test.txt")

upload_path = "tests/test_memory_filemanager.py"
def test_file_remove(file_manager: FileManager, test_file_create: str) -> None:
upload_path = "tests/test.txt"

file_manager.put(test_file_create, upload_path)
file_manager.remove(upload_path)

def test_file_list(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
def test_file_list(file_manager: FileManager, test_file_create: str) -> None:
upload_path = "tests/test.txt"

file_manager.put(test_file_create, upload_path)
results = file_manager.list("tests/")
# Replace \\ with / for Windows compatibility
results = [r.replace("\\", "/") for r in results]
assert upload_path in results

def test_file_move(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
def test_file_move(file_manager: FileManager, test_file_create: str) -> None:
upload_path = "tests/test.txt"

file_manager.put(test_file_create, upload_path)
file_manager.move(upload_path, "tests/test_memory_filemanager2.py")
file_manager.move(upload_path, "tests/test2.txt")

results = file_manager.list("tests/")
# Replace \\ with / for Windows compatibility
results = [r.replace("\\", "/") for r in results]

assert "tests/test_memory_filemanager2.py" in results
assert "tests/test2.txt" in results
assert upload_path not in results

def test_file_copy(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
def test_file_copy(file_manager: FileManager, test_file_create: str) -> None:
upload_path = "tests/test.txt"

file_manager.put(test_file_create, upload_path)
file_manager.copy(upload_path, "tests/test_memory_filemanager2.py")
file_manager.copy(upload_path, "tests/test2.txt")

file_manager.list("tests/")

results = file_manager.list("tests/")
# Replace \\ with / for Windows compatibility
results = [r.replace("\\", "/") for r in results]

assert "tests/test_memory_filemanager2.py" in results
assert "tests/test2.txt" in results
assert upload_path in results

def test_file_get(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
def test_file_get(file_manager: FileManager, test_file_create: str) -> None:
upload_path = "tests/test.txt"

file_manager.put(test_file_create, upload_path)
file_manager.get(upload_path, "test_memory_filemanager.py")
file_manager.get(upload_path, "test_download.txt")

with Path.open(Path("test_memory_filemanager.py")) as f:
with Path.open(Path("test_download.txt")) as f:
assert f.read() == "Hello World!"

Path.unlink(Path("test_memory_filemanager.py"))
Path.unlink(Path("test_download.txt"))
34 changes: 13 additions & 21 deletions tests/test_memory_filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

@pytest.fixture()
def test_file_create() -> Generator[Path, None, None]:

path_to_file = Path("test.txt")

with Path.open(path_to_file, "w") as f:
Expand All @@ -17,70 +16,63 @@ def test_file_create() -> Generator[Path, None, None]:


def test_file_manager_create() -> None:

file_manager = FileManager() # noqa: F841

def test_file_put(test_file_create: str) -> None:

file_manager = FileManager()

file_manager.put(test_file_create, "tests/test_memory_filemanager.py")
file_manager.put(test_file_create, "tests/text.txt")

def test_file_remove(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
upload_path = "tests/text.txt"

file_manager.put(test_file_create, upload_path)
file_manager.remove(upload_path)

def test_file_list(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
upload_path = "tests/text.txt"

file_manager.put(test_file_create, upload_path)
results = file_manager.list("tests/")
assert upload_path in results

def test_file_move(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
upload_path = "tests/text.txt"

file_manager.put(test_file_create, upload_path)
file_manager.move(upload_path, "tests/test_memory_filemanager2.py")
file_manager.move(upload_path, "tests/text2.txt")

file_manager.list("tests/")
assert "tests/test_memory_filemanager2.py" in file_manager.list("tests/")
assert "tests/text2.txt" in file_manager.list("tests/")
assert upload_path not in file_manager.list("tests/")

def test_file_copy(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
upload_path = "tests/tests/text.txt"

file_manager.put(test_file_create, upload_path)
file_manager.copy(upload_path, "tests/test_memory_filemanager2.py")
file_manager.copy(upload_path, "tests/text2.txt")

file_manager.list("tests/")
assert "tests/test_memory_filemanager2.py" in file_manager.list("tests/")
assert "tests/text2.txt" in file_manager.list("tests/")
assert upload_path in file_manager.list("tests/")

def test_file_get(test_file_create: str) -> None:

file_manager = FileManager()

upload_path = "tests/test_memory_filemanager.py"
upload_path = "tests/text.txt"

file_manager.put(test_file_create, upload_path)
file_manager.get(upload_path, "test_memory_filemanager.py")
file_manager.get(upload_path, "text_download.txt")

with Path.open(Path("test_memory_filemanager.py")) as f:
with Path.open(Path("text_download.txt")) as f:
assert f.read() == "Hello World!"

Path.unlink(Path("test_memory_filemanager.py"))
Path.unlink(Path("text_download.txt"))

0 comments on commit 30fbefe

Please sign in to comment.