Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit test coverage for decorators: ignore sig as it is obsolescent #490

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
catalogs: A list of URLs for localization resources

"""
super(LocalizationDecorator, self).__init__()
super().__init__()
self.locale = locale
self.localizable = list(localizable) if localizable else []
self.catalogs = list(catalogs) if catalogs else []
Expand Down
72 changes: 72 additions & 0 deletions aries_cloudagent/messaging/decorators/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json
import pytest
import uuid

from copy import deepcopy
from datetime import datetime, timezone
from time import time
from unittest import TestCase

from marshmallow import fields

from ....messaging.models.base import BaseModel, BaseModelSchema

from ..base import BaseDecoratorSet, DECORATOR_PREFIX


class SampleDecorator(BaseModel):
"""Sample model for base decorator tests."""

class Meta:
"""Sample decorator metadata."""

schema_class = "SampleDecoratorSchema"

def __init__(self, score: int, **kwargs):
"""Initializer."""
super().__init__(**kwargs)
self.score = score


class SampleDecoratorSchema(BaseModelSchema):
"""Sample schema decorator for base decorator tests."""

class Meta:
model_class = SampleDecorator

score = fields.Int(required=True)


class TestBaseDecoratorSet(TestCase):
def test_base_decorator_set(self):
MODELS = {"a": SampleDecorator}
deco_set = BaseDecoratorSet(MODELS)
assert type(deco_set) == BaseDecoratorSet
assert not deco_set.fields
assert deco_set.models == MODELS
assert deco_set.prefix == DECORATOR_PREFIX
assert BaseDecoratorSet.__name__ in str(deco_set)

deco_set_copy = deco_set.copy()
assert type(deco_set_copy) == BaseDecoratorSet
assert not deco_set_copy.fields
assert deco_set_copy.models == MODELS
assert deco_set_copy.prefix == DECORATOR_PREFIX

assert not deco_set.has_field("x")
deco_set.field("x")
assert not deco_set.has_field("x") # empty
assert not len(deco_set.field("x"))
deco_set.remove_field("x")
assert not deco_set.has_field("x")

deco_set.add_model("c", SampleDecorator)
assert "c" in deco_set.models
deco_set.remove_model("c")
assert "c" not in deco_set.models

with pytest.raises(ValueError):
deco_set["a"] = None
deco_set["a"] = {"score": 23}
deco_set["a"] = SampleDecorator(23)
deco_set.load_decorator("a", None)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ...models.base import BaseModel, BaseModelSchema

from ..base import BaseDecoratorSet
from ..default import DecoratorSet, DEFAULT_MODELS


class SimpleModel(BaseModel):
Expand All @@ -25,6 +26,10 @@ class Meta:


class TestDecoratorSet(TestCase):
def test_deco_set(self):
deco_set = DecoratorSet()
assert all(k in deco_set.models for k in DEFAULT_MODELS)

def test_extract(self):

decor_value = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from ..localization_decorator import LocalizationDecorator

from unittest import TestCase


class TestThreadDecorator(TestCase):

LOCALE = "en-ca"
LOCALIZABLE = ["a", "b"]
CATALOGS = ["http://192.168.56.111/my-project/catalog.json"]

def test_init(self):
decorator = LocalizationDecorator()
assert decorator.locale is None
assert decorator.localizable == []
assert decorator.catalogs == []

decorator = LocalizationDecorator(
locale=TestThreadDecorator.LOCALE,
localizable=TestThreadDecorator.LOCALIZABLE,
catalogs=TestThreadDecorator.CATALOGS,
)
assert decorator.locale == TestThreadDecorator.LOCALE
assert decorator.localizable == TestThreadDecorator.LOCALIZABLE
assert decorator.catalogs == TestThreadDecorator.CATALOGS

def test_serialize_load(self):
decorator = LocalizationDecorator(
locale=TestThreadDecorator.LOCALE,
localizable=TestThreadDecorator.LOCALIZABLE,
catalogs=TestThreadDecorator.CATALOGS,
)

dumped = decorator.serialize()
loaded = LocalizationDecorator.deserialize(dumped)

assert loaded.locale == self.LOCALE
assert loaded.localizable == self.LOCALIZABLE
assert loaded.catalogs == self.CATALOGS
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ def test_serialize_load(self):
assert loaded.pthid == self.parent_id
assert loaded.sender_order == self.sender_order
assert loaded.received_orders == self.received_orders

loaded.pthid = "dummy"
assert loaded.pthid == "dummy"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from datetime import datetime
from unittest import TestCase

from ...util import datetime_to_str
from ..timing_decorator import TimingDecorator, TimingDecoratorSchema


NOW = datetime.now()


class TestTimingDecorator(TestCase):
def test_serialize_load(self):
deco = TimingDecorator(in_time=NOW, out_time=NOW,)

assert deco.in_time == datetime_to_str(NOW)
assert deco.out_time == datetime_to_str(NOW)
assert not deco.stale_time
assert not deco.expires_time
assert not deco.delay_milli
assert not deco.wait_until_time

dumped = deco.serialize()
loaded = TimingDecorator.deserialize(dumped)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from unittest import TestCase

from ...valid import UUIDFour
from ..transport_decorator import TransportDecorator, TransportDecoratorSchema


class TestTransportDecorator(TestCase):
def test_serialize_load(self):
deco = TransportDecorator(
return_route="all",
return_route_thread=UUIDFour.EXAMPLE,
queued_message_count=23,
)

assert deco.return_route == "all"
assert deco.return_route_thread == UUIDFour.EXAMPLE
assert deco.queued_message_count == 23

dumped = deco.serialize()
loaded = TransportDecorator.deserialize(dumped)
6 changes: 3 additions & 3 deletions aries_cloudagent/messaging/decorators/transport_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from marshmallow import fields, validate

from ..models.base import BaseModel, BaseModelSchema
from ..valid import UUIDFour
from ..valid import UUIDFour, WHOLE_NUM


class TransportDecorator(BaseModel):
Expand All @@ -33,7 +33,7 @@ def __init__(
return_route_thread: Identify the thread to enable return routing for
queued_message_count: Indicate the number of queued messages
"""
super(TransportDecorator, self).__init__()
super().__init__()
self.return_route = return_route
self.return_route_thread = return_route_thread
self.queued_message_count = queued_message_count
Expand All @@ -59,5 +59,5 @@ class Meta:
example=UUIDFour.EXAMPLE,
)
queued_message_count = fields.Int(
required=False, description="Number of queued messages", example=3,
required=False, description="Number of queued messages", **WHOLE_NUM
)