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