def test_index_match_oid(self):
        # Setup: Create a mock generator and node collection
        generator, mock_objects = _get_mock_node_generator()
        node_collection = node.NodeCollection(generator)

        # If: I get an item that has a matching oid
        output = node_collection[456]

        # Then: The item I have should be the expected item
        self.assertIs(output, mock_objects[1])
    def test_index_bad_type(self):
        # Setup: Create a mock generator and node collection
        generator = mock.MagicMock()
        node_collection = node.NodeCollection(generator)

        # If: I ask for items with an invalid type for the index
        # Then: I should get an exception
        with self.assertRaises(TypeError):
            # noinspection PyTypeChecker
            obj = node_collection[1.2]  # noqa
    def test_index_no_match_name(self):
        # Setup: Create a mock generator and node collection
        generator, mock_objects = _get_mock_node_generator()
        node_collection = node.NodeCollection(generator)

        # If: I get an item that doesn't have a matching name
        # Then:
        # ... I should get an exception
        with self.assertRaises(NameError):
            obj = node_collection['c']  # noqa
    def test_len(self):
        # Setup: Create a mock generator and node collection
        generator, mock_objects = _get_mock_node_generator()
        node_collection = node.NodeCollection(generator)

        # If: I ask for the length of the node collection
        length = len(node_collection)

        # Then: The length should be equal to the length of the objects
        self.assertEqual(length, len(mock_objects))
    def test_iterator(self):
        # Setup: Create a mock generator and node collection
        generator, mock_objects = _get_mock_node_generator()
        node_collection = node.NodeCollection(generator)

        # If: I iterate over the items in the collection
        output = [n for n in node_collection]

        # Then: The list should be equivalent to the list of objects
        self.assertListEqual(output, mock_objects)
    def test_init(self):
        # Setup: Create a mock generator
        generator = mock.MagicMock()

        # If: I initialize a node collection
        node_collection = node.NodeCollection(generator)

        # Then:
        # ... The internal properties should be set properly
        self.assertIs(node_collection._generator, generator)
        self.assertIsNone(node_collection._items_impl)
    def test_items_loaded(self):
        # Setup: Create a mock generator and node collection, mock that it's loaded
        generator = mock.MagicMock()
        node_collection = node.NodeCollection(generator)
        node_collection._items_impl = {}

        # If: I access the items property
        output = node_collection._items

        # Then: The generator should not have been called
        generator.assert_not_called()
        self.assertIs(output, node_collection._items_impl)
    def test_reset(self):
        # Setup: Create a mock generator and node collection that has been loaded
        generator, mock_objects = _get_mock_node_generator()
        node_collection = node.NodeCollection(generator)
        obj = node_collection[123]  # noqa

        # If: I reset the collection
        node_collection.reset()

        # Then:
        # ... The item collection should be none
        self.assertIsNone(node_collection._items_impl)
    def test_items_not_loaded(self):
        # Setup: Create a mock generator and node collection
        generator = mock.MagicMock(return_value={})
        node_collection = node.NodeCollection(generator)

        # If: I access the items property
        output = node_collection._items

        # Then: The generator should have been called
        generator.assert_called_once()
        self.assertIs(output, node_collection._items_impl)

        # ... Make sure the generator has not been called
        generator.assert_called_once()
示例#10
0
    def test_refresh(self):
        # Setup:
        # ... Create a node object
        server = Server(utils.MockConnection(None))
        node_obj = utils.MockNodeObject(server, None, 'obj_name')

        # ... Add a couple child collections
        node_generator = mock.MagicMock()
        collection1 = node.NodeCollection(node_generator)
        collection1.reset = mock.MagicMock()
        collection2 = node.NodeCollection(node_generator)
        collection2.reset = mock.MagicMock()
        node_obj._child_collections = {
            'collection1': collection1,
            'collection2': collection2
        }

        # ... Add a couple property collections
        prop_generator = mock.MagicMock()
        props1 = node.NodeLazyPropertyCollection(prop_generator)
        props1.reset = mock.MagicMock()
        props2 = node.NodeLazyPropertyCollection(prop_generator)
        props2.reset = mock.MagicMock()
        node_obj._property_collections = [props1, props2]

        # If: I refresh the object
        node_obj.refresh()

        # Then: The child collections should have been reset
        # noinspection PyUnresolvedReferences
        collection1.reset.assert_called_once()
        # noinspection PyUnresolvedReferences
        collection2.reset.assert_called_once()
        # noinspection PyUnresolvedReferences
        props1.reset.assert_called_once()
        # noinspection PyUnresolvedReferences
        props2.reset.assert_called_once()