166 lines
4.1 KiB
Python
166 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
# coding: utf8
|
|
|
|
"""test_ldpy.
|
|
|
|
Testing `ldpy.py`
|
|
|
|
Author: frederic zind 2022-09-24
|
|
Licenses: GNU AGPL v3: http://www.gnu.org/licenses/
|
|
"""
|
|
import logging
|
|
from pytest import mark, raises
|
|
|
|
import ldpy
|
|
from ldpy import LAST_E_CHOICES
|
|
|
|
# Min and Max values for the request retrieving the last entries.
|
|
LAST_E_MIN = LAST_E_CHOICES[0] - 1
|
|
LAST_E_MAX = len(LAST_E_CHOICES) + 1
|
|
|
|
|
|
# Faking options from argparse
|
|
class FakeOptions: # pylint: disable=too-few-public-methods
|
|
"""Options object mock."""
|
|
|
|
debug = False
|
|
last = None
|
|
mapping = False
|
|
|
|
def __init__(self, options):
|
|
"""Built loggers options."""
|
|
if "debug" in options:
|
|
self.debug = True
|
|
if "last" in options:
|
|
self.last = 3
|
|
if "mapping" in options:
|
|
self.mapping = True
|
|
|
|
|
|
# ###
|
|
# Testing setup_logging()
|
|
# ###
|
|
def test_logging_debug():
|
|
"""Logger level is DEBUG. Set up with --debug argument."""
|
|
options = FakeOptions(["debug"])
|
|
ldpy.setup_logging(options)
|
|
|
|
assert isinstance((ldpy.logger), logging.Logger)
|
|
assert ldpy.logger.level == 10
|
|
|
|
|
|
def test_logging_default():
|
|
"""Logger level is INFO. Set up without arguments."""
|
|
options = FakeOptions([])
|
|
ldpy.setup_logging(options)
|
|
|
|
assert isinstance((ldpy.logger), logging.Logger)
|
|
assert ldpy.logger.level == 20
|
|
|
|
|
|
# ###
|
|
# Testing parse_args()
|
|
# ###
|
|
def test_parse_args():
|
|
"""Mapping argument is set."""
|
|
cli_options = ldpy.parse_args(["--debug", "--mapping"])
|
|
assert cli_options.debug
|
|
assert cli_options.mapping
|
|
|
|
|
|
def test_parse_args_last_set():
|
|
"""Last argument is set to a non-default value."""
|
|
cli_options = ldpy.parse_args(["--debug", "--last", "2"])
|
|
assert cli_options.debug
|
|
assert cli_options.last == 2
|
|
|
|
|
|
def test_parse_args_last_const():
|
|
"""Last argument is set to a non-default value."""
|
|
cli_options = ldpy.parse_args(["--debug", "--last"])
|
|
assert cli_options.debug
|
|
assert cli_options.last == 3
|
|
|
|
|
|
# ###
|
|
# Testing strip_demo_entries()
|
|
# ###
|
|
def test_strip_demo_entries():
|
|
"""Remove keys/values to ease human readinq in demo stream."""
|
|
payload = {
|
|
"hits": {
|
|
"hits": [
|
|
{
|
|
"_id": "-",
|
|
"_source": {
|
|
"X-OVH-": "-",
|
|
"X-OVH-INPUT": "-",
|
|
"gl2_source": "-",
|
|
"gl2_source_node": "-",
|
|
"id": "-",
|
|
"source": "-",
|
|
"category": "-",
|
|
"title": "-",
|
|
"message": "-",
|
|
"rating_num": 42,
|
|
"streams": "-",
|
|
"timestamp": "-",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
}
|
|
stripped_entry = ldpy.strip_demo_entries(payload)[0]
|
|
|
|
for key in ["source", "category", "title", "message", "rating_num", "timestamp"]:
|
|
stripped_entry.pop(key)
|
|
|
|
assert len(stripped_entry) == 0
|
|
|
|
|
|
# ###
|
|
# Testing get_last_entries()
|
|
# ###
|
|
@mark.parametrize("entry_np", [LAST_E_MIN, LAST_E_MAX])
|
|
def test_get_last_entries_out_of_range(entry_np):
|
|
"""Value is out of range for the last entries."""
|
|
with raises(ValueError):
|
|
ldpy.get_last_entries(entry_np)
|
|
|
|
|
|
@mark.parametrize("entry_np", LAST_E_CHOICES)
|
|
def test_get_last_entries_in_range(entry_np):
|
|
"""Value is in range for the last entries."""
|
|
response = ldpy.get_last_entries(entry_np)
|
|
assert len(response["hits"]["hits"]) == entry_np
|
|
|
|
|
|
# ###
|
|
# Testing main()
|
|
# ###
|
|
def test_main_without_option():
|
|
"""Called without option."""
|
|
options = FakeOptions([])
|
|
|
|
with raises(NotImplementedError):
|
|
ldpy.main(options)
|
|
|
|
|
|
def test_main_demo_with_mapping():
|
|
"""Called with mapping option.
|
|
|
|
`main()` just tranfers `get_map_props()` return: this test is not really useful
|
|
"""
|
|
options = FakeOptions(["mapping"])
|
|
response = ldpy.main(options)
|
|
|
|
assert isinstance(response, list)
|
|
|
|
|
|
def test_main_demo_with_last_const():
|
|
"""Called with last option."""
|
|
options = FakeOptions(["last"])
|
|
response = ldpy.main(options)
|
|
|
|
assert isinstance(response, str)
|