Пример #1
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])
Пример #2
0
    def test_getitem_returns_raw_function_when_id_in_protected_set(self):
        c = MedleyContainer()
        c._keys = Mock(__contains__=Mock(return_value=True))
        c._protected = Mock(__contains__=Mock(return_value=True))

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

        self.assertEqual(c.__getitem__('foo'), self.foo)
Пример #3
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')
Пример #4
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)
Пример #5
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)
Пример #6
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()
Пример #7
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')
Пример #8
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')
Пример #9
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()
Пример #10
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')
Пример #11
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')
Пример #12
0
    def test_setitem_allows_non_function_values(self):
        c = MedleyContainer()
        c._frozen = Mock(__contains__=Mock(return_value=False))
        c._keys = Mock(add=Mock())
        c._values = MagicMock()

        c.__setitem__('str', 'str')
        c._keys.add.assert_called_with('str')
        c._values.__setitem__.assert_called_with('str', 'str')

        c.__setitem__('dict', {})
        c._keys.add.assert_called_with('dict')
        c._values.__setitem__.assert_called_with('dict', {})

        c.__setitem__('list', [])
        c._keys.add.assert_called_with('list')
        c._values.__setitem__.assert_called_with('list', [])

        c.__setitem__('tuple', ('foo', 'bar'))
        c._keys.add.assert_called_with('tuple')
        c._values.__setitem__.assert_called_with('tuple', ('foo', 'bar'))

        r = range(10)
        c.__setitem__('range', r)
        c._keys.add.assert_called_with('range')
        c._values.__setitem__.assert_called_with('range', r)

        c.__setitem__('regex', r'foo')
        c._keys.add.assert_called_with('regex')
        c._values.__setitem__.assert_called_with('regex', r'foo')

        c.__setitem__('set', set())
        c._keys.add.assert_called_with('set')
        c._values.__setitem__.assert_called_with('set', set())

        c.__setitem__('frozenset', frozenset())
        c._keys.add.assert_called_with('frozenset')
        c._values.__setitem__.assert_called_with('frozenset', frozenset())

        c.__setitem__('boolean', False)
        c._keys.add.assert_called_with('boolean')
        c._values.__setitem__.assert_called_with('boolean', False)

        c.__setitem__('int', int(10))
        c._keys.add.assert_called_with('int')
        c._values.__setitem__.assert_called_with('int', int(10))

        c.__setitem__('float', float(10.1))
        c._keys.add.assert_called_with('float')
        c._values.__setitem__.assert_called_with('float', float(10.1))

        c.__setitem__('complex', complex(10))
        c._keys.add.assert_called_with('complex')
        c._values.__setitem__.assert_called_with('complex', complex(10))

        c.__setitem__('bytes', b'bytes')
        c._keys.add.assert_called_with('bytes')
        c._values.__setitem__.assert_called_with('bytes', b'bytes')

        c.__setitem__('bytearray', bytearray(b'bytearray'))
        c._keys.add.assert_called_with('bytearray')
        c._values.__setitem__.assert_called_with('bytearray',
                                                 bytearray(b'bytearray'))

        c.__setitem__('none', None)
        c._keys.add.assert_called_with('none')
        c._values.__setitem__.assert_called_with('none', None)
Пример #13
0
    def test_keys_returns_valid_list_of_keys(self):
        c = MedleyContainer()
        c._values = {'foo': self.foo, 'bar': self.bar, 'baz': self.baz}

        self.assertEqual(sorted(list(c.keys())), sorted(['foo', 'bar', 'baz']))
        self.assertEqual(sorted(list(c)), sorted(['foo', 'bar', 'baz']))
Пример #14
0
    def test_extend_throws_error_if_service_id_is_not_function_def(self):
        c = MedleyContainer()
        c._values = {'foo': 'bar'}

        with self.assertRaises(Exception):
            c.extend('foo', self.foo)