Exemple #1
0
    def test_register_property_collection(self):
        # Setup: Create a node object
        server = Server(utils.MockPGServerConnection())
        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 #2
0
    def test_urn_recursive(self):
        # Setup:
        # ... Create a node object with a parent
        server = Server(utils.MockPGServerConnection())
        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}')
Exemple #3
0
    def test_register_child_collection(self):
        # Setup: Create a node object
        server = Server(utils.MockPGServerConnection())
        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)
Exemple #4
0
def _get_node_for_parents_mock_connection():
    # ... Create a mockup of a server connection with a mock executor
    mock_action = mock.Mock()
    mock_objs = [{'name': 'abc', 'oid': 123}, {'name': 'def', 'oid': 456}]
    mock_executor = mock.MagicMock(return_value=([{}, {}], mock_objs))
    mock_server = Server(utils.MockPGServerConnection(None, version="10101"),
                         mock_action)
    mock_server.connection.execute_dict = mock_executor

    return mock_server, mock_executor, mock_objs
Exemple #5
0
    def test_get_obj_by_urn_invalid_collection(self):
        # Setup: Create a node object (without any collections under it)
        server = Server(utils.MockPGServerConnection())
        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)
Exemple #6
0
    def test_get_obj_by_urn_base_case(self):
        # Setup: Create a node object
        server = Server(utils.MockPGServerConnection())
        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 #7
0
    def test_role_get_database_node(self):
        # If: I create a DB that is connected
        name = 'dbname'
        mock_server = Server(
            utils.MockPGServerConnection(None, name='not_connected'))
        db = Role(mock_server, name)

        # Then:
        node = db.get_database_node()

        # assert:
        self.assertIsNone(node)
Exemple #8
0
    def test_get_nodes_for_parent_with_parent(self):
        # Setup:
        # ... Create a server connection that will return some mock node rows
        mock_server, mock_executor, mock_objs = _get_node_for_parents_mock_connection(
        )

        # ... Create a mock _from_node generator (so we can validate calls)
        mock_obj = {}
        mock_from_node = mock.MagicMock(return_value=mock_obj)

        # ... Create a mock template rendered
        mock_render = mock.MagicMock(return_value="SQL")
        mock_template_path = mock.MagicMock(return_value="path")

        # ... Create an object that will be the parent of these nodes
        name = 'postgres'
        parent = Database(mock_server, name)
        parent._oid = 123
        parent._connection = utils.MockPGServerConnection(None,
                                                          version="10101")
        parent._connection.execute_dict = mock_executor

        # ... Patch the template rendering, and the _from_node_query
        patch_render_template = 'smo.utils.templating.render_template'
        patch_template_path = 'smo.utils.templating.get_template_path'
        patch_from_node_query = 'tests.pgsmo_tests.utils.MockNodeObject._from_node_query'

        with mock.patch(patch_render_template, mock_render, create=True), \
                mock.patch(patch_template_path, mock_template_path, create=True), \
                mock.patch(patch_from_node_query, mock_from_node, create=True):
            # If: I ask for a collection of nodes *with a parent object*
            nodes = utils.MockNodeObject.get_nodes_for_parent(
                mock_server, parent)

        # Then:
        # ... The template path and template renderer should have been called once
        mock_template_path.assert_called_once_with('template_root',
                                                   'nodes.sql',
                                                   mock_server.version)
        mock_render.assert_called_once_with('path',
                                            macro_roots=None,
                                            **{'parent_id': 123})

        # ... A query should have been executed
        mock_executor.assert_called_once_with('SQL')

        # ... The _from_node should have been called twice with the results of the query
        mock_from_node.assert_any_call(mock_server, parent, **mock_objs[0])
        mock_from_node.assert_any_call(mock_server, parent, **mock_objs[1])

        # ... The output should be a list of objects the _from_node returned
        self.assertIsInstance(nodes, list)
        self.assertListEqual(nodes, [mock_obj, mock_obj])
Exemple #9
0
    def test_database_get_database_node(self):
        # If: I create a DB that is connected
        name = 'dbname'
        mock_server = Server(
            utils.MockPGServerConnection(None, name='not_connected'))
        db = Database(mock_server, name)

        # Then:
        node = db.get_database_node()

        # assert:
        self.assertIsNotNone(node)
        self.assertEqual(node.__class__.__name__, 'Database')
Exemple #10
0
    def test_get_obj_by_urn_recurses(self):
        # Setup: Create a node object with a collection under it
        server = Server(utils.MockPGServerConnection())
        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)
Exemple #11
0
    def test_init(self):
        # If: I create a node object
        server = Server(utils.MockPGServerConnection())
        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 #12
0
    def test_table_get_database_node(self):
        # If: I create a DB that is connected
        mock_server = Server(
            utils.MockPGServerConnection(None, name='not_connected'))
        db = Database(mock_server, 'dbname')
        schema = Schema(mock_server, db, 'schema')
        table = Table(mock_server, schema, 'table')

        # check for schema:
        node = schema.get_database_node()

        # assert:
        self.assertIsNotNone(node)
        self.assertEqual(node.__class__.__name__, 'Database')

        # check for table:
        node = table.get_database_node()

        # assert:
        self.assertIsNotNone(node)
        self.assertEqual(node.__class__.__name__, 'Database')
Exemple #13
0
    def test_urn_basecase(self):
        # Setup:
        # ... Create a node object
        server = Server(utils.MockPGServerConnection())
        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}/')
Exemple #14
0
    def test_refresh(self):
        # Setup:
        # ... Create a node object
        server = Server(utils.MockPGServerConnection())
        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()