Skip to content

Commit

Permalink
Move mock from fixture to test
Browse files Browse the repository at this point in the history
Only a single test is using the mock and it is a single line to set up.
Note that using the context manager is only required when you want the mock teared down during the test.
  • Loading branch information
twiggler committed Sep 23, 2024
1 parent 9da0ee3 commit 440f7ec
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions tests/loaders/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Iterator
from unittest.mock import MagicMock, patch

import paho.mqtt.client as mqtt
import pytest

from dissect.target import Target
Expand Down Expand Up @@ -68,13 +69,6 @@ def read(self, *args) -> MockSeekMessage | None:
return None


@pytest.fixture
def mock_client(monkeypatch: pytest.MonkeyPatch) -> None:
import paho.mqtt.client as mqtt

monkeypatch.setattr(mqtt, "Client", MQTTMock)


@pytest.fixture
def mock_broker() -> Iterator[MockBroker]:
yield MockBroker()
Expand All @@ -93,7 +87,7 @@ def mock_broker() -> Iterator[MockBroker]:
@patch.object(time, "sleep") # improve speed during test, no need to wait for peers
def test_remote_loader_stream(
time: MagicMock,
mock_client: None,
monkeypatch: pytest.MonkeyPatch,
alias: str,
hosts: list[str],
disks: list[int],
Expand All @@ -102,6 +96,8 @@ def test_remote_loader_stream(
read: int,
expected: bytes,
) -> None:
monkeypatch.setattr(mqtt, "Client", MQTTMock)

broker = Broker("0.0.0.0", "1884", "key", "crt", "ca", "case1", "user", "pass")
broker.connect()
broker.mqtt_client.fill_disks(disks)
Expand Down

0 comments on commit 440f7ec

Please sign in to comment.