test_subscribers.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the 'License'). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://aws.amazon.com/apache2.0/
  8. #
  9. # or in the 'license' file accompanying this file. This file is
  10. # distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. from s3transfer.exceptions import InvalidSubscriberMethodError
  14. from s3transfer.subscribers import BaseSubscriber
  15. from tests import unittest
  16. class ExtraMethodsSubscriber(BaseSubscriber):
  17. def extra_method(self):
  18. return 'called extra method'
  19. class NotCallableSubscriber(BaseSubscriber):
  20. on_done = 'foo'
  21. class NoKwargsSubscriber(BaseSubscriber):
  22. def on_done(self):
  23. pass
  24. class OverrideMethodSubscriber(BaseSubscriber):
  25. def on_queued(self, **kwargs):
  26. return kwargs
  27. class OverrideConstructorSubscriber(BaseSubscriber):
  28. def __init__(self, arg1, arg2):
  29. self.arg1 = arg1
  30. self.arg2 = arg2
  31. class TestSubscribers(unittest.TestCase):
  32. def test_can_instantiate_base_subscriber(self):
  33. try:
  34. BaseSubscriber()
  35. except InvalidSubscriberMethodError:
  36. self.fail('BaseSubscriber should be instantiable')
  37. def test_can_call_base_subscriber_method(self):
  38. subscriber = BaseSubscriber()
  39. try:
  40. subscriber.on_done(future=None)
  41. except Exception as e:
  42. self.fail(
  43. 'Should be able to call base class subscriber method. '
  44. 'instead got: %s' % e
  45. )
  46. def test_subclass_can_have_and_call_additional_methods(self):
  47. subscriber = ExtraMethodsSubscriber()
  48. self.assertEqual(subscriber.extra_method(), 'called extra method')
  49. def test_can_subclass_and_override_method_from_base_subscriber(self):
  50. subscriber = OverrideMethodSubscriber()
  51. # Make sure that the overridden method is called
  52. self.assertEqual(subscriber.on_queued(foo='bar'), {'foo': 'bar'})
  53. def test_can_subclass_and_override_constructor_from_base_class(self):
  54. subscriber = OverrideConstructorSubscriber('foo', arg2='bar')
  55. # Make sure you can create a custom constructor.
  56. self.assertEqual(subscriber.arg1, 'foo')
  57. self.assertEqual(subscriber.arg2, 'bar')
  58. def test_invalid_arguments_in_constructor_of_subclass_subscriber(self):
  59. # The override constructor should still have validation of
  60. # constructor args.
  61. with self.assertRaises(TypeError):
  62. OverrideConstructorSubscriber()
  63. def test_not_callable_in_subclass_subscriber_method(self):
  64. with self.assertRaisesRegex(
  65. InvalidSubscriberMethodError, 'must be callable'
  66. ):
  67. NotCallableSubscriber()
  68. def test_no_kwargs_in_subclass_subscriber_method(self):
  69. with self.assertRaisesRegex(
  70. InvalidSubscriberMethodError, 'must accept keyword'
  71. ):
  72. NoKwargsSubscriber()