Пример #1
0
    def test_raw_returns_function_when_exists(self):
        c = MedleyContainer()
        c._keys = Mock()
        c._keys.__contains__ = Mock(return_value=True)
        c._raw = {'foo': self.foo}

        self.assertEqual(c.raw('foo'), self.foo)
Пример #2
0
    def test_getitem_allows_non_function_values(self):
        c = MedleyContainer()
        c._keys = set([
            'str', 'dict', 'list', 'tuple', 'range', 'regex', 'set',
            'frozenset', 'boolean', 'int', 'float', 'complex', 'bytes',
            'bytearray', 'none'
        ])

        c._values = {
            'str': 'foo',
            'dict': {
                'foo': 'bar'
            },
            'list': ['foo'],
            'tuple': ('foo', ),
            'range': range(10),
            'regex': r'foo',
            'set': set(),
            'frozenset': frozenset(),
            'boolean': False,
            'int': int(10),
            'float': float(10.1),
            'complex': complex(10),
            'bytes': b'bytes',
            'bytearray': bytearray(b'bytes'),
            'none': None
        }

        for key in c._keys:
            self.assertEqual(c[key], c._values[key])
Пример #3
0
    def test_extend_throws_error_when_service_id_does_not_exist(self):
        c = MedleyContainer()
        c._keys = Mock()
        c._keys.__contains__ = Mock(return_value=False)

        with self.assertRaises(Exception):
            c.extend('foo', self.foo)
Пример #4
0
    def test_register_does_not_call_setitem_when_no_second_argument(self):
        c = MedleyContainer()
        provider = Mock(register=Mock())

        with patch.object(c, '__setitem__') as setitem:
            c.register(provider)
            setitem.assert_not_called()
Пример #5
0
    def test_service_decorator_calls_setitem(self):
        c = MedleyContainer()
        c.__setitem__ = Mock()

        @c.service('foo')
        def foo(c):
            return 'bar'

        c.__setitem__.assert_called_once()
Пример #6
0
    def test_create_extends_decorator_calls_extend(self):
        c = MedleyContainer()
        c.extend = Mock()

        @c.extends('foo')
        def foo(c):
            return 'bar'

        c.extend.assert_called_once()
Пример #7
0
    def test_extends_wraps_function_and_calls_setitem(self):
        c = MedleyContainer()
        c._keys = set(['foo'])
        c._values = {'foo': self.foo}

        with patch.object(MedleyContainer, '__setitem__',
                          wraps=c.__setitem__) as setitem:
            result = c.extend('foo', self.bar)
            self.assertEqual(type(result), types.FunctionType)
            self.assertEqual(c['foo'], result(c))
            setitem.assert_called_once()
Пример #8
0
    def test_create_factory_decorator_calls_factory(self):
        c = MedleyContainer()
        c.__setitem__ = Mock()
        c.factory = Mock(return_value='bar')

        @c.create_factory('foo')
        def foo(c):
            return 'bar'

        c.__setitem__.assert_called_once_with('foo', 'bar')
        c.factory.assert_called_once()
Пример #9
0
    def test_register_sets_ids_from_second_argument(self):
        c = MedleyContainer()
        provider = Mock(register=Mock())

        with patch.object(c, '__setitem__') as setitem:
            c.register(provider, {
                'foo': self.foo,
                'bar': self.bar,
                'baz': self.baz
            })

            self.assertEqual(setitem.call_count, 3)
Пример #10
0
    def test_factory_throws_error_if_arg_not_function(self):
        c = MedleyContainer()

        with self.assertRaises(Exception):
            c.factory('foo')

        with self.assertRaises(Exception):
            c.factory(False)

        with self.assertRaises(Exception):
            c.factory(r'match')

        with self.assertRaises(Exception):
            c.factory(0xf)
Пример #11
0
    def test_match_returns_matched_functions_by_key_name(self):
        c = MedleyContainer()
        c._keys = set(['foo', 'bar', 'baz'])

        def call_fake(id):
            values = {'foo': self.foo, 'bar': self.bar, 'baz': self.baz}

            return values[id]

        with patch.object(MedleyContainer, '__getitem__',
                          wraps=call_fake) as getitem:
            result = c.match("foo")
            getitem.assert_called_with('foo')

            self.assertEqual(len(result), 1)
            self.assertEqual(result.pop(), self.foo)

            result = c.match("(foo|bar)")
            self.assertEqual(len(result), 2)
            self.assertEqual(self.foo in result, True)
            self.assertEqual(self.bar in result, True)

            result = c.match("(foo|bar|baz)")
            self.assertEqual(len(result), 3)
            self.assertEqual(self.foo in result, True)
            self.assertEqual(self.bar in result, True)
            self.assertEqual(self.baz in result, True)
            self.assertEqual(self.bat in result, False)

            result = c.match("bat")
            self.assertEqual(len(result), 0)
Пример #12
0
    def test_constructor_loads_default_values(self):
        c = MedleyContainer()

        self.assertEqual(c._values, {})
        self.assertEqual(c._raw, {})
        self.assertEqual(c._factories, set())
        self.assertEqual(c._protected, set())
        self.assertEqual(c._frozen, set())
        self.assertEqual(c._keys, set())
Пример #13
0
    def test_multi_dot_setitem_access(self):
        with patch.object(MedleyContainer, '__setitem__') as setitem:
            c = MedleyContainer()

            c['foo.bar'] = self.foo
            setitem.assert_called_with('foo.bar', self.foo)

            c['baz.bat'] = self.baz
            setitem.assert_called_with('baz.bat', self.baz)
Пример #14
0
    def test_constructor_calls_set_multiple_times_when_object_is_provided(
            self):
        with patch.object(MedleyContainer, '__setitem__'):
            c = MedleyContainer({
                'foo': lambda c: None,
                'bar': lambda c: None,
                'baz': lambda c: None
            })

            self.assertEqual(3, c.__setitem__.call_count)
Пример #15
0
    def test_getitem_returns_raw_function_when_id_in_raw_set(self):
        c = MedleyContainer()
        c._keys = Mock(__contains__=Mock(return_value=True))
        c._protected = Mock(__contains__=Mock(return_value=False))

        c._raw = {'foo': self.foo}
        c._values = {'foo': self.foo}

        self.assertEqual(c.__getitem__('foo'), self.foo)
Пример #16
0
    def test_setitem_adds_id_to_both_values_and_keys_attr(self):
        c = MedleyContainer()
        c._frozen = Mock(__contains__=Mock(return_value=False))
        c._keys = Mock(add=Mock())
        c._values = MagicMock()

        c.__setitem__('foo', self.foo)
        c._values.__setitem__.assert_called_with('foo', self.foo)
        c._keys.add.assert_called_with('foo')
Пример #17
0
    def test_getitem_stores_raw_function_and_freezes(self):
        c = MedleyContainer()
        c._keys = Mock(__contains__=Mock(return_value=True))
        c._protected = Mock(__contains__=Mock(return_value=False))
        c._factories = Mock(__contains__=Mock(return_value=False))
        c._frozen = Mock(add=Mock())

        c._values = {'foo': self.foo}

        self.assertEqual(c.__getitem__('foo'), 'foo')
        self.assertEqual(c._values, {'foo': 'foo'})
        self.assertEqual(c._raw, {'foo': self.foo})
        c._frozen.add.assert_called_with('foo')
Пример #18
0
    def test_throws_error_on_invalid_id(self):
        c = MedleyContainer()

        with self.assertRaises(Exception):
            c['foo']

        with self.assertRaises(Exception):
            c['bar']

        with self.assertRaises(Exception):
            c['baz']
Пример #19
0
    def test_delitem_does_not_delete_from_factories_or_protected_when_id_is_str(
            self):
        c = MedleyContainer()
        c._keys = Mock(__contains__=Mock(return_value=True), discard=Mock())
        c._protected = Mock(discard=Mock())
        c._factories = Mock(discard=Mock())
        c._frozen = Mock(discard=Mock())

        c._values = {'foo': 'foo'}
        c.__delitem__('foo')

        c._factories.discard.assert_not_called()
        c._protected.discard.assert_not_called()
        self.assertEqual(c._values, {})
        self.assertEqual(c._raw, {})
        c._frozen.discard.assert_called_with('foo')
        c._keys.discard.assert_called_with('foo')
Пример #20
0
    def test_delitem_deletes_from_everywhere_wwhen_id_is_function(self):
        c = MedleyContainer()
        c._keys = Mock(__contains__=Mock(return_value=True), discard=Mock())
        c._protected = Mock(discard=Mock())
        c._factories = Mock(discard=Mock())
        c._frozen = Mock(discard=Mock())

        c._values = {'foo': self.foo}
        c._raw = {'foo': self.foo}

        self.assertFalse(c.__delitem__('foo'))
        c._factories.discard.assert_called_with(self.foo)
        c._protected.discard.assert_called_with(self.foo)
        self.assertEqual(c._values, {})
        self.assertEqual(c._raw, {})
        c._frozen.discard.assert_called_with('foo')
        c._keys.discard.assert_called_with('foo')
Пример #21
0
    def test_getitem_executes_factory_if_exists(self):
        c = MedleyContainer()
        c._keys = Mock(__contains__=Mock(return_value=True))
        c._protected = Mock(__contains__=Mock(return_value=False))
        c._factories = Mock(__contains__=Mock(return_value=True))

        c._values = {'foo': self.foo}

        self.assertEqual(c.__getitem__('foo'), 'foo')
        self.foo.assert_called_once_with(c)
Пример #22
0
    def test_del_operator(self):
        with patch.object(MedleyContainer, '__delitem__') as delitem:
            c = MedleyContainer()

            del c['foo']
            delitem.assert_called_with('foo')

            del c['bar']
            delitem.assert_called_with('bar')

            del c['baz']
            delitem.assert_called_with('baz')
Пример #23
0
    def test_getitem_returns_value_when_id_is_not_function(self):
        c = MedleyContainer()
        c._keys = Mock(__contains__=Mock(return_value=True))
        c._protected = Mock(__contains__=Mock(return_value=False))

        c._values = {'foo': 'foo', 'bar': {}, 'baz': [], 'bat': False}

        self.assertEqual(c.__getitem__('foo'), 'foo')
        self.assertEqual(c.__getitem__('bar'), {})
        self.assertEqual(c.__getitem__('baz'), [])
        self.assertEqual(c.__getitem__('bat'), False)
Пример #24
0
    def test_factory_adds_factory_function_to_factories_attr(self):
        c = MedleyContainer()
        factories = set()

        with patch.object(c, '_factories', wraps=factories):
            c.factory(self.foo)
            c._factories.add.assert_called_with(self.foo)

            c.factory(self.bar)
            c._factories.add.assert_called_with(self.bar)

            c.factory(self.baz)
            c._factories.add.assert_called_with(self.baz)

            self.assertEqual(c._factories.add.call_count, 3)
Пример #25
0
    def test_factory_prevents_duplicates(self):
        c = MedleyContainer()
        factories = set()

        with patch.object(c, '_factories', wraps=factories):
            c.factory(self.foo)
            c._factories.add.assert_called_with(self.foo)

            c.factory(self.foo)
            c._factories.add.assert_called_with(self.foo)

            c.factory(self.foo)
            c._factories.add.assert_called_with(self.foo)

            self.assertEqual(c._factories.add.call_count, 3)
            self.assertEqual(len(factories), 1)
Пример #26
0
    def test_raw_returns_raw_function_from_values_attr_when_it_does_not_exist_in_raw_attr(
            self):
        c = MedleyContainer()
        c._keys = Mock()
        c._keys.__contains__ = Mock(return_value=True)
        c._raw = Mock()
        c._raw.__contains__ = Mock(return_value=False)
        c._values = {'foo': self.foo}

        self.assertEqual(c.raw('foo'), self.foo)

        c._keys.__contains__.assert_called_with('foo')
        c._raw.__contains__.assert_called_with('foo')
Пример #27
0
    def test_extends_adds_wrapped_function_to_factories_when_exists(self):
        c = MedleyContainer()
        c._keys = set(['foo'])
        c._values = {'foo': self.foo}
        factories = set([self.foo])

        with patch.object(c, '_factories') as f:
            f.__contains__.side_effect = factories.__contains__
            f.add.side_effect = factories.add
            f.remove.side_effect = factories.remove

            c.extend('foo', self.bar)
            f.__contains__.assert_called_with(self.foo)
            f.remove.assert_called_once()
            f.add.assert_called_once()
Пример #28
0
    def test_in_operator(self):
        def call_fake(id):
            values = {
                'foo': self.foo,
                '@foo': self.foo,
                '#foo': self.foo,
                '$foo': self.foo,
                'bar': self.bar,
                'baz': self.baz,
                'bat.ban': self.bat
            }

            return id in values

        with patch.object(MedleyContainer, '__contains__',
                          wraps=call_fake) as contains:
            c = MedleyContainer()

            self.assertEqual('foo' in c, True)
            contains.assert_called_with('foo')

            self.assertEqual('@foo' in c, True)
            contains.assert_called_with('@foo')

            self.assertEqual('#foo' in c, True)
            contains.assert_called_with('#foo')

            self.assertEqual('$foo' in c, True)
            contains.assert_called_with('$foo')

            self.assertEqual('bar' in c, True)
            contains.assert_called_with('bar')

            self.assertEqual('baz' in c, True)
            contains.assert_called_with('baz')

            self.assertEqual('bat.ban' in c, True)
            contains.assert_called_with('bat.ban')

            self.assertEqual('fail' in c, False)
            contains.assert_called_with('fail')
Пример #29
0
    def test_setitem_access(self):
        with patch.object(MedleyContainer, '__setitem__') as setitem:
            c = MedleyContainer()

            c['foo'] = self.foo
            setitem.assert_called_with('foo', self.foo)

            c['@foo'] = self.foo
            setitem.assert_called_with('@foo', self.foo)

            c['$foo'] = self.foo
            setitem.assert_called_with('$foo', self.foo)

            c['#foo'] = self.foo
            setitem.assert_called_with('#foo', self.foo)

            c['bar'] = self.bar
            setitem.assert_called_with('bar', self.bar)

            c['_baz'] = self.baz
            setitem.assert_called_with('_baz', self.baz)
Пример #30
0
    def test_getitem_access(self):
        def call_fake(id):
            values = {
                'foo': self.foo,
                '@foo': self.foo,
                '#foo': self.foo,
                '$foo': self.foo,
                'bar': self.bar,
                'baz': self.baz,
                'bat.ban': self.bat
            }

            return values[id]

        with patch.object(MedleyContainer, '__getitem__',
                          wraps=call_fake) as getitem:
            c = MedleyContainer()

            self.assertEqual(c['foo'], self.foo)
            getitem.assert_called_with('foo')

            self.assertEqual(c['@foo'], self.foo)
            getitem.assert_called_with('@foo')

            self.assertEqual(c['#foo'], self.foo)
            getitem.assert_called_with('#foo')

            self.assertEqual(c['$foo'], self.foo)
            getitem.assert_called_with('$foo')

            self.assertEqual(c['bar'], self.bar)
            getitem.assert_called_with('bar')

            self.assertEqual(c['baz'], self.baz)
            getitem.assert_called_with('baz')

            self.assertEqual(c['bat.ban'], self.bat)
            getitem.assert_called_with('bat.ban')