Esempio n. 1
0
    def _save_af_to_db(self, nodes, root_path):
        if nodes is None:
            return None
        else:
            for node in nodes:
                asset = {
                    'id': node['path'],
                    'name': node['name'],
                    'description': node.get('description'),
                    'template': node.get('template'),
                    'parent_id': root_path,
                    'is_leaf': len(node.get('assets', [])) == 0,
                    'categories': node.get('categories')
                }
                self.session.add(Asset(**asset))
                for attribute in node['attributes'] or []:
                    pi_point = attribute['point']['name'] if attribute['point'] is not None else None
                    attribute = {
                        'id': "{}|{}".format(node['path'], attribute.get('name')),
                        'asset_id': node['path'],
                        'name': attribute.get('name'),
                        'type': attribute.get('type'),
                        'pi_point': pi_point,
                        'description': attribute.get('description'),
                        'categories': None if attribute.get('categories') is None
                        else [category['name'] for category in attribute['categories']]
                    }
                    self.session.add(Attribute(**attribute))

                self._save_af_to_db(
                    node.get('assets'), node['path']
                )
        return None
def test_get_asset_children(managed_feeds_postgres_dao, postgres_session):

    postgres_session.add(
        Asset(id="\\\\EC2AMAZ-0EE3VGR\\NuGreen",
              parent_id=None,
              name="NuGreen",
              is_leaf=False))
    postgres_session.add(
        Asset(id="\\\\EC2AMAZ-0EE3VGR\\NuGreen\\Houston",
              parent_id="\\\\EC2AMAZ-0EE3VGR\\NuGreen",
              name="Houston",
              is_leaf=True))
    children = managed_feeds_postgres_dao.get_asset_children(
        r'\\EC2AMAZ-0EE3VGR\NuGreen')

    assert children == [{
        "id": "\\\\EC2AMAZ-0EE3VGR\\NuGreen\\Houston",
        "name": "Houston",
        "isLeaf": True
    }]
def test_search_asset_attributes_by_attribute_name(managed_feeds_postgres_dao,
                                                   postgres_session):
    postgres_session.add_all([
        Asset(name="Root",
              id=r"\\Root",
              description=None,
              template=None,
              categories=["cat1"],
              parent_id=None,
              is_leaf=False),
        Asset(name="Child 1",
              id=r"\\Root\Child 1",
              description=None,
              template=None,
              categories=["cat1", "cat2"],
              parent_id=r"\\Root",
              is_leaf=False),
        Asset(name="Subchild 1",
              id=r"\\Root\Child 1\Subchild 1",
              description=None,
              template=None,
              categories=["cat2", "cat3"],
              parent_id=r"\\Root\Child 1",
              is_leaf=True),
        Asset(name="Subchild 2",
              id=r"\\Root\Child 1\Subchild 2",
              description=None,
              template=None,
              categories=["cat3"],
              parent_id=r"\\Root\Child 1",
              is_leaf=True),
        Attribute(name="test attr",
                  id=r"\\Root\Child 1|test attr",
                  asset_id=r"\\Root\Child 1",
                  type=None,
                  pi_point=None,
                  categories=["attr cat1", "attr cat2"]),
        Attribute(name="test attr2",
                  id=r"\\Root\Child 1|test attr2",
                  asset_id=r"\\Root\Child 1",
                  type=None,
                  pi_point=None,
                  categories=["attr cat1", "attr cat2"]),
        Attribute(name="subchild 1 attr",
                  id=r"\\Root\Child 1\Subchild 1|subchild 1 attr",
                  asset_id=r"\\Root\Child 1\Subchild 1",
                  type=None,
                  pi_point="PI",
                  categories=["attr cat2", "attr cat3"]),
        Attribute(name="subchild 1 attr2",
                  id=r"\\Root\Child 1\Subchild 1|subchild 1 attr2",
                  asset_id=r"\\Root\Child 1\Subchild 1",
                  type=None,
                  pi_point="PI",
                  categories=None),
        Attribute(name="subchild 2 attr",
                  id=r"\\Root\Child 1\Subchild 2|subchild 2 attr",
                  asset_id=r"\\Root\Child 1\Subchild 2",
                  type=None,
                  pi_point="PI",
                  categories=["attr cat3"]),
        Attribute(name="subchild 2 attr2",
                  id=r"\\Root\Child 1\Subchild 2|subchild 2 attr2",
                  asset_id=r"\\Root\Child 1\Subchild 2",
                  type=None,
                  pi_point="PI2",
                  categories=["attr cat2", "attr cat3"]),
        Attribute(name="subchild 2 attr3",
                  id=r"\\Root\Child 1\Subchild 2|subchild 2 attr3",
                  asset_id=r"\\Root\Child 1\Subchild 2",
                  type=None,
                  pi_point="PI2",
                  categories=["attr cat2", "attr cat3"])
    ])

    search_results = managed_feeds_postgres_dao.search_asset_attributes(
        asset_id=r'\\Root\Child 1',
        filters=[{
            'type': 'attribute',
            'parameter': 'name',
            'value': 'test attr'
        }])

    expected = {
        'attributes': [{
            'asset_id': '\\\\Root\\Child 1',
            'categories': ['attr cat1', 'attr cat2'],
            'description': None,
            'id': '\\\\Root\\Child 1|test attr',
            'name': 'test attr',
            'pi_point': None,
            'type': None,
            'subscription_status': None
        }],
        'pi_points_total_count':
        0,
        'total_count':
        1
    }

    assert search_results == expected
def test_search_assets_without_filters(managed_feeds_postgres_dao,
                                       postgres_session):
    postgres_session.add_all([
        Asset(name="Root",
              id=r"\\Root",
              description=None,
              template=None,
              categories=["cat1"],
              parent_id=None,
              is_leaf=False),
        Asset(name="Child 1",
              id=r"\\Root\Child 1",
              description=None,
              template=None,
              categories=["cat1", "cat2"],
              parent_id=r"\\Root",
              is_leaf=False),
        Asset(name="Subchild 1",
              id=r"\\Root\Child 1\Subchild 1",
              description=None,
              template=None,
              categories=["cat2", "cat3"],
              parent_id=r"\\Root\Child 1",
              is_leaf=True),
        Asset(name="Subchild 2",
              id=r"\\Root\Child 1\Subchild 2",
              description=None,
              template=None,
              categories=["cat3"],
              parent_id=r"\\Root\Child 1",
              is_leaf=True),
        Attribute(name="test attr",
                  id=r"\\Root\Child 1|test attr",
                  asset_id=r"\\Root\Child 1",
                  type=None,
                  pi_point=None,
                  categories=["attr cat1", "attr cat2"]),
        Attribute(name="test attr2",
                  id=r"\\Root\Child 1|test attr2",
                  asset_id=r"\\Root\Child 1",
                  type=None,
                  pi_point=None,
                  categories=["attr cat1", "attr cat2"]),
        Attribute(name="subchild 1 attr",
                  id=r"\\Root\Child 1\Subchild 1|subchild 1 attr",
                  asset_id=r"\\Root\Child 1\Subchild 1",
                  type=None,
                  pi_point="PI",
                  categories=["attr cat2", "attr cat3"]),
        Attribute(name="subchild 1 attr2",
                  id=r"\\Root\Child 1\Subchild 1|subchild 1 attr2",
                  asset_id=r"\\Root\Child 1\Subchild 1",
                  type=None,
                  pi_point="PI",
                  categories=None),
        Attribute(name="subchild 2 attr",
                  id=r"\\Root\Child 1\Subchild 2|subchild 2 attr",
                  asset_id=r"\\Root\Child 1\Subchild 2",
                  type=None,
                  pi_point="PI",
                  categories=["attr cat3"]),
        Attribute(name="subchild 2 attr2",
                  id=r"\\Root\Child 1\Subchild 2|subchild 2 attr2",
                  asset_id=r"\\Root\Child 1\Subchild 2",
                  type=None,
                  pi_point="PI2",
                  categories=["attr cat2", "attr cat3"]),
        Attribute(name="subchild 2 attr3",
                  id=r"\\Root\Child 1\Subchild 2|subchild 2 attr3",
                  asset_id=r"\\Root\Child 1\Subchild 2",
                  type=None,
                  pi_point="PI2",
                  categories=["attr cat2", "attr cat3"])
    ])

    search_results = managed_feeds_postgres_dao.search_assets(filters=[])

    assert search_results['pi_points_total_count'] == 2
    assert search_results['total_count'] == 4