def test_call_no_func(self, mocker): mock_ismethod = mocker.patch.object( injector.inspect, 'ismethod', return_value=False, ) mock_get_method_self = mocker.patch.object( injector.six, 'get_method_self', return_value='obj', ) mock_get_method_function = mocker.patch.object( injector.six, 'get_method_function', return_value='method', ) mock_from_func = mocker.patch.object( injector.WantSignature, 'from_func', ) obj = injector.Injector() with pytest.raises(TypeError): obj(a=4, b=5, c=6) mock_ismethod.assert_not_called() mock_get_method_self.assert_not_called() mock_get_method_function.assert_not_called() mock_from_func.assert_not_called() mock_from_func.return_value.assert_not_called()
def test_setitem(self): obj = injector.Injector() obj['a'] = 1 assert obj._available == {'a': 1} assert obj._keys == set(['a'])
def test_delitem_missing(self): obj = injector.Injector() with pytest.raises(KeyError): del obj['a'] assert obj._available == {} assert obj._deferred == {} assert obj._keys == set()
def injector(self): """ Retrieve the dependency injector from the WSGI environment. """ if 'micropath.injector' not in self.environ: self.environ['micropath.injector'] = injector.Injector() return self.environ['micropath.injector']
def test_delitem_deferred_only(self): obj = injector.Injector() obj._deferred['a'] = 'deferred' obj._keys = set(['a']) del obj['a'] assert obj._available == {} assert obj._deferred == {} assert obj._keys == set()
def test_cleanup(self, mocker): mock_InjectorCleanup = mocker.patch.object( injector, 'InjectorCleanup', ) obj = injector.Injector() result = obj.cleanup() assert result == mock_InjectorCleanup.return_value mock_InjectorCleanup.assert_called_once_with(obj)
def test_getitem_missing(self, mocker): mock_call = mocker.patch.object( injector.Injector, '__call__', return_value='deferred', ) obj = injector.Injector() with pytest.raises(KeyError): obj['a'] assert obj._available == {} mock_call.assert_not_called()
def test_getitem_deferred(self, mocker): mock_call = mocker.patch.object( injector.Injector, '__call__', return_value='deferred', ) obj = injector.Injector() obj._deferred['a'] = 'func' obj._keys = set(['a']) assert obj['a'] == 'deferred' assert obj._available == {'a': 'deferred'} mock_call.assert_called_once_with('func')
def test_getitem_available(self, mocker): mock_call = mocker.patch.object( injector.Injector, '__call__', return_value='deferred', ) obj = injector.Injector() obj._available['a'] = 1 obj._keys = set(['a']) assert obj['a'] == 1 assert obj._available == {'a': 1} mock_call.assert_not_called()
def test_call_method(self, mocker): mock_ismethod = mocker.patch.object( injector.inspect, 'ismethod', return_value=True, ) mock_get_method_self = mocker.patch.object( injector.six, 'get_method_self', return_value='obj', ) mock_get_method_function = mocker.patch.object( injector.six, 'get_method_function', return_value='method', ) mock_from_func = mocker.patch.object( injector.WantSignature, 'from_func', ) obj = injector.Injector() result = obj('func', 1, 2, 3, a=4, b=5, c=6) assert result == mock_from_func.return_value.return_value mock_ismethod.assert_called_once_with('func') mock_get_method_self.assert_called_once_with('func') mock_get_method_function.assert_called_once_with('func') mock_from_func.assert_called_once_with('method') mock_from_func.return_value.assert_called_once_with( ('obj', 1, 2, 3), obj, { 'a': 4, 'b': 5, 'c': 6 }, )
def test_set_deferred(self): obj = injector.Injector() obj.set_deferred('a', 'deferred') assert obj._deferred == {'a': 'deferred'}
def test_iter(self): obj = injector.Injector() obj._keys |= set(['a', 'b', 'c']) assert set(iter(obj)) == obj._keys
def test_len(self): obj = injector.Injector() obj._keys |= set(['a', 'b', 'c']) assert len(obj) == 3
def test_init(self): result = injector.Injector() assert result._available == {} assert result._deferred == {} assert result._keys == set()