-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add background task to clear expired downloads.
- Loading branch information
1 parent
39ff976
commit 2d16505
Showing
13 changed files
with
414 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import logging | ||
from datetime import timedelta | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from ytdl_api.commands import remove_expired_downloads | ||
from ytdl_api.datasource import IDataSource | ||
from ytdl_api.schemas.models import Download | ||
from ytdl_api.utils import get_datetime_now | ||
|
||
from .utils import get_example_download_instance | ||
|
||
|
||
@pytest.fixture | ||
def mocked_logger(): | ||
mocked_logger = Mock(spec=logging.Logger) | ||
mocked_logger.info.return_value = None # type: ignore | ||
mocked_logger.error.return_value = None # type: ignore | ||
return mocked_logger | ||
|
||
|
||
@pytest.fixture | ||
def example_expired_downloads(datasource: IDataSource): | ||
dt_now = get_datetime_now() | ||
expiration_delta = timedelta(days=7) | ||
expired_downloads = [ | ||
get_example_download_instance( | ||
client_id="test", | ||
media_format="mp4", | ||
status="downloaded", | ||
when_submitted=dt_now - expiration_delta - timedelta(days=1), | ||
), | ||
get_example_download_instance( | ||
client_id="test", media_format="mp4", status="downloaded", when_submitted=dt_now - expiration_delta | ||
), | ||
get_example_download_instance( | ||
client_id="test", | ||
media_format="mp4", | ||
status="downloaded", | ||
when_submitted=dt_now - timedelta(days=1), | ||
), | ||
get_example_download_instance( | ||
client_id="test", | ||
media_format="mp4", | ||
status="downloaded", | ||
when_submitted=dt_now, | ||
), | ||
] | ||
for download in expired_downloads: | ||
datasource.put_download(download) | ||
yield expiration_delta, expired_downloads | ||
datasource.clear_downloads() | ||
|
||
|
||
def test_hard_remove_downloads( | ||
fake_local_storage, datasource, mocked_logger, example_expired_downloads: tuple[timedelta, list[Download]] | ||
): | ||
expiration_delta, downloads = example_expired_downloads | ||
client_id = downloads[0].client_id | ||
|
||
assert len(datasource.fetch_downloads_by_client_id(client_id)) == len(downloads) | ||
|
||
# Call the function | ||
remove_expired_downloads(fake_local_storage, datasource, expiration_delta, mocked_logger) | ||
|
||
mocked_logger.info.assert_called_with("Soft deleted expired downloads from database.") | ||
|
||
assert len(datasource.fetch_downloads_by_client_id(client_id)) == 2 | ||
expired_download1 = downloads[0] | ||
assert datasource.get_download(expired_download1.client_id, expired_download1.key) is None | ||
expired_download2 = downloads[1] | ||
assert datasource.get_download(expired_download1.client_id, expired_download2.key) is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import asyncio | ||
import logging | ||
|
||
import pytest | ||
from _pytest.capture import CaptureFixture | ||
from _pytest.logging import LogCaptureFixture | ||
|
||
from ytdl_api.utils import repeat_at | ||
|
||
|
||
@pytest.fixture | ||
def logger(): | ||
return logging.getLogger("test") | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_repeat_at(capsys: CaptureFixture[str]): | ||
""" | ||
Simple Test Case for repeat_at | ||
""" | ||
|
||
@repeat_at(cron="* * * * *", max_repetitions=3) | ||
async def print_hello(): | ||
print("Hello") | ||
|
||
print_hello() | ||
await asyncio.sleep(1) | ||
out, err = capsys.readouterr() | ||
assert err == "" | ||
assert out == "" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_repeat_at_with_logger(caplog: LogCaptureFixture, logger: logging.Logger): | ||
""" | ||
Test Case for repeat_at with logger and raising exception. | ||
""" | ||
|
||
@repeat_at(cron="* * * * *", logger=logger, max_repetitions=3) | ||
async def print_hello(): | ||
raise Exception("Hello") | ||
|
||
print_hello() | ||
await asyncio.sleep(60) | ||
|
||
captured_logs = caplog.records | ||
|
||
assert len(captured_logs) == 1 | ||
assert hasattr(captured_logs[0], "exc_text") | ||
assert 'raise Exception("Hello")' in captured_logs[0].exc_text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.