Пример #1
0
    def test_delete_typeattr(self):

        template = self.test_add_template()

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

        templatetype = JSONObject()
        templatetype.name = "Test type name @ %s" % (datetime.datetime.now())
        templatetype.alias = templatetype.name + " alias"
        templatetype.resource_type = 'NODE'
        templatetype.template_id = template.id

        tattr_1 = JSONObject()
        tattr_1.attr_id = attr_1.id

        tattr_2 = JSONObject()
        tattr_2.attr_id = attr_2.id

        templatetype.typeattrs = [tattr_1, tattr_2]

        new_type = JSONObject(add_templatetype(templatetype))

        tattr_2.type_id = new_type.id

        delete_typeattr(tattr_2)

        updated_type = JSONObject(get_templatetype(new_type.id))

        log.info(len(updated_type.typeattrs))

        assert len(updated_type.typeattrs
                   ) == 1, "Resource type attr did not add correctly"
Пример #2
0
    def test_delete_typeattr(self, client, mock_template):

        template = mock_template

        attr_1 = client.testutils.create_attribute("link_attr_1", dimension='Pressure')
        attr_2 = client.testutils.create_attribute("link_attr_2", dimension='Speed')

        templatetype = JSONObject()
        templatetype.name = "Test type name @ %s"%(datetime.datetime.now())
        templatetype.alias = templatetype.name + " alias"
        templatetype.resource_type = 'NODE'
        templatetype.template_id = template.id

        tattr_1 = JSONObject()
        tattr_1.attr_id = attr_1.id

        tattr_2 = JSONObject()
        tattr_2.attr_id = attr_2.id

        templatetype.typeattrs = [tattr_1, tattr_2]

        new_type = JSONObject(client.add_templatetype(templatetype))

        assert len(new_type.typeattrs) == 2

        typeattr_to_delete = new_type.typeattrs[0]

        client.delete_typeattr(typeattr_to_delete.id)

        updated_type = JSONObject(client.get_templatetype(new_type.id))


        assert len(updated_type.typeattrs) == 1, "Resource type attr did not add correctly"
Пример #3
0
    def add_attributes(self, client, proj):
        #Create some attributes, which we can then use to put data on our nodes
        attr1 = client.testutils.create_attribute("proj_attr_1")
        attr2 = client.testutils.create_attribute("proj_attr_2")
        attr3 = client.testutils.create_attribute("proj_attr_3")

        proj_attr_1 = JSONObject({})
        proj_attr_1.id = -1
        proj_attr_1.attr_id = attr1.id
        proj_attr_2 = JSONObject({})
        proj_attr_2.attr_id = attr2.id
        proj_attr_2.id = -2
        proj_attr_3 = JSONObject({})
        proj_attr_3.attr_id = attr3.id
        proj_attr_3.id = -3

        attributes = []

        attributes.append(proj_attr_1)
        attributes.append(proj_attr_2)
        attributes.append(proj_attr_3)

        proj.attributes = attributes

        return proj
Пример #4
0
    def test_update_type(self):

        template = self.get_template()

        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')

        templatetype = JSONObject()
        templatetype.name = "Test type name @ %s" % (datetime.datetime.now())
        templatetype.alias = templatetype.name + " alias"
        templatetype.template_id = self.get_template().id
        templatetype.resource_type = 'NODE'
        templatetype.template_id = template.id

        tattr_1 = JSONObject()
        tattr_1.attr_id = attr_1.id

        tattr_2 = JSONObject()
        tattr_2.attr_id = attr_2.id

        templatetype.typeattrs = [tattr_1, tattr_2]

        new_type_i = add_templatetype(templatetype)
        new_type_j = JSONObject(new_type_i)

        assert new_type_j.name == templatetype.name, "Names are not the same!"
        assert new_type_j.alias == templatetype.alias, "Aliases are not the same!"
        assert new_type_j.id is not templatetype, "New type has no ID!"
        assert new_type_j.id > 0, "New type has incorrect ID!"

        assert len(new_type_j.typeattrs
                   ) == 2, "Resource type attrs did not add correctly"
        new_type_j.name = "Updated type name @ %s" % (datetime.datetime.now())
        new_type_j.alias = templatetype.name + " alias"
        new_type_j.resource_type = 'NODE'

        tattr_3 = JSONObject()
        tattr_3.attr_id = attr_3.id
        tattr_3.description = "Descripton of added typeattr"
        tattr_3.properties = {"update_type_test_property": "property value"}
        new_type_j.typeattrs.append(tattr_3)

        new_type_j.typeattrs[0].description = "Updated typeattr description"

        updated_type_i = update_templatetype(new_type_j)
        updated_type_j = JSONObject(updated_type_i)

        assert new_type_j.name == updated_type_j.name, "Names are not the same!"
        assert new_type_j.alias == updated_type_j.alias, "Aliases are not the same!"
        assert new_type_j.id == updated_type_j.id, "type ids to not match!"
        assert new_type_j.id > 0, "New type has incorrect ID!"
        assert new_type_j.typeattrs[
            0].description == "Updated typeattr description"
        assert new_type_j.typeattrs[-1].properties[
            'update_type_test_property'] == "property value"

        assert len(updated_type_j.typeattrs
                   ) == 3, "Template type attrs did not update correctly"
Пример #5
0
    def test_add_type(self, client, mock_template):

        template = mock_template

        attr_1 = client.testutils.create_attribute("link_attr_1",
                                                   dimension='Pressure')
        attr_2 = client.testutils.create_attribute("link_attr_2",
                                                   dimension='Speed')
        attr_3 = client.testutils.create_attribute("node_attr_1",
                                                   dimension='Volume')

        templatetype = JSONObject()
        templatetype.name = "Test type name @ %s" % (datetime.datetime.now())
        templatetype.alias = "%s alias" % templatetype.name
        templatetype.resource_type = 'LINK'
        templatetype.template_id = template.id
        templatetype.layout = {"color": "red", "shapefile": "blah.shp"}

        templatetype.typeattrs = []

        tattr_1 = JSONObject()
        tattr_1.attr_id = attr_1.id
        tattr_1.description = "added type description 1"
        tattr_1.properties = {"add_type_test_property": "property value"}
        templatetype.typeattrs.append(tattr_1)

        tattr_2 = JSONObject()
        tattr_2.attr_id = attr_2.id
        tattr_1.description = "added type description 2"
        templatetype.typeattrs.append(tattr_2)

        tattr_3 = JSONObject()
        tattr_3.attr_id = attr_3.id
        templatetype.typeattrs.append(tattr_3)

        new_type_i = client.add_templatetype(templatetype)
        new_type_j = JSONObject(new_type_i)

        assert new_type_j.name == templatetype.name, "Names are not the same!"
        assert new_type_j.alias == templatetype.alias, "Aliases are not the same!"
        assert new_type_j.layout == templatetype.layout, "Layouts are not the same!"
        assert new_type_j.id is not None, "New type has no ID!"
        assert new_type_j.id > 0, "New type has incorrect ID!"

        assert len(new_type_j.typeattrs
                   ) == 3, "Resource type attrs did not add correctly"

        return new_type_j
Пример #6
0
    def test_add_typeattr(self, client, mock_template):

        attr_1 = client.testutils.create_attribute("link_attr_1",
                                                   dimension='Pressure')
        attr_2 = client.testutils.create_attribute("link_attr_2",
                                                   dimension='Speed')
        attr_3 = client.testutils.create_attribute("node_attr_1",
                                                   dimension='Volume')

        templatetype = JSONObject()
        templatetype.name = "Test type name @ %s" % (datetime.datetime.now())
        templatetype.alias = templatetype.name + " alias"
        templatetype.template_id = mock_template.id
        templatetype.resource_type = 'NODE'

        tattr_1 = JSONObject()
        tattr_1.attr_id = attr_1.id

        tattr_2 = JSONObject()
        tattr_2.attr_id = attr_2.id
        tattr_2.description = "Description of typeattr from test_add_typeattr"
        tattr_2.properties = {"test_property": "property value"}

        templatetype.typeattrs = [tattr_1, tattr_2]

        new_type = JSONObject(client.add_templatetype(templatetype))

        tattr_3 = JSONObject()
        tattr_3.attr_id = attr_3.id
        tattr_3.type_id = new_type.id
        tattr_3.description = "Description of additional typeattr from test_add_typeattr"
        tattr_3.properties = {"add_typeattr_test_property": "property value"}

        log.info("Adding Test Type attribute")

        client.add_typeattr(tattr_3)

        updated_type = JSONObject(client.get_templatetype(new_type.id))

        assert len(
            updated_type.typeattrs) == 3, "Type attr did not add correctly"

        assert eval(updated_type.typeattrs[-1].properties
                    )['add_typeattr_test_property'] == "property value"
Пример #7
0
    def create_templatetype(self, template_id):
        """
            Create a template type object (but don't add it to the DB)
            Args:
                template_id: The ID of the template to which to add this type
            Returns:
                JSONObject with some default values and the correct template ID
        """
        attr_1 = self.create_attribute("link_attr_1", dimension='Pressure')
        attr_2 = self.create_attribute("link_attr_2", dimension='Speed')
        attr_3 = self.create_attribute("link_attr_3", dimension='Volume')

        templatetype = JSONObject()
        templatetype.name = "Test type name @ %s"%(datetime.datetime.now())
        templatetype.alias = "%s alias" % templatetype.name
        templatetype.resource_type = 'LINK'
        templatetype.template_id = template_id
        templatetype.layout = {"color": "red", "shapefile": "blah.shp"}

        templatetype.typeattrs = []

        tattr_1 = JSONObject()
        tattr_1.attr_id = attr_1.id
        tattr_1.description = "added type description 1"
        tattr_1.properties = {"add_type_test_property": "property value"}
        templatetype.typeattrs.append(tattr_1)

        tattr_2 = JSONObject()
        tattr_2.attr_id = attr_2.id
        tattr_1.description = "added type description 2"
        templatetype.typeattrs.append(tattr_2)

        tattr_3 = JSONObject()
        tattr_3.attr_id = attr_3.id
        templatetype.typeattrs.append(tattr_3)

        return templatetype
Пример #8
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
Пример #9
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
Пример #10
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)
Пример #11
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