From 18cba67735477e14decddf4777d1d1233639fc41 Mon Sep 17 00:00:00 2001 From: sklump Date: Mon, 4 May 2020 13:27:17 +0000 Subject: [PATCH] unit test coverage for decorators: ignore sig as it is obsolescent Signed-off-by: sklump --- .../decorators/localization_decorator.py | 2 +- .../messaging/decorators/tests/test_base.py | 72 +++++++++++++++++++ .../decorators/tests/test_decorator_set.py | 5 ++ .../tests/test_localization_decorator.py | 39 ++++++++++ .../decorators/tests/test_thread_decorator.py | 3 + .../decorators/tests/test_timing_decorator.py | 23 ++++++ .../tests/test_transport_decorator.py | 20 ++++++ .../decorators/transport_decorator.py | 6 +- 8 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 aries_cloudagent/messaging/decorators/tests/test_base.py create mode 100644 aries_cloudagent/messaging/decorators/tests/test_localization_decorator.py create mode 100644 aries_cloudagent/messaging/decorators/tests/test_timing_decorator.py create mode 100644 aries_cloudagent/messaging/decorators/tests/test_transport_decorator.py diff --git a/aries_cloudagent/messaging/decorators/localization_decorator.py b/aries_cloudagent/messaging/decorators/localization_decorator.py index dcb922209b..f45b6ca17e 100644 --- a/aries_cloudagent/messaging/decorators/localization_decorator.py +++ b/aries_cloudagent/messaging/decorators/localization_decorator.py @@ -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 [] diff --git a/aries_cloudagent/messaging/decorators/tests/test_base.py b/aries_cloudagent/messaging/decorators/tests/test_base.py new file mode 100644 index 0000000000..f1d1662edd --- /dev/null +++ b/aries_cloudagent/messaging/decorators/tests/test_base.py @@ -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) diff --git a/aries_cloudagent/messaging/decorators/tests/test_decorator_set.py b/aries_cloudagent/messaging/decorators/tests/test_decorator_set.py index 6c8cd99154..6acd7e8b4d 100644 --- a/aries_cloudagent/messaging/decorators/tests/test_decorator_set.py +++ b/aries_cloudagent/messaging/decorators/tests/test_decorator_set.py @@ -4,6 +4,7 @@ from ...models.base import BaseModel, BaseModelSchema from ..base import BaseDecoratorSet +from ..default import DecoratorSet, DEFAULT_MODELS class SimpleModel(BaseModel): @@ -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 = {} diff --git a/aries_cloudagent/messaging/decorators/tests/test_localization_decorator.py b/aries_cloudagent/messaging/decorators/tests/test_localization_decorator.py new file mode 100644 index 0000000000..93e7e32d4b --- /dev/null +++ b/aries_cloudagent/messaging/decorators/tests/test_localization_decorator.py @@ -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 diff --git a/aries_cloudagent/messaging/decorators/tests/test_thread_decorator.py b/aries_cloudagent/messaging/decorators/tests/test_thread_decorator.py index 57a6ee695a..898930233f 100644 --- a/aries_cloudagent/messaging/decorators/tests/test_thread_decorator.py +++ b/aries_cloudagent/messaging/decorators/tests/test_thread_decorator.py @@ -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" diff --git a/aries_cloudagent/messaging/decorators/tests/test_timing_decorator.py b/aries_cloudagent/messaging/decorators/tests/test_timing_decorator.py new file mode 100644 index 0000000000..8e3de33072 --- /dev/null +++ b/aries_cloudagent/messaging/decorators/tests/test_timing_decorator.py @@ -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) diff --git a/aries_cloudagent/messaging/decorators/tests/test_transport_decorator.py b/aries_cloudagent/messaging/decorators/tests/test_transport_decorator.py new file mode 100644 index 0000000000..e2cca257cc --- /dev/null +++ b/aries_cloudagent/messaging/decorators/tests/test_transport_decorator.py @@ -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) diff --git a/aries_cloudagent/messaging/decorators/transport_decorator.py b/aries_cloudagent/messaging/decorators/transport_decorator.py index 0f9bb5c2de..22cc0c1f26 100644 --- a/aries_cloudagent/messaging/decorators/transport_decorator.py +++ b/aries_cloudagent/messaging/decorators/transport_decorator.py @@ -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): @@ -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 @@ -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 )