From 4c5b7cfd329b6ad78be4df64d8ad796ce934394c Mon Sep 17 00:00:00 2001 From: David Huber Date: Mon, 18 Nov 2024 09:05:02 -0600 Subject: [PATCH] Add FileHandler tests. --- tests/test_file_utils.py | 39 +++++++++++++++++++++++++++++++-------- tests/test_sqlitedb.py | 2 +- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/tests/test_file_utils.py b/tests/test_file_utils.py index 6236621..4feac5f 100644 --- a/tests/test_file_utils.py +++ b/tests/test_file_utils.py @@ -1,4 +1,5 @@ import os +import pytest from wxflow import FileHandler @@ -27,6 +28,12 @@ def test_mkdir(tmp_path): assert os.path.exists(dd) +def test_bad_mkdir(): + # Attempt to create a directory in an unwritable parent directory + with pytest.raises(OSError): + FileHandler({'mkdir': ["/dev/null/foo"]}).sync() + + def test_copy(tmp_path): """ Test for copying files: @@ -35,6 +42,7 @@ def test_copy(tmp_path): tmp_path - pytest fixture """ + # Test 1 (nominal operation) - Creating a directory and copying files to it input_dir_path = tmp_path / 'my_input_dir' # Create the input directory @@ -56,7 +64,7 @@ def test_copy(tmp_path): for src, dest in zip(src_files, dest_files): copy_list.append([src, dest]) - # Create config object for FileHandler + # Create config dictionary for FileHandler config = {'copy': copy_list} # Copy input files to output files @@ -66,17 +74,32 @@ def test_copy(tmp_path): for ff in dest_files: assert os.path.isfile(ff) - # Create a config object for copying optional non-existent files (c.txt does not exist) + # Test 2 - Attempt to copy files to a non-writable directory + # Create a list of bad targets (/dev/null is unwritable) + bad_dest_files = ["/dev/null/a.txt", "/dev/null/bb.txt"] + + bad_copy_list = [] + for src, dest in zip(src_files, bad_dest_files): + bad_copy_list.append([src, dest]) + + # Create a config dictionary for FileHandler + bad_config = {'copy': bad_copy_list} + + # Attempt to copy + with pytest.raises(OSError): + FileHandler(bad_config).sync() + + # Test 3 - Attempt to copy missing, optional files to a writable directory + # Create a config dictionary (c.txt does not exist) copy_list.append([input_dir_path / 'c.txt', output_dir_path / 'c.txt']) config = {'copy_opt': copy_list} - # Copy input files to output files + # Copy input files to output files (should not raise an error) FileHandler(config).sync() - # Create a config object for copying required non-existent files (c.txt does not exist) + # Test 4 - Attempt to copy missing, required files to a writable directory + # Create a config dictionary (c.txt does not exist) config = {'copy_req': copy_list} - try: + c_file = input_dir_path / 'c.txt' + with pytest.raises(FileNotFoundError, match=f"Source file '{c_file}' does not exist"): FileHandler(config).sync() - except FileNotFoundError as e: - c_file = input_dir_path / 'c.txt' - assert f"Source file '{c_file}' does not exist" in str(e) diff --git a/tests/test_sqlitedb.py b/tests/test_sqlitedb.py index 2909987..035da90 100644 --- a/tests/test_sqlitedb.py +++ b/tests/test_sqlitedb.py @@ -56,7 +56,7 @@ def test_remove_column(db): column_name = "address" db.remove_column("test_table", column_name) - # Verify that the column exists in the test table + # Verify that the column no longer exists in the test table assert not column_exists(db, "test_table", column_name)