|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +from __future__ import absolute_import, unicode_literals |
| 4 | + |
| 5 | +from boxsdk.object.cloneable import Cloneable |
| 6 | +from boxsdk.util.api_call_decorator import api_call |
| 7 | +from mock import NonCallableMock |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def api_call_result(): |
| 13 | + return {'bar': 'ƒøø'} |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(name='api_call_method') |
| 17 | +def api_call_method_fixture(api_call_result): |
| 18 | + |
| 19 | + @api_call |
| 20 | + def api_call_method(self, *args, **kwargs): |
| 21 | + return self, args, kwargs, api_call_result |
| 22 | + |
| 23 | + return api_call_method |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def cloneable_subclass_with_api_call_method(api_call_method): |
| 28 | + api_call_method_fixture = api_call_method |
| 29 | + |
| 30 | + # pylint:disable=abstract-method |
| 31 | + class CloneableSubclass(Cloneable): |
| 32 | + api_call_method = api_call_method_fixture |
| 33 | + |
| 34 | + return CloneableSubclass |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def mock_cloneable(cloneable_subclass_with_api_call_method): |
| 39 | + |
| 40 | + # pylint:disable=abstract-method |
| 41 | + class MockCloneable(cloneable_subclass_with_api_call_method, NonCallableMock): |
| 42 | + pass |
| 43 | + |
| 44 | + return MockCloneable(spec_set=cloneable_subclass_with_api_call_method, name='Cloneable') |
| 45 | + |
| 46 | + |
| 47 | +def test_api_call_is_decorator(): |
| 48 | + |
| 49 | + @api_call |
| 50 | + def func(): |
| 51 | + pass |
| 52 | + |
| 53 | + assert callable(func) |
| 54 | + assert hasattr(func, '__get__') |
| 55 | + |
| 56 | + |
| 57 | +def test_api_call_decorated_function_must_be_a_method(): |
| 58 | + |
| 59 | + @api_call |
| 60 | + def func(): |
| 61 | + pass |
| 62 | + |
| 63 | + with pytest.raises(TypeError): |
| 64 | + func() |
| 65 | + |
| 66 | + |
| 67 | +def test_api_call_decorated_method_must_be_a_cloneable_method(): |
| 68 | + |
| 69 | + class Cls(object): |
| 70 | + @api_call |
| 71 | + def func(self): |
| 72 | + pass |
| 73 | + |
| 74 | + obj = Cls() |
| 75 | + with pytest.raises(TypeError): |
| 76 | + obj.func() |
| 77 | + |
| 78 | + |
| 79 | +def test_api_call_decorated_method_must_be_bound_to_an_instance_of_the_owner(mock_cloneable, api_call_method): |
| 80 | + # pylint:disable=abstract-method |
| 81 | + class CloneableSubclass2(Cloneable): |
| 82 | + pass |
| 83 | + |
| 84 | + with pytest.raises(TypeError): |
| 85 | + api_call_method.__get__(mock_cloneable, CloneableSubclass2) |
| 86 | + |
| 87 | + |
| 88 | +def test_api_call_decorated_method_returns_itself_when_bound_to_none(api_call_method, cloneable_subclass_with_api_call_method): |
| 89 | + assert api_call_method.__get__(None, Cloneable) is api_call_method |
| 90 | + assert not hasattr(api_call_method.__get__(None, Cloneable), '__self__') |
| 91 | + assert cloneable_subclass_with_api_call_method.api_call_method is api_call_method |
| 92 | + assert not hasattr(cloneable_subclass_with_api_call_method.api_call_method, '__self__') |
| 93 | + |
| 94 | + |
| 95 | +def test_api_call_decorated_method_binds_to_instance(mock_cloneable, api_call_method): |
| 96 | + assert api_call_method.__get__(mock_cloneable, Cloneable) is not api_call_method |
| 97 | + assert api_call_method.__get__(mock_cloneable, Cloneable).__self__ is mock_cloneable |
| 98 | + assert mock_cloneable.api_call_method is not api_call_method |
| 99 | + assert mock_cloneable.api_call_method.__self__ is mock_cloneable |
| 100 | + |
| 101 | + |
| 102 | +def test_api_call_decorated_method_delegates_to_wrapped_method(mock_cloneable, api_call_result): |
| 103 | + args = (1, 2, 'ƒøø', 'bar') |
| 104 | + kwargs = {'bar': 'ƒøø'} |
| 105 | + assert mock_cloneable.api_call_method(*args, **kwargs) == (mock_cloneable, args, kwargs, api_call_result) |
| 106 | + |
| 107 | + |
| 108 | +def test_api_call_decorated_method_can_be_called_as_an_unbound_method_with_an_instance_as_the_first_argument( |
| 109 | + mock_cloneable, |
| 110 | + api_call_result, |
| 111 | + cloneable_subclass_with_api_call_method, |
| 112 | +): |
| 113 | + args = (1, 2, 'ƒøø', 'bar') |
| 114 | + kwargs = {'bar': 'ƒøø'} |
| 115 | + api_call_method = cloneable_subclass_with_api_call_method.api_call_method |
| 116 | + assert api_call_method(mock_cloneable, *args, **kwargs) == (mock_cloneable, args, kwargs, api_call_result) |
0 commit comments