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_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 Xtest_add_warnings_for_colons(self): """Test add add warning fot colons.""" 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) 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)) 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)) self.assertEqual(result, expected_result, message)
def test_on_radExposure_toggled(self): """Test exposure radio button toggle behaviour works""" dialog = KeywordsDialog(PARENT, IFACE) # Set other radio button checked first so that radExposure is not # toggled dialog.radHazard.setChecked(True) # Then click the radExposure button = dialog.radExposure button.click() message = ('Toggling the exposure radio did not add a category ' 'to the keywords list.') assert dialog.get_value_for_key('category') == 'exposure', message
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_radExposure_toggled(self): """Test exposure radio button toggle behaviour works""" dialog = KeywordsDialog(PARENT, IFACE) # Set other radio button checked first so that radExposure is not # toggled dialog.radHazard.setChecked(True) # Then click the radExposure button = dialog.radExposure button.click() message = ( 'Toggling the exposure radio did not add a category ' 'to the keywords list.') assert dialog.get_value_for_key('category') == 'exposure', message
def test_get_value_for_key(self): """Test get_value_for_key works.""" layer = clone_raster_layer(name='tsunami_wgs84', extension='.tif', include_keywords=True, source_directory=test_data_path('hazard')) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) key = 'category' expected_value = 'hazard' value = dialog.get_value_for_key(key) message = 'The value for key %s should be %s, but it returns %s' % ( key, expected_value, value) self.assertEqual(value, expected_value, message)
def Xtest_add_warnings_for_colons(self): """Test add add warning fot colons.""" 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) 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)) 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)) self.assertEqual(result, expected_result, message)
def test_on_pbn_remove_clicked(self): """Test pressing remove works on key list""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) result = dialog.lstKeywords.count() expected_result = 0 message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message) dialog.add_list_entry('bar', 'foo') result = dialog.lstKeywords.count() expected_result = 1 message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_get_value_for_key(self): """Test get_value_for_key works.""" layer = clone_raster_layer( name='tsunami_wgs84', extension='.tif', include_keywords=True, source_directory=test_data_path('hazard') ) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) key = 'category' expected_value = 'hazard' value = dialog.get_value_for_key(key) message = 'The value for key %s should be %s, but it returns %s' % ( key, expected_value, value) self.assertEqual(value, expected_value, message)
def test_set_subcategory_list(self): """Test set subcategory list works""" dialog = KeywordsDialog(PARENT, IFACE) subcategory_list = OrderedDict([ ('population [density]', 'population [density]'), ('population [count]', 'population [count]'), ('building', 'building'), ('building [osm]', 'building [osm]'), ('building [sigab]', 'building [sigab]'), ('road', 'road') ]) selected_item = 'building' dialog.set_subcategory_list(subcategory_list, selected_item) result = str(dialog.cboSubcategory.currentText()) message = ('\nGot: %s\nExpected: %s\n' % (result, selected_item)) self.assertTrue(result == selected_item, message)
def test_on_subcategory_currentindexchanged(self): """Test subcategory combo change event works""" dialog = KeywordsDialog(PARENT, IFACE) button = dialog.radHazard button.setChecked(True) button = dialog.radExposure button.click() combo = dialog.cboSubcategory combo.setCurrentIndex(1) # change from 'Not set' to 'structure' message = ( 'Changing the subcategory did not add %s to the keywords list' % combo.currentText()) key = dialog.get_value_for_key('subcategory') self.assertTrue(key is not None, message) assert key in str(combo.currentText()), message
def test_set_subcategory_list(self): """Test set subcategory list works""" dialog = KeywordsDialog(PARENT, IFACE) subcategory_list = OrderedDict([ ('population [density]', 'population [density]'), ('population [count]', 'population [count]'), ('building', 'building'), ('building [osm]', 'building [osm]'), ('building [sigab]', 'building [sigab]'), ('road', 'road')]) selected_item = 'building' dialog.set_subcategory_list(subcategory_list, selected_item) result = str(dialog.cboSubcategory.currentText()) message = ('\nGot: %s\nExpected: %s\n' % (result, selected_item)) self.assertTrue(result == selected_item, message)
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_load_state_from_keywords(self): """Test load_state_from_keywords works.""" layer = clone_raster_layer(name='padang_tsunami_mw8', extension='.tif', include_keywords=True, source_directory=test_data_path('hazard')) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.load_state_from_keywords() keywords = dialog.get_keywords() expected_keywords = { 'title': 'A tsunami in Padang (Mw 8.8)', 'category': 'hazard', 'subcategory': 'tsunami', 'unit': 'm' } message = 'The keyword should be %s, but it returns %s' % ( expected_keywords, keywords) self.assertEqual(keywords, expected_keywords, message)
def test_on_rad_postprocessing_toggled(self): """Test postprocessing radio button toggle behaviour works""" layer = clone_shp_layer( name='district_osm_jakarta', include_keywords=True, source_directory=test_data_path('boundaries')) defaults = get_defaults() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) # Click hazard/exposure button first so that it won't take default # from keywords file dialog.radExposure.click() # Now click the postprocessing button button = dialog.radPostprocessing 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['FEMALE_RATIO_ATTR_KEY']), dialog.global_default_string, 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['FEMALE_RATIO_KEY'])), defaults['FEMALE_RATIO'], message)
def test_load_state_from_keywords(self): """Test load_state_from_keywords works.""" layer = clone_raster_layer(name='tsunami_wgs84', extension='.tif', include_keywords=True, source_directory=test_data_path('hazard')) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.load_state_from_keywords() keywords = dialog.get_keywords() expected_keywords = { 'category': 'hazard', 'unit': 'metres_depth', 'subcategory': 'tsunami', 'data_type': 'continuous', 'title': 'Tsunami' } message = 'The keyword should be %s, but it returns %s' % ( expected_keywords, keywords) self.assertEqual(keywords, expected_keywords, message)
def test_add_user_defined_keyword(self): """Test add user defined keyword when ok button is pressed.""" layer = clone_raster_layer(name='padang_tsunami_mw8', extension='.tif', include_keywords=True, source_directory=test_data_path('hazard')) # Set the keywords dialog dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.radUserDefined.setChecked(True) dialog.leKey.setText('foo') dialog.leValue.setText('bar') ok_button = dialog.buttonBox.button(QtGui.QDialogButtonBox.Ok) ok_button.click() expected_result = 'bar' result = dialog.get_value_for_key('foo') message = 'The key %s should have value %s, but it returns %s' % ( 'foo', expected_result, result) self.assertEqual(result, expected_result, message)
def test_load_state_from_keywords(self): """Test load_state_from_keywords works.""" layer = clone_raster_layer( name='padang_tsunami_mw8', extension='.tif', include_keywords=True, source_directory=test_data_path('hazard') ) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.load_state_from_keywords() keywords = dialog.get_keywords() expected_keywords = { 'title': 'A tsunami in Padang (Mw 8.8)', 'category': 'hazard', 'subcategory': 'tsunami', 'unit': 'm'} message = 'The keyword should be %s, but it returns %s' % ( expected_keywords, keywords) self.assertEqual(keywords, expected_keywords, message)
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_load_state_from_keywords(self): """Test load_state_from_keywords works.""" layer = clone_raster_layer( name='tsunami_wgs84', extension='.tif', include_keywords=True, source_directory=test_data_path('hazard') ) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.load_state_from_keywords() keywords = dialog.get_keywords() expected_keywords = { 'category': 'hazard', 'unit': 'metres_depth', 'subcategory': 'tsunami', 'data_type': 'continuous', 'title': 'Tsunami'} message = 'The keyword should be %s, but it returns %s' % ( expected_keywords, keywords) self.assertEqual(keywords, expected_keywords, message)
def test_add_user_defined_keyword(self): """Test add user defined keyword when ok button is pressed.""" layer = clone_raster_layer( name='padang_tsunami_mw8', extension='.tif', include_keywords=True, source_directory=test_data_path('hazard') ) # Set the keywords dialog dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.radUserDefined.setChecked(True) dialog.leKey.setText('foo') dialog.leValue.setText('bar') ok_button = dialog.buttonBox.button(QtGui.QDialogButtonBox.Ok) ok_button.click() expected_result = 'bar' result = dialog.get_value_for_key('foo') message = 'The key %s should have value %s, but it returns %s' % ( 'foo', expected_result, result) self.assertEqual(result, expected_result, message)
def test_show_help(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 dialog = KeywordsDialog(PARENT, IFACE) button = dialog.buttonBox.button(QtGui.QDialogButtonBox.Help) button.click() message = 'Help dialog was not created when help button pressed' self.assertTrue(dialog.helpDialog is not None, message)
def test_on_rad_postprocessing_toggled(self): """Test postprocessing radio button toggle behaviour works""" layer = clone_shp_layer(name='district_osm_jakarta', include_keywords=True, source_directory=test_data_path('boundaries')) defaults = get_defaults() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) # Click hazard/exposure button first so that it won't take default # from keywords file dialog.radExposure.click() # Now click the postprocessing button button = dialog.radPostprocessing 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['FEMALE_RATIO_ATTR_KEY']), dialog.global_default_string, 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['FEMALE_RATIO_KEY'])), defaults['FEMALE_RATIO'], message)
def test_on_pbn_add_to_list1_clicked(self): """Test adding an item to the list using predefined form works""" dialog = KeywordsDialog(PARENT, IFACE, layer=None) 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_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_dsb_female_ratio_default_value_changed(self): """Test hazard radio button toggle behaviour works""" layer = clone_shp_layer(name='district_osm_jakarta', include_keywords=True, source_directory=test_data_path('boundaries')) defaults = get_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.do_not_use_string) message = (dialog.do_not_use_string + ' 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['FEMALE_RATIO_ATTR_KEY']), dialog.do_not_use_string, 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['FEMALE_RATIO']) is None), \ message # set to PEREMPUAN attribute attribute = 'PEREMPUAN' index = female_ratio_box.findText(attribute) message = 'The attribute %s is not found in the layer' % attribute self.assertNotEqual(index, -1, message) female_ratio_box.setCurrentIndex(index) message = ('Toggling the female ratio attribute combo to %s' ' did not add it to the keywords list.') % attribute self.assertEqual( dialog.get_value_for_key(defaults['FEMALE_RATIO_ATTR_KEY']), attribute, message) message = ('Toggling the female ratio attribute combo to %s' ' did not disable dsbFemaleRatioDefault.') % attribute is_enabled = dialog.dsbFemaleRatioDefault.isEnabled() self.assertFalse(is_enabled, message) message = ('Toggling the female ratio attribute combo to %s' ' did not remove the keyword.') % attribute self.assertIsNone(dialog.get_value_for_key(defaults['FEMALE_RATIO']), message)
def test_check_aggregation(self): """Test for keywords dialog's behavior for aggregation layer.""" layer = clone_shp_layer(name='district_osm_jakarta', include_keywords=True, source_directory=test_data_path('boundaries')) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) # Load existing keywords keywords = dialog.get_keywords() expected_keywords = { u'category': u'postprocessing', u'aggregation attribute': u'KAB_NAME', u'title': u'D\xedstr\xedct\'s of Jakarta', u'elderly ratio attribute': u'Global default', u'youth ratio default': u'0.26', u'elderly ratio default': u'0.08', u'adult ratio attribute': u'Global default', u'female ratio attribute': u'PEREMPUAN', u'youth ratio attribute': u'Global default', u'adult ratio default': u'0.66' } message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) # Check age ratios are valid good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (True, good_sum_ratio) self.assertEqual(True, good_sum_ratio, message) # Change youth ratio attribute to Don't Use dialog.cboYouthRatioAttribute.setCurrentIndex(1) keywords = dialog.get_keywords() expected_keywords = { u'category': u'postprocessing', u'aggregation attribute': u'KAB_NAME', u'title': u'D\xedstr\xedct\'s of Jakarta', u'elderly ratio attribute': u'Global default', u'elderly ratio default': u'0.08', u'adult ratio attribute': u'Global default', u'female ratio attribute': u'PEREMPUAN', u'youth ratio attribute': u'Don\'t use', u'adult ratio default': u'0.66' } message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (True, good_sum_ratio) self.assertEqual(True, good_sum_ratio, message) # Change youth ratio attribute to Global Default # Change youth ratio default to 0.99 dialog.cboYouthRatioAttribute.setCurrentIndex(0) dialog.dsbYouthRatioDefault.setValue(0.99) keywords = dialog.get_keywords() expected_keywords = { u'category': u'postprocessing', u'aggregation attribute': u'KAB_NAME', u'title': u'D\xedstr\xedct\'s of Jakarta', u'elderly ratio attribute': u'Global default', u'elderly ratio default': u'0.08', u'adult ratio attribute': u'Global default', u'female ratio attribute': u'PEREMPUAN', u'youth ratio attribute': u'Global default', u'youth ratio default': u'0.99', u'adult ratio default': u'0.66' } message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (False, good_sum_ratio) self.assertEqual(False, good_sum_ratio, message) # We need to delete reference to layer on Windows before removing # the files del layer del dialog.layer # Using clone_shp_layer the files are saved in testing dir under # InaSAFE temp dir shutil.rmtree(temp_dir(sub_dir='testing'))
def test_check_aggregation(self): """Test for keywords dialog's behavior for aggregation layer.""" layer = clone_shp_layer( name='district_osm_jakarta', include_keywords=True, source_directory=test_data_path('boundaries')) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) # Load existing keywords keywords = dialog.get_keywords() expected_keywords = { u'category': u'postprocessing', u'aggregation attribute': u'KAB_NAME', u'title': u'D\xedstr\xedct\'s of Jakarta', u'elderly ratio attribute': u'Global default', u'youth ratio default': u'0.26', u'elderly ratio default': u'0.08', u'adult ratio attribute': u'Global default', u'female ratio attribute': u'PEREMPUAN', u'youth ratio attribute': u'Global default', u'adult ratio default': u'0.66'} message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) # Check age ratios are valid good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (True, good_sum_ratio) self.assertEqual(True, good_sum_ratio, message) # Change youth ratio attribute to Don't Use dialog.cboYouthRatioAttribute.setCurrentIndex(1) keywords = dialog.get_keywords() expected_keywords = { u'category': u'postprocessing', u'aggregation attribute': u'KAB_NAME', u'title': u'D\xedstr\xedct\'s of Jakarta', u'elderly ratio attribute': u'Global default', u'elderly ratio default': u'0.08', u'adult ratio attribute': u'Global default', u'female ratio attribute': u'PEREMPUAN', u'youth ratio attribute': u'Don\'t use', u'adult ratio default': u'0.66'} message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (True, good_sum_ratio) self.assertEqual(True, good_sum_ratio, message) # Change youth ratio attribute to Global Default # Change youth ratio default to 0.99 dialog.cboYouthRatioAttribute.setCurrentIndex(0) dialog.dsbYouthRatioDefault.setValue(0.99) keywords = dialog.get_keywords() expected_keywords = { u'category': u'postprocessing', u'aggregation attribute': u'KAB_NAME', u'title': u'D\xedstr\xedct\'s of Jakarta', u'elderly ratio attribute': u'Global default', u'elderly ratio default': u'0.08', u'adult ratio attribute': u'Global default', u'female ratio attribute': u'PEREMPUAN', u'youth ratio attribute': u'Global default', u'youth ratio default': u'0.99', u'adult ratio default': u'0.66'} message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (False, good_sum_ratio) self.assertEqual(False, good_sum_ratio, message) # We need to delete reference to layer on Windows before removing # the files del layer del dialog.layer # Using clone_shp_layer the files are saved in testing dir under # InaSAFE temp dir shutil.rmtree(temp_dir(sub_dir='testing'))
class Plugin(object): """The QGIS interface implementation for the InaSAFE plugin. This class acts as the 'glue' between QGIS and our custom logic. It creates a toolbar and menu bar entry and launches the InaSAFE user interface if these are activated. """ def __init__(self, iface): """Class constructor. On instantiation, the plugin instance will be assigned a copy of the QGIS iface object which will allow this plugin to access and manipulate the running QGIS instance that spawned it. :param iface:Quantum GIS iface instance. This instance is automatically passed to the plugin by QGIS when it loads the plugin. :type iface: QGisAppInterface """ # Save reference to the QGIS interface self.iface = iface self.dock_widget = None self.action_import_dialog = None self.action_save_scenario = None self.action_batch_runner = None self.action_shake_converter = None self.action_minimum_needs = None self.action_global_minimum_needs = None self.action_impact_merge_dlg = None self.key_action = None self.action_options = None self.action_keywords_dialog = None self.action_keywords_wizard = None self.action_function_centric_wizard = None self.action_extent_selector = None self.translator = None self.toolbar = None self.actions = [] # list of all QActions we create for InaSAFE self.action_dock = None self.action_toggle_rubberbands = None self.message_bar_item = None # Flag indicating if toolbar should show only common icons or not self.full_toolbar = False # print self.tr('InaSAFE') # For enable/disable the keyword editor icon self.iface.currentLayerChanged.connect(self.layer_changed) # noinspection PyArgumentList def change_i18n(self, new_locale): """Change internationalisation for the plugin. Override the system locale and then see if we can get a valid translation file for whatever locale is effectively being used. :param new_locale: the new locale i.e. 'id', 'af', etc. :type new_locale: str :raises: TranslationLoadException """ os.environ['INASAFE_LANG'] = str(new_locale) LOGGER.debug('%s %s %s' % ( new_locale, QLocale.system().name(), os.environ['INASAFE_LANG'])) root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) translation_path = os.path.join( root, 'safe_qgis', 'i18n', 'inasafe_' + str(new_locale) + '.qm') if os.path.exists(translation_path): self.translator = QTranslator() result = self.translator.load(translation_path) if not result: message = 'Failed to load translation for %s' % new_locale raise TranslationLoadError(message) # noinspection PyTypeChecker,PyCallByClass QCoreApplication.installTranslator(self.translator) LOGGER.debug('%s %s' % ( translation_path, os.path.exists(translation_path))) # noinspection PyMethodMayBeStatic def tr(self, message): """Get the translation for a string using Qt translation API. We implement this ourselves since we do not inherit QObject. :param message: String for translation. :type message: str, QString :returns: Translated version of message. :rtype: QString """ # noinspection PyTypeChecker,PyArgumentList,PyCallByClass return QCoreApplication.translate('Plugin', message) def add_action(self, action, add_to_toolbar=True): """Add a toolbar icon to the InaSAFE toolbar. :param action: The action that should be added to the toolbar. :type action: QAction :param add_to_toolbar: Flag indicating whether the action should also be added to the InaSAFE toolbar. Defaults to True. :type add_to_toolbar: bool """ # store in the class list of actions for easy plugin unloading self.actions.append(action) self.iface.addPluginToMenu(self.tr('InaSAFE'), action) if add_to_toolbar: self.toolbar.addAction(action) # noinspection PyPep8Naming def initGui(self): """Gui initialisation procedure (for QGIS plugin api). .. note:: Don't change the name of this method from initGui! This method is called by QGIS and should be used to set up any graphical user interface elements that should appear in QGIS by default (i.e. before the user performs any explicit action with the plugin). """ self.toolbar = self.iface.addToolBar('InaSAFE') self.toolbar.setObjectName('InaSAFEToolBar') # Import dock here as it needs to be imported AFTER i18n is set up from safe.gui.widgets.dock import Dock self.dock_widget = None # -------------------------------------- # Create action for plugin dockable window (show/hide) # -------------------------------------- # pylint: disable=W0201 icon = resources_path('img', 'icons', 'icon.svg') self.action_dock = QAction( QIcon(icon), self.tr('Toggle InaSAFE Dock'), self.iface.mainWindow()) self.action_dock.setObjectName('InaSAFEDockToggle') self.action_dock.setStatusTip(self.tr( 'Show/hide InaSAFE dock widget')) self.action_dock.setWhatsThis(self.tr( 'Show/hide InaSAFE dock widget')) self.action_dock.setCheckable(True) self.action_dock.setChecked(True) self.action_dock.triggered.connect(self.toggle_dock_visibility) self.add_action(self.action_dock) # -------------------------------------- # Create action for keywords editor # -------------------------------------- icon = resources_path('img', 'icons', 'show-keyword-editor.svg') self.action_keywords_dialog = QAction( QIcon(icon), self.tr('InaSAFE Keyword Editor'), self.iface.mainWindow()) self.action_keywords_dialog.setStatusTip(self.tr( 'Open InaSAFE keywords editor')) self.action_keywords_dialog.setWhatsThis(self.tr( 'Open InaSAFE keywords editor')) self.action_keywords_dialog.setEnabled(False) self.action_keywords_dialog.triggered.connect( self.show_keywords_editor) self.add_action( self.action_keywords_dialog, add_to_toolbar=self.full_toolbar) # -------------------------------------- # Create action for keywords creation wizard # ------------------------------------- icon = resources_path('img', 'icons', 'show-keyword-wizard.svg') self.action_keywords_wizard = QAction( QIcon(icon), self.tr('InaSAFE Keywords Creation Wizard'), self.iface.mainWindow()) self.action_keywords_wizard.setStatusTip(self.tr( 'Open InaSAFE keywords creation wizard')) self.action_keywords_wizard.setWhatsThis(self.tr( 'Open InaSAFE keywords creation wizard')) self.action_keywords_wizard.setEnabled(False) self.action_keywords_wizard.triggered.connect( self.show_keywords_wizard) self.add_action(self.action_keywords_wizard) # -------------------------------------- # Create action for IF-centric wizard # -------------------------------------- icon = resources_path('img', 'icons', 'show-wizard.svg') self.action_function_centric_wizard = QAction( QIcon(icon), self.tr('InaSAFE Impact Function Centric Wizard'), self.iface.mainWindow()) self.action_function_centric_wizard.setStatusTip(self.tr( 'Open InaSAFE impact function centric wizard')) self.action_function_centric_wizard.setWhatsThis(self.tr( 'Open InaSAFE impact function centric wizard')) self.action_function_centric_wizard.setEnabled(True) self.action_function_centric_wizard.triggered.connect( self.show_function_centric_wizard) self.add_action(self.action_function_centric_wizard) # -------------------------------------- # Create action for options dialog # -------------------------------------- icon = resources_path('img', 'icons', 'configure-inasafe.svg') self.action_options = QAction( QIcon(icon), self.tr('InaSAFE Options'), self.iface.mainWindow()) self.action_options.setStatusTip(self.tr( 'Open InaSAFE options dialog')) self.action_options.setWhatsThis(self.tr( 'Open InaSAFE options dialog')) self.action_options.triggered.connect(self.show_options) self.add_action(self.action_options, add_to_toolbar=self.full_toolbar) # --------------------------------------- # Create action for minimum needs dialog # --------------------------------------- icon = resources_path('img', 'icons', 'show-minimum-needs.svg') self.action_minimum_needs = QAction( QIcon(icon), self.tr('InaSAFE Minimum Needs Tool'), self.iface.mainWindow()) self.action_minimum_needs.setStatusTip(self.tr( 'Open InaSAFE minimum needs tool')) self.action_minimum_needs.setWhatsThis(self.tr( 'Open InaSAFE minimum needs tool')) self.action_minimum_needs.triggered.connect(self.show_minimum_needs) self.add_action( self.action_minimum_needs, add_to_toolbar=self.full_toolbar) # ---------------------------------------------- # Create action for global minimum needs dialog # ---------------------------------------------- icon = resources_path('img', 'icons', 'show-global-minimum-needs.svg') self.action_global_minimum_needs = QAction( QIcon(icon), self.tr('InaSAFE Global Minimum Needs Configuration'), self.iface.mainWindow()) self.action_global_minimum_needs.setStatusTip(self.tr( 'Open InaSAFE global minimum needs configuration')) self.action_global_minimum_needs.setWhatsThis(self.tr( 'Open InaSAFE global minimum needs configuration')) self.action_global_minimum_needs.triggered.connect( self.show_global_minimum_needs_configuration) self.add_action( self.action_global_minimum_needs, add_to_toolbar=self.full_toolbar) # --------------------------------------- # Create action for converter dialog # --------------------------------------- icon = resources_path('img', 'icons', 'show-converter-tool.svg') self.action_shake_converter = QAction( QIcon(icon), self.tr('InaSAFE Converter'), self.iface.mainWindow()) self.action_shake_converter.setStatusTip(self.tr( 'Open InaSAFE Converter')) self.action_shake_converter.setWhatsThis(self.tr( 'Open InaSAFE Converter')) self.action_shake_converter.triggered.connect( self.show_shakemap_importer) self.add_action( self.action_shake_converter, add_to_toolbar=self.full_toolbar) # --------------------------------------- # Create action for batch runner dialog # --------------------------------------- icon = resources_path('img', 'icons', 'show-batch-runner.svg') self.action_batch_runner = QAction( QIcon(icon), self.tr('InaSAFE Batch Runner'), self.iface.mainWindow()) self.action_batch_runner.setStatusTip(self.tr( 'Open InaSAFE Batch Runner')) self.action_batch_runner.setWhatsThis(self.tr( 'Open InaSAFE Batch Runner')) self.action_batch_runner.triggered.connect(self.show_batch_runner) self.add_action( self.action_batch_runner, add_to_toolbar=self.full_toolbar) # --------------------------------------- # Create action for save scenario dialog # --------------------------------------- icon = resources_path('img', 'icons', 'save-as-scenario.svg') self.action_save_scenario = QAction( QIcon(icon), self.tr('Save current scenario'), self.iface.mainWindow()) message = self.tr('Save current scenario to text file') self.action_save_scenario.setStatusTip(message) self.action_save_scenario.setWhatsThis(message) # noinspection PyUnresolvedReferences self.action_save_scenario.triggered.connect(self.save_scenario) self.add_action( self.action_save_scenario, add_to_toolbar=self.full_toolbar) # -------------------------------------- # Create action for import OSM Dialog # -------------------------------------- icon = resources_path('img', 'icons', 'show-osm-download.svg') self.action_import_dialog = QAction( QIcon(icon), self.tr('InaSAFE OpenStreetMap Downloader'), self.iface.mainWindow()) self.action_import_dialog.setStatusTip(self.tr( 'InaSAFE OpenStreetMap Downloader')) self.action_import_dialog.setWhatsThis(self.tr( 'InaSAFE OpenStreetMap Downloader')) self.action_import_dialog.triggered.connect(self.show_osm_downloader) self.add_action(self.action_import_dialog) # -------------------------------------- # Create action for impact layer merge Dialog # -------------------------------------- icon = resources_path('img', 'icons', 'show-impact-merge.svg') self.action_impact_merge_dlg = QAction( QIcon(icon), self.tr('InaSAFE Impact Layer Merge'), self.iface.mainWindow()) self.action_impact_merge_dlg.setStatusTip(self.tr( 'InaSAFE Impact Layer Merge')) self.action_impact_merge_dlg.setWhatsThis(self.tr( 'InaSAFE Impact Layer Merge')) self.action_impact_merge_dlg.triggered.connect(self.show_impact_merge) self.add_action( self.action_impact_merge_dlg, add_to_toolbar=self.full_toolbar) # -------------------------------------- # create dockwidget and tabify it with the legend # -------------------------------------- self.dock_widget = Dock(self.iface) self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dock_widget) myLegendTab = self.iface.mainWindow().findChild(QApplication, 'Legend') if myLegendTab: self.iface.mainWindow().tabifyDockWidget( myLegendTab, self.dock_widget) self.dock_widget.raise_() # # Hook up a slot for when the dock is hidden using its close button # or view-panels # self.dock_widget.visibilityChanged.connect(self.toggle_inasafe_action) # Also deal with the fact that on start of QGIS dock may already be # hidden. self.action_dock.setChecked(self.dock_widget.isVisible()) # pylint: disable=W0201 # --------------------------------------- # Create action for toggling rubber bands # --------------------------------------- icon = resources_path('img', 'icons', 'toggle-rubber-bands.svg') self.action_toggle_rubberbands = QAction( QIcon(icon), self.tr('Toggle scenario outlines'), self.iface.mainWindow()) message = self.tr('Toggle rubber bands showing scenarion extents.') self.action_toggle_rubberbands.setStatusTip(message) self.action_toggle_rubberbands.setWhatsThis(message) # Set initial state self.action_toggle_rubberbands.setCheckable(True) settings = QSettings() flag = bool(settings.value( 'inasafe/showRubberBands', False, type=bool)) self.action_toggle_rubberbands.setChecked(flag) # noinspection PyUnresolvedReferences self.action_toggle_rubberbands.triggered.connect( self.dock_widget.toggle_rubber_bands) self.add_action(self.action_toggle_rubberbands) # --------------------------------------- # Create action for analysis extent dialog # --------------------------------------- icon = resources_path('img', 'icons', 'set-extents-tool.svg') self.action_extent_selector = QAction( QIcon(icon), self.tr('Set the analysis area for InaSAFE'), self.iface.mainWindow()) self.action_extent_selector.setStatusTip(self.tr( 'Set the analysis area for InaSAFE')) self.action_extent_selector.setWhatsThis(self.tr( 'Set the analysis area for InaSAFE')) self.action_extent_selector.triggered.connect( self.show_extent_selector) self.add_action(self.action_extent_selector) # noinspection PyMethodMayBeStatic def clear_modules(self): """Unload inasafe functions and try to return QGIS to before InaSAFE. """ from safe.impact_functions import core core.unload_plugins() # next lets force remove any inasafe related modules modules = [] for module in sys.modules: if 'inasafe' in module: # Check if it is really one of our modules i.e. exists in the # plugin directory tokens = module.split('.') path = '' for myToken in tokens: path += os.path.sep + myToken parent = os.path.abspath(os.path.join( __file__, os.path.pardir, os.path.pardir)) full_path = os.path.join(parent, path + '.py') if os.path.exists(os.path.abspath(full_path)): LOGGER.debug('Removing: %s' % module) modules.append(module) for module in modules: del (sys.modules[module]) for module in sys.modules: if 'inasafe' in module: print module # Lets also clean up all the path additions that were made package_path = os.path.abspath(os.path.join( os.path.dirname(__file__), os.path.pardir)) LOGGER.debug('Path to remove: %s' % package_path) # We use a list comprehension to ensure duplicate entries are removed LOGGER.debug(sys.path) sys.path = [y for y in sys.path if package_path not in y] LOGGER.debug(sys.path) def unload(self): """GUI breakdown procedure (for QGIS plugin api). .. note:: Don't change the name of this method from unload! This method is called by QGIS and should be used to *remove* any graphical user interface elements that should appear in QGIS. """ # Remove the plugin menu item and icon for myAction in self.actions: self.iface.removePluginMenu(self.tr('InaSAFE'), myAction) self.iface.removeToolBarIcon(myAction) self.iface.mainWindow().removeDockWidget(self.dock_widget) self.iface.mainWindow().removeToolBar(self.toolbar) self.dock_widget.setVisible(False) self.dock_widget.destroy() self.iface.currentLayerChanged.disconnect(self.layer_changed) def toggle_inasafe_action(self, checked): """Check or un-check the toggle inaSAFE toolbar button. This slot is called when the user hides the inaSAFE panel using its close button or using view->panels. :param checked: True if the dock should be shown, otherwise False. :type checked: bool """ self.action_dock.setChecked(checked) # Run method that performs all the real work def toggle_dock_visibility(self): """Show or hide the dock widget.""" if self.dock_widget.isVisible(): self.dock_widget.setVisible(False) else: self.dock_widget.setVisible(True) self.dock_widget.raise_() def show_extent_selector(self): """Show the extent selector widget for defining analysis extents.""" # import here only so that it is AFTER i18n set up from safe.gui.tools.extent_selector_dialog import ExtentSelectorDialog widget = ExtentSelectorDialog( self.iface, self.iface.mainWindow(), extent=self.dock_widget.extent.user_extent, crs=self.dock_widget.extent.user_extent_crs) widget.clear_extent.connect( self.dock_widget.extent.clear_user_analysis_extent) widget.extent_defined.connect( self.dock_widget.define_user_analysis_extent) # Needs to be non modal to support hide -> interact with map -> show widget.show() # non modal def show_minimum_needs(self): """Show the minimum needs dialog.""" # import here only so that it is AFTER i18n set up from safe.gui.tools.minimum_needs.needs_calculator_dialog import ( NeedsCalculatorDialog ) dialog = NeedsCalculatorDialog(self.iface.mainWindow()) dialog.show() # non modal def show_global_minimum_needs_configuration(self): """Show the minimum needs dialog.""" # import here only so that it is AFTER i18n set up from safe.gui.tools.minimum_needs.needs_manager_dialog import ( NeedsManagerDialog) dialog = NeedsManagerDialog(self.iface.mainWindow()) dialog.exec_() # modal def show_impact_merge(self): """Show the impact layer merge dialog.""" # import here only so that it is AFTER i18n set up from safe.gui.tools.impact_merge_dialog import ImpactMergeDialog dialog = ImpactMergeDialog(self.iface.mainWindow()) dialog.exec_() # modal def show_options(self): """Show the options dialog.""" # import here only so that it is AFTER i18n set up from safe.gui.tools.options_dialog import OptionsDialog dialog = OptionsDialog( self.iface, self.dock_widget, self.iface.mainWindow()) dialog.exec_() # modal def show_keywords_editor(self): """Show the keywords editor.""" # import here only so that it is AFTER i18n set up from safe.gui.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 # noinspection PyTypeChecker,PyArgumentList 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 # Fix for filtered-layer except InvalidParameterError, e: # noinspection PyTypeChecker,PyTypeChecker,PyArgumentList QMessageBox.warning( None, self.tr('Invalid Layer'), e.message ) return dialog = KeywordsDialog( self.iface.mainWindow(), self.iface, self.dock_widget) dialog.exec_() # modal
def test_layer_without_keywords(self): """Test load state from keywords works.""" layer = make_keywordless_layer() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.load_state_from_keywords()
def test_on_dsb_female_ratio_default_value_changed(self): """Test hazard radio button toggle behaviour works""" layer = clone_shp_layer( name='district_osm_jakarta', include_keywords=True, source_directory=test_data_path('boundaries')) defaults = get_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.do_not_use_string) message = (dialog.do_not_use_string + ' 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['FEMALE_RATIO_ATTR_KEY']), dialog.do_not_use_string, 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['FEMALE_RATIO']) is None), \ message # set to PEREMPUAN attribute attribute = 'PEREMPUAN' index = female_ratio_box.findText(attribute) message = 'The attribute %s is not found in the layer' % attribute self.assertNotEqual(index, -1, message) female_ratio_box.setCurrentIndex(index) message = ( 'Toggling the female ratio attribute combo to %s' ' did not add it to the keywords list.') % attribute self.assertEqual( dialog.get_value_for_key(defaults['FEMALE_RATIO_ATTR_KEY']), attribute, message) message = ( 'Toggling the female ratio attribute combo to %s' ' did not disable dsbFemaleRatioDefault.') % attribute is_enabled = dialog.dsbFemaleRatioDefault.isEnabled() self.assertFalse(is_enabled, message) message = ( 'Toggling the female ratio attribute combo to %s' ' did not remove the keyword.') % attribute self.assertIsNone( dialog.get_value_for_key(defaults['FEMALE_RATIO']), message)