예제 #1
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"
예제 #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 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"
예제 #4
0
    def test_add_resource_type(self):

        template = self.get_template()
        types = template.templatetypes
        type_name = types[0].name
        type_id = types[0].id

        project = self.create_project('test')
        network = JSONObject()

        nnodes = 3
        nlinks = 2
        x = [0, 0, 1]
        y = [0, 1, 0]

        network.nodes = []
        network.links = []

        for i in range(nnodes):
            node = JSONObject()
            node.id = i * -1
            node.name = 'Node ' + str(i)
            node.description = 'Test node ' + str(i)
            node.x = x[i]
            node.y = y[i]

            type_summary = JSONObject()
            type_summary.template_id = template.id
            type_summary.template_name = template.name
            type_summary.id = type_id
            type_summary.name = type_name

            node.types = [type_summary]

            network.nodes.append(node)

        for i in range(nlinks):
            link = JSONObject()
            link.id = i * -1
            link.name = 'Link ' + str(i)
            link.description = 'Test link ' + str(i)
            link.node_1_id = network.nodes[i].id
            link.node_2_id = network.nodes[i + 1].id

            network.links.append(link)

        network.project_id = project.id
        network.name = 'Test @ %s' % (datetime.datetime.now())
        network.description = 'A network for SOAP unit tests.'

        net_summary = add_network(network, user_id=self.user_id)
        new_net = get_network(net_summary.id, user_id=self.user_id)

        for node in new_net.nodes:
            assert node.types is not None and node.types[
                0].type_name == "Node type"
            "type was not added 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 build_network(self, project_id=None, num_nodes=10, new_proj=True, map_projection='EPSG:4326'):
        start = datetime.datetime.now()
        if project_id is None:
            proj_name = None

            if new_proj is True:
                proj_name = "Test Project @ %s"%(datetime.datetime.now())
                project_id = self.create_project(name=proj_name).id
            else:
                project_id = project_id

        LOG.debug("Project creation took: %s"%(datetime.datetime.now()-start))
        start = datetime.datetime.now()

        template = self.create_template()

        LOG.debug("Attribute creation took: %s"%(datetime.datetime.now()-start))
        start = datetime.datetime.now()

        # Put an attribute on a group
        group_ra = JSONObject(dict(
            ref_id = None,
            ref_key = 'GROUP',
            attr_is_var = 'N',
            attr_id = template.templatetypes[2].typeattrs[0].attr_id,
            id = -1
        ))
        group_attrs = [group_ra]

        nodes = []
        links = []

        prev_node = None
        ra_index = 2

        network_type = template.templatetypes[0]
        node_type = template.templatetypes[1]
        link_type = template.templatetypes[2]
        for n in range(num_nodes):
            node = self.create_node(n*-1, node_name="Node %s"%(n))

            #From our attributes, create a resource attr for our node
            #We don't assign data directly to these resource attributes. This
            #is done when creating the scenario -- a scenario is just a set of
            #data for a given list of resource attributes.
            node_ra1 = JSONObject(dict(
                ref_key = 'NODE',
                ref_id = None,
                attr_id = node_type.typeattrs[0].attr_id,
                id = ra_index * -1,
                attr_is_var = 'N',
            ))
            ra_index = ra_index + 1
            node_ra2 = JSONObject(dict(
                ref_key = 'NODE',
                ref_id = None,
                attr_id = node_type.typeattrs[1].attr_id,
                id = ra_index * -1,
                attr_is_var = 'Y',
            ))
            ra_index = ra_index + 1
            node_ra3 = JSONObject(dict(
                ref_key = 'NODE',
                ref_id = None,
                attr_id = node_type.typeattrs[2].attr_id,
                id = ra_index * -1,
                attr_is_var = 'N',
            ))
            ra_index = ra_index + 1
            node_ra4 = JSONObject(dict(
                ref_key = 'NODE',
                ref_id = None,
                attr_id = node_type.typeattrs[3].attr_id,
                id = ra_index * -1,
                attr_is_var = 'N',
            ))
            ra_index = ra_index + 1

            node.attributes = [node_ra1, node_ra2, node_ra3, node_ra4]

            type_summary = JSONObject(dict(
                template_id = template.id,
                template_name = template.name,
                id = node_type.id,
                name = node_type.name
            ))

            type_summary_arr = [type_summary]

            node.types = type_summary_arr

            nodes.append(node)

            if prev_node is not None:
                #Connect the two nodes with a link
                link = self.create_link(
                    n*-1,
                    node['name'],
                    prev_node['name'],
                    node['id'],
                    prev_node['id'])

                link_ra1 = JSONObject(dict(
                    ref_id = None,
                    ref_key = 'LINK',
                    id = ra_index * -1,
                    attr_id = link_type.typeattrs[0].attr_id,
                    attr_is_var = 'N',
                ))
                ra_index = ra_index + 1
                link_ra2 = JSONObject(dict(
                    ref_id = None,
                    ref_key = 'LINK',
                    attr_id = link_type.typeattrs[1].attr_id,
                    id = ra_index * -1,
                    attr_is_var = 'N',
                ))
                ra_index = ra_index + 1
                link_ra3 = JSONObject(dict(
                    ref_id = None,
                    ref_key = 'LINK',
                    attr_id = link_type.typeattrs[2].attr_id,
                    id = ra_index * -1,
                    attr_is_var = 'N',
                ))
                ra_index = ra_index + 1

                link.attributes = [link_ra1, link_ra2, link_ra3]
                if link['id'] % 2 == 0:
                    type_summary_arr = []
                    type_summary = JSONObject()
                    type_summary.template_id = template.id
                    type_summary.template_name = template.name
                    type_summary.id = link_type.id
                    type_summary.name = link_type.name

                    type_summary_arr.append(type_summary)

                    link.types = type_summary_arr
                links.append(link)

            prev_node = node

        #A network must contain an array of links. In this case, the array

        LOG.debug("Making nodes & links took: %s"%(datetime.datetime.now()-start))
        start = datetime.datetime.now()

        #Create the scenario
        scenario = JSONObject()
        scenario.id = -1
        scenario.name = 'Scenario 1'
        scenario.description = 'Scenario Description'
        scenario.layout = json.dumps({'app': ["Unit Test1", "Unit Test2"]})
        scenario.start_time = datetime.datetime.now()
        scenario.end_time = scenario.start_time + datetime.timedelta(hours=1)
        scenario.time_step = 1 # one second intervals.

        #Multiple data (Called ResourceScenario) means an array.
        scenario_data = []

        group_array = []
        group = JSONObject(dict(
            id = -1,
            name = "Test Group",
            description = "Test group description"
        ))

        group.attributes = group_attrs

        group_array.append(group)

        group_item_array = []
        group_item_1 = JSONObject(dict(
            ref_key = 'NODE',
            ref_id = nodes[0]['id'],
            group_id = group['id'],
        ))
        group_item_2 = JSONObject(dict(
            ref_key = 'NODE',
            ref_id = nodes[1]['id'],
            group_id = group['id'],
        ))
        group_item_array = [group_item_1, group_item_2]

        scenario.resourcegroupitems = group_item_array

        #This is an example of 3 diffent kinds of data

        #For Links use the following:
        #A simple string (Descriptor)
        #A multi-dimensional array.

        #For nodes, use the following:
        #A time series, where the value may be a 1-D array

        nodes[0].attributes
        for n in nodes:
            for na in n.attributes:
                if na.get('attr_is_var', 'N') == 'N':
                    if na['attr_id'] == node_type.typeattrs[0].attr_id:
                        #less than 10 and with 1 decimal place,
                        #as per the restriction in the template
                        dataset = self.create_scalar(na, 1.1, unit='cm^3')
                    elif na['attr_id'] == node_type.typeattrs[2].attr_id:
                        #incorrect unit to test the validation
                        dataset = self.create_timeseries(na, 'cm^3')
                    elif na['attr_id'] == node_type.typeattrs[3].attr_id:
                        dataset = self.create_dataframe(na, unit='m^3 s^-1')
                elif na.get('attr_is_var', 'Y') == 'Y':
                    if na['attr_id'] == node_type.typeattrs[1].attr_id:
                        # correct unit (speed)
                        dataset = self.create_scalar(na, unit='m s^-1')
                scenario_data.append(dataset)
        count = 0
        for l in links:
            for na in l.attributes:
                if na['attr_id'] == link_type.typeattrs[0].attr_id:
                    array = self.create_array(na)
                    scenario_data.append(array)
                    count = count + 1
                elif na['attr_id'] == link_type.typeattrs[1].attr_id:
                    descriptor = self.create_descriptor(na)
                    scenario_data.append(descriptor)
                    count = count + 1

        grp_timeseries = self.create_timeseries(group_attrs[0], 'cm^3')#incorrect unit to test validation

        scenario_data.append(grp_timeseries)

        #Set the scenario's data to the array we have just populated
        scenario.resourcescenarios = scenario_data

        #A network can have multiple scenarios, so they are contained in
        #a scenario array
        scenario_array = []
        scenario_array.append(scenario)

        LOG.debug("Scenario definition took: %s"%(datetime.datetime.now()-start))
        #This can also be defined as a simple dictionary, but I do it this
        #way so I can check the value is correct after the network is created.
        layout = dict(
            color = 'red',
            shapefile = 'blah.shp'
        )

        node_array = nodes
        link_array = links

        net_attr = self.create_attribute("net_attr_b", dimension='Pressure')

        ra_index = ra_index + 1
        net_ra_notmpl = JSONObject(dict(
            ref_id = None,
            ref_key = 'NETWORK',
            attr_is_var = 'N',
            attr_id = net_attr.id,
            id = ra_index*-1
        ))
        ra_index = ra_index + 1
        net_ra_tmpl = JSONObject(dict(
            ref_id = None,
            ref_key = 'NETWORK',
            attr_is_var = 'N',
            attr_id = network_type.typeattrs[0].attr_id,
            id = ra_index*-1
        ))
        net_attrs = [net_ra_notmpl, net_ra_tmpl]

        net_type_summary_arr = []
        net_type_summary = JSONObject(dict(
            template_id = template.id,
            template_name = template.name,
            id = network_type.id,
            name = network_type.name,
        ))
        net_type_summary_arr.append(net_type_summary)

        network = JSONObject(dict(
            name = 'Network @ %s'%datetime.datetime.now(),
            description = 'Test network with 2 nodes and 1 link',
            project_id = project_id,
            links = link_array,
            nodes = node_array,
            layout = layout,
            scenarios = scenario_array,
            resourcegroups = group_array,
            projection = map_projection,
            attributes = net_attrs,
            types = net_type_summary_arr,
        ))

        return network