示例#1
0
    def test_get_dict(self):

        #Upload the xml file initally to avoid having to manage 2 template files
        xml_tmpl = self.test_add_xml()

        template_dict = get_template_as_dict(xml_tmpl.id)

        #Error that there's already a template with this name.
        self.assertRaises(HydraError,
                          import_template_dict,
                          template_dict,
                          allow_update=False)

        typename = template_dict['template']['templatetypes'][0]['name']

        template_dict['template']['templatetypes'][
            0].name = typename + "_updated"

        #Finds a template with this name and updates it to mirror this dict.
        #This includes deleting types if they're not in this dict.
        #Changing the name of a type has this effect, as a new template does not have
        #any reference to existing types in Hydra.
        updated_template = JSONObject(import_template_dict(template_dict))

        assert updated_template['templatetypes'][-1][
            'name'] == typename + "_updated"

        #Now put it back to the original name so other tests will work
        template_dict['template']['templatetypes'][0].name = typename
        updated_template = JSONObject(import_template_dict(template_dict))

        assert updated_template['templatetypes'][-1]['name'] == typename
示例#2
0
    def add_rule(self,
                 client,
                 network,
                 name='A Test Rule',
                 text='e=mc^2',
                 scenario_id=None,
                 ref_key=None,
                 ref_id=None,
                 types=[]):
        """
            A utility function which creates a rule and associates it
            with either a resource type, resource instance or resource
            instance / scenario pair.
        """

        rule = JSONObject({
            'name': name,
            'value': text,
            'scenario_id': scenario_id,
            'ref_key': 'NETWORK' if ref_key is None else ref_key,
            'ref_id': network.id if ref_id is None else ref_id,
            'types': [{
                'code': typecode
            } for typecode in types]
        })

        new_rule_j = JSONObject(
            client.add_rule(scenario_id=None,
                            rule=rule,
                            include_network_users=True))

        return new_rule_j
示例#3
0
    def test_remove_type_from_resource(self):
        network = self.create_network_with_data()
        template = self.get_template()
        templatetype = template.templatetypes[0]

        node_to_assign = network.nodes[0]

        result1_i = assign_type_to_resource(templatetype.id, 'NODE',
                                            node_to_assign.id)

        result1_j = JSONObject(result1_i)

        node_j = JSONObject(get_node(node_to_assign.id))

        assert node_j.types is not None, \
            'Assigning type did not work properly.'

        assert str(result1_j.id) in [str(x.type_id) for x in node_j.types]

        remove_result = remove_type_from_resource(templatetype.id, 'NODE',
                                                  node_to_assign.id)
        print remove_result
        assert remove_result == 'OK'

        updated_node_j = JSONObject(get_node(node_to_assign.id))

        assert updated_node_j.types is None or str(result1_j.id) not in [
            str(x.type_id) for x in updated_node_j.types
        ]
示例#4
0
    def test_add_rule_with_type(self, client, network_with_data):

        typecode = 'a_new_rule_type'
        typecode1 = 'a_new_rule_type_1'

        client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule',
                'code': typecode
            }))
        client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule',
                'code': typecode1
            }))

        rulename = 'Added Rule'
        ruletext = 'e=mc^3'  #yes this is delibrate, so it's different to the default
        new_rule_j = self.add_rule(client,
                                   network_with_data,
                                   name=rulename,
                                   text=ruletext,
                                   types=[typecode, typecode1])

        assert new_rule_j.id is not None
        assert new_rule_j.name == rulename
        assert new_rule_j.value == ruletext
        assert len(new_rule_j.types) == 2

        assert new_rule_j.id in [
            r.id for r in client.get_rules_of_type(typecode)
        ]
示例#5
0
def session_with_pywr_template(session):

    attributes = [JSONObject(a) for a in generate_pywr_attributes()]

    # The response attributes have ids now.
    response_attributes = hydra_base.add_attributes(attributes)

    # Convert to a simple dict for local processing.
    attribute_ids = {a.name: a.id for a in response_attributes}

    default_data_set_ids = {}
    for attribute_name, dataset in PYWR_DEFAULT_DATASETS.items():

        # Patch the default datasets to convert unit to unit_id.
        unit = dataset.pop('unit', None)
        if unit is not None and 'unit_id' not in dataset:
            u = hydra_base.get_unit_by_abbreviation(unit)
            dataset['unit_id'] = u.id
        else:
            dataset['unit_id'] = None

        hydra_dataset = hydra_base.add_dataset(flush=True, **dataset)
        default_data_set_ids[attribute_name] = hydra_dataset.id

    config = load_template_config('full')
    template = generate_pywr_template(attribute_ids, default_data_set_ids, config)

    hydra_base.add_template(JSONObject(template))

    yield session
示例#6
0
    def test_update_attribute(self, client):
        test_attr = JSONObject({
            "name": f'Test Attribute {datetime.datetime.now()}',
            "dimension_id": None
        })

        new_attr = client.add_attribute(test_attr)
        new_attr.name = f"Test attr updated {datetime.datetime.now()}"
        updated_attr = client.update_attribute(new_attr)
        assert new_attr.id == updated_attr.id and \
                updated_attr.name == new_attr.name, \
                "update_attribute didn't work"
        #Try update this again (should have no effect)
        updated_attr.description = "Updated description"
        updated_attr_1 = client.update_attribute(updated_attr)
        assert updated_attr_1.description == "Updated description"

        #Now add a new attribute which should fail when updated
        test_attr_fail = JSONObject({
            "name": f'Test Attribute {datetime.datetime.now()}',
            "dimension_id": None
        })

        new_attr_fail = client.add_attribute(test_attr_fail)
        #set the name to be the same as the other attribute
        new_attr_fail.name = new_attr.name

        #this should fail because there's already an attribute with this naem
        #and dimension (since we've just set it.)
        with pytest.raises(HydraError):
            client.update_attribute(new_attr_fail)
示例#7
0
    def create_network_with_data(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.build_network(project_id, num_nodes, new_proj=new_proj,
                                   map_projection=map_projection)

        #LOG.debug(network)
        start = datetime.datetime.now()
        LOG.info("Creating network...")
        response_network_summary = JSONObject(self.client.add_network(network))

        LOG.info("Network Creation took: %s"%(datetime.datetime.now()-start))
        if ret_full_net is True:
            LOG.info("Fetching new network...:")
            start = datetime.datetime.now()
            net = self.client.get_network(response_network_summary.id, include_data=True)
            response_net = JSONObject(net)
            LOG.info("Network Retrieval took: %s"%(datetime.datetime.now()-start))
            self.check_network(network, response_net)
            return response_net
        else:
           return response_network_summary
示例#8
0
    def test_add_attributes(self, client):
        test_attrs = [
            JSONObject({
                "name": 'Test Attribute 1',
                "dimension_id": None
            }),
            JSONObject({
                "name": 'Test Attribute 2',
                "dimension_id": 1
            })
        ]
        new_attrs_list_1 = client.add_attributes(test_attrs)

        all_attributes_after_add_1 = client.get_attributes()

        #Try adding the attributes again. It should ignore them as theyr'e already there.
        new_attrs_list_2 = client.add_attributes(test_attrs)

        all_attributes_after_add_2 = client.get_attributes()

        #This should have returned the attributes with the IDS from the first insert
        assert sorted([a.id for a in new_attrs_list_1
                       ]) == sorted([a.id for a in new_attrs_list_2])

        assert len(all_attributes_after_add_1) == len(
            all_attributes_after_add_2)

        attributeset = set([(a.name, a.dimension)
                            for a in all_attributes_after_add_1])

        #Ensure that there are no duplicates by checking that the length of the set
        #of name/dimension pairs is the same as the length of all attributes
        assert len(attributeset) == len(all_attributes_after_add_2)
示例#9
0
    def test_remove_type_from_resource(self, client, mock_template,
                                       network_with_data):
        network = network_with_data
        template = mock_template
        templatetype = template.templatetypes[0]

        node_to_assign = network.nodes[0]

        result1_i = client.assign_type_to_resource(templatetype.id, 'NODE',
                                                   node_to_assign.id)

        result1_j = JSONObject(result1_i)

        node_j = JSONObject(client.get_node(node_to_assign.id))

        assert node_j.types is not None, \
            'Assigning type did not work properly.'

        assert str(result1_j.id) in [str(x.type_id) for x in node_j.types]

        remove_result = client.remove_type_from_resource(
            templatetype.id, 'NODE', node_to_assign.id)
        assert remove_result == 'OK'

        updated_node_j = JSONObject(client.get_node(node_to_assign.id))

        assert updated_node_j.types is None or str(result1_j.id) not in [
            str(x.type_id) for x in updated_node_j.types
        ]
示例#10
0
    def test_clone_rule(self, client, network_with_data):

        ruletype_A_j = client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule',
                'code': 'a_new_rule'
            }))
        ruletype_B_j = client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule 1',
                'code': 'a_new_rule_1'
            }))

        rulename = 'Added Rule'
        ruletext = 'e=mc^3'  #yes this is delibrate, so it's different to the default
        new_rule_j = self.add_rule(
            client,
            network_with_data,
            name=rulename,
            text=ruletext,
            types=[ruletype_A_j.code, ruletype_B_j.code])

        assert new_rule_j.id is not None
        assert new_rule_j.name == rulename
        assert new_rule_j.value == ruletext

        cloned_rule = client.clone_rule(new_rule_j.id)

        assert cloned_rule.name == new_rule_j.name
        assert cloned_rule.value == new_rule_j.value
        assert cloned_rule.id is not None
        assert cloned_rule.id != new_rule_j.id
        assert len(cloned_rule.types) == 2
        assert [t.code for t in new_rule_j.types
                ] == [t.code for t in cloned_rule.types]
示例#11
0
    def test_reverse_exclusive_add_attribute_group_items(
            self, client, projectmaker, network_with_data,
            attributegroupmaker):
        """
            add attributes to an exclusive group that are already in another group
        """

        project = projectmaker.create()

        #convenience naming
        network = network_with_data

        #Create two groups -- attributes which are associated with a network,
        #and everything else.
        group_1 = attributegroupmaker.create(project.id, "Network Attributes",
                                             'Y')
        group_2 = attributegroupmaker.create(project.id, "Node Attributes")

        network_attributes = []
        node_attributes = []

        for netattr in network.attributes:
            network_attributes.append(
                JSONObject({
                    'attr_id': netattr.attr_id,
                    'network_id': network.id,
                    'group_id': group_1.id
                }))
            #Put these attributes into both groups. THis should fail, as group 1
            #is exclusive, and already has these attributes
            node_attributes.append(
                JSONObject({
                    'attr_id': netattr.attr_id,
                    'network_id': network.id,
                    'group_id': group_2.id
                }))

        node_attr_tracker = []
        for node in network.nodes:
            for node_attr in node.attributes:
                if node_attr.attr_id not in node_attr_tracker:
                    node_attributes.append(
                        JSONObject({
                            'attr_id': node_attr.attr_id,
                            'network_id': network.id,
                            'group_id': group_2.id
                        }))
                    node_attr_tracker.append(node_attr.attr_id)

        log.info(
            "Adding items to group 2 (node attributes, plus network attributes)"
        )
        client.add_attribute_group_items(node_attributes)

        #add attributes to an exclusive group that are already in another group
        with pytest.raises(hb.HydraError):
            log.info("Adding items to group 1 (network attributes)")
            client.add_attribute_group_items(network_attributes)
示例#12
0
    def test_delete_all_duplicate_attributes(self, client, network_with_data):

        duplicate_attribute = JSONObject({'name': 'duplicate', 'dimension_id': None})

        #use dedicated testing function  which allows duplicates
        dupe_attr_1 = client.add_attribute_no_checks(duplicate_attribute)
        dupe_attr_2 = client.add_attribute_no_checks(duplicate_attribute)


        all_attrs = client.get_attributes()

        assert dupe_attr_1.id in [a.id for a in all_attrs]
        assert dupe_attr_2.id in [a.id for a in all_attrs]

        #add duplicate resource attributes
        client.add_resource_attribute('NETWORK', network_with_data.id, dupe_attr_1.id, 'Y')
        client.add_resource_attribute('NETWORK', network_with_data.id, dupe_attr_2.id, 'Y')

        #check the dupes are there
        updated_net = client.get_network(network_with_data.id)
        updated_net_ras = [ra.attr_id for ra in updated_net.attributes]
        assert dupe_attr_1.id in updated_net_ras
        assert dupe_attr_2.id in updated_net_ras

        #now add duplicate attrs to the template type
        templatetype_to_update = network_with_data.types[0].id
        client.add_typeattr(JSONObject({'type_id': templatetype_to_update,
                                        'attr_id': dupe_attr_1.id}))
        client.add_typeattr(JSONObject({'type_id': templatetype_to_update,
                                        'attr_id': dupe_attr_2.id}))

        #check the dupes are there
        updated_type = client.get_templatetype(templatetype_to_update)
        assert dupe_attr_1.id in [ta.attr_id for ta in updated_type.typeattrs]
        assert dupe_attr_1.id in [ta.attr_id for ta in updated_type.typeattrs]

        client.delete_all_duplicate_attributes()

        #check the dupes are gone
        updated_net = client.get_network(network_with_data.id)
        updated_net_ras = [ra.attr_id for ra in updated_net.attributes]
        assert dupe_attr_1.id in updated_net_ras
        assert dupe_attr_2.id not in updated_net_ras

        #check the dupes are gone
        updated_type = client.get_templatetype(templatetype_to_update)
        assert dupe_attr_1.id in [ta.attr_id for ta in updated_type.typeattrs]
        assert dupe_attr_2.id not in [ta.attr_id for ta in updated_type.typeattrs]

        reduced_attrs = client.get_attributes()

        #check that the first attr is there, but the dupe is not.
        #the one to keep should be the one with the lowest ID
        lowest_id = min(dupe_attr_1.id, dupe_attr_2.id)
        assert lowest_id in [a.id for a in reduced_attrs]
        assert dupe_attr_2.id not in [a.id for a in reduced_attrs]
示例#13
0
    def test_update_rule(self, client, network_with_data):

        typecode = 'a_new_rule_type'
        typecode1 = 'a_new_rule_type_1'

        client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule',
                'code': typecode
            }))
        client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule',
                'code': typecode1
            }))

        rulename = 'Added Rule'
        ruletext = 'e=mc^3'  #yes this is delibrate, so it's different to the default

        new_rule_j = self.add_rule(client,
                                   network_with_data,
                                   name=rulename,
                                   text=ruletext,
                                   types=[typecode])

        new_rule_j = client.get_rule(
            new_rule_j.id)  #do this to get all the DB server defaults

        assert len(new_rule_j.types) == 1

        new_rule_j.name = 'Updated Rule'
        new_rule_j.format = 'text'
        new_rule_j.value = 'e=mc2'  #fix the error
        new_rule_j.types.append(JSONObject({'code': typecode1}))

        client.update_rule(new_rule_j)

        updated_rule_j = client.get_rule(new_rule_j.id)

        assert updated_rule_j.name == 'Updated Rule'
        assert updated_rule_j.value == 'e=mc2'
        assert updated_rule_j.format == 'text'
        assert updated_rule_j.scenario_id is None
        assert len(updated_rule_j.types) == 2

        #update the rule again, setting it on a scenario
        updated_rule_j.scenario_id = network_with_data.scenarios[0].id

        client.update_rule(updated_rule_j)

        updated_rule_j_2 = client.get_rule(updated_rule_j.id)

        assert updated_rule_j_2.scenario_id == network_with_data.scenarios[
            0].id
示例#14
0
    def test_get_dict(self, client, template_json_object):

        # Upload the xml file initally to avoid having to manage 2 template files
        xml_tmpl = template_json_object

        template_dict = client.get_template_as_dict(xml_tmpl.id)

        # Error that there's already a template with this name.
        with pytest.raises(HydraError):
            client.import_template_dict(template_dict, allow_update=False)

        typename = template_dict['template']['templatetypes'][0]['name']

        template_dict['template']['templatetypes'][
            0].name = typename + "_updated"

        # Finds a template with this name and updates it to mirror this dict.
        # This includes deleting types if they're not in this dict.
        # Changing the name of a type has this effect, as a new template does not have
        # any reference to existing types in Hydra.

        updated_template = JSONObject(
            client.import_template_dict(template_dict))

        type_names = []

        for templatetype in updated_template.templatetypes:
            type_names.append(templatetype.name)

        assert len(type_names) == 2
        assert typename + "_updated" in type_names and typename not in type_names

        # Now put it back to the original name so other tests will work
        log.info("Reverting the type's name")
        template_dict['template']['templatetypes'][0].name = typename
        updated_template = JSONObject(
            client.import_template_dict(template_dict))

        #just double-check that the JSON import works also
        updated_template = JSONObject(
            client.import_template_json(json.dumps(template_dict)))

        type_names = []
        for templatetype in updated_template.templatetypes:
            type_names.append(templatetype.name)

        assert len(type_names) == 2
        assert typename in type_names and typename + "_updated" not in type_names

        log.info("Checking to ensure Template has been updated correctly...")
        # one final check to ensure that the type has been deleted
        check_template_i = client.get_template(updated_template.id)

        assert len(check_template_i.templatetypes) == 2
示例#15
0
    def test_apply_template_to_network_twice(self, client, mock_template, network_with_data):
        net_to_update = network_with_data
        template = mock_template

        # Test the links as it's easier
        empty_links = []
        for l in net_to_update.links:
            if l.types is None:
                empty_links.append(l.id)

        # Add the resource attributes to the links, so we can guarantee
        # that these links will match those in the template.
        for t in template.templatetypes:
            if t.resource_type == 'LINK':
                link_type = t
                break

        link_ra_1 = JSONObject(dict(
            attr_id=link_type.typeattrs[0].attr_id
        ))
        link_ra_2 = JSONObject(dict(
            attr_id=link_type.typeattrs[1].attr_id
        ))
        for link in net_to_update.links:
            if link.types is None:
                link.attributes.append(link_ra_1)
                link.attributes.append(link_ra_2)

        network = client.update_network(net_to_update)

        for n in network.nodes:
            assert len(n.types) == 1
            assert n.types[0].name == 'Default Node'

        client.apply_template_to_network(template.id, network.id)

        client.apply_template_to_network(template.id, network.id)

        network = client.get_network(network.id)

        assert len(network.types) == 2

        assert 'Network Type' in [t.name for t in network.types]

        for l in network.links:
            if l.id in empty_links:
                assert l.types is not None
                assert len(n.types) == 1
                assert l.types[0].name == 'Link type'

        for n in network.nodes:
            assert len(n.types) == 1
            assert n.types[0].name == 'Default Node'
示例#16
0
    def test_delete_rule_type(self, client, network_with_data):
        ruletype_A_j = client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule',
                'code': 'a_new_rule'
            }))
        ruletype_B_j = client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule 1',
                'code': 'a_new_rule_1'
            }))

        scenario_id = network_with_data.scenarios[0].id

        #Create 3 rules, 2 of type A and 1 of type B
        self.add_rule(client,
                      network_with_data,
                      name="Test1",
                      types=[ruletype_A_j.code])
        self.add_rule(client,
                      network_with_data,
                      name="Test2",
                      types=[ruletype_A_j.code])
        self.add_rule(client,
                      network_with_data,
                      name="Test3",
                      types=[ruletype_A_j.code, ruletype_B_j.code],
                      scenario_id=scenario_id)
        self.add_rule(client,
                      network_with_data,
                      name="Rule Type B",
                      types=[ruletype_B_j.code])

        #Get all the rules of type A, of which there should be 2
        rules_of_type = client.get_rules_of_type(ruletype_A_j.code)
        rules_of_type_in_scenario = client.get_rules_of_type(
            ruletype_A_j.code, scenario_id=scenario_id)

        assert len(rules_of_type) >= 3
        #Check that the added nodes are indeed present
        assert {'Test1', 'Test2',
                'Test3'}.issubset({r.name
                                   for r in rules_of_type})
        assert len(rules_of_type_in_scenario) == 1

        client.purge_rule_type_definition('a_new_rule')

        assert len(client.get_rules_of_type(ruletype_A_j.code)) == 0

        #check all the rules are still there
        assert len(client.get_resource_rules('NETWORK',
                                             network_with_data.id)) == 4
示例#17
0
    def test_delete_attribute_group_items(self, client, projectmaker,
                                          network_with_data,
                                          attributegroupmaker):
        project = projectmaker.create()

        #convenience naming
        network = network_with_data

        #Create two groups -- attributes which are associated with a network,
        #and everything else.
        group_1 = attributegroupmaker.create(project.id, "Network Attributes")
        group_2 = attributegroupmaker.create(project.id, "Node Attributes")

        network_attributes = []
        for netattr in network.attributes:
            network_attributes.append(
                JSONObject({
                    'attr_id': netattr.attr_id,
                    'network_id': network.id,
                    'group_id': group_1.id
                }))

        node_attr_tracker = []
        node_attributes = []
        for node in network.nodes:
            for node_attr in node.attributes:
                if node_attr.attr_id not in node_attr_tracker:
                    node_attributes.append(
                        JSONObject({
                            'attr_id': node_attr.attr_id,
                            'network_id': network.id,
                            'group_id': group_2.id
                        }))
                    node_attr_tracker.append(node_attr.attr_id)

        client.add_attribute_group_items(network_attributes)

        client.add_attribute_group_items(node_attributes)

        all_items_in_network = client.get_network_attributegroup_items(
            network.id)

        assert len(all_items_in_network
                   ) == len(network_attributes) + len(node_attributes)

        #Now remove all the node attributes
        client.delete_attribute_group_items(node_attributes)

        all_items_in_network = client.get_network_attributegroup_items(
            network.id)

        assert len(all_items_in_network) == len(network_attributes)
示例#18
0
    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.'
示例#19
0
    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."
示例#20
0
    def create_attributes(self):
        dimension = "Volumetric flow rate"
        attrs = []
        attrs.append(
            JSONObject({
                'name':
                "Multi-added Attr 1",
                'dimension_id':
                self.client.get_dimension_by_name(dimension).id,
                'description':
                "Attribute 1 from a test of adding multiple attributes",
            }))
        attrs.append(
            JSONObject({
                'name':
                "Multi-added Attr 2",
                'dimension_id':
                self.client.get_dimension_by_name(dimension).id,
                'description':
                "Attribute 2 from a test of adding multiple attributes",
            }))

        existing_attrs = []
        new_attrs = []

        for a in attrs:
            LOG.info("Getting attribute %s, %s", a.name, a.dimension_id)

            attr = self.client.get_attribute_by_name_and_dimension(
                a.name, a.dimension_id)
            if attr is not None and attr != {}:
                # The attribute already exists
                existing_attrs.append(attr)
            else:
                # The attribute is new
                new_attrs.append(a)

        added_attrs = self.client.add_attributes(new_attrs)

        # The existing attributes summed with the inserted ones are the total of "attrs" array
        total_attrs = existing_attrs + added_attrs

        assert len(total_attrs) == len(attrs)

        for my_attr in total_attrs:
            assert len(
                list(
                    filter(lambda x: x.description == my_attr.description,
                           attrs))) > 0

        return attrs
示例#21
0
def session_with_pywr_template(session):

    attributes = [JSONObject(a) for a in generate_pywr_attributes()]

    # The response attributes have ids now.
    response_attributes = hydra_base.add_attributes(attributes)

    # Convert to a simple dict for local processing.
    attribute_ids = {a.attr_name: a.attr_id for a in response_attributes}

    template = generate_pywr_template(attribute_ids)

    hydra_base.add_template(JSONObject(template))

    yield session
示例#22
0
    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'
示例#23
0
    def test_get_rule_type_definitions(self, client):
        client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule',
                'code': 'a_new_rule'
            }))
        client.add_rule_type_definition(
            JSONObject({
                'name': 'A new Rule 1',
                'code': 'a_new_rule_1'
            }))

        rule_types_j = client.get_rule_type_definitions()

        assert len(rule_types_j) == 2
示例#24
0
    def test_get_templates(self, client, mock_template):

        templates = [JSONObject(t) for t in client.get_templates()]
        for t in templates:
            for typ in t.templatetypes:
                assert typ.resource_type is not None
        assert len(templates) > 0, "Templates were not retrieved!"
示例#25
0
    def create_array(self, resource_attr):
        #A scenario attribute is a piece of data associated
        #with a resource attribute.
        #[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

        arr = json.dumps([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])

        metadata_array = json.dumps({'created_by': 'Test user'})

        dataset = Dataset(dict(
            id=None,
            type = 'array',
            name = 'my array',
            unit_id = self.get_unit('bar').id,
            hidden = 'N',
            value = arr,
            metadata = metadata_array,
        ))

        scenario_attr = JSONObject(dict(
            attr_id = resource_attr.attr_id,
            resource_attr_id = resource_attr.id,
            dataset = dataset,
        ))

        return scenario_attr
示例#26
0
    def create_dataframe(self, resource_attr, name='Test Data Frame', dataframe_value=None, unit='m^3 s^-1'):
        #A scenario attribute is a piece of data associated
        #with a resource attribute.

        if dataframe_value is None:
            val_1 = 1
            val_2 = 2
            val_3 = 3

            dataframe_value = {"test_column": {'key1': val_1,
                        'key2': val_2,
                        'key3': val_3}}

        metadata = {'created_by': 'Test user'}

        dataset = Dataset(dict(
            id=None,
            type = 'dataframe',
            name = name,
            unit_id = self.get_unit(unit).id,
            hidden = 'N',
            value = json.dumps(dataframe_value),
            metadata = metadata
        ))

        scenario_attr = JSONObject(dict(
            attr_id = resource_attr.attr_id,
            resource_attr_id = resource_attr.id,
            dataset = dataset,
        ))

        return scenario_attr
示例#27
0
    def create_network_with_child_scenario(self, project_id=None,
                                        num_nodes=10,
                                        ret_full_net=True,
                                        new_proj=False,
                                        map_projection='EPSG:4326',
                                        levels=2):
        """
            Create a network with two scenarios -- one with data in it, and its child containing
            none of its own data.
        """
        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)
        parent_scenario = network.scenarios[0]
        parent_scenario_id = parent_scenario.id
        scenario_count = len(network.scenarios) + 1
        for level in range(levels-1):
            new_scenario_j = self.client.create_child_scenario(parent_scenario_id, "Scenario {0}".format(scenario_count))
            parent_scenario_id = new_scenario_j.id
            scenario_count = scenario_count + 1


        updated_network = JSONObject(self.client.get_network(network.id, include_data=True))

        return updated_network
示例#28
0
    def update_template(self, template_id):

        template = JSONObject(self.client.get_template(template_id))
        new_net_attr = self.create_attribute("net_attr_d", dimension='Monetary Value')

        for tmpltype in template.templatetypes:
            if tmpltype.resource_type == 'NETWORK':
                typeattr_1 = JSONObject(dict(
                    attr_id = new_net_attr.id,
                    data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1},
                    unit_id = self.get_unit('USD').id,
                ))
                tmpltype.typeattrs.append(typeattr_1)
                break

        template = self.client.update_template(template)
示例#29
0
 def test_get_templates(self):
     self.get_template()
     templates = [JSONObject(t) for t in get_templates()]
     for t in templates:
         for typ in t.templatetypes:
             assert typ.resource_type is not None
     assert len(templates) > 0, "Templates were not retrieved!"
示例#30
0
    def test_delete_template(self, client, network_with_data, mock_template):

        #Only applicable for tests. TODO: make this not rubbish.
        client.template.ATTR_CACHE = {}

        network = network_with_data
        new_template = mock_template

        retrieved_template_i = client.get_template(new_template.id)
        assert retrieved_template_i is not None

        retrieved_template_j = JSONObject(retrieved_template_i)

        client.apply_template_to_network(retrieved_template_j.id, network.id)

        updated_network = client.get_network(network.id)
        assert len(updated_network.types) == 2

        expected_net_type = None
        for t in new_template.templatetypes:
            if t.resource_type == 'NETWORK':
                expected_net_type = t.id

        network_types = [t.id for t in updated_network.types]

        assert expected_net_type in network_types

        client.delete_template(new_template.id)

        with pytest.raises(HydraError):
            client.get_template(new_template.id)

        network_deleted_templatetypes = client.get_network(network.id)

        assert len(network_deleted_templatetypes.types) == 1
示例#31
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]