Ejemplo n.º 1
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)
Ejemplo n.º 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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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')
Ejemplo n.º 5
0
    def test_getitem_throws_error_if_id_does_not_exist(self):
        c = MedleyContainer()
        c._keys = Mock(__contains__=Mock(return_value=False))

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