|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from s3transfer.exceptions import InvalidSubscriberMethodError |
|
from s3transfer.subscribers import BaseSubscriber |
|
from tests import unittest |
|
|
|
|
|
class ExtraMethodsSubscriber(BaseSubscriber): |
|
def extra_method(self): |
|
return 'called extra method' |
|
|
|
|
|
class NotCallableSubscriber(BaseSubscriber): |
|
on_done = 'foo' |
|
|
|
|
|
class NoKwargsSubscriber(BaseSubscriber): |
|
def on_done(self): |
|
pass |
|
|
|
|
|
class OverrideMethodSubscriber(BaseSubscriber): |
|
def on_queued(self, **kwargs): |
|
return kwargs |
|
|
|
|
|
class OverrideConstructorSubscriber(BaseSubscriber): |
|
def __init__(self, arg1, arg2): |
|
self.arg1 = arg1 |
|
self.arg2 = arg2 |
|
|
|
|
|
class TestSubscribers(unittest.TestCase): |
|
def test_can_instantiate_base_subscriber(self): |
|
try: |
|
BaseSubscriber() |
|
except InvalidSubscriberMethodError: |
|
self.fail('BaseSubscriber should be instantiable') |
|
|
|
def test_can_call_base_subscriber_method(self): |
|
subscriber = BaseSubscriber() |
|
try: |
|
subscriber.on_done(future=None) |
|
except Exception as e: |
|
self.fail( |
|
'Should be able to call base class subscriber method. ' |
|
'instead got: %s' % e |
|
) |
|
|
|
def test_subclass_can_have_and_call_additional_methods(self): |
|
subscriber = ExtraMethodsSubscriber() |
|
self.assertEqual(subscriber.extra_method(), 'called extra method') |
|
|
|
def test_can_subclass_and_override_method_from_base_subscriber(self): |
|
subscriber = OverrideMethodSubscriber() |
|
|
|
self.assertEqual(subscriber.on_queued(foo='bar'), {'foo': 'bar'}) |
|
|
|
def test_can_subclass_and_override_constructor_from_base_class(self): |
|
subscriber = OverrideConstructorSubscriber('foo', arg2='bar') |
|
|
|
self.assertEqual(subscriber.arg1, 'foo') |
|
self.assertEqual(subscriber.arg2, 'bar') |
|
|
|
def test_invalid_arguments_in_constructor_of_subclass_subscriber(self): |
|
|
|
|
|
with self.assertRaises(TypeError): |
|
OverrideConstructorSubscriber() |
|
|
|
def test_not_callable_in_subclass_subscriber_method(self): |
|
with self.assertRaisesRegex( |
|
InvalidSubscriberMethodError, 'must be callable' |
|
): |
|
NotCallableSubscriber() |
|
|
|
def test_no_kwargs_in_subclass_subscriber_method(self): |
|
with self.assertRaisesRegex( |
|
InvalidSubscriberMethodError, 'must accept keyword' |
|
): |
|
NoKwargsSubscriber() |
|
|