Skip to content

Commit

Permalink
feat: unsuccessful copies now logged
Browse files Browse the repository at this point in the history
  • Loading branch information
roryai committed Jan 5, 2024
1 parent a512179 commit 783b62f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/file_copier.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def __copy_file(self, file):
file.source_filepath, file.destination_filepath)
else:
file.copied = False

Logger(file.destination_directory()).log_unsuccessful_copy(
file.source_filepath, file.destination_filepath)
self.file_gateway.update_copied(file)

def __file_copied(self, file):
Expand Down
11 changes: 8 additions & 3 deletions app/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ def __init__(self, destination_directory):
self.log_file_path = self.__init_log_file(destination_directory)

def log_successful_copy(self, source_file_path, destination_filepath):
self.__write_to_logfile(source_file_path, destination_filepath)
log_entry = f'Copy succeeded: {source_file_path} copied to {destination_filepath}'
self.__write_to_logfile(log_entry)

def log_unsuccessful_copy(self, source_file_path, destination_filepath):
log_entry = f'Copy failed: {source_file_path} not copied to {destination_filepath}'
self.__write_to_logfile(log_entry)

def __init_log_file(self, destination_directory):
timestamp = datetime.now().strftime('%Y-%m-%d-%H%M.%S')
Expand All @@ -28,6 +33,6 @@ def __init_log_file(self, destination_directory):

return log_file_path

def __write_to_logfile(self, source_file_path, destination_filepath):
def __write_to_logfile(self, log_entry):
with open(self.log_file_path, 'a') as file:
file.write(f'{source_file_path} copied to {destination_filepath}')
file.write(log_entry)
16 changes: 14 additions & 2 deletions test/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,25 @@ def test_init_log_file_creates_file():
assert os.path.basename(logger.log_file_path) == expected_filename


def test_write_to_logfile():
def test_successful_write_to_logfile():
source_file_path = 'test_source.txt'
destination_filepath = '/destination' + source_file_path
logger.log_successful_copy(source_file_path, destination_filepath)

with open(logger.log_file_path, 'r') as file:
content = file.read()

expected_entry = f'{source_file_path} copied to {destination_filepath}'
expected_entry = f'Copy succeeded: {source_file_path} copied to {destination_filepath}'
assert expected_entry in content


def test_unsuccessful_write_to_logfile():
source_file_path = 'test_source.txt'
destination_filepath = '/destination' + source_file_path
logger.log_unsuccessful_copy(source_file_path, destination_filepath)

with open(logger.log_file_path, 'r') as file:
content = file.read()

expected_entry = f'Copy failed: {source_file_path} not copied to {destination_filepath}'
assert expected_entry in content

0 comments on commit 783b62f

Please sign in to comment.