示例#1
0
    def test_delete_scenario(self):
        net = self.test_clone()

        scenarios_before = net.scenarios

        assert len(scenarios_before) == 2

        hydra_base.delete_scenario(scenarios_before[1].id, user_id=self.user_id)

        updated_net = JSONObject(hydra_base.get_network(net.id, user_id=self.user_id))

        scenarios_after_delete = updated_net.scenarios

        assert len(scenarios_after_delete) == 1

        hydra_base.activate_scenario(scenarios_before[1].id, user_id=self.user_id)

        updated_net2 = JSONObject(hydra_base.get_network(net.id, user_id=self.user_id))

        scenarios_after_reactivate = updated_net2.scenarios

        assert len(scenarios_after_reactivate) == 2
        
        hydra_base.delete_scenario(scenarios_before[1].id, user_id=self.user_id)
        hydra_base.clean_up_network(net.id, user_id=self.user_id)
        updated_net3 = JSONObject(hydra_base.get_network(net.id, user_id=self.user_id))
        scenarios_after_cleanup = updated_net3.scenarios
        assert len(scenarios_after_cleanup) == 1
        self.assertRaises(HydraError, self.get_scenario, scenarios_before[1].id)
示例#2
0
def test_export(session_with_pywr_network, root_user_id):
    pywr_network_id, pywr_json_filename = session_with_pywr_network

    # Fetch the network
    network = hydra_base.get_network(pywr_network_id,
                                     user_id=root_user_id,
                                     include_data='Y')
    # Fetch all the attributes
    attributes = hydra_base.get_attributes()
    # TODO not sure why this returns SQLAlchemy object?
    # TODO rename this to map/lookup
    attributes = {attr.id: JSONObject(attr) for attr in attributes}

    # We also need the template to get the node types
    template = JSONObject(
        hydra_base.get_template_by_name(pywr_template_name('Full')))

    exporter = PywrHydraExporter(network, attributes, template)

    pywr_data_exported = exporter.get_pywr_data()

    # Check transformed data is about right
    with open(pywr_json_filename) as fh:
        pywr_data = json.load(fh)

    assert_identical_pywr_data(pywr_data, pywr_data_exported)
示例#3
0
    def test_clone(self):

        network =  self.create_network_with_data()
       
        assert len(network.scenarios) == 1, "The network should have only one scenario!"

        #self.create_constraint(network)
        
        network = JSONObject(hydra_base.get_network(network.id, user_id=self.user_id))

        scenario = network.scenarios[0]
        scenario_id = scenario.id

        clone = self.clone_scenario(scenario_id)
        new_scenario = self.get_scenario(clone.id)


        updated_network = JSONObject(hydra_base.get_network(new_scenario.network_id, user_id=self.user_id))


        assert len(updated_network.scenarios) == 2, "The network should have two scenarios!"

        assert updated_network.scenarios[1].resourcescenarios is not None, "Data was not cloned!"

        scen_2_val = updated_network.scenarios[1].resourcescenarios[0].value.id
        scen_1_val = network.scenarios[0].resourcescenarios[0].value.id
        
        assert scen_2_val == scen_1_val, "Data was not cloned correctly"


  #      scen_1_constraint  = network.scenarios[0].constraints.Constraint[0].value
        #scen_2_constraint  = updated_network.scenarios[1].constraints.Constraint[0].value
#
 #       assert scen_1_constraint == scen_2_constraint, "Constraints did not clone correctly!"
        
        scen_1_resourcegroupitems = network.scenarios[0].resourcegroupitems
        scen_2_resourcegroupitems = updated_network.scenarios[1].resourcegroupitems
        
        assert len(scen_1_resourcegroupitems) == len(scen_2_resourcegroupitems)

        return updated_network
示例#4
0
    def test_set_resourcescenario_dataset(self):

        """
            Test the direct setting of a dataset id on a resource scenario    
        """

        network =  self.create_network_with_data()
       

        network = hydra_base.get_network(network.id, user_id=self.user_id)

        scenario = network.scenarios[0]
        source_scenario_id = scenario.id

        clone = self.clone_scenario(source_scenario_id)
        cloned_scenario = self.get_scenario(clone.id)

        resource_scenario = cloned_scenario.resourcescenarios[0]
        resource_attr_id = resource_scenario.resource_attr_id

        dataset = Dataset()
        dataset.type = 'descriptor'
        dataset.name = 'Max Capacity'
        dataset.unit = 'metres / second'
        dataset.dimension = 'number of units per time unit'

        dataset.value = 'I am an updated test!' 

        new_ds = hydra_base.add_dataset(dataset, user_id=self.user_id)

        hydra_base.set_resourcescenario_dataset(resource_attr_id, source_scenario_id, new_ds.id, user_id=self.user_id)

        updated_net = hydra_base.get_network(network.id, user_id=self.user_id)

        updated_scenario = updated_net.scenarios[0]
        scenario_rs = updated_scenario.resourcescenarios
        for rs in scenario_rs:
            if rs.resource_attr_id == resource_attr_id:
                assert rs.value.value == 'I am an updated test!'
示例#5
0
    def test_purge_scenario(self):
        net = self.test_clone()

        scenarios_before = net.scenarios

        assert len(scenarios_before) == 2

        hydra_base.purge_scenario(scenarios_before[1].id, user_id=self.user_id)

        updated_net = JSONObject(hydra_base.get_network(net.id, user_id=self.user_id))

        scenarios_after = updated_net.scenarios

        assert len(scenarios_after) == 1

        self.assertRaises(HydraError, self.get_scenario, scenarios_before[1].id)

        assert str(scenarios_after[0]) == str(scenarios_before[0])
示例#6
0
    def test_copy_data_from_scenario(self):

        """
            Test copy_data_from_scenario : test that one scenario
            can be updated to contain the data of another with the same
            resource attrs.
        """

        network =  self.create_network_with_data()
       

        network = hydra_base.get_network(network.id, user_id=self.user_id)

        scenario = network.scenarios[0]
        source_scenario_id = scenario.id

        clone = self.clone_scenario(source_scenario_id)
        cloned_scenario = self.get_scenario(clone.id)

        resource_scenario = cloned_scenario.resourcescenarios[0]
        resource_attr_id = resource_scenario.resource_attr_id

        dataset = Dataset()
        dataset.type = 'descriptor'
        dataset.name = 'Max Capacity'
        dataset.unit = 'metres / second'
        dataset.dimension = 'number of units per time unit'
 
        dataset.value = 'I am an updated test!'
 

        hydra_base.add_data_to_attribute(source_scenario_id, resource_attr_id, dataset, user_id=self.user_id)

        scenario_diff = hydra_base.compare_scenarios(source_scenario_id, cloned_scenario.id, user_id=self.user_id)
        
        assert len(scenario_diff.resourcescenariosDiff) == 1, "Data comparison was not successful!"

        hydra_base.copy_data_from_scenario(resource_attr_id, cloned_scenario.id, source_scenario_id, user_id=self.user_id)

        scenario_diff = hydra_base.compare_scenarios(source_scenario_id, cloned_scenario.id, user_id=self.user_id)
        
        assert scenario_diff.resourcescenarios == None, "Scenario update was not successful!"
示例#7
0
    def test_lock_scenario(self):

        network =  self.create_network_with_data()
       
        network = hydra_base.get_network(network.id, user_id=self.user_id)

        scenario_to_lock = network.scenarios[0]
        scenario_id = scenario_to_lock.id
        
        log.info('Cloning scenario %s'%scenario_id)
        clone = self.clone_scenario(scenario_id)
        unlocked_scenario = self.get_scenario(clone.id)
        
        log.info("Locking scenario")
        hydra_base.lock_scenario(scenario_id, user_id=self.user_id)

        locked_scenario = self.get_scenario(scenario_id)

        assert locked_scenario.locked == 'Y'

        dataset = Dataset()
       
        dataset.type = 'descriptor'
        dataset.name = 'Max Capacity'
        dataset.unit = 'metres / second'
        dataset.dimension = 'number of units per time unit'
 
        dataset.value = 'I am an updated test!' 

        
        locked_resource_scenarios = []
        for rs in locked_scenario.resourcescenarios:
            if rs.value.type == 'descriptor':
                locked_resource_scenarios.append(rs)

        unlocked_resource_scenarios = []
        for rs in unlocked_scenario.resourcescenarios:
            if rs.value.type == 'descriptor':
                unlocked_resource_scenarios.append(rs)

        resource_attr_id = unlocked_resource_scenarios[0].resource_attr_id
       
        locked_resource_scenarios_value = None
        for rs in locked_scenario.resourcescenarios:
            if rs.resource_attr_id == resource_attr_id:
                locked_resource_scenarios_value = rs.value

        unlocked_resource_scenarios_value = None
        for rs in unlocked_scenario.resourcescenarios:
            if rs.resource_attr_id == resource_attr_id:
                unlocked_resource_scenarios_value = rs.value
        log.info("Updating a shared dataset")
        ds = unlocked_resource_scenarios_value
        ds.dimension = 'updated_dimension'
        updated_ds = JSONObject(hydra_base.update_dataset(ds, user_id=self.user_id))

        updated_unlocked_scenario = self.get_scenario(unlocked_scenario.id)
        #This should not have changed
        updated_locked_scenario = self.get_scenario(locked_scenario.id)

        locked_resource_scenarios_value = None
        for rs in updated_locked_scenario.resourcescenarios:
            if rs.resource_attr_id == resource_attr_id:
                locked_resource_scenarios_value = rs.value

        unlocked_resource_scenarios_value = None
        for rs in updated_unlocked_scenario.resourcescenarios:
            if rs.resource_attr_id == resource_attr_id:
                unlocked_resource_scenarios_value = rs.value

        self.assertRaises(HydraError, hydra_base.add_data_to_attribute, scenario_id, resource_attr_id, dataset, user_id=self.user_id)
    
        #THe most complicated situation is this:
        #Change a dataset in an unlocked scenario, which is shared by a locked scenario.
        #The original dataset should stay connected to the locked scenario and a new
        #dataset should be created for the edited scenario.
        hydra_base.add_data_to_attribute(unlocked_scenario.id, resource_attr_id, dataset, user_id=self.user_id)

        updated_unlocked_scenario = self.get_scenario(unlocked_scenario.id)
        #This should not have changed
        updated_locked_scenario = self.get_scenario(locked_scenario.id)

        locked_resource_scenarios_value = None
        for rs in updated_locked_scenario.resourcescenarios:
            if rs.resource_attr_id == resource_attr_id:
                locked_resource_scenarios_value = rs.value

        unlocked_resource_scenarios_value = None
        for rs in updated_unlocked_scenario.resourcescenarios:
            if rs.resource_attr_id == resource_attr_id:
                unlocked_resource_scenarios_value = rs.value


        assert locked_resource_scenarios_value.value != unlocked_resource_scenarios_value.value

        item_to_remove = locked_scenario.resourcegroupitems[0].id
        self.assertRaises(HydraError, hydra_base.delete_resourcegroupitem, item_to_remove, user_id=self.user_id)
        log.info("Locking scenario")
        hydra_base.unlock_scenario(scenario_id, user_id=self.user_id)

        locked_scenario = self.get_scenario(scenario_id)

        assert locked_scenario.locked == 'N'
示例#8
0
    def test_compare(self):

        network =  self.create_network_with_data()
       

        assert len(network.scenarios) == 1, "The network should have only one scenario!"

    #    self.create_constraint(network)
        
        network = JSONObject(hydra_base.get_network(network.id, user_id=self.user_id))

        scenario = network.scenarios[0]
        scenario_id = scenario.id

        clone = self.clone_scenario(scenario_id)
        new_scenario = self.get_scenario(clone.id)

    #    self.create_constraint(network, constant=4)

        resource_scenario = new_scenario.resourcescenarios[0]
        resource_attr_id = resource_scenario.resource_attr_id

        dataset = Dataset()
       
        dataset = Dataset()
        dataset.type = 'descriptor'
        dataset.name = 'Max Capacity'
        dataset.unit = 'metres / second'
        dataset.dimension = 'number of units per time unit'
 
        dataset.value ='I am an updated test!' 

        hydra_base.add_data_to_attribute(scenario_id, resource_attr_id, dataset, user_id=self.user_id)

        item_to_remove = new_scenario.resourcegroupitems[0].id
        hydra_base.delete_resourcegroupitem(item_to_remove, user_id=self.user_id)

        updated_network = JSONObject(hydra_base.get_network(new_scenario.network_id, user_id=self.user_id))

        scenarios = updated_network.scenarios
        
        scenario_1 = None
        scenario_2 = None
        for s in scenarios:
            if s.id == new_scenario.id:
                scenario_1 = s 
            else:
                scenario_2 = s

        scenario_diff = hydra_base.compare_scenarios(scenario_1.id, scenario_2.id, user_id=self.user_id)
        
        #print "Comparison result: %s"%(scenario_diff)

        assert len(scenario_diff.resourcescenariosDiff) == 1, "Data comparison was not successful!"

     #   assert len(scenario_diff.constraints.common_constraints) == 1, "Constraint comparison was not successful!"
        
     #   assert len(scenario_diff.constraints.scenario_2_constraints) == 1, "Constraint comparison was not successful!"

        assert len(scenario_diff.groups.scenario_2_items) == 1, "Group comparison was not successful!"
        assert scenario_diff.groups.scenario_1_items is None, "Group comparison was not successful!"

        return updated_network