🎨 Lighten `__name__ == '__main__'` #11
Follow python best practices: https://docs.python.org/3/library/__main__.html#idiomatic-usage
This commit is contained in:
parent
a52cc176fd
commit
754227de1d
26
ldpy.py
26
ldpy.py
|
@ -110,6 +110,10 @@ def parse_args(args=sys.argv[1:]):
|
|||
type=int,
|
||||
)
|
||||
|
||||
if len(sys.argv) == 1:
|
||||
parser.print_help(sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
return parser.parse_args(args)
|
||||
|
||||
|
||||
|
@ -164,30 +168,30 @@ def strip_std_field(fields):
|
|||
return fields
|
||||
|
||||
|
||||
def main(options):
|
||||
def main():
|
||||
"""Execute as script. Functions related to the arguments passed."""
|
||||
options = parse_args()
|
||||
setup_logging(options)
|
||||
result = None
|
||||
|
||||
if options.mapping:
|
||||
response = request_map_props()
|
||||
result = request_map_props()
|
||||
|
||||
elif options.last:
|
||||
response = request_last_entries(
|
||||
result = request_last_entries(
|
||||
options.last, strip_std_field(request_map_props())
|
||||
)
|
||||
logger.debug(
|
||||
"Last '%s' entries for '%s' stream:", options.last, client.LDP_STREAM_NAME
|
||||
)
|
||||
logger.debug(response)
|
||||
logger.debug(result)
|
||||
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
return response
|
||||
if not options.debug and result:
|
||||
pp(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pargs = parse_args()
|
||||
setup_logging(pargs)
|
||||
result = main(pargs)
|
||||
|
||||
if not pargs.debug and result:
|
||||
pp(result)
|
||||
sys.exit(main())
|
||||
|
|
|
@ -146,31 +146,67 @@ def test_request_last_entries_in_range(opt_last, fields):
|
|||
# ###
|
||||
# Testing main()
|
||||
# ###
|
||||
def test_main_without_option():
|
||||
|
||||
|
||||
def mock_parse_args_without_option():
|
||||
"""Fakes the argument parsing as empty."""
|
||||
return FakeOptions([])
|
||||
|
||||
|
||||
def mock_parse_args_with_mapping():
|
||||
"""Fakes the argument parsing with mappings option."""
|
||||
return FakeOptions((["mapping"]))
|
||||
|
||||
|
||||
def mock_parse_args_with_last_const():
|
||||
"""Fakes the argument parsing with last option set by const."""
|
||||
return FakeOptions((["last"]))
|
||||
|
||||
|
||||
def test_main_without_option(monkeypatch):
|
||||
"""Called without option."""
|
||||
options = FakeOptions([])
|
||||
monkeypatch.setattr("ldpy.parse_args", mock_parse_args_without_option)
|
||||
|
||||
with raises(NotImplementedError):
|
||||
ldpy.main(options)
|
||||
ldpy.main()
|
||||
|
||||
|
||||
@pytest.mark.vcr()
|
||||
def test_main_demo_with_mapping():
|
||||
def test_main_demo_with_mapping(monkeypatch, capsys):
|
||||
"""Called with mapping option.
|
||||
|
||||
`main()` just transfers `request_map_props()` return: this test is not really useful
|
||||
"""
|
||||
options = FakeOptions(["mapping"])
|
||||
response = ldpy.main(options)
|
||||
monkeypatch.setattr("ldpy.parse_args", mock_parse_args_with_mapping)
|
||||
ldpy.main()
|
||||
|
||||
assert isinstance(response, list)
|
||||
captured = capsys.readouterr()
|
||||
|
||||
assert captured.err == ""
|
||||
assert captured.out != ""
|
||||
|
||||
all_fields = STD_FIELDS.copy()
|
||||
all_fields.extend(DEMO_STREAM_FIELDS)
|
||||
for field in all_fields:
|
||||
assert field in captured.out
|
||||
|
||||
|
||||
@pytest.mark.vcr()
|
||||
def test_main_demo_with_last_const():
|
||||
def test_main_demo_with_last_const(monkeypatch, capsys):
|
||||
"""Called with last option."""
|
||||
options = FakeOptions(["last"])
|
||||
response = ldpy.main(options)
|
||||
monkeypatch.setattr("ldpy.parse_args", mock_parse_args_with_last_const)
|
||||
ldpy.main()
|
||||
|
||||
assert isinstance(response, list)
|
||||
assert isinstance(response[0], dict)
|
||||
captured = capsys.readouterr()
|
||||
|
||||
assert captured.err == ""
|
||||
assert captured.out != ""
|
||||
|
||||
for field in DEMO_STREAM_FIELDS:
|
||||
assert field in captured.out
|
||||
|
||||
# Because "_source" is in captured.out
|
||||
fields = STD_FIELDS.copy()
|
||||
fields.remove("source")
|
||||
for field in fields:
|
||||
assert field not in captured.out
|
||||
|
|
Loading…
Reference in New Issue