Skip to content

Commit

Permalink
attachment test cases improved
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Dec 7, 2024
1 parent 79aae7b commit f08f033
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
33 changes: 17 additions & 16 deletions apprise/attachment/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,25 +269,26 @@ def exists(self, retrieve_if_missing=True):
cache = self.template_args['cache']['default'] \
if self.cache is None else self.cache

if self.download_path and os.path.isfile(self.download_path) \
and cache:

# We have enough reason to look further into our cached content
# and verify it has not expired.
if cache is True:
# return our fixed content as is; we will always cache it
return True
try:
if self.download_path and os.path.isfile(self.download_path) \
and cache:

# Verify our cache time to determine whether we will get our
# content again.
try:
age_in_sec = time.time() - os.stat(self.download_path).st_mtime
if age_in_sec <= cache:
# We have enough reason to look further into our cached content
# and verify it has not expired.
if cache is True:
# return our fixed content as is; we will always cache it
return True

except (OSError, IOError):
# The file is not present
pass
# Verify our cache time to determine whether we will get our
# content again.
age_in_sec = \
time.time() - os.stat(self.download_path).st_mtime
if age_in_sec <= cache:
return True

except (OSError, IOError):

Check warning on line 289 in apprise/attachment/base.py

View check run for this annotation

Codecov / codecov/patch

apprise/attachment/base.py#L289

Added line #L289 was not covered by tests
# The file is not present
pass

Check warning on line 291 in apprise/attachment/base.py

View check run for this annotation

Codecov / codecov/patch

apprise/attachment/base.py#L291

Added line #L291 was not covered by tests

return False if not retrieve_if_missing else self.download()

Expand Down
7 changes: 7 additions & 0 deletions test/test_attach_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ def test_attach_file():
aa = AppriseAttachment(location=ContentLocation.HOSTED)
assert aa.add(path) is False

response = AppriseAttachment.instantiate(path)
assert len(response) > 0

# Test the inability to get our file size
with mock.patch('os.path.getsize', side_effect=(True, OSError)):
assert len(response) == 0


def test_attach_file_base64():
"""
Expand Down

0 comments on commit f08f033

Please sign in to comment.