def test_get_value_for_key(self): """Test get value for key works""" make_padang_layer() dialog = KeywordsDialog(PARENT, IFACE) expected_value = 'hazard' value = dialog.get_value_for_key('category') self.assertEqual(value, expected_value)
def test_addWarningsForColons(self): """Test add entry to list works""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.reset(False) myDialog.add_list_entry('bar', 'fo:o') myResult = myDialog.get_value_for_key('bar') myExpectedResult = 'fo.o' myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage # # Check the user gets a message if they put colons in the value # myExpectedResult = 'Colons are not allowed, replaced with "."' myResult = str(myDialog.lblMessage.text()) myMessage = ('lblMessage error \nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage # # Check the user gets a message if they put colons in the key # myDialog.add_list_entry('ba:r', 'foo') myExpectedResult = 'Colons are not allowed, replaced with "."' myResult = str(myDialog.lblMessage.text()) myMessage = ('lblMessage error \nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage
def show_keywords_editor(self): """Show the keywords editor.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.keywords_dialog import KeywordsDialog # Next block is a fix for #776 if self.iface.activeLayer() is None: return try: keyword_io = KeywordIO() keyword_io.read_keywords(self.iface.activeLayer()) except UnsupportedProviderError: # noinspection PyUnresolvedReferences,PyCallByClass QMessageBox.warning( None, self.tr('Unsupported layer type'), self.tr('The layer you have selected cannot be used for ' 'analysis because its data type is unsupported.')) return # End of fix for #776 # Fix for #793 except NoKeywordsFoundError: # we will create them from scratch in the dialog pass # End of fix for #793 dialog = KeywordsDialog(self.iface.mainWindow(), self.iface, self.dock_widget) dialog.exec_() # modal
def test_add_warnings_for_colons(self): """Test add entry to list works""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) dialog.add_list_entry('bar', 'fo:o') result = dialog.get_value_for_key('bar') expected_result = 'fo.o' message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) #print 'Dict', dialog.getKeywords() self.assertEqual(result, expected_result, message) # # Check the user gets a message if they put colons in the value # expected_result = 'Colons are not allowed, replaced with "."' result = str(dialog.lblMessage.text()) message = ('lblMessage error \nGot: %s\nExpected: %s\n' % (result, expected_result)) #print 'Dict', dialog.getKeywords() self.assertEqual(result, expected_result, message) # # Check the user gets a message if they put colons in the key # dialog.add_list_entry('ba:r', 'foo') expected_result = 'Colons are not allowed, replaced with "."' result = str(dialog.lblMessage.text()) message = ('lblMessage error \nGot: %s\nExpected: %s\n' % (result, expected_result)) #print 'Dict', dialog.getKeywords() self.assertEqual(result, expected_result, message)
def test_on_pbnAdvanced_toggled(self): """Test advanced button toggle behaviour works""" makePadangLayer() myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.pbnAdvanced myButton.setChecked(False) # noinspection PyArgumentList QTest.mouseClick(myButton, QtCore.Qt.LeftButton) myState = myDialog.grpAdvanced.isHidden() myExpectedState = False myMessage = ('Advanced options did not become visible when' ' the advanced button was clicked\nGot' '%s\nExpected\n%s\n' % (myState, myExpectedState)) assert myState == myExpectedState, myMessage # Now hide advanced again and test... # noinspection PyArgumentList QTest.mouseClick(myButton, QtCore.Qt.LeftButton) myState = myDialog.grpAdvanced.isHidden() myExpectedState = True myMessage = ('Advanced options did not become hidden when' ' the advanced button was clicked again\nGot' '%s\nExpected\n%s\n' % (myState, myExpectedState)) assert not myDialog.grpAdvanced.isVisible(), myMessage
def test_on_rad_postprocessing_toggled(self): """Test hazard radio button toggle behaviour works""" layer = make_polygon_layer() defaults = breakdown_defaults() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) button = dialog.radPostprocessing button.setChecked(False) button.click() message = ('Toggling the postprocessing radio did not add a ' 'category to the keywords list.') self.assertEqual(dialog.get_value_for_key('category'), 'postprocessing', message) message = ('Toggling the postprocessing radio did not add an ' 'aggregation attribute to the keywords list.') self.assertEqual(dialog.get_value_for_key(defaults['AGGR_ATTR_KEY']), 'KAB_NAME', message) message = ('Toggling the postprocessing radio did not add a ' 'female ratio attribute to the keywords list.') self.assertEqual( dialog.get_value_for_key(defaults['FEM_RATIO_ATTR_KEY']), dialog.tr('Use default'), message) message = ('Toggling the postprocessing radio did not add a ' 'female ratio default value to the keywords list.') self.assertEqual( float(dialog.get_value_for_key(defaults['FEM_RATIO_KEY'])), defaults['FEM_RATIO'], message)
def test_on_radPostprocessing_toggled(self): """Test hazard radio button toggle behaviour works""" myLayer = makePolygonLayer() myDefaults = defaults() myDialog = KeywordsDialog(PARENT, IFACE, layer=myLayer) myButton = myDialog.radPostprocessing myButton.setChecked(False) # noinspection PyArgumentList QTest.mouseClick(myButton, QtCore.Qt.LeftButton) myMessage = ('Toggling the postprocessing radio did not add a ' 'category to the keywords list.') assert myDialog.get_value_for_key( 'category') == 'postprocessing', myMessage myMessage = ('Toggling the postprocessing radio did not add an ' 'aggregation attribute to the keywords list.') assert myDialog.get_value_for_key( myDefaults['AGGR_ATTR_KEY']) == 'KAB_NAME', myMessage myMessage = ('Toggling the postprocessing radio did not add a ' 'female ratio attribute to the keywords list.') assert myDialog.get_value_for_key( myDefaults['FEM_RATIO_ATTR_KEY']) == myDialog.tr('Use default'), \ myMessage myMessage = ('Toggling the postprocessing radio did not add a ' 'female ratio default value to the keywords list.') assert float(myDialog.get_value_for_key( myDefaults['FEM_RATIO_KEY'])) == myDefaults['FEM_RATIO'], myMessage
def test_reset(self): """Test form reset works""" dialog = KeywordsDialog(PARENT, IFACE) dialog.leTitle.setText('Foo') dialog.reset(False) expected_result = '' result = dialog.leTitle.text() message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_getValueForKey(self): """Test get value for key works""" makePadangLayer() myDialog = KeywordsDialog(PARENT, IFACE) myExpectedValue = 'hazard' myValue = myDialog.get_value_for_key('category') myMessage = ('\nExpected key value of %s\nGot %s' % (myExpectedValue, myValue)) assert myValue == myExpectedValue, myMessage
def test_set_category(self): """Test set category works.""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) dialog.set_category('hazard') expected_result = 'hazard' result = dialog.get_value_for_key('category') message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_add_list_entry(self): """Test add entry to list works.""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) dialog.add_list_entry('bar', 'foo') result = dialog.get_value_for_key('bar') expected_result = 'foo' message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_on_radHazard_toggled(self): """Test hazard radio button toggle behaviour works""" myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.radHazard myButton.setChecked(False) myButton.click() myMessage = ('Toggling the hazard radio did not add a category ' 'to the keywords list.') assert myDialog.get_value_for_key('category') == 'hazard', myMessage
def show_keywords_editor(self): """Show the keywords editor.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.keywords_dialog import KeywordsDialog if self.iface.activeLayer() is None: return myDialog = KeywordsDialog(self.iface.mainWindow(), self.iface, self.dockWidget) myDialog.exec_() # modal
def test_on_rad_hazard_toggled(self): """Test hazard radio button toggle behaviour works""" dialog = KeywordsDialog(PARENT, IFACE) button = dialog.radHazard button.setChecked(False) button.click() message = ('Toggling the hazard radio did not add a category ' 'to the keywords list.') self.assertEqual(dialog.get_value_for_key('category'), 'hazard', message)
def test_on_radHazard_toggled(self): """Test hazard radio button toggle behaviour works""" myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.radHazard myButton.setChecked(False) # noinspection PyArgumentList QTest.mouseClick(myButton, QtCore.Qt.LeftButton) myMessage = ('Toggling the hazard radio did not add a category ' 'to the keywords list.') assert myDialog.get_value_for_key('category') == 'hazard', myMessage
def test_reset(self): """Test form reset works""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.leTitle.setText('Foo') myDialog.reset(False) myExpectedResult = '' myResult = myDialog.leTitle.text() myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) assert myResult == myExpectedResult, myMessage
def test_remove_iItem_by_key(self): """Test remove item by its key works.""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) dialog.add_list_entry('bar', 'foo') dialog.remove_item_by_key('bar') result = dialog.lstKeywords.count() expected_result = 0 message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_addListEntry(self): """Test add entry to list works""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.reset(False) myDialog.add_list_entry('bar', 'foo') myResult = myDialog.get_value_for_key('bar') myExpectedResult = 'foo' myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage
def test_on_dsbFemaleRatioDefault_valueChanged(self): """Test hazard radio button toggle behaviour works""" myLayer = makePolygonLayer() myDefaults = defaults() myDialog = KeywordsDialog(PARENT, IFACE, layer=myLayer) myButton = myDialog.radPostprocessing myButton.setChecked(False) # noinspection PyArgumentList QTest.mouseClick(myButton, QtCore.Qt.LeftButton) myFemaleRatioAttrBox = myDialog.cboFemaleRatioAttribute #set to Don't use myIndex = myFemaleRatioAttrBox.findText( myDialog.tr('Don\'t use')) myMessage = (myDialog.tr('Don\'t use') + ' not found') assert (myIndex != -1), myMessage myFemaleRatioAttrBox.setCurrentIndex(myIndex) myMessage = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not add it to the keywords list.') assert myDialog.get_value_for_key( myDefaults['FEM_RATIO_ATTR_KEY']) == myDialog.tr('Don\'t use'), \ myMessage myMessage = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not disable dsbFemaleRatioDefault.') myIsEnabled = myDialog.dsbFemaleRatioDefault.isEnabled() assert not myIsEnabled, myMessage myMessage = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not remove the keyword.') assert (myDialog.get_value_for_key(myDefaults['FEM_RATIO']) is None), \ myMessage #set to TEST_REAL myIndex = myFemaleRatioAttrBox.findText('TEST_REAL') myMessage = 'TEST_REAL not found' assert (myIndex != -1), myMessage myFemaleRatioAttrBox.setCurrentIndex(myIndex) myMessage = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not add it to the keywords list.') assert myDialog.get_value_for_key( myDefaults['FEM_RATIO_ATTR_KEY']) == 'TEST_REAL', myMessage myMessage = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not disable dsbFemaleRatioDefault.') myIsEnabled = myDialog.dsbFemaleRatioDefault.isEnabled() assert not myIsEnabled, myMessage myMessage = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not remove the keyword.') assert (myDialog.get_value_for_key(myDefaults['FEM_RATIO']) is None), myMessage
def Xtest_on_radExposure_toggled(self): """Test exposure radio button toggle behaviour works""" # Cannot get this test to work, but it works fine in the safe_qgis myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.radExposure myButton.setChecked(False) myButton.click() myMessage = ('Toggling the exposure radio did not add a category ' 'to the keywords list.') assert myDialog.get_value_for_key('category') == 'exposure', myMessage
def test_setCategory(self): """Test set category works""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.reset(False) myDialog.set_category('hazard') myExpectedResult = 'hazard' myResult = myDialog.get_value_for_key('category') myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage
def test_on_pbn_add_to_list1_clicked(self): """Test adding an item to the list using predefined form works""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) dialog.radPredefined.setChecked(True) dialog.cboKeyword.setCurrentIndex(2) expected_result = 'foo' dialog.lePredefinedValue.setText(expected_result) dialog.on_pbnAddToList1_clicked() result = dialog.get_value_for_key('datatype') message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_removeItemByKey(self): """Test remove item by its key works""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.reset(False) myDialog.add_list_entry('bar', 'foo') myDialog.remove_item_by_key('bar') myResult = myDialog.lstKeywords.count() myExpectedResult = 0 myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage
def test_showHelp(self): """Test that help button works""" # ... and this is how you skip it using nosetests #prevent unreachable code errors in pylint #pylint: disable=W0101 raise SkipTest("This test hangs Jenkins.") # noinspection PyUnreachableCode myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.buttonBox.button(QtGui.QDialogButtonBox.Help) myButton.click() myMessage = 'Help dialog was not created when help button pressed' assert myDialog.helpDialog is not None, myMessage
def test_on_dsb_female_ratio_default_value_changed(self): """Test hazard radio button toggle behaviour works""" layer = make_polygon_layer() defaults = breakdown_defaults() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) button = dialog.radPostprocessing button.setChecked(False) button.click() female_ratio_box = dialog.cboFemaleRatioAttribute #set to Don't use index = female_ratio_box.findText(dialog.tr('Don\'t use')) message = (dialog.tr('Don\'t use') + ' not found') self.assertNotEqual(index, -1, message) female_ratio_box.setCurrentIndex(index) message = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not add it to the keywords list.') self.assertEqual( dialog.get_value_for_key(defaults['FEM_RATIO_ATTR_KEY']), dialog.tr('Don\'t use'), message) message = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not disable dsbFemaleRatioDefault.') is_enabled = dialog.dsbFemaleRatioDefault.isEnabled() assert not is_enabled, message message = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not remove the keyword.') assert (dialog.get_value_for_key(defaults['FEM_RATIO']) is None), \ message #set to TEST_REAL index = female_ratio_box.findText('TEST_REAL') message = 'TEST_REAL not found' assert (index != -1), message female_ratio_box.setCurrentIndex(index) message = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not add it to the keywords list.') assert dialog.get_value_for_key( defaults['FEM_RATIO_ATTR_KEY']) == 'TEST_REAL', message message = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not disable dsbFemaleRatioDefault.') is_enabled = dialog.dsbFemaleRatioDefault.isEnabled() assert not is_enabled, message message = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not remove the keyword.') assert (dialog.get_value_for_key(defaults['FEM_RATIO']) is None), message
def test_on_pbn_add_to_list2_clicked(self): """Test adding an item to the list using user defined form works""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) dialog.radUserDefined.setChecked(True) dialog.leKey.setText('foo') dialog.leValue.setText('bar') expected_result = 'bar' dialog.lePredefinedValue.setText(expected_result) dialog.on_pbnAddToList2_clicked() result = dialog.get_value_for_key('foo') message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_on_pbnAddToList1_clicked(self): """Test adding an item to the list using predefined form works""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.reset(False) myDialog.radPredefined.setChecked(True) myDialog.cboKeyword.setCurrentIndex(2) myExpectedResult = 'foo' myDialog.lePredefinedValue.setText(myExpectedResult) myDialog.on_pbnAddToList1_clicked() myResult = myDialog.get_value_for_key('datatype') myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) assert myResult == myExpectedResult, myMessage
def test_remove_item_by_value(self): """Test remove item by its value works""" make_padang_layer() dialog = KeywordsDialog(PARENT, IFACE) dialog.remove_item_by_value('hazard') keywords = dialog.get_keywords() expected_keywords = { 'source': 'USGS', 'title': 'An earthquake in Padang like in 2009', 'subcategory': 'earthquake', 'unit': 'MMI' } self.assertEqual(keywords, expected_keywords)
def test_layer_without_keywords(self): """Test load state from keywords works""" myDialog = KeywordsDialog(PARENT, IFACE) myLayer = make_keywordless_layer() myDialog.layer = myLayer myDialog.load_state_from_keywords() myKeywords = myDialog.get_keywords() #check that a default title is given (see #https://github.com/AIFDR/inasafe/issues/111) myExpectedKeywords = { 'category': 'exposure', 'title': 'Keywordless Layer'} myMessage = ('\nGot: %s\nExpected: %s\n' % (myKeywords, myExpectedKeywords)) assert myKeywords == myExpectedKeywords, myMessage
def test_removeItemByValue(self): """Test remove item by its value works""" makePadangLayer() myDialog = KeywordsDialog(PARENT, IFACE) myDialog.remove_item_by_value('hazard') myKeywords = myDialog.get_keywords() myExpectedKeywords = {'source': 'USGS', 'title': 'An earthquake in Padang like in 2009', 'subcategory': 'earthquake', 'unit': 'MMI'} myMessage = ('\nGot: %s\nExpected: %s\n' % (myKeywords, myExpectedKeywords)) assert myKeywords == myExpectedKeywords, myMessage