Skip to content

Commit

Permalink
fix unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mgor committed Nov 5, 2024
1 parent a1858a6 commit 47f191d
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 27 deletions.
12 changes: 3 additions & 9 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,9 @@ jobs:
id: pytest
run: python -m pytest --ignore=tests/e2e

- name: coverage (not windows)
if: runner.os != 'Windows'
id: coverage-not-windows
run: python -m coverage report --omit=**/*messagequeue*,**/mq/* --fail-under=90

- name: coverage (windows)
if: runner.os == 'Windows'
id: coverage-windows
run: python -m coverage report --omit=**/*messagequeue*,**/mq/* --fail-under=89
- name: coverage
id: coverage
run: python -m coverage report --omit=**/*messagequeue*,**/mq/*,**/__version__.py --fail-under=89

test-e2e:
name: 'e2e tests ${{ matrix.run_mode }}'
Expand Down
2 changes: 1 addition & 1 deletion grizzly/gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from grizzly.exceptions import TaskTimeoutError

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
import logging
from collections.abc import Generator

Expand Down
2 changes: 1 addition & 1 deletion grizzly/locust.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
]


if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from collections.abc import Generator, Iterator

from gevent.fileobject import FileObjectThread
Expand Down
2 changes: 1 addition & 1 deletion grizzly/testdata/variables/csv_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

from . import AtomicVariable

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from grizzly.context import GrizzlyContextScenario


Expand Down
2 changes: 1 addition & 1 deletion grizzly/testdata/variables/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

from . import AtomicVariable

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from grizzly.context import GrizzlyContextScenario


Expand Down
2 changes: 1 addition & 1 deletion grizzly/testdata/variables/directory_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

from . import AtomicVariable

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from grizzly.context import GrizzlyContextScenario


Expand Down
2 changes: 1 addition & 1 deletion grizzly/testdata/variables/integer_incrementer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

from . import AtomicVariable, AtomicVariablePersist

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from grizzly.context import GrizzlyContextScenario


Expand Down
2 changes: 1 addition & 1 deletion grizzly/testdata/variables/json_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

from . import AtomicVariable

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from grizzly.context import GrizzlyContextScenario


Expand Down
2 changes: 1 addition & 1 deletion grizzly/testdata/variables/random_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from . import AtomicVariable

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from grizzly.context import GrizzlyContextScenario


Expand Down
2 changes: 1 addition & 1 deletion grizzly/testdata/variables/random_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

from . import AtomicVariable

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from grizzly.context import GrizzlyContextScenario


Expand Down
2 changes: 1 addition & 1 deletion grizzly/users/servicebus.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@

from . import GrizzlyUser, grizzlycontext

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from collections.abc import Generator

MAX_LENGTH = 65
Expand Down
2 changes: 1 addition & 1 deletion grizzly/utils/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from grizzly.utils import _print_table
from grizzly_extras.async_message.utils import async_message_request

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from http.cookiejar import CookieJar

from zmq import sugar as ztypes
Expand Down
2 changes: 1 addition & 1 deletion grizzly_extras/async_message/mq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from .rfh2 import Rfh2Decoder, Rfh2Encoder

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from collections.abc import Generator
from threading import Event

Expand Down
2 changes: 1 addition & 1 deletion grizzly_extras/async_message/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from grizzly_extras.async_message import AsyncMessageError, AsyncMessageRequest, AsyncMessageResponse
from grizzly_extras.exceptions import StopScenario

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from zmq import sugar as ztypes

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion grizzly_extras/azure/aad.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from requests.adapters import HTTPAdapter, Retry
from typing_extensions import Self

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from types import TracebackType

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion grizzly_extras/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import TYPE_CHECKING, Callable, TypeVar
from unittest.mock import ANY

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from collections.abc import Iterable

T = TypeVar('T')
Expand Down
53 changes: 50 additions & 3 deletions tests/unit/test_grizzly/users/test_iothub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import json
from dataclasses import dataclass
from typing import TYPE_CHECKING, cast
from uuid import UUID

import pytest
from azure.iot.device import IoTHubDeviceClient
from azure.iot.device import IoTHubDeviceClient, Message
from azure.storage.blob._blob_client import BlobClient
from azure.storage.blob._models import ContentSettings

Expand All @@ -16,9 +17,9 @@
from grizzly.testdata.communication import TestdataConsumer
from grizzly.testdata.utils import transform
from grizzly.types import RequestMethod, ScenarioState
from grizzly.users.iothub import IotHubUser
from grizzly.users.iothub import IotHubUser, IotMessageDecoder, MessageJsonSerializer
from grizzly_extras.transformer import TransformerContentType
from tests.helpers import SOME
from tests.helpers import ANY, SOME

if TYPE_CHECKING: # pragma: no cover
from unittest.mock import MagicMock
Expand Down Expand Up @@ -86,6 +87,44 @@ def parent_fixture(grizzly_fixture: GrizzlyFixture, mocker: MockerFixture) -> Pa
)


def test_message_json_serializer() -> None:
uuid = UUID(bytes=b'foobar31337fooba', version=4)
decoder = MessageJsonSerializer()

assert decoder.default(uuid) == '666f6f62-6172-4331-b333-37666f6f6261'
assert decoder.default(b'foobar') == 'foobar'

with pytest.raises(TypeError):
assert decoder.default(10) == 10


def test_iot_message_decoder(mocker: MockerFixture) -> None:
decoder = IotMessageDecoder(arg='message')

instance_mock = mocker.MagicMock()
instance_mock.device_id = 'foobar'

message = Message(None, message_id='foobar', content_encoding='utf-8', content_type='application/json')
message.custom_properties = {'bar': 'foo'}

actual_metrics, actual_tags = decoder(instance_mock, tags={'foo': 'bar'}, return_value=None, message=message, exception=None)

assert actual_metrics == {'error': None, 'size': ANY(int), 'message_id': 'foobar'}
assert actual_tags == {'identifier': instance_mock.device_id, 'foo': 'bar', 'bar': 'foo'}

exception = RuntimeError('error')

actual_metrics, actual_tags = decoder(instance_mock, tags={'foo': 'bar'}, return_value=None, message=message, exception=exception)

assert actual_metrics == {'error': str(exception), 'size': ANY(int), 'message_id': 'foobar'}
assert actual_tags == {'identifier': instance_mock.device_id, 'foo': 'bar', 'bar': 'foo'}

actual_metrics, actual_tags = decoder(instance_mock, tags=None, return_value=None, message=message, exception=exception)

assert actual_metrics == {'error': str(exception), 'size': ANY(int), 'message_id': 'foobar'}
assert actual_tags == {'identifier': instance_mock.device_id, 'bar': 'foo'}


class TestIotHubUser:
def test_on_start(self, parent_fixture: ParentFixture) -> None:
assert not hasattr(parent_fixture.user, 'iot_client')
Expand Down Expand Up @@ -323,3 +362,11 @@ def test_send_empty_payload(self, parent_fixture: ParentFixture, mocker: MockerF
gzip_compress.assert_not_called()
parent_fixture.blob_client_mock.upload_blob.assert_not_called()
parent_fixture.iot_device_mock.notify_blob_upload_status.assert_not_called()

def test__extract(self, parent_fixture: ParentFixture) -> None:
assert isinstance(parent_fixture.user, IotHubUser)

assert parent_fixture.user._extract({'foo': {'bar': 'baz'}}, '$.foo.bar') == ['baz']

assert parent_fixture.user._extract('{"foo": {"bar": "baz"}}', '$.foo.bar') == ['baz']

0 comments on commit 47f191d

Please sign in to comment.