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!"
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