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"
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 create_network_with_extra_group(self, project_id=None, num_nodes=10, ret_full_net=True, new_proj=False, map_projection='EPSG:4326'): """ Test adding data to a network through a scenario. This test adds attributes to one node and then assigns data to them. It assigns a descriptor, array and timeseries to the attributes node. """ network = self.create_network_with_data( project_id=project_id, num_nodes=num_nodes, ret_full_net=ret_full_net, new_proj=new_proj, map_projection=map_projection) group = JSONObject({}) group.network_id = network.id group.id = -1 group.name = 'test new group' group.description = 'test new group' template_id = network.types[0].template_id template = JSONObject(self.client.get_template(template_id)) type_summary_arr = [] type_summary = JSONObject({}) type_summary.id = template.id type_summary.name = template.name type_summary.id = template.templatetypes[2].id type_summary.name = template.templatetypes[2].name type_summary_arr.append(type_summary) group.types = type_summary_arr new_group = self.client.add_group(network.id, group) group_attr_ids = [] for resource_attr in new_group.attributes: group_attr_ids.append(resource_attr.attr_id) updated_network = self.client.get_network(network.id) return updated_network
def test_delete_unit(self, client): # Add a new unit to a new dimension testdim = {'name': 'Test dimension'} new_dimension = client.add_dimension(testdim) testunit = JSONObject({}) testunit.name = 'Test' testunit.abbreviation = 'ttt' testunit.cf = 21 testunit.lf = 42 testunit.dimension_id = new_dimension.id new_unit = client.add_unit(testunit) # Check if the unit has been added unitlist = list(client.get_dimension(new_dimension.id).units) assert len(unitlist) > 0 and unitlist[0]['abbreviation'] == testunit.abbreviation, \ "The adding has not worked properly" result = client.delete_unit(new_unit.id) unitlist = list(client.get_dimension(new_dimension.id).units) assert len(unitlist) == 0, \ "Deleting unit didn't work correctly." client.delete_dimension(new_dimension.id)
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"
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"
def test_add_unit(self, client): # Add a new unit to an existing static dimension new_unit = JSONObject({}) new_unit.name = 'Teaspoons per second' new_unit.abbreviation = 'tsp s^-1' new_unit.cf = 0 # Constant conversion factor new_unit.lf = 1.47867648e-05 # Linear conversion factor new_unit.dimension_id = client.get_dimension_by_name( 'Volumetric flow rate').id new_unit.info = 'A flow of one tablespoon per second.' client.add_unit(new_unit) dimension_loaded = client.get_dimension(new_unit.dimension_id) assert len(list(filter(lambda x: x["name"] == new_unit["name"], dimension_loaded.units))) > 0 , \ "Adding new unit didn't work." # Add a new unit to a custom dimension testdim = {'name': 'Test dimension'} new_dimension = client.add_dimension(testdim) testunit = JSONObject({}) testunit.name = 'Test' testunit.abbreviation = 'ttt' testunit.cf = 21 testunit.lf = 42 testunit.dimension_id = new_dimension.id new_unit = client.add_unit(testunit) new_dimension_loaded = client.get_dimension(new_dimension.id) unitlist = list(new_dimension_loaded.units) assert len(unitlist) == 1, \ "Adding a new unit didn't work as expected" assert unitlist[0]["name"] == 'Test', \ "Adding a new unit didn't work as expected" client.delete_dimension(new_dimension.id)
def test_load(self, client): project = JSONObject({}) project.name = 'Test Project %s' % (datetime.datetime.now()) project.description = \ 'A project created through the SOAP interface.' project = client.add_project(project) new_project = client.get_project(project.id) assert new_project.name == project.name, \ "project_name is not loaded correctly." assert project.description == new_project.description,\ "project_description did not load correctly."
def test_set_project_status(self, client): project = JSONObject({}) project.name = 'SOAP test %s' % (datetime.datetime.now()) project.description = \ 'A project created through the SOAP interface.' project = client.add_project(project) client.set_project_status(project.id, 'X') proj = client.get_project(project.id) assert proj.status == 'X', \ 'Deleting project did not work correctly.'
def test_get_projects(self, client): project = JSONObject({}) project.name = 'SOAP test %s' % (datetime.datetime.now()) project.description = \ 'A project created through the SOAP interface.' project = client.add_project(project) projects = client.get_projects(pytest.root_user_id) assert len(projects) > 0, "Projects for user were not retrieved." assert projects[0].status == 'A'
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_project(name=None): if name is None: name = "Unittest Project" try: p = JSONObject(hydra_base.get_project_by_name(name, user_id=user_id)) return p except Exception: project = JSONObject() project.name = name project.description = "Project which contains all unit test networks" project = JSONObject(hydra_base.add_project(project, user_id=user_id)) hydra_base.share_project(project.id, ["UserA", "UserB", "UserC"], 'N', 'Y', user_id=user_id) return project
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"
def create_project(self, name=None, share=True): if name is None: name = "Unittest Project" user_projects = self.client.get_project_by_name(name) if len(user_projects) == 0: project = JSONObject() project.name = name project.description = "Project which contains all unit test networks" project = JSONObject(self.client.add_project(project)) if share is True: self.client.share_project(project.id, ["UserA", "UserB", "UserC"], 'N', 'Y') return project else: return user_projects[0]
def create_project(name=None): if name is None: name = "Unittest Project" user_projects = hydra_base.get_project_by_name(name, user_id=user_id) if len(user_projects) == 0: project = JSONObject() project.name = name project.description = "Project which contains all unit test networks" project = JSONObject(hydra_base.add_project(project, user_id=user_id)) hydra_base.share_project(project.id, ["UserA", "UserB", "UserC"], 'N', 'Y', user_id=user_id) return project else: return user_projects[0]
def create_project(name=None): if name is None: name = "Unittest Project" user_projects = hydra_base.get_project_by_name(name, user_id=user_id) if len(user_projects) == 0: project = JSONObject() project.name = name project.description = "Project which contains all unit test networks" project = JSONObject(hydra_base.add_project(project, user_id=user_id)) hydra_base.share_project(project.id, ["UserA", "UserB", "UserC"], 'N', 'Y', user_id=user_id) return project else: return user_projects[0]
def test_update(self, client, network_with_data): """ The network here is necessary for a scenario with ID 1 to exist. Under normal circumstances, scenario 1 will always exist, as it's an initial requirement of the database setup. """ project = JSONObject({}) project.name = 'SOAP test %s' % (datetime.datetime.now()) project.description = \ 'A project created through the SOAP interface.' project = self.add_attributes(client, project) project = self.add_data(client, project) new_project_i = client.add_project(project) project_j = client.get_project(new_project_i.id) new_project = copy.deepcopy(project_j) new_project.description = \ 'An updated project created through the Hydra Base interface.' updated_project = client.update_project(new_project) assert project_j.id == updated_project.id, \ "project_id changed on update." assert project_j.created_by is not None, \ "created by is null." assert project_j.name == updated_project.name, \ "project_name changed on update." assert project_j.description != updated_project.description,\ "project_description did not update" assert updated_project.description == \ 'An updated project created through the Hydra Base interface.', \ "Update did not work correctly." rs_to_check = client.get_project_attribute_data(new_project.id)[0] assert rs_to_check.dataset.type == 'descriptor' and \ rs_to_check.dataset.value == 'just project desscriptor', \ "There is an inconsistency with the attributes."
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 test_update_unit(self, client): # Add a new unit to a new dimension testdim = {'name': 'Test dimension'} new_dimension = client.add_dimension(testdim) testunit = JSONObject({}) testunit.name = 'Test' testunit.abbreviation = 'ttt' testunit.cf = 21 testunit.lf = 42 testunit.dimension_id = new_dimension.id new_unit = client.add_unit(testunit) # Update it new_unit.cf = 0 new_unit_modified = client.update_unit(new_unit) unitlist = list(client.get_dimension(new_dimension.id).units) assert len(unitlist) > 0 and int(unitlist[0]['cf']) == 0, \ "Updating unit didn't work correctly." client.delete_dimension(new_dimension.id)
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 test_update_template(self, client): #Only applicable for tests. client.template.ATTR_CACHE = {} # Defining attributes attribute_1 = client.testutils.create_attribute("link_attr_1", dimension='Pressure') attribute_2 = client.testutils.create_attribute("link_attr_2", dimension='Speed') attribute_3 = client.testutils.create_attribute("node_attr_1", dimension='Volume') # Defining template template = JSONObject() template.name = 'Test Template @ %s'%datetime.datetime.now() template.templatetypes = [] template_type_1 = JSONObject({ "name" : "Node type 2", "alias" : "Node type 2 alias", "resource_type" : 'NODE', "typeattrs" : [] }) template_type_2 = JSONObject({ "name" : "Link type 2", "alias" : "Link type 2 alias", "resource_type" : 'LINK', "typeattrs" : [] }) # Definind a typeattr data structure type_attribute_1 = JSONObject({ "attr_id" : attribute_1.id, "unit_id" : client.get_unit_by_abbreviation('bar').id, "description" : "typeattr description 1", "properties" : {"test_property": "property value"} }) template_type_1.typeattrs.append(type_attribute_1) type_attribute_2 = JSONObject({ "attr_id" : attribute_2.id, "unit_id" : client.get_unit_by_abbreviation('mph').id, "description" : "typeattr description 2" }) template_type_2.typeattrs.append(type_attribute_2) template.templatetypes.append(template_type_1) template.templatetypes.append(template_type_2) new_template_i = client.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_id == client.get_unit_by_abbreviation('bar').id #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 a new type new_type = JSONObject({ "name" : "New Node type", "alias" : "New Node type alias", "resource_type" : 'NODE', "typeattrs" : [] }) new_template_j.templatetypes.append(new_type) #add an template attr to one of the types type_attribute_3 = JSONObject({ "attr_id" : attribute_3.id, "description" : "updated typeattr description 1", "properties" : {"test_property_of_added_type": "property value"} }) new_template_j.templatetypes[0].typeattrs.append(type_attribute_3) updated_template_j = client.update_template(new_template_j) updated_template_retrieved_j = client.get_template(new_template_j.id) assert updated_template_retrieved_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' """ old_attribute_type_to_restore = updated_template_j.templatetypes[0].typeattrs[0].unit_id updated_template_j.templatetypes[0].typeattrs[0].unit_id = client.get_unit_by_abbreviation('m^3').id with pytest.raises(HydraError): client.update_template(updated_template_j) # Restoring the old value to not fail later for this reason updated_template_j.templatetypes[0].typeattrs[0].unit_id = old_attribute_type_to_restore """ Testing the case in which the attribute has no dimension while the typeattr has UNIT_ID that is not none """ attribute_dimension_none = client.testutils.create_attribute("node_attr_dimension_none", dimension=None) template_type_dimension_none = JSONObject({ "name" : "Node type dimension_none", "alias" : "Node type dimension_none alias", "resource_type" : 'NODE', "typeattrs" : [] }) type_attribute_dimension_none = JSONObject({ "attr_id" : attribute_dimension_none.id, "unit_id" : client.get_unit_by_abbreviation('bar').id, "description" : "type_attribute_dimension_none description 1", "properties" : {"test_property": "property value"} }) template_type_dimension_none.typeattrs.append(type_attribute_dimension_none) updated_template_j.templatetypes.append(template_type_dimension_none) with pytest.raises(HydraError): client.update_template(updated_template_j) # It MUST fail because the unit_id is not consistent to dimension_id none
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
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
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)