def test_urn_recursive(self):
        # Setup:
        # ... Create a node object with a parent
        server = Server(utils.MockConnection(None))
        node_obj1 = utils.MockNodeObject(server, None, 'parent_name')
        node_obj1._oid = 123

        node_obj2 = utils.MockNodeObject(server, node_obj1, 'obj_name')
        node_obj2._oid = 456

        # If: I get the URN for the child node object
        urn = node_obj2.urn

        # Then:
        # ... I expect it to be formatted as a URL
        parsed_url = parse.urlparse(urn)

        # ... The netlocation should be equal to the urn base (with added slashes)
        # NOTE: Server URN Base is tested in the server class unit tests
        self.assertEqual(f'//{parsed_url.netloc}/', server.urn_base)

        # ... The path should have multiple folders under it (list comprehension removes empty strings)
        split_path = [x for x in parsed_url.path.split('/') if x]
        self.assertEqual(len(split_path), 2)

        # ... The parent path should be first
        self.assertEqual(split_path[0],
                         f'{node_obj1.__class__.__name__}.{node_obj1.oid}')

        # ... The child path should be second
        self.assertEqual(split_path[1],
                         f'{node_obj2.__class__.__name__}.{node_obj2.oid}')
def _get_mock_node_generator():
    server = Server(utils.MockConnection(None))

    mock_object1 = utils.MockNodeObject(server, None, 'a')
    mock_object1._oid = 123

    mock_object2 = utils.MockNodeObject(server, None, 'b')
    mock_object2._oid = 456

    mock_objects = [mock_object1, mock_object2]
    return mock.MagicMock(return_value=mock_objects), mock_objects
    def test_get_obj_by_urn_recurses(self):
        # Setup: Create a node object with a collection under it
        server = Server(utils.MockConnection(None))
        db_obj = utils.MockNodeObject(server, None, 'db_name')
        sc_obj = utils.MockNodeObject(server, db_obj, 'schema_name')
        db_obj._child_collections = {'Schema': {123: sc_obj}}

        # If: I ask for an object that recurses
        fragment = '/Schema.123/'
        obj = db_obj.get_object_by_urn(fragment)

        # Then: The object I get back should be the same as the one I created
        self.assertIs(obj, sc_obj)
    def test_register_child_collection(self):
        # Setup: Create a node object
        server = Server(utils.MockConnection(None))
        node_obj = utils.MockNodeObject(server, None, 'obj_name')

        # If: I register a child collection
        mock_class1 = mock.MagicMock()
        mock_class1.__name__ = 'mock_class1'
        mock_class1.get_nodes_for_parent = mock.MagicMock()
        collection1 = node_obj._register_child_collection(mock_class1)

        # Then
        # ... The returned collection should be a collection with the given generator
        self.assertIsInstance(collection1, node.NodeCollection)

        # ... The collection should be added to the list of registered collections
        self.assertEqual(len(node_obj._child_collections), 1)

        # If: I add another one
        mock_class2 = mock.MagicMock()
        mock_class2.__name__ = 'mock_class2'
        mock_class2.get_nodes_for_parent = mock.MagicMock()
        collection2 = node_obj._register_child_collection(mock_class2)

        # Then: The collection should be appended to the list of registered collections
        self.assertEqual(len(node_obj._child_collections), 2)
        self.assertTrue(
            mock_class1.__name__ in node_obj._child_collections.keys())
        self.assertTrue(
            mock_class2.__name__ in node_obj._child_collections.keys())
        self.assertIs(node_obj._child_collections[mock_class1.__name__],
                      collection1)
        self.assertIs(node_obj._child_collections[mock_class2.__name__],
                      collection2)
    def test_register_property_collection(self):
        # Setup: Create a node object
        server = Server(utils.MockConnection(None))
        node_obj = utils.MockNodeObject(server, None, 'obj_name')

        # If: I register a property collection
        generator = mock.MagicMock()
        collection1 = node_obj._register_property_collection(generator)

        # Then:
        # ... The returned collection should be a collection with the provided generator
        self.assertIsInstance(collection1, node.NodeLazyPropertyCollection)
        self.assertIs(collection1._generator, generator)

        # ... The collection should be added to the list of registered collections
        self.assertEqual(len(node_obj._property_collections), 2)
        self.assertIn(collection1, node_obj._property_collections)

        # If: I add another one
        collection2 = node_obj._register_property_collection(generator)

        # Then: The collection should be appended to the list of registered collections
        self.assertEqual(len(node_obj._property_collections), 3)
        self.assertIn(collection1, node_obj._property_collections)
        self.assertIn(collection2, node_obj._property_collections)
Exemple #6
0
    def test_from_node_query(self):
        # If: I create a new object from a node row with the expected parent type
        mock_server = Server(utils.MockConnection(None))
        mock_parent = utils.MockNodeObject(
            mock_server, None,
            'parent') if not self.parent_expected_to_be_none else None

        obj = self.class_for_test._from_node_query(mock_server, mock_parent,
                                                   **self.node_query)

        # Then:
        # ... The returned object must be an instance of the class
        NodeObjectTestBase.unittest.assertIsInstance(obj, NodeObject)
        NodeObjectTestBase.unittest.assertIsInstance(obj, self.class_for_test)

        # ... Validate the node object properties
        utils.assert_threeway_equals(mock_server, obj._server, obj.server)
        utils.assert_threeway_equals(self.node_query['oid'], obj._oid, obj.oid)
        utils.assert_threeway_equals(self.node_query['name'], obj._name,
                                     obj.name)

        # ... Validate the basic properties
        for attr, value in self.basic_properties.items():
            NodeObjectTestBase.unittest.assertEqual(getattr(obj, attr), value)

        # ... Validate the collections
        for attr in self.collections:
            NodeObjectTestBase.unittest.assertIsInstance(
                getattr(obj, attr), NodeCollection)

        # ... Call the validation function
        self._custom_validate_from_node(obj, mock_server)
    def test_get_obj_by_urn_invalid_collection(self):
        # Setup: Create a node object (without any collections under it)
        server = Server(utils.MockConnection(None))
        node_obj = utils.MockNodeObject(server, None, 'obj_name')

        with self.assertRaises(ValueError):
            # If: I have a URN fragment that goes into a collection that doesn't exist
            # Then: I should get an exception
            fragment = '/Database.123/'
            node_obj.get_object_by_urn(fragment)
    def test_init(self):
        # If: I create a node object
        server = Server(utils.MockConnection(None))
        parent = utils.MockNodeObject(server, None, 'parent')
        node_obj = utils.MockNodeObject(server, parent, 'abc')

        # Then: The properties should be assigned as defined
        utils.assert_threeway_equals(None, node_obj._oid, node_obj.oid)
        utils.assert_threeway_equals('abc', node_obj._name, node_obj.name)
        utils.assert_threeway_equals(server, node_obj._server, node_obj.server)
        utils.assert_threeway_equals(parent, node_obj._parent, node_obj.parent)

        self.assertDictEqual(node_obj._child_collections, {})
        self.assertEqual(len(node_obj._property_collections), 1)

        self.assertIsInstance(node_obj._full_properties,
                              node.NodeLazyPropertyCollection)
        self.assertEqual(node_obj._full_properties._generator,
                         node_obj._property_generator)
Exemple #9
0
    def _test_scripting_mixins(self):
        # Setup: Create an instance of the object
        mock_server = Server(utils.MockConnection(None))

        mock_grand_parent = utils.MockNodeObject(
            mock_server, None,
            'grandparent') if not self.parent_expected_to_be_none else None
        mock_parent = utils.MockNodeObject(
            mock_server, mock_grand_parent,
            'parent') if not self.parent_expected_to_be_none else None

        name = 'test'
        obj = self.init_lambda(mock_server, mock_parent, name)
        obj._full_properties = self.property_query

        if isinstance(obj, ScriptableCreate):
            # If: I script for create
            script = obj.create_script()

            # Then: The script should successfully return
            utils.assert_is_not_none_or_whitespace(script)

        if isinstance(obj, ScriptableDelete):
            # If: I script for delete
            script = obj.delete_script()

            # Then: The script should successfully return
            utils.assert_is_not_none_or_whitespace(script)

        if isinstance(obj, ScriptableUpdate):
            # If: I script for update
            script = obj.update_script()

            # Then: The script should successfully return
            utils.assert_is_not_none_or_whitespace(script)

        if isinstance(obj, ScriptableSelect):
            # If: I script for select
            script = obj.select_script()

            # Then: The script should successfully return
            utils.assert_is_not_none_or_whitespace(script)
    def test_get_obj_by_urn_base_case(self):
        # Setup: Create a node object
        server = Server(utils.MockConnection(None))
        node_obj = utils.MockNodeObject(server, None, 'obj_name')

        # If: I have a URN fragment that returns the object
        fragment = '/'
        obj = node_obj.get_object_by_urn(fragment)

        # Then: I should get that object back
        self.assertIs(obj, node_obj)
Exemple #11
0
    def test_full_properties(self):
        # Setup:
        # NOTE: We're *not* mocking out the template rendering b/c this will verify that there's a template
        # ... Create a mock query execution that will return the properties
        mock_exec_dict = mock.MagicMock(return_value=([],
                                                      [self.property_query]))

        # ... Create an instance of the class
        mock_server = Server(utils.MockConnection(None))
        mock_server.connection.execute_dict = mock_exec_dict

        mock_grand_grand_parent = utils.MockNodeObject(
            mock_server, None, 'grandgrandparent'
        ) if not self.parent_expected_to_be_none else None
        mock_grand_parent = utils.MockNodeObject(
            mock_server, mock_grand_grand_parent,
            'grandparent') if not self.parent_expected_to_be_none else None
        mock_parent = utils.MockNodeObject(
            mock_server, mock_grand_parent,
            'parent') if not self.parent_expected_to_be_none else None
        name = 'test'
        obj = self.init_lambda(mock_server, mock_parent, name)

        self._full_properties_helper(obj, mock_server)
Exemple #12
0
    def test_init(self):
        # If: I create an instance of the provided class
        mock_server = Server(utils.MockConnection(None))
        mock_parent = utils.MockNodeObject(
            mock_server, None,
            'parent') if not self.parent_expected_to_be_none else None

        name = 'test'
        obj = self.init_lambda(mock_server, mock_parent, name)

        # Then:
        # ... Perform the init validation
        # noinspection PyTypeChecker
        self._init_validation(obj, mock_server, mock_parent, name)

        # ... Call the custom validation function
        self._custom_validate_init(obj, mock_server)
    def test_urn_basecase(self):
        # Setup:
        # ... Create a node object
        server = Server(utils.MockConnection(None))
        node_obj = utils.MockNodeObject(server, None, 'obj_name')
        node_obj._oid = 123

        # If: I get the URN for a node object
        urn = node_obj.urn

        # Then:
        # ... I expect it to be formatted as a URL
        parsed_url = parse.urlparse(urn)

        # ... The netlocation should be equal to the urn base (with added slashes)
        # NOTE: Server URN Base is tested in the server class unit tests
        self.assertEqual(f'//{parsed_url.netloc}/', server.urn_base)

        # ... The path should match Class.OID (with added slashes)
        self.assertEqual(parsed_url.path,
                         f'/{node_obj.__class__.__name__}.{node_obj.oid}/')
    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()