Skip to content

Commit 73afa58

Browse files
authored
Merge pull request #89 from GabrielSalla/add-branch-coverage
Add branch coverage
2 parents ddf9699 + f7981e4 commit 73afa58

File tree

11 files changed

+112
-5
lines changed

11 files changed

+112
-5
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ filterwarnings = [
9797
"error",
9898
"ignore:datetime.datetime.utcnow:DeprecationWarning:botocore",
9999
]
100+
[tool.coverage.run]
101+
branch = true
100102

101103
[tool.coverage.report]
102104
omit =[

src/components/monitors_loader/monitor_module_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from notifications import BaseNotification
66

77

8-
class MonitorModule(Protocol):
8+
class MonitorModule(Protocol): # pragma: no cover
99
"""Class that represents a base monitor module structure"""
1010

1111
SENTINELA_MONITOR_ID: int

src/databases/protocols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
@runtime_checkable
5-
class Pool(Protocol):
5+
class Pool(Protocol): # pragma: no cover
66
PATTERNS: list[str]
77
name: str
88

src/message_queue/protocols.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from typing import Any, Protocol, runtime_checkable
22

33

4-
class Message(Protocol):
4+
class Message(Protocol): # pragma: no cover
55
id: str
66

77
@property
88
def content(self) -> dict[str, Any]: ...
99

1010

1111
@runtime_checkable
12-
class Queue(Protocol):
12+
class Queue(Protocol): # pragma: no cover
1313
def __init__(self, config: dict[str, Any]) -> None: ...
1414

1515
@property

src/notifications/base_notification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@runtime_checkable
7-
class BaseNotification(Protocol):
7+
class BaseNotification(Protocol): # pragma: no cover
88
min_priority_to_send: int = 5
99

1010
def reactions_list(self) -> list[tuple[str, list[reaction_function_type]]]: ...

src/utils/log.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,7 @@ def setup() -> None:
6565
stream.setFormatter(FriendlyFormatter(configs.logging.format))
6666
elif configs.logging.mode == "json":
6767
stream.setFormatter(JsonFormatter(configs.logging.fields))
68+
else:
69+
raise ValueError(f"Unknown logging mode: '{configs.logging.mode}'")
6870

6971
logging.basicConfig(level=logging.INFO, handlers=[stream])

tests/components/controller/test_controller.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,34 @@ async def test_run(monkeypatch, clear_queue, clear_database):
394394
)
395395

396396

397+
async def test_run_no_sleep(mocker, monkeypatch):
398+
"""'run' should not sleep if the controller completes the loop and another one is already
399+
triggered"""
400+
monkeypatch.setattr(configs, "load_sample_monitors", True)
401+
monkeypatch.setattr(configs, "internal_monitors_path", "tests/sample_monitors/internal")
402+
monkeypatch.setattr(configs, "sample_monitors_path", "tests/sample_monitors/others")
403+
404+
run_procedures_mock = AsyncMock()
405+
monkeypatch.setattr(controller, "run_procedures", run_procedures_mock)
406+
407+
monkeypatch.setattr(controller, "is_triggered", lambda *args, **kwargs: True)
408+
409+
sleep_spy: AsyncMock = mocker.spy(app, "sleep")
410+
411+
# Run the controller for a while then stop it
412+
await monitors_loader._register_monitors()
413+
await monitors_loader._load_monitors(None)
414+
415+
controller_task = asyncio.create_task(controller.run())
416+
await asyncio.sleep(0.5)
417+
418+
# Stop the app and wait for the controller task
419+
app.stop()
420+
await asyncio.wait_for(controller_task, timeout=0.5)
421+
422+
sleep_spy.assert_not_called()
423+
424+
397425
async def test_run_current_task_error(caplog, monkeypatch):
398426
"""'run' should log an error if it can't get the current task and finish the execution"""
399427
monkeypatch.setattr(asyncio, "current_task", lambda: None)

tests/module_loader/test_loader.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,18 @@ def test_load_module_from_string():
288288

289289
assert module_path == Path("tmp") / module_name / f"{module_name}.py"
290290
assert module.get_value() == 200
291+
292+
293+
def test_load_module_from_string_with_base_path():
294+
"""'load_module_from_string' should create the module file and load it with a custom base
295+
path"""
296+
module_name = "test_load_module_from_string_with_base_path"
297+
module_code = "def get_value(): return {n}"
298+
299+
base_path = Path("tmp") / "some_test_path"
300+
module_path, module = loader.load_module_from_string(
301+
module_name, module_code.format(n=987), base_path=str(base_path)
302+
)
303+
304+
assert module_path == base_path / module_name / f"{module_name}.py"
305+
assert module.get_value() == 987

tests/plugins/aws/queues/sqs/test_sqs_queue.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,41 @@ async def delete_queue(queue_url: str) -> None:
4242
assert e.response["Error"]["Code"] == "AWS.SimpleQueueService.NonExistentQueue"
4343

4444

45+
@pytest.mark.parametrize(
46+
"config",
47+
[
48+
{
49+
"type": "plugin.aws.sqs",
50+
"name": "app",
51+
"url": "http://motoserver:5000/123456789012/app",
52+
"region": "us-east-1",
53+
"create_queue": True,
54+
"queue_wait_message_time": 2,
55+
"queue_visibility_time": 15,
56+
},
57+
{
58+
"type": "plugin.aws.sqs",
59+
"name": "other",
60+
"url": "some_other_url",
61+
# No region
62+
"create_queue": False,
63+
"queue_wait_message_time": 5,
64+
"queue_visibility_time": 20,
65+
},
66+
],
67+
)
68+
async def test_queue_init_aws_client_params(config):
69+
"""'Queue' should initialize with the correct AWS client parameters"""
70+
queue = sqs_queue.Queue(config=config)
71+
72+
assert queue._aws_client_params["credential_name"] == "application"
73+
assert queue._aws_client_params["service"] == "sqs"
74+
if "region" in config:
75+
assert queue._aws_client_params["region_name"] == config["region"]
76+
else:
77+
assert "region_name" not in queue._aws_client_params
78+
79+
4580
@pytest.mark.parametrize("queue_wait_message_time", [1, 2, 3, 4, 5])
4681
async def test_queue_wait_message_time(queue_wait_message_time):
4782
queue = sqs_queue.Queue(

tests/utils/test_exception_handling.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ async def error() -> None:
4242
logger_error_spy.assert_called_once_with("error function timed out")
4343

4444

45+
async def test_catch_exceptions_timeout_no_message(caplog):
46+
"""'catch_exceptions' should not log any message if a 'TimeoutError' exception is raised and
47+
no timeout message is provided"""
48+
with catch_exceptions():
49+
await asyncio.wait_for(asyncio.sleep(0.1), timeout=0.01)
50+
51+
assert_message_not_in_log(caplog, "Exception caught successfully, going on")
52+
assert_message_not_in_log(caplog, "TimeoutError")
53+
54+
4555
async def test_catch_exceptions_base_exception(caplog):
4656
"""'catch_exceptions' should log the exception message if an exception inherited from
4757
'BaseSentinelaException' is raised"""

0 commit comments

Comments
 (0)