Exemplo n.º 1
0
def mock_template(client):
    link_attr_1 = client.testutils.create_attribute("link_attr_1",
                                                    dimension='Pressure')
    link_attr_2 = client.testutils.create_attribute("link_attr_2",
                                                    dimension='Speed')
    node_attr_1 = client.testutils.create_attribute("node_attr_1",
                                                    dimension='Volume')
    node_attr_2 = client.testutils.create_attribute("node_attr_2",
                                                    dimension='Speed')
    net_attr_1 = client.testutils.create_attribute("net_attr_2",
                                                   dimension='Speed')

    template = JSONObject()
    template.name = 'Test template @ %s' % datetime.datetime.now()

    layout = {}
    layout['groups'] = '<groups>...</groups>'

    template.layout = layout

    template.templatetypes = []
    # **********************
    # type 1           #
    # **********************
    type1 = JSONObject()
    type1.name = "Node type"
    type1.alias = "Node type alias"
    type1.resource_type = 'NODE'

    type1.typeattrs = []

    tattr_1 = JSONObject()
    tattr_1.attr_id = node_attr_1.id
    tattr_1.description = "Type attribute 1 description"
    tattr_1.properties = {'test_property': "test property add type"}
    tattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1}
    type1.typeattrs.append(tattr_1)

    tattr_2 = JSONObject()
    tattr_2.attr_id = node_attr_2.id
    tattr_2.description = "Type attribute 2 description"
    tattr_2.data_restriction = {'INCREASING': None}
    type1.typeattrs.append(tattr_2)

    template.templatetypes.append(type1)
    # **********************
    # type 2           #
    # **********************
    type2 = JSONObject()
    type2.name = "Link type"
    type2.alias = "Link type alias"
    type2.resource_type = 'LINK'

    type2.typeattrs = []

    tattr_1 = JSONObject()
    tattr_1.attr_id = link_attr_1.id
    type2.typeattrs.append(tattr_1)

    tattr_2 = JSONObject()
    tattr_2.attr_id = link_attr_2.id
    type2.typeattrs.append(tattr_2)

    template.templatetypes.append(type2)

    # **********************
    # type 3           #
    # **********************
    type3 = JSONObject()
    type3.name = "Network Type"
    type3.alias = "Network Type alias"
    type3.resource_type = 'NETWORK'

    type3.typeattrs = []

    tattr_3 = JSONObject()
    tattr_3.attr_id = net_attr_1.id
    tattr_3.data_restriction = {}

    type3.typeattrs.append(tattr_3)

    template.templatetypes.append(type3)

    new_template_i = client.add_template(template)
    # TODO: HACK to load the attr
    for tt in new_template_i.templatetypes:
        for ta in tt.typeattrs:
            ta.attr

    new_template_j = JSONObject(new_template_i)
    return new_template_j
Exemplo n.º 2
0
    def test_add_template(self):

        link_attr_1 = self.create_attr("link_attr_1", dimension='Pressure')
        link_attr_2 = self.create_attr("link_attr_2", dimension='Speed')
        node_attr_1 = self.create_attr("node_attr_1", dimension='Volume')
        node_attr_2 = self.create_attr("node_attr_2", dimension='Speed')
        net_attr_1 = self.create_attr("net_attr_2", dimension='Speed')

        template = JSONObject()
        template.name = 'Test template @ %s' % datetime.datetime.now()

        layout = {}
        layout['groups'] = '<groups>...</groups>'

        template.layout = layout

        template.templatetypes = []
        #**********************
        #type 1           #
        #**********************
        type1 = JSONObject()
        type1.name = "Node type"
        type1.alias = "Node type alias"
        type1.resource_type = 'NODE'

        type1.typeattrs = []

        tattr_1 = JSONObject()
        tattr_1.attr_id = node_attr_1.id
        tattr_1.description = "Type attribute 1 description"
        tattr_1.properties = {'test_property': "test property add type"}
        tattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1}
        type1.typeattrs.append(tattr_1)

        tattr_2 = JSONObject()
        tattr_2.attr_id = node_attr_2.id
        tattr_2.description = "Type attribute 2 description"
        tattr_2.data_restriction = {'INCREASING': None}
        type1.typeattrs.append(tattr_2)

        template.templatetypes.append(type1)
        #**********************
        #type 2           #
        #**********************
        type2 = JSONObject()
        type2.name = "Link type"
        type2.alias = "Link type alias"
        type2.resource_type = 'LINK'

        type2.typeattrs = []

        tattr_1 = JSONObject()
        tattr_1.attr_id = link_attr_1.id
        type2.typeattrs.append(tattr_1)

        tattr_2 = JSONObject()
        tattr_2.attr_id = link_attr_2.id
        type2.typeattrs.append(tattr_2)

        template.templatetypes.append(type2)

        #**********************
        #type 3           #
        #**********************
        type3 = JSONObject()
        type3.name = "Network Type"
        type3.alias = "Network Type alias"
        type3.resource_type = 'NETWORK'

        type3.typeattrs = []

        tattr_3 = JSONObject()
        tattr_3.attr_id = net_attr_1.id
        tattr_3.data_restriction = {}

        type3.typeattrs.append(tattr_3)

        template.templatetypes.append(type3)

        new_template_i = add_template(template)
        #TODO: HACK to load the attr
        for tt in new_template_i.templatetypes:
            for ta in tt.typeattrs:
                ta.attr

        new_template_j = JSONObject(new_template_i)

        assert new_template_j.name == template.name, "Names are not the same!"
        assert str(new_template_j.layout) == str(
            template.layout), "Layouts are not the same!"
        assert new_template_j.id is not None, "New Template has no ID!"
        assert new_template_j.id > 0, "New Template has incorrect ID!"

        assert len(new_template_j.templatetypes
                   ) == 3, "Resource types did not add correctly"
        for t in new_template_j.templatetypes[0].typeattrs:
            assert t.attr_id in (node_attr_1.id, node_attr_2.id)
            "Node types were not added correctly!"

        for t in new_template_j.templatetypes[1].typeattrs:
            assert t.attr_id in (link_attr_1.id, link_attr_2.id)
            "Node types were not added correctly!"

        return new_template_j
Exemplo n.º 3
0
    def test_update_template(self):

        attr_1 = self.create_attr("link_attr_1", dimension='Pressure')
        attr_2 = self.create_attr("link_attr_2", dimension='Speed')
        attr_3 = self.create_attr("node_attr_1", dimension='Volume')

        template = JSONObject()

        template.name = 'Test Template @ %s' % datetime.datetime.now()

        template.templatetypes = []

        type_1 = JSONObject()
        type_1.name = "Node type 2"
        type_1.alias = "Node type 2 alias"
        type_1.resource_type = 'NODE'
        type_1.typeattrs = []

        type_2 = JSONObject()
        type_2.name = "Link type 2"
        type_2.alias = "Link type 2 alias"
        type_2.resource_type = 'LINK'
        type_2.typeattrs = []

        tattr_1 = JSONObject()
        tattr_1.attr_id = attr_1.id
        tattr_1.unit = 'bar'
        tattr_1.description = "typeattr description 1"
        tattr_1.properties = {"test_property": "property value"}
        type_1.typeattrs.append(tattr_1)

        tattr_2 = JSONObject()
        tattr_2.attr_id = attr_2.id
        tattr_2.unit = 'mph'
        tattr_2.description = "typeattr description 2"
        type_2.typeattrs.append(tattr_2)

        template.templatetypes.append(type_1)
        template.templatetypes.append(type_2)

        new_template_i = add_template(template)
        new_template_j = JSONObject(new_template_i)

        assert new_template_j.name == template.name, "Names are not the same!"
        assert new_template_j.id is not None, "New Template has no ID!"
        assert new_template_j.id > 0, "New Template has incorrect ID!"

        assert len(new_template_j.templatetypes
                   ) == 2, "Resource types did not add correctly"
        assert len(new_template_j.templatetypes[0].typeattrs
                   ) == 1, "Resource type attrs did not add correctly"
        assert new_template_j.templatetypes[0].typeattrs[0].unit == 'bar'

        #update the name of one of the types
        new_template_j.templatetypes[0].name = "Test type 3"
        updated_type_id = new_template_j.templatetypes[0].id

        #add an template attr to one of the types
        tattr_3 = JSONObject()
        tattr_3.attr_id = attr_3.id
        tattr_3.description = "updated typeattr description 1"
        tattr_3.properties = {"test_property_of_added_type": "property value"}
        new_template_j.templatetypes[0].typeattrs.append(tattr_3)

        updated_template_i = update_template(new_template_j)
        updated_template_j = JSONObject(updated_template_i)

        assert updated_template_j.name == template.name, "Names are not the same!"

        updated_type = None
        for tmpltype in new_template_j.templatetypes:
            if tmpltype.id == updated_type_id:
                updated_type = tmpltype
                break

        assert updated_type.name == "Test type 3", "Resource types did not update correctly"

        assert len(
            updated_type.typeattrs
        ) == 2, "Resource type template attr did not update correctly"

        #Test that when setting a unit on a type attr, it matches the dimension of its attr
        #In this case, setting m^3(Volume) fails as the attr has a dimension of 'Pressure'
        updated_template_j.templatetypes[0].typeattrs[0].unit = 'm^3'
        self.assertRaises(HydraError, update_template, updated_template_j)
Exemplo n.º 4
0
    def create_template(self):

        net_attr1 = self.create_attribute("net_attr_a", dimension='Volume')
        net_attr2 = self.create_attribute("net_attr_c", dimension=None)
        link_attr_1 = self.create_attribute("link_attr_a",
                                            dimension='Pressure')
        link_attr_2 = self.create_attribute("link_attr_b", dimension='Speed')
        link_attr_3 = self.create_attribute("link_attr_c", dimension='Length')
        node_attr_1 = self.create_attribute("node_attr_a", dimension='Volume')
        node_attr_2 = self.create_attribute("node_attr_b", dimension='Speed')
        node_attr_3 = self.create_attribute("node_attr_c",
                                            dimension='Monetary value')
        node_attr_4 = self.create_attribute("node_attr_d",
                                            dimension='Volumetric flow rate')
        group_attr_1 = self.create_attribute("grp_attr_1",
                                             dimension='Monetary value')
        group_attr_2 = self.create_attribute("grp_attr_2", dimension=None)

        template = JSONObject()
        template['name'] = 'Default Template ' + str(datetime.datetime.now())

        types = []
        #**********************
        #network type         #
        #**********************
        net_type = JSONObject()
        net_type.name = "Default Network"
        net_type.alias = "Test type alias"
        net_type.resource_type = 'NETWORK'

        typeattrs = []

        typeattr_1 = JSONObject()
        typeattr_1.attr_id = net_attr1.id
        typeattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1}
        typeattr_1.unit_id = self.get_unit('m^3').id
        typeattrs.append(typeattr_1)

        typeattr_2 = JSONObject()
        typeattr_2.attr_id = net_attr2.id
        typeattrs.append(typeattr_2)

        net_type.typeattrs = typeattrs

        types.append(net_type)
        #**********************
        # node type           #
        #**********************
        node_type = JSONObject()
        node_type.name = "Default Node"
        node_type.alias = "Test type alias"
        node_type.resource_type = 'NODE'

        typeattrs = []

        typeattr_1 = JSONObject()
        typeattr_1.attr_id = node_attr_1.id
        typeattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1}
        typeattr_1.unit_id = self.get_unit('m^3').id
        typeattrs.append(typeattr_1)

        typeattr_2 = JSONObject()
        typeattr_2.attr_id = node_attr_2.id
        typeattr_2.data_restriction = {'INCREASING': None}
        typeattrs.append(typeattr_2)

        typeattr_3 = JSONObject()
        typeattr_3.attr_id = node_attr_3.id
        typeattrs.append(typeattr_3)

        typeattr_4 = JSONObject()
        typeattr_4.attr_id = node_attr_4.id
        typeattr_4.unit_id = self.get_unit("m^3 s^-1").id
        typeattrs.append(typeattr_4)

        node_type.typeattrs = typeattrs

        types.append(node_type)
        #**********************
        #link type            #
        #**********************
        link_type = JSONObject()
        link_type.name = "Default Link"
        link_type.resource_type = 'LINK'

        typeattrs = []

        typeattr_1 = JSONObject()
        typeattr_1.attr_id = link_attr_1.id
        typeattrs.append(typeattr_1)

        typeattr_2 = JSONObject()
        typeattr_2.attr_id = link_attr_2.id
        typeattrs.append(typeattr_2)

        typeattr_3 = JSONObject()
        typeattr_3.attr_id = link_attr_3.id
        typeattrs.append(typeattr_3)

        link_type.typeattrs = typeattrs

        types.append(link_type)

        #**********************
        #group type           #
        #**********************
        group_type = JSONObject()
        group_type.name = "Default Group"
        group_type.resource_type = 'GROUP'

        typeattrs = []

        typeattr_1 = JSONObject()
        typeattr_1.attr_id = group_attr_1.id
        typeattrs.append(typeattr_1)

        typeattr_2 = JSONObject()
        typeattr_2.attr_id = group_attr_2.id
        typeattrs.append(typeattr_2)

        group_type.typeattrs = typeattrs

        types.append(group_type)

        template.templatetypes = types

        new_template_i = self.client.add_template(template)
        new_template = JSONObject(new_template_i)

        assert new_template.name == template.name, "Names are not the same!"
        assert new_template.id is not None, "New Template has no ID!"
        assert new_template.id > 0, "New Template has incorrect ID!"

        assert len(new_template.templatetypes) == len(
            types), "Resource types did not add correctly"
        for t in new_template.templatetypes[1].typeattrs:
            assert t.attr_id in (node_attr_1.id, node_attr_2.id, node_attr_3.id, node_attr_4.id),\
                "Node types were not added correctly!"

        for t in new_template.templatetypes[2].typeattrs:
            assert t.attr_id in (link_attr_1.id, link_attr_2.id, link_attr_3.id),\
                "Link types were not added correctly!"

        return new_template