def test_edit_activity_param(qtbot): """ Alter names, amounts and formulas. Introduce dependencies through formulas """ table = ActivityParameterTable() qtbot.addWidget(table) table.sync(table.build_df()) # Fill rows with new variables table.rename_parameter(table.proxy_model.index(0, 0), "edit_act_1", True) table.model.setData(table.model.index(0, 2), "test_db3 * 3") table.rename_parameter(table.proxy_model.index(1, 0), "edit_act_2", True) table.model.setData(table.model.index(1, 2), "edit_act_1 - 3") # Test updated values assert ActivityParameter.get(name="edit_act_1").amount == 25.5 assert ActivityParameter.get(name="edit_act_2").amount == 22.5
def get_activity_parameters(self, act): data = [ o.dict for o in ActivityParameter.select().where( ActivityParameter.database == act[0], ActivityParameter.code == act[1], ) ] if not data: return {} dct = self.order_dicts(data, "parameter") dct["group"] = ActivityParameter.get(database=act[0], code=act[1],).group return dct
def parameterize_exchanges(self, key: tuple) -> None: """ Used whenever a formula is set on an exchange in an activity. If no `ActivityParameter` exists for the key, generate one immediately """ if ActivityParameter.get_or_none(database=key[0], code=key[1]) is None: signals.add_activity_parameter.emit(key) param = ActivityParameter.get(database=key[0], code=key[1]) act = bw.get_activity(key) bw.parameters.remove_exchanges_from_group(param.group, act) bw.parameters.add_exchanges_to_group(param.group, act) ActivityParameter.recalculate_exchanges(param.group) signals.parameters_changed.emit()
def as_gsa_tuple(self) -> tuple: """Return the parameter data formatted as follows: - Parameter name - Scope [global/activity] - Associated activity [or None] - Value """ if self.group == "project" or self.group in bw.databases: scope = "global" associated = None else: scope = "activity" p = ActivityParameter.get(name=self.name, group=self.group) associated = (p.database, p.code) return self.name, scope, associated, self.amount
def test_edit_activity_param(qtbot, monkeypatch): """ Alter names, amounts and formulas. Introduce dependencies through formulas """ table = ActivityParameterTable() qtbot.addWidget(table) table.model.sync() # Fill rows with new variables monkeypatch.setattr( QtWidgets.QInputDialog, "getText", staticmethod(lambda *args, **kwargs: ("edit_act_1", True))) table.model.handle_parameter_rename(table.proxy_model.index(0, 0)) table.model.setData(table.model.index(0, 2), "test_db3 * 3") monkeypatch.setattr( QtWidgets.QInputDialog, "getText", staticmethod(lambda *args, **kwargs: ("edit_act_2", True))) table.model.handle_parameter_rename(table.proxy_model.index(1, 0)) table.model.setData(table.model.index(1, 2), "edit_act_1 - 3") # Test updated values assert ActivityParameter.get(name="edit_act_1").amount == 25.5 assert ActivityParameter.get(name="edit_act_2").amount == 22.5