def test_table_uncertainty_delegate(qtbot, bw2test, monkeypatch): """ Open the uncertainty delegate to test all related methods within the table. """ table = ProjectParameterTable() qtbot.addWidget(table) table.add_parameter() table.sync(table.build_df()) assert isinstance(table.itemDelegateForColumn(3), UncertaintyDelegate) delegate = UncertaintyDelegate(table) option = QtWidgets.QStyleOptionViewItem() option.rect = QtCore.QRect(0, 0, 100, 100) index = table.proxy_model.index(0, 3) rect = table.visualRect(index) qtbot.mouseClick(table.viewport(), QtCore.Qt.LeftButton, pos=rect.center()) editor = delegate.createEditor(table, option, index) qtbot.addWidget(editor) # Test displayText assert delegate.displayText("1", None) == "No uncertainty" assert delegate.displayText("nan", None) == "Undefined or unknown uncertainty" delegate.setEditorData(editor, index) delegate.setModelData(editor, table.proxy_model, index) monkeypatch.setattr(QtCore.QModelIndex, "data", lambda *args, **kwargs: "nan") delegate.setEditorData(editor, index)
def test_delete_project_param(qtbot): """ Try and delete project parameters through the tab. """ table = ProjectParameterTable() qtbot.addWidget(table) table.sync(table.build_df()) # The 2nd parameter cannot be deleted param = table.get_parameter(table.proxy_model.index(1, 0)) assert not param.is_deletable() # Delete the 3rd parameter, removing the dependency table.delete_parameter(table.proxy_model.index(2, 0)) # 2nd parameter can now be deleted, so delete it. assert param.is_deletable() table.delete_parameter(table.proxy_model.index(1, 0))
def test_downstream_dependency(qtbot): """ A database parameter uses a project parameter in its formula. Means we can't delete it right? """ table = ProjectParameterTable() qtbot.addWidget(table) table.sync(table.build_df()) # First parameter of the project table is used by the database parameter param = table.get_parameter(table.proxy_model.index(0, 0)) assert not param.is_deletable()
def test_edit_project_param(qtbot): """ Edit the existing parameter to have new values. """ table = ProjectParameterTable() qtbot.addWidget(table) table.sync(table.build_df()) # Edit both the name and the amount of the first parameter. table.rename_parameter(table.proxy_model.index(0, 0), "test_project") table.model.setData(table.model.index(0, 1), 2.5) # Check that parameter is correctly stored in brightway. assert ProjectParameter.get(name="test_project").amount == 2.5 # Now edit the formula directly (without delegate) with qtbot.waitSignal(signals.parameters_changed, timeout=1000): table.model.setData(table.model.index(0, 2), "2 + 3") assert ProjectParameter.get(name="test_project").amount == 5 # Now edit the formula of the 3rd param to use the 2nd param with qtbot.waitSignal(signals.parameters_changed, timeout=1000): table.model.setData(table.model.index(2, 2), "param_2 + 3") assert ProjectParameter.get(name="param_3").amount == 4