safe.test.utilities module

Helper module for gui test suite

class safe.test.utilities.FakeLayer(source=None)[source]

Bases: object

A Mock layer.

Parameters:source
source()[source]

Get the sources as defined in init

Returns:sources
class safe.test.utilities.RedirectStreams(stdout=None, stderr=None)[source]

Bases: object

Context manager for redirection of stdout and stderr.

This is from http://stackoverflow.com/questions/6796492/ python-temporarily-redirect-stdout-stderr

In this context, the class is used to get rid of QGIS output in the test suite - BUT IT DOESN’T WORK (Maybe because QGIS starts its providers in a different process?)

Usage:

devnull = open(os.devnull, ‘w’) print(‘Fubar’)

with RedirectStreams(stdout=devnull, stderr=devnull):
print(“You’ll never see me”)

print(“I’m back!”)

safe.test.utilities.assert_hash_for_file(hash_string, filename)[source]

Assert that a files hash matches its expected hash. :param filename: :param hash_string:

safe.test.utilities.assert_hashes_for_file(hashes, filename)[source]

Assert that a files has matches one of a list of expected hashes :param filename: the filename :param hashes: the hash of the file

safe.test.utilities.canvas_list()[source]

Return a string representing the list of canvas layers.

Returns:The returned string will list layers in correct order but formatted with line breaks between each entry.
Return type:str
safe.test.utilities.check_image(control_image_path, test_image_path, tolerance=1000)[source]

Compare a test image against a known good image.

Parameters:
  • tolerance (int) – How many pixels may be different between the two images.
  • test_image_path (str) – The Image being checked (must have same dimensions as the control image).
  • control_image_path (str) – The image representing expected output.
Returns:

Two tuple consisting of success or failure indicator and a message providing analysis comparison notes.

Return type:

(bool, str)

safe.test.utilities.check_images(control_image, test_image_path, tolerance=1000)[source]

Compare a test image against a collection of known good images.

Parameters:
  • tolerance (int) – How many pixels may be different between the two images.
  • test_image_path (str) – The Image being checked (must have same dimensions as the control image). Must be full path to image.
  • control_image (str) – The basename for the control image. The .png extension will automatically be added and the test image dir (safe/test/data/control/images) will be prepended. e.g. addClassToLegend will cause the control image of test/data/control/images/addClassToLegend.png to be used.
Returns:

Success or failure indicator, message providing analysis, comparison notes

Return type:

bool, str

safe.test.utilities.clip_layers(first_layer_path, second_layer_path)[source]

Clip and resample layers with the reference to the first layer.

Parameters:
  • first_layer_path (str) – Path to the first layer path.
  • second_layer_path (str) – Path to the second layer path.
Returns:

Path to the clipped datasets (clipped 1st layer, clipped 2nd layer).

Return type:

tuple(str, str)

:raise
FileNotFoundError
safe.test.utilities.clone_csv_layer(name, source_directory, target_directory='test')[source]

Helper function that copies a test csv layer and returns it.

Parameters:
  • name (str) – The default name for the csv layer.
  • source_directory (str) – Directory where the file is located.
  • target_directory (str) – Subdirectory in InaSAFE temp dir that we want to put the files into. Default to ‘test’.
safe.test.utilities.clone_raster_layer(name, extension, include_keywords, source_directory, target_directory='test')[source]

Helper function that copies a test raster.

Parameters:
  • name (str) – The default name for the raster layer.
  • extension (str) – The extension of the raster file.
  • include_keywords (bool) – Include keywords file if True.
  • source_directory (str) – Directory where the file is located.
  • target_directory (str) – Subdirectory in InaSAFE temp dir that we want to put the files into. Default to ‘testing’.
safe.test.utilities.clone_shp_layer(name, include_keywords, source_directory, target_directory='test')[source]

Helper function that copies a test shp layer and returns it.

Parameters:
  • name (str) – The default name for the shp layer.
  • include_keywords (bool) – Include keywords file if True.
  • source_directory (str) – Directory where the file is located.
  • target_directory (str) – Subdirectory in InaSAFE temp dir that we want to put the files into. Default to ‘test’.
safe.test.utilities.combine_coordinates(x, y)[source]

Make list of all combinations of points for x and y coordinates :param x: :param y:

safe.test.utilities.combos_to_string(dock)[source]

Helper to return a string showing the state of all combos.

Parameters:dock (Dock) – A dock instance to get the state of combos from.
Returns:A descriptive list of the contents of each combo with the active combo item highlighted with a >> symbol.
Return type:unicode
safe.test.utilities.compare_wkt(a, b, tol=1e-06)[source]

Helper function to compare WKT geometries with given tolerance Taken from QGIS test suite

Parameters:
  • a (str) – Input WKT geometry
  • b (str) – Expected WKT geometry
  • tol (float) – compare tolerance
Returns:

True on success, False on failure

Return type:

bool

safe.test.utilities.formatted_list(layer_list)[source]

Return a string representing a list of layers

Parameters:layer_list (list) – A list of layers.
Returns:The returned string will list layers in correct order but formatted with line breaks between each entry.
Return type:str
safe.test.utilities.get_function_index(dock, function_id)[source]

Get the combo index for a function given its function_id.

Parameters:
  • dock (Dock) – A dock instance.
  • function_id (str) – The function id e.g. FloodEvacuationImpactFunction.
safe.test.utilities.get_platform_name()[source]

Get a platform name for this host.

e.g OSX10.8 Windows7-SP1-AMD64 LinuxMint-14-x86_64

safe.test.utilities.get_qgis_app()[source]

Start one QGIS application to test against.

Returns:Handle to QGIS app, canvas, iface and parent. If there are any errors the tuple members will be returned as None.
Return type:(QgsApplication, CANVAS, IFACE, PARENT)

If QGIS is already running the handle to that app will be returned.

safe.test.utilities.get_ui_state(dock)[source]

Get state of the 3 combos on the DOCK dock.

This method is purely for testing and not to be confused with the saveState and restoreState methods of dock.

Parameters:dock (Dock) – The dock instance to get the state from.
Returns:A dictionary of key, value pairs. See below for details.
Return type:dict

Example return:: python

{‘Hazard’: ‘flood’,
‘Exposure’: ‘population’, ‘Impact Function Title’: ‘be affected’, ‘Impact Function Id’: ‘FloodImpactFunction’, ‘Run Button Enabled’: False}
safe.test.utilities.hash_for_file(filename)[source]

Return an md5 checksum for a file :param filename:

safe.test.utilities.load_layer(layer_path)[source]

Helper to load and return a single QGIS layer

Parameters:layer_path (str) – Path name to raster or vector file.
Returns:tuple containing layer and its layer_purpose.
Return type:(QgsMapLayer, str)
safe.test.utilities.load_layers(layer_list, clear_flag=True, dock=None)[source]

Helper function to load layers as defined in a python list.

Parameters:
  • dock (Dock) – A valid dock instance.
  • clear_flag (bool) – Whether to clear currently loaded layers before loading the new layers.
  • layer_list (list(str)) – A list of layer’s paths to load.
safe.test.utilities.load_standard_layers(dock=None)[source]

Helper function to load standard layers into the dialog.

Parameters:dock (Dock) – A valid dock instance.
safe.test.utilities.populate_dock(dock)[source]

A helper function to populate the DOCK and set it to a valid state.

Parameters:dock (Dock) – A dock instance.
safe.test.utilities.remove_vector_temp_file(file_path)[source]

Helper function that removes temp file created during test.

Also its keywords file will be removed.

Parameters:file_path (str) – File path to be removed.
safe.test.utilities.set_batemans_bay_extent(dock=None)[source]

Zoom to an area occupied by both Batemans Bay layers in geo crs.

Parameters:dock (Dock) – A dock widget - if supplied, the extents will also be set as the user extent and an appropriate CRS set.
safe.test.utilities.set_canvas_crs(epsg_id, enable_projection=False)[source]

Helper to set the crs for the CANVAS before a test is run.

Parameters:
  • epsg_id (int) – Valid EPSG identifier
  • enable_projection (bool) – whether on the fly projections should be enabled on the CANVAS. Default to False.
safe.test.utilities.set_geo_extent(bounding_box, dock=None)[source]

Zoom to an area specified given bounding box.

Parameters:
  • bounding_box (list) – List containing [xmin, ymin, xmax, ymax]
  • dock (Dock) – A dock widget - if supplied, the extents will also be set as the user extent and an appropriate CRS set.
safe.test.utilities.set_jakarta_extent(dock=None)[source]

Zoom to an area occupied by both Jakarta layers in Geo.

Parameters:dock (Dock) – A dock widget - if supplied, the extents will also be set as the user extent and an appropriate CRS set.
safe.test.utilities.set_jakarta_google_extent(dock=None)[source]

Zoom to an area occupied by both Jakarta layers in 900913 crs.

Parameters:dock (Dock) – A dock widget - if supplied, the extents will also be set as the user extent and an appropriate CRS set.
safe.test.utilities.set_manila_extent(dock=None)[source]

Zoom to an area occupied by both Manila layers in Geo.

Parameters:dock (Dock) – A dock widget - if supplied, the extents will also be set as the user extent and an appropriate CRS set.
safe.test.utilities.set_padang_extent(dock=None)[source]

Zoom to an area occupied by both both Padang layers.

Parameters:dock (Dock) – A dock widget - if supplied, the extents will also be set as the user extent and an appropriate CRS set.
safe.test.utilities.set_small_jakarta_extent(dock=None)[source]

Zoom to an area occupied by both Jakarta layers in Geo.

Parameters:dock (Dock) – A dock widget - if supplied, the extents will also be set as the user extent and an appropriate CRS set.
safe.test.utilities.set_yogya_extent(dock=None)[source]

Zoom to an area occupied by both Jakarta layers in Geo.

Parameters:dock (Dock) – A dock widget - if supplied, the extents will also be set as the user extent and an appropriate CRS set.
safe.test.utilities.setup_scenario(dock, hazard, exposure, function_id, function=None, ok_button_flag=True, aggregation_layer=None, aggregation_enabled_flag=None)[source]

Helper function to set the gui state to a given scenario.

Parameters:
  • dock (Dock) – Dock instance.
  • hazard (str) – Name of the hazard combo entry to set.
  • exposure (str) – Name of exposure combo entry to set.
  • function (str) – Name of the function combo entry to set.
  • function_id (str) – Impact function id that should be used.
  • ok_button_flag (bool) – Optional - whether the ok button should be enabled after this scenario is set up.
  • aggregation_layer (str) – Optional - which layer should be used for aggregation
  • aggregation_enabled_flag (bool) – Optional -whether it is expected that aggregation should be enabled when the scenario is loaded.

We require both function and function_id because safe allows for multiple functions with the same name but different id’s so we need to be sure we have the right one.

Note

Layers are not actually loaded - the calling function is responsible for that.

Returns:Two tuple indicating if the setup was successful, and a message indicating why it may have failed.
Return type:(bool, str)
safe.test.utilities.test_data_path(*args)[source]

Return the absolute path to the InaSAFE test data or directory path.

New in version 3.0.

Parameters:args (list) – List of path e.g. [‘control’, ‘files’, ‘test-error-message.txt’] or [‘control’, ‘scenarios’] to get the path to scenarios dir.
Returns:Absolute path to the test data or dir path.
Return type:str