mock for info log

This commit is contained in:
Jesse Bannon 2023-02-20 10:32:59 -08:00
parent 1450d36d8d
commit 505e297bd0
3 changed files with 15 additions and 10 deletions

View file

@ -26,7 +26,7 @@ def output_directory():
@contextlib.contextmanager @contextlib.contextmanager
def assert_debug_log(logger: logging.Logger, expected_message: str): def assert_logs(logger: logging.Logger, expected_message: str, log_level: str = "debug"):
""" """
Patches any function, but calls the original function. Patches any function, but calls the original function.
Intended to see if the particular function is called. Intended to see if the particular function is called.
@ -36,7 +36,7 @@ def assert_debug_log(logger: logging.Logger, expected_message: str):
def _wrapped_debug(*args, **kwargs): def _wrapped_debug(*args, **kwargs):
debug_logger.info(*args, **kwargs) debug_logger.info(*args, **kwargs)
with patch.object(logger, "debug", wraps=_wrapped_debug) as patched_debug: with patch.object(logger, log_level, wraps=_wrapped_debug) as patched_debug:
yield yield
for call_args in patched_debug.call_args_list: for call_args in patched_debug.call_args_list:

View file

@ -2,7 +2,7 @@ import copy
import mergedeep import mergedeep
import pytest import pytest
from conftest import assert_debug_log from conftest import assert_logs
from expected_download import assert_expected_downloads from expected_download import assert_expected_downloads
from expected_transaction_log import assert_transaction_log_matches from expected_transaction_log import assert_transaction_log_matches
@ -67,9 +67,10 @@ class TestDateRange:
) )
if not dry_run: if not dry_run:
# try downloading again, ensure nothing more was downloaded # try downloading again, ensure nothing more was downloaded
with assert_debug_log( with assert_logs(
logger=ytdl_sub.downloaders.downloader.download_logger, logger=ytdl_sub.downloaders.downloader.download_logger,
expected_message="ExistingVideoReached, stopping additional downloads", expected_message="ExistingVideoReached, stopping additional downloads",
log_level="info",
): ):
transaction_log = recent_channel_subscription.download() transaction_log = recent_channel_subscription.download()
assert_transaction_log_matches( assert_transaction_log_matches(
@ -137,7 +138,7 @@ class TestDateRange:
# First, download recent vids. Always download since we want to test dry-run # First, download recent vids. Always download since we want to test dry-run
# on the rolling recent portion. # on the rolling recent portion.
with assert_debug_log( with assert_logs(
logger=ytdl_sub.downloaders.downloader.download_logger, logger=ytdl_sub.downloaders.downloader.download_logger,
expected_message="RejectedVideoReached, stopping additional downloads", expected_message="RejectedVideoReached, stopping additional downloads",
): ):
@ -156,9 +157,10 @@ class TestDateRange:
# Then, download the rolling recent vids subscription. This should remove one of the # Then, download the rolling recent vids subscription. This should remove one of the
# two videos # two videos
with assert_debug_log( with assert_logs(
logger=ytdl_sub.downloaders.downloader.download_logger, logger=ytdl_sub.downloaders.downloader.download_logger,
expected_message="ExistingVideoReached, stopping additional downloads", expected_message="ExistingVideoReached, stopping additional downloads",
log_level="info",
): ):
transaction_log = rolling_recent_channel_subscription.download(dry_run=dry_run) transaction_log = rolling_recent_channel_subscription.download(dry_run=dry_run)
@ -182,9 +184,10 @@ class TestDateRange:
# Invoke the rolling download again, ensure downloading stopped early from it already # Invoke the rolling download again, ensure downloading stopped early from it already
# existing # existing
if not dry_run: if not dry_run:
with assert_debug_log( with assert_logs(
logger=ytdl_sub.downloaders.downloader.download_logger, logger=ytdl_sub.downloaders.downloader.download_logger,
expected_message="ExistingVideoReached, stopping additional downloads", expected_message="ExistingVideoReached, stopping additional downloads",
log_level="info",
): ):
transaction_log = rolling_recent_channel_subscription.download() transaction_log = rolling_recent_channel_subscription.download()

View file

@ -1,5 +1,5 @@
import pytest import pytest
from conftest import assert_debug_log from conftest import assert_logs
from e2e.conftest import mock_run_from_cli from e2e.conftest import mock_run_from_cli
from expected_download import assert_expected_downloads from expected_download import assert_expected_downloads
from expected_transaction_log import assert_transaction_log_matches from expected_transaction_log import assert_transaction_log_matches
@ -82,9 +82,10 @@ class TestPlaylist:
# Ensure another invocation will hit ExistingVideoReached # Ensure another invocation will hit ExistingVideoReached
if not dry_run: if not dry_run:
with assert_debug_log( with assert_logs(
logger=ytdl_sub.downloaders.downloader.download_logger, logger=ytdl_sub.downloaders.downloader.download_logger,
expected_message="ExistingVideoReached, stopping additional downloads", expected_message="ExistingVideoReached, stopping additional downloads",
log_level="info",
): ):
_ = playlist_subscription.download() _ = playlist_subscription.download()
@ -129,9 +130,10 @@ class TestPlaylist:
if not dry_run: if not dry_run:
# Ensure another invocation will hit ExistingVideoReached # Ensure another invocation will hit ExistingVideoReached
with assert_debug_log( with assert_logs(
logger=ytdl_sub.downloaders.downloader.download_logger, logger=ytdl_sub.downloaders.downloader.download_logger,
expected_message="ExistingVideoReached, stopping additional downloads", expected_message="ExistingVideoReached, stopping additional downloads",
log_level="info",
): ):
_ = mock_run_from_cli(args=args)[0][1] _ = mock_run_from_cli(args=args)[0][1]