126 lines
3.1 KiB
Python
126 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
# coding: utf8
|
|
|
|
"""tests_<SCRIPT_NAME>.
|
|
|
|
Testing `main.py`
|
|
|
|
Author: {developer} <{mail}> {date}
|
|
Licence: GNU AGPL v3: http://www.gnu.org/licenses/
|
|
|
|
This file is part of [`<GIT REPO NAME>`](<GIT REPO URL>)
|
|
"""
|
|
import logging
|
|
import shlex
|
|
|
|
from pytest import mark
|
|
|
|
import main
|
|
|
|
|
|
# Faking options from argparse
|
|
class FakeOptions: # pylint: disable=too-few-public-methods
|
|
"""Options object mock."""
|
|
|
|
debug = False
|
|
foo = None # pylint: disable=disallowed-name
|
|
bar = False # pylint: disable=disallowed-name
|
|
|
|
def __init__(self, options):
|
|
"""Built loggers options."""
|
|
if "debug" in options:
|
|
self.debug = True
|
|
if "foo" in options:
|
|
self.foo = True # pylint: disable=disallowed-name
|
|
if "bar" in options:
|
|
self.bar = "BAZ" # pylint: disable=disallowed-name
|
|
|
|
|
|
def mock_parse_args_bar():
|
|
"""Mock for parse_args()."""
|
|
return FakeOptions("bar")
|
|
|
|
|
|
def mock_parse_args_foo():
|
|
"""Mock for parse_args()."""
|
|
return FakeOptions("foo")
|
|
|
|
|
|
# Faking logging
|
|
class FakeLogging: # pylint: disable=too-few-public-methods
|
|
"""Options object mock."""
|
|
|
|
|
|
def mock_setup_logging(useless_parameter): # pylint: disable=unused-argument
|
|
"""Mock for setup_logging()."""
|
|
return FakeLogging()
|
|
|
|
|
|
# ###
|
|
# Testing setup_logging()
|
|
# ###
|
|
def test_logging_debug():
|
|
"""Logger level is DEBUG. Set up with --debug argument."""
|
|
options = FakeOptions(["debug"])
|
|
main.setup_logging(options)
|
|
|
|
assert isinstance((main.logger), logging.Logger)
|
|
assert main.logger.level == 10
|
|
|
|
|
|
def test_logging_default():
|
|
"""Logger level is INFO. Set up without arguments."""
|
|
options = FakeOptions([])
|
|
main.setup_logging(options)
|
|
|
|
assert isinstance((main.logger), logging.Logger)
|
|
assert main.logger.level == 20
|
|
|
|
|
|
# ###
|
|
# Testing parse_args()
|
|
# ###
|
|
@mark.parametrize(
|
|
"args, expected",
|
|
[
|
|
("-df", {"debug": True, "foo_var": True, "bar_var": None}),
|
|
("--debug --foo", {"debug": True, "foo_var": True, "bar_var": None}),
|
|
("-db", {"debug": True, "foo_var": False, "bar_var": "BAR"}),
|
|
(
|
|
"-db BAZ",
|
|
{
|
|
"debug": True,
|
|
"foo_var": False,
|
|
"bar_var": "BAZ",
|
|
},
|
|
),
|
|
],
|
|
)
|
|
def test_parse_args(args, expected):
|
|
"""Mapping argument is set."""
|
|
cli_options = main.parse_args(shlex.split(args))
|
|
assert cli_options.debug == expected["debug"]
|
|
assert cli_options.foo == expected["foo_var"]
|
|
assert cli_options.bar == expected["bar_var"]
|
|
|
|
|
|
# ###
|
|
# Testing main()
|
|
# ###
|
|
def test_main_demo_with_foo(monkeypatch, caplog):
|
|
"""Called with options."""
|
|
monkeypatch.setattr("main.parse_args", mock_parse_args_foo)
|
|
monkeypatch.setattr("main.setup_logging", mock_setup_logging)
|
|
main.main()
|
|
|
|
assert caplog.text == "INFO pytest:main.py:155 I am foo\n"
|
|
|
|
|
|
def test_main_demo_with_bar(monkeypatch, caplog):
|
|
"""Called with options."""
|
|
monkeypatch.setattr("main.parse_args", mock_parse_args_bar)
|
|
monkeypatch.setattr("main.setup_logging", mock_setup_logging)
|
|
main.main()
|
|
|
|
assert caplog.text == "INFO pytest:main.py:158 I am BAZ\n"
|