def test_result_styling(self): """Test that colours and opacity from a model are correctly styled.""" # Push OK with the left mouse button print '--------------------' print combos_to_string(self.dock) result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) self.dock.accept() # self.dock.analysis.get_impact_layer() safe_layer = self.dock.analysis.impact_layer qgis_layer = read_impact_layer(safe_layer) style = safe_layer.get_style_info() setRasterStyle(qgis_layer, style) # simple test for now - we could test explicity for style state # later if needed. message = ( 'Raster layer was not assigned a Singleband pseudocolor' ' renderer as expected.') self.assertTrue( qgis_layer.renderer().type() == 'singlebandpseudocolor', message)
def test_defaults(self): """Test the GUI in its default state""" print combos_to_string(DOCK) self.assertEqual(DOCK.cboHazard.currentIndex(), 0) self.assertEqual(DOCK.cboExposure.currentIndex(), 0) self.assertEqual(DOCK.cboFunction.currentIndex(), 0) self.assertEqual(DOCK.cboAggregation.currentIndex(), 0)
def test_result_styling(self): """Test that ouputs from a model are correctly styled (colours and opacity. """ # Push OK with the left mouse button print '--------------------' print combos_to_string(DOCK) result, message = setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='People', function='Need evacuation', function_id='Flood Evacuation Function') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) DOCK.accept() # DOCK.analysis.get_impact_layer() safe_layer = DOCK.analysis.get_impact_layer() qgis_layer = read_impact_layer(safe_layer) style = safe_layer.get_style_info() setRasterStyle(qgis_layer, style) # simple test for now - we could test explicity for style state # later if needed. message = ('Raster layer was not assigned a Singleband pseudocolor' ' renderer as expected.') self.assertTrue( qgis_layer.renderer().type() == 'singlebandpseudocolor', message)
def test_state(self): """Check if the save/restore state methods work. See also https://github.com/AIFDR/inasafe/issues/58 """ # default selected layer is the third layer exposure # so, decrease the index by one to change it DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1) DOCK.save_state() expected_dict = get_ui_state(DOCK) # myState = DOCK.state # Now reset and restore and check that it gets the old state # Html is not considered in restore test since the ready # message overwrites it in dock implementation DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1) DOCK.restore_state() result_dict = get_ui_state(DOCK) message = 'Got unexpected state: %s\nExpected: %s\n%s' % ( result_dict, expected_dict, combos_to_string(DOCK)) self.assertTrue(expected_dict == result_dict, message) # Corner case test when two layers can have the # same functions - when switching layers the selected function should # remain unchanged self.tearDown() file_list = [ test_data_path('hazard', 'jakarta_flood_design.tif'), test_data_path('hazard', 'continuous_flood_20_20.asc'), test_data_path('exposure', 'pop_binary_raster_20_20.asc') ] hazard_layer_count, exposure_layer_count = load_layers(file_list) self.assertTrue(hazard_layer_count == 2) self.assertTrue(exposure_layer_count == 1) # we will have 2 impact function available right now: # - ContinuousHazardPopulationFunction, titled: 'Be impacted' # - FloodEvacuationRasterHazardFunction, titled: 'Need evacuation' # set it to the second for testing purposes DOCK.cboFunction.setCurrentIndex(1) DOCK.cboHazard.setCurrentIndex(0) DOCK.cboExposure.setCurrentIndex(0) expected_function = str(DOCK.cboFunction.currentText()) # Now move down one hazard in the combo then verify # the function remains unchanged DOCK.cboHazard.setCurrentIndex(1) current_function = str(DOCK.cboFunction.currentText()) message = ( 'Expected selected impact function to remain unchanged when ' 'choosing a different hazard of the same category:' ' %s\nExpected: %s\n%s' % ( expected_function, current_function, combos_to_string(DOCK))) self.assertTrue(expected_function == current_function, message) DOCK.cboHazard.setCurrentIndex(0) # Selected function should remain the same # RM: modified it, because there is generic one right now as the first. # the selected one should be FloodEvacuationRasterHazardFunction DOCK.cboFunction.setCurrentIndex(1) expected = 'Need evacuation' function = DOCK.cboFunction.currentText() message = 'Expected: %s, Got: %s' % (expected, function) self.assertTrue(function == expected, message)
def test_defaults(self): """Test the GUI in its default state""" print combos_to_string(self.dock) self.assertEqual(self.dock.cboHazard.currentIndex(), 0) self.assertEqual(self.dock.cboExposure.currentIndex(), 0) self.assertEqual(self.dock.cboFunction.currentIndex(), 0) self.assertEqual(self.dock.cboAggregation.currentIndex(), 0)
def test_state(self): """Check if the save/restore state methods work. See also https://github.com/AIFDR/inasafe/issues/58 """ # default selected layer is the third layer exposure # so, decrease the index by one to change it DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1) DOCK.save_state() expected_dict = get_ui_state(DOCK) # myState = DOCK.state # Now reset and restore and check that it gets the old state # Html is not considered in restore test since the ready # message overwrites it in dock implementation DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1) DOCK.restore_state() result_dict = get_ui_state(DOCK) message = 'Got unexpected state: %s\nExpected: %s\n%s' % ( result_dict, expected_dict, combos_to_string(DOCK)) self.assertTrue(expected_dict == result_dict, message) # Corner case test when two layers can have the # same functions - when switching layers the selected function should # remain unchanged self.tearDown() file_list = [ test_data_path('hazard', 'jakarta_flood_design.tif'), test_data_path('hazard', 'continuous_flood_20_20.asc'), test_data_path('exposure', 'pop_binary_raster_20_20.asc') ] hazard_layer_count, exposure_layer_count = load_layers(file_list) self.assertTrue(hazard_layer_count == 2) self.assertTrue(exposure_layer_count == 1) # we will have 2 impact function available right now: # - ContinuousHazardPopulationFunction, titled: 'Be impacted' # - FloodEvacuationRasterHazardFunction, titled: 'Need evacuation' # set it to the second for testing purposes DOCK.cboFunction.setCurrentIndex(1) DOCK.cboHazard.setCurrentIndex(0) DOCK.cboExposure.setCurrentIndex(0) expected_function = str(DOCK.cboFunction.currentText()) # Now move down one hazard in the combo then verify # the function remains unchanged DOCK.cboHazard.setCurrentIndex(1) current_function = str(DOCK.cboFunction.currentText()) message = ( 'Expected selected impact function to remain unchanged when ' 'choosing a different hazard of the same category:' ' %s\nExpected: %s\n%s' % (expected_function, current_function, combos_to_string(DOCK))) self.assertTrue(expected_function == current_function, message) DOCK.cboHazard.setCurrentIndex(0) # Selected function should remain the same # RM: modified it, because there is generic one right now as the first. # the selected one should be FloodEvacuationRasterHazardFunction DOCK.cboFunction.setCurrentIndex(1) expected = 'Need evacuation' function = DOCK.cboFunction.currentText() message = 'Expected: %s, Got: %s' % (expected, function) self.assertTrue(function == expected, message)
def test_state(self): """Check if the save/restore state methods work. See also https://github.com/AIFDR/inasafe/issues/58 """ DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1) DOCK.save_state() expected_dict = get_ui_state(DOCK) # myState = DOCK.state # Now reset and restore and check that it gets the old state # Html is not considered in restore test since the ready # message overwrites it in dock implementation DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1) DOCK.restore_state() result_dict = get_ui_state(DOCK) message = 'Got unexpected state: %s\nExpected: %s\n%s' % ( result_dict, expected_dict, combos_to_string(DOCK)) self.assertTrue(expected_dict == result_dict, message) # Corner case test when two layers can have the # same functions - when switching layers the selected function should # remain unchanged self.tearDown() file_list = [ join(HAZDATA, 'Flood_Design_Depth_Jakarta_geographic.asc'), join(HAZDATA, 'Flood_Current_Depth_Jakarta_geographic.asc'), join(TESTDATA, 'Population_Jakarta_geographic.asc') ] hazard_layer_count, exposure_layer_count = load_layers(file_list) self.assertTrue(hazard_layer_count == 2) self.assertTrue(exposure_layer_count == 1) DOCK.cboFunction.setCurrentIndex(0) DOCK.cboHazard.setCurrentIndex(0) DOCK.cboExposure.setCurrentIndex(0) expected_function = str(DOCK.cboFunction.currentText()) # Now move down one hazard in the combo then verify # the function remains unchanged DOCK.cboHazard.setCurrentIndex(1) current_function = str(DOCK.cboFunction.currentText()) message = ( 'Expected selected impact function to remain unchanged when ' 'choosing a different hazard of the same category:' ' %s\nExpected: %s\n%s' % (expected_function, current_function, combos_to_string(DOCK))) self.assertTrue(expected_function == current_function, message) DOCK.cboHazard.setCurrentIndex(0) # Selected function should remain the same expected = 'Need evacuation' function = DOCK.cboFunction.currentText() message = 'Expected: %s, Got: %s' % (expected, function) self.assertTrue(function == expected, message)
def test_state(self): """Check if the save/restore state methods work. See also https://github.com/AIFDR/inasafe/issues/58 """ DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1) DOCK.save_state() expected_dict = get_ui_state(DOCK) # myState = DOCK.state # Now reset and restore and check that it gets the old state # Html is not considered in restore test since the ready # message overwrites it in dock implementation DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1) DOCK.restore_state() result_dict = get_ui_state(DOCK) message = 'Got unexpected state: %s\nExpected: %s\n%s' % ( result_dict, expected_dict, combos_to_string(DOCK)) self.assertTrue(expected_dict == result_dict, message) # Corner case test when two layers can have the # same functions - when switching layers the selected function should # remain unchanged self.tearDown() file_list = [ join(HAZDATA, 'Flood_Design_Depth_Jakarta_geographic.asc'), join(HAZDATA, 'Flood_Current_Depth_Jakarta_geographic.asc'), join(TESTDATA, 'Population_Jakarta_geographic.asc')] hazard_layer_count, exposure_layer_count = load_layers(file_list) self.assertTrue(hazard_layer_count == 2) self.assertTrue(exposure_layer_count == 1) DOCK.cboFunction.setCurrentIndex(0) DOCK.cboHazard.setCurrentIndex(0) DOCK.cboExposure.setCurrentIndex(0) expected_function = str(DOCK.cboFunction.currentText()) # Now move down one hazard in the combo then verify # the function remains unchanged DOCK.cboHazard.setCurrentIndex(1) current_function = str(DOCK.cboFunction.currentText()) message = ( 'Expected selected impact function to remain unchanged when ' 'choosing a different hazard of the same category:' ' %s\nExpected: %s\n%s' % ( expected_function, current_function, combos_to_string(DOCK))) self.assertTrue(expected_function == current_function, message) DOCK.cboHazard.setCurrentIndex(0) # Selected function should remain the same expected = 'Need evacuation' function = DOCK.cboFunction.currentText() message = 'Expected: %s, Got: %s' % (expected, function) self.assertTrue(function == expected, message)
def test_issue71(self): """Test issue #71 in github - cbo changes should update ok button.""" # See https://github.com/AIFDR/inasafe/issues/71 # Push OK with the left mouse button print 'Using QGIS: %s' % qgis_version() self.tearDown() button = DOCK.pbnRunStop # First part of scenario should have enabled run file_list = [ join(HAZDATA, 'Flood_Current_Depth_Jakarta_geographic.asc'), join(TESTDATA, 'Population_Jakarta_geographic.asc')] hazard_layer_count, exposure_layer_count = load_layers(file_list) message = ( 'Incorrect number of Hazard layers: expected 1 got %s' % hazard_layer_count) self.assertTrue(hazard_layer_count == 1, message) message = ( 'Incorrect number of Exposure layers: expected 1 got %s' % exposure_layer_count) self.assertTrue(exposure_layer_count == 1, message) message = 'Run button was not enabled' self.assertTrue(button.isEnabled(), message) # Second part of scenario - run disabled when adding invalid layer # and select it - run should be disabled path = os.path.join(TESTDATA, 'issue71.tif') file_list = [path] # This layer has incorrect keywords clear_flag = False _, _ = load_layers(file_list, clear_flag) # set exposure to : Population Count (5kmx5km) # by moving one down DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1) actual_dict = get_ui_state(DOCK) expected_dict = { 'Run Button Enabled': False, 'Impact Function Id': '', 'Impact Function Title': '', 'Hazard': 'A flood in Jakarta like in 2007', 'Exposure': 'Population Count (5kmx5km)'} message = (( 'Run button was not disabled when exposure set to \n%s' '\nUI State: \n%s\nExpected State:\n%s\n%s') % ( DOCK.cboExposure.currentText(), actual_dict, expected_dict, combos_to_string(DOCK))) self.assertTrue(expected_dict == actual_dict, message) # Now select again a valid layer and the run button # should be enabled DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1) message = ( 'Run button was not enabled when exposure set to \n%s' % DOCK.cboExposure.currentText()) self.assertTrue(button.isEnabled(), message)
def test_issue71(self): """Test issue #71 in github - cbo changes should update ok button.""" # See https://github.com/AIFDR/inasafe/issues/71 settings = QtCore.QSettings() settings.setValue('inasafe/analysis_extents_mode', 'HazardExposure') self.tearDown() button = self.dock.pbnRunStop # First part of scenario should have enabled run file_list = [ standard_data_path('hazard', 'continuous_flood_20_20.asc'), standard_data_path('exposure', 'pop_binary_raster_20_20.asc') ] hazard_layer_count, exposure_layer_count = load_layers(file_list) message = ('Incorrect number of Hazard layers: expected 1 got %s' % hazard_layer_count) self.assertTrue(hazard_layer_count == 1, message) message = ('Incorrect number of Exposure layers: expected 1 got %s' % exposure_layer_count) self.assertTrue(exposure_layer_count == 1, message) message = 'Run button was not enabled' self.assertTrue(button.isEnabled(), message) # Second part of scenario - run disabled when adding invalid layer # and select it - run should be disabled path = os.path.join(TESTDATA, 'issue71.tif') file_list = [path] # This layer has incorrect keywords clear_flag = False _, _ = load_layers(file_list, clear_flag) # set exposure to : Population Count (5kmx5km) # by moving one down self.dock.cboExposure.setCurrentIndex( self.dock.cboExposure.currentIndex() + 1) actual_dict = get_ui_state(self.dock) expected_dict = { 'Run Button Enabled': False, 'Impact Function Id': '', 'Impact Function Title': '', 'Hazard': 'Continuous Flood', 'Exposure': 'Population Count (5kmx5km)' } message = (('Run button was not disabled when exposure set to \n%s' '\nUI State: \n%s\nExpected State:\n%s\n%s') % (self.dock.cboExposure.currentText(), actual_dict, expected_dict, combos_to_string(self.dock))) self.assertTrue(expected_dict == actual_dict, message) # Now select again a valid layer and the run button # should be enabled self.dock.cboExposure.setCurrentIndex( self.dock.cboExposure.currentIndex() - 1) message = ('Run button was not enabled when exposure set to \n%s' % self.dock.cboExposure.currentText()) self.assertTrue(button.isEnabled(), message)
def test_state(self): """Check if the save/restore state methods work. See also https://github.com/AIFDR/inasafe/issues/58 """ # default selected layer is the third layer exposure # so, decrease the index by one to change it self.dock.cboExposure.setCurrentIndex( self.dock.cboExposure.currentIndex() - 1) self.dock.save_state() expected_dict = get_ui_state(self.dock) # myState = self.dock.state # Now reset and restore and check that it gets the old state # Html is not considered in restore test since the ready # message overwrites it in dock implementation self.dock.cboExposure.setCurrentIndex( self.dock.cboExposure.currentIndex() - 1) self.dock.restore_state() result_dict = get_ui_state(self.dock) message = 'Got unexpected state: %s\nExpected: %s\n%s' % ( result_dict, expected_dict, combos_to_string(self.dock)) self.assertTrue(expected_dict == result_dict, message) # Corner case test when two layers can have the # same functions - when switching layers the selected function should # remain unchanged self.tearDown() file_list = [ test_data_path('hazard', 'jakarta_flood_design.tif'), test_data_path('hazard', 'continuous_flood_20_20.asc'), test_data_path('exposure', 'pop_binary_raster_20_20.asc') ] hazard_layer_count, exposure_layer_count = load_layers(file_list) message = 'Expecting 2 hazard layers, got %s' % hazard_layer_count self.assertTrue(hazard_layer_count == 2, message) message = 'Expecting 1 exposure layer, got %s' % exposure_layer_count self.assertTrue(exposure_layer_count == 1, message) # we will have 1 impact function available right now: # - FloodEvacuationRasterHazardFunction, titled: 'Need evacuation' # set it to the second for testing purposes self.dock.cboFunction.setCurrentIndex(0) self.dock.cboHazard.setCurrentIndex(0) self.dock.cboExposure.setCurrentIndex(0) expected_function = str(self.dock.cboFunction.currentText()) hazard_layer = self.dock.get_hazard_layer() hazard_keywords = self.dock.keyword_io.read_keywords(hazard_layer) exposure_layer = self.dock.get_exposure_layer() exposure_keywords = self.dock.keyword_io.read_keywords(exposure_layer) # Now move down one hazard in the combo then verify # the function remains unchanged self.dock.cboHazard.setCurrentIndex(1) current_function = str(self.dock.cboFunction.currentText()) hazard_layer = self.dock.get_hazard_layer() hazard_keywords = self.dock.keyword_io.read_keywords(hazard_layer) exposure_layer = self.dock.get_exposure_layer() exposure_keywords = self.dock.keyword_io.read_keywords(exposure_layer) from pprint import pprint print 'Exposure keywords' pprint(exposure_keywords) print 'Hazard keywords' pprint(hazard_keywords) message = ( 'Expected selected impact function to remain unchanged when ' 'choosing a different hazard of the same category.\n') message += 'Expected IF: "%s"\n' % expected_function message += 'Current IF: "%s"\n' % current_function message += 'Current Dock State: %s' % combos_to_string(self.dock) message += 'Hazard Keywords\n' message += str(hazard_keywords) + '\n' message += 'Exposure Keywords\n' message += str(exposure_keywords) + '\n' self.assertTrue(expected_function == current_function, message) self.dock.cboHazard.setCurrentIndex(0) # Selected function should remain the same expected = 'Need evacuation' function = self.dock.cboFunction.currentText() message = 'Expected: %s, Got: %s' % (expected, function) self.assertTrue(function == expected, message)
def test_issue71(self): """Test issue #71 in github - cbo changes should update ok button.""" # See https://github.com/AIFDR/inasafe/issues/71 # Push OK with the left mouse button print 'Using QGIS: %s' % qgis_version() settings = QtCore.QSettings() settings.setValue( 'inasafe/analysis_extents_mode', 'HazardExposure') self.tearDown() button = self.dock.pbnRunStop # First part of scenario should have enabled run file_list = [ test_data_path('hazard', 'continuous_flood_20_20.asc'), test_data_path('exposure', 'pop_binary_raster_20_20.asc')] hazard_layer_count, exposure_layer_count = load_layers(file_list) message = ( 'Incorrect number of Hazard layers: expected 1 got %s' % hazard_layer_count) self.assertTrue(hazard_layer_count == 1, message) message = ( 'Incorrect number of Exposure layers: expected 1 got %s' % exposure_layer_count) self.assertTrue(exposure_layer_count == 1, message) message = 'Run button was not enabled' self.assertTrue(button.isEnabled(), message) # Second part of scenario - run disabled when adding invalid layer # and select it - run should be disabled path = os.path.join(TESTDATA, 'issue71.tif') file_list = [path] # This layer has incorrect keywords clear_flag = False _, _ = load_layers(file_list, clear_flag) # set exposure to : Population Count (5kmx5km) # by moving one down self.dock.cboExposure.setCurrentIndex( self.dock.cboExposure.currentIndex() + 1) actual_dict = get_ui_state(self.dock) expected_dict = { 'Run Button Enabled': False, 'Impact Function Id': '', 'Impact Function Title': '', 'Hazard': 'Continuous Flood', 'Exposure': 'Population Count (5kmx5km)'} message = (( 'Run button was not disabled when exposure set to \n%s' '\nUI State: \n%s\nExpected State:\n%s\n%s') % ( self.dock.cboExposure.currentText(), actual_dict, expected_dict, combos_to_string(self.dock))) self.assertTrue(expected_dict == actual_dict, message) # Now select again a valid layer and the run button # should be enabled self.dock.cboExposure.setCurrentIndex( self.dock.cboExposure.currentIndex() - 1) message = ( 'Run button was not enabled when exposure set to \n%s' % self.dock.cboExposure.currentText()) self.assertTrue(button.isEnabled(), message)