Skip to content

Commit

Permalink
Add FileHandler tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidHuber-NOAA committed Nov 18, 2024
1 parent c0345b1 commit 4c5b7cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
39 changes: 31 additions & 8 deletions tests/test_file_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import pytest

from wxflow import FileHandler

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
2 changes: 1 addition & 1 deletion tests/test_sqlitedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 4c5b7cf

Please sign in to comment.