def test_len(self):
        # Setup: Create a mock generator and property collection
        generator, mock_results = _get_mock_property_generator()
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # If: I get the length of the collection
        output = len(prop_collection)

        # Then: The length should be the same as the length of the objects returned
        self.assertEqual(output, len(mock_results))
    def test_keys(self):
        # Setup: Create a mock generator and property collection
        generator, mock_results = _get_mock_property_generator()
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # If: I get the keys of the collection
        keys = prop_collection.keys()

        # Then: They should be the same as the keys in the results
        self.assertListEqual(list(keys), list(mock_results.keys()))
    def test_get_has_match(self):
        # Setup: Create a mock generator and property collection
        generator, mock_objects = _get_mock_property_generator()
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # If: I get an item that does exist
        output = prop_collection.get('prop1')

        # Then: dict.get default should be returned
        self.assertEqual(output, prop_collection._items.get('prop1'))
    def test_index_match(self):
        # Setup: Create a mock generator and node collection
        generator, mock_objects = _get_mock_property_generator()
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # If: I get an item that exists
        output = prop_collection['prop1']

        # Then: The item should be the expected item
        self.assertEqual(output, mock_objects['prop1'])
    def test_get_no_match_no_default(self):
        # Setup: Create a mock generator and property collection
        generator, mock_objects = _get_mock_property_generator()
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # If: I get an item that doesn't exist and do not provide a default
        output = prop_collection.get('does_not_exist')

        # Then: dict.get default should be returned
        self.assertEqual(output, prop_collection._items.get('does_not_exist'))
    def test_iterator(self):
        # Setup: Create a mock generator and property collection
        generator, mock_results = _get_mock_property_generator()
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # If: I iterate over the items in the collection
        output = [item for item in prop_collection]
        expected_output = [item for item in mock_results]

        # Then: The dictionary I rebuilt from the iteration should match the original dictionary
        self.assertListEqual(output, expected_output)
    def test_items_not_loaded(self):
        # Setup: Create a mock generator, property collection
        generator = mock.MagicMock(return_value={})
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # If: I look at the list of items after they
        output = prop_collection._items

        # Then: The generator should have been called
        generator.assert_called_once()
        self.assertIs(output, prop_collection._items_impl)
    def test_items_loaded(self):
        # Setup: Create a mock generator, property collection and mock that it's loaded
        generator = mock.MagicMock()
        prop_collection = node.NodeLazyPropertyCollection(generator)
        prop_collection._items_impl = {}

        # If: I look at the list of items after they
        output = prop_collection._items

        # Then: The generator should not have been called
        generator.assert_not_called()
        self.assertIs(output, prop_collection._items_impl)
    def test_reset(self):
        # Setup: Create a mock generator and property collection and force it to load
        generator, mock_results = _get_mock_property_generator()
        prop_collection = node.NodeLazyPropertyCollection(generator)
        obj = prop_collection[
            'prop1']  # Force the collection to load    # noqa

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

        # Then:
        # ... The item collection should be none
        self.assertIsNone(prop_collection._items_impl)
    def test_index_no_match_oid(self):
        # Setup: Create a mock generator and property collection
        generator, mock_objects = _get_mock_property_generator()
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # If: I get an item that doesn't have a matching oid
        # Then:
        # ... I should get an exception
        with self.assertRaises(KeyError):
            obj = prop_collection['does_not_exist']  # noqa

        # ... The generator should have been called, tho
        generator.assert_called_once()
        self.assertIs(prop_collection._items, mock_objects)
    def test_index_bad_type(self):
        # Setup: Create a mock generator and property collection
        generator = mock.MagicMock()
        prop_collection = node.NodeLazyPropertyCollection(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 = prop_collection[1.2]  # noqa

        # ... The generator should not have been called
        generator.assert_not_called()
    def test_init(self):
        # Setup: Create a mock generator
        generator = mock.MagicMock()

        # If: I initialize a node property collection
        prop_collection = node.NodeLazyPropertyCollection(generator)

        # Then:
        # ... Internal state should be appropriately setup
        self.assertIs(prop_collection._generator, generator)
        self.assertIsNone(prop_collection._items_impl)

        # ... The generator should not have been called
        generator.assert_not_called()
    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()