예제 #1
0
    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()
예제 #2
0
    def test_setitem(self):
        obj = injector.Injector()

        obj['a'] = 1

        assert obj._available == {'a': 1}
        assert obj._keys == set(['a'])
예제 #3
0
    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()
예제 #4
0
    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']
예제 #5
0
    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()
예제 #6
0
    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)
예제 #7
0
    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()
예제 #8
0
    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')
예제 #9
0
    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()
예제 #10
0
    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
            },
        )
예제 #11
0
    def test_set_deferred(self):
        obj = injector.Injector()

        obj.set_deferred('a', 'deferred')

        assert obj._deferred == {'a': 'deferred'}
예제 #12
0
    def test_iter(self):
        obj = injector.Injector()
        obj._keys |= set(['a', 'b', 'c'])

        assert set(iter(obj)) == obj._keys
예제 #13
0
    def test_len(self):
        obj = injector.Injector()
        obj._keys |= set(['a', 'b', 'c'])

        assert len(obj) == 3
예제 #14
0
    def test_init(self):
        result = injector.Injector()

        assert result._available == {}
        assert result._deferred == {}
        assert result._keys == set()