more config test coverage
This commit is contained in:
parent
bd1e68730a
commit
613952420f
3 changed files with 39 additions and 5 deletions
|
|
@ -304,7 +304,7 @@ def main() -> List[Tuple[Subscription, FileHandlerTransactionLog]]:
|
||||||
args, extra_args = parser.parse_known_args()
|
args, extra_args = parser.parse_known_args()
|
||||||
|
|
||||||
# Load the config
|
# Load the config
|
||||||
config: ConfigFile = ConfigFile(name="config", value={})
|
config: ConfigFile = ConfigFile(name="default_config", value={})
|
||||||
if args.config:
|
if args.config:
|
||||||
config = ConfigFile.from_file_path(args.config)
|
config = ConfigFile.from_file_path(args.config)
|
||||||
elif os.path.isfile(DEFAULT_CONFIG_FILE_NAME):
|
elif os.path.isfile(DEFAULT_CONFIG_FILE_NAME):
|
||||||
|
|
|
||||||
|
|
@ -43,18 +43,19 @@ class ConfigFile(ConfigValidator):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_dict(cls, config_dict: dict) -> "ConfigFile":
|
def from_dict(cls, config_dict: dict, name: str = "") -> "ConfigFile":
|
||||||
"""
|
"""
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
config_dict:
|
config_dict:
|
||||||
The config in dictionary format
|
The config in dictionary format
|
||||||
|
name:
|
||||||
|
Name of the config
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
Config file validator
|
Config file validator
|
||||||
"""
|
"""
|
||||||
return ConfigFile(name="", value=config_dict)
|
return ConfigFile(name=name, value=config_dict)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_file_path(cls, config_path: str) -> "ConfigFile":
|
def from_file_path(cls, config_path: str) -> "ConfigFile":
|
||||||
|
|
@ -81,7 +82,7 @@ class ConfigFile(ConfigValidator):
|
||||||
f"Did you set --config correctly?"
|
f"Did you set --config correctly?"
|
||||||
) from exc
|
) from exc
|
||||||
|
|
||||||
return ConfigFile.from_dict(config_dict)
|
return ConfigFile.from_dict(name=config_path, config_dict=config_dict)
|
||||||
|
|
||||||
def as_dict(self) -> Dict[str, Any]:
|
def as_dict(self) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import contextlib
|
import contextlib
|
||||||
import logging
|
import logging
|
||||||
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
|
@ -7,7 +8,10 @@ import pytest
|
||||||
|
|
||||||
from src.ytdl_sub import __local_version__
|
from src.ytdl_sub import __local_version__
|
||||||
from src.ytdl_sub.main import main
|
from src.ytdl_sub.main import main
|
||||||
|
from ytdl_sub.cli.main_args_parser import DEFAULT_CONFIG_FILE_NAME
|
||||||
|
from ytdl_sub.config.config_file import ConfigFile
|
||||||
from ytdl_sub.utils.exceptions import ValidationException
|
from ytdl_sub.utils.exceptions import ValidationException
|
||||||
|
from ytdl_sub.utils.file_handler import FileHandler
|
||||||
from ytdl_sub.utils.logger import Logger
|
from ytdl_sub.utils.logger import Logger
|
||||||
from ytdl_sub.utils.logger import LoggerLevels
|
from ytdl_sub.utils.logger import LoggerLevels
|
||||||
|
|
||||||
|
|
@ -94,6 +98,7 @@ def test_args_after_sub_work(mock_sys_exit):
|
||||||
|
|
||||||
assert mock_sub.call_count == 1
|
assert mock_sub.call_count == 1
|
||||||
assert mock_sub.call_args.kwargs["subscription_paths"] == ["subscriptions.yaml"]
|
assert mock_sub.call_args.kwargs["subscription_paths"] == ["subscriptions.yaml"]
|
||||||
|
assert mock_sub.call_args.kwargs["config"]._name == "examples/tv_show_config.yaml"
|
||||||
assert Logger._LOGGER_LEVEL == LoggerLevels.VERBOSE
|
assert Logger._LOGGER_LEVEL == LoggerLevels.VERBOSE
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -107,9 +112,37 @@ def test_no_config_works(mock_sys_exit):
|
||||||
|
|
||||||
assert mock_sub.call_count == 1
|
assert mock_sub.call_count == 1
|
||||||
assert mock_sub.call_args.kwargs["subscription_paths"] == ["subscriptions.yaml"]
|
assert mock_sub.call_args.kwargs["subscription_paths"] == ["subscriptions.yaml"]
|
||||||
|
assert mock_sub.call_args.kwargs["config"]._name == "default_config"
|
||||||
assert Logger._LOGGER_LEVEL == LoggerLevels.VERBOSE
|
assert Logger._LOGGER_LEVEL == LoggerLevels.VERBOSE
|
||||||
|
|
||||||
|
|
||||||
|
def test_uses_default_config_if_present(mock_sys_exit):
|
||||||
|
# If a config exists in the ytdl-sub root dir, just use that and do not delete it
|
||||||
|
preexisting_default_config = os.path.isfile(DEFAULT_CONFIG_FILE_NAME)
|
||||||
|
if not preexisting_default_config:
|
||||||
|
open(DEFAULT_CONFIG_FILE_NAME, "a").close()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with mock_sys_exit(expected_exit_code=0), patch.object(
|
||||||
|
sys,
|
||||||
|
"argv",
|
||||||
|
["ytdl-sub", "sub", "--log-level", "verbose"],
|
||||||
|
), patch(
|
||||||
|
"ytdl_sub.cli.main._download_subscriptions_from_yaml_files"
|
||||||
|
) as mock_sub, patch.object(
|
||||||
|
ConfigFile, "from_file_path", new=lambda _: ConfigFile(name="test default", value={})
|
||||||
|
):
|
||||||
|
main()
|
||||||
|
|
||||||
|
assert mock_sub.call_count == 1
|
||||||
|
assert mock_sub.call_args.kwargs["subscription_paths"] == ["subscriptions.yaml"]
|
||||||
|
assert mock_sub.call_args.kwargs["config"]._name == "test default"
|
||||||
|
assert Logger._LOGGER_LEVEL == LoggerLevels.VERBOSE
|
||||||
|
finally:
|
||||||
|
if not preexisting_default_config:
|
||||||
|
FileHandler.delete(DEFAULT_CONFIG_FILE_NAME)
|
||||||
|
|
||||||
|
|
||||||
def test_no_positional_arg_command(mock_sys_exit):
|
def test_no_positional_arg_command(mock_sys_exit):
|
||||||
with mock_sys_exit(expected_exit_code=1), patch.object(
|
with mock_sys_exit(expected_exit_code=1), patch.object(
|
||||||
sys,
|
sys,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue