svir.test package

Submodules

svir.test.qgis_interface module

Fake QGIS Interface.

class svir.test.qgis_interface.QgisInterface(canvas)[source]

Bases: PyQt5.QtCore.QObject

Class to expose qgis objects and functions to plugins.

This class is here for enabling us to run unit tests only, so most methods are simply stubs.

activeLayer()[source]

Get pointer to the active layer (layer selected in the legend).

addDockWidget(area, dock_widget)[source]

Add a dock widget to the main window.

Parameters:
  • area – Where in the ui the dock should be placed.
  • dock_widget (QDockWidget) – A dock widget to add to the UI.
addLayer(layer)[source]

Handle a layer being added to the registry so it shows up in canvas.

Parameters:layer – list<QgsMapLayer> list of map layers that were added
addLayers(layers)[source]

Handle layers being added to the registry so they show up in canvas.

Parameters:layers – list<QgsMapLayer> list of map layers that were added

Note

The QgsInterface api does not include this method, it is added here as a helper to facilitate testing.

addRasterLayer(path, base_name)[source]

Add a raster layer given a raster layer file name

Parameters:
  • path (str) – Path to layer.
  • base_name (str) – Base name for layer.
addToolBar(name)[source]

Add toolbar with specified name.

Parameters:name (str) – Name for the toolbar.
addToolBarIcon(action)[source]

Add an icon to the plugins toolbar.

Parameters:action (QAction) – Action to add to the toolbar.
addVectorLayer(path, base_name, provider_key)[source]

Add a vector layer.

Parameters:
  • path (str) – Path to layer.
  • base_name (str) – Base name for layer.
  • provider_key (str) – Provider key e.g. ‘ogr’
currentLayerChanged
layerSavedAs
layers()[source]
legendInterface()[source]

Get the legend.

See also discussion at:

https://github.com/AIFDR/inasafe/pull/924/

Implementation added for version 3.2.

mainWindow()[source]

Return a pointer to the main window.

In case of QGIS it returns an instance of QgisApp.

mapCanvas()[source]

Return a pointer to the map canvas.

messageBar()[source]

Get the message bar.

New in version 3.2.

Returns:A QGIS message bar instance
Return type:QgsMessageBar
newProject()[source]

Create new project.

removeAllLayers()[source]

Remove layers from the canvas before they get deleted.

Note

This is NOT part of the QgisInterface API but is needed to support QgsProject.instance().removeAllLayers().

removeToolBarIcon(action)[source]

Remove an action (icon) from the plugin toolbar.

Parameters:action (QAction) – Action to add to the toolbar.
setActiveLayer(layer)[source]

Set the currently active layer in the legend. :param layer: Layer to make active. :type layer: QgsMapLayer, QgsVectorLayer, QgsRasterLayer

zoomFull()[source]

Zoom to the map full extent.

zoomToActiveLayer()[source]

Zoom to extent of active layer.

zoomToNext()[source]

Zoom to next view extent.

zoomToPrevious()[source]

Zoom to previous view extent.

svir.test.qgis_legend_interface module

Mock like implementation for the QgsLegendInterface - used for testing.

class svir.test.qgis_legend_interface.QgisLegend(canvas)[source]

Bases: object

A fake QgsLegendInterface with minimal implementation.

isLayerVisible(layer)[source]

Fake implementation for QgisLegendInterface.isLayerVisible.

Since this is a fake interface pretending to be a real running QGIS app and we do not have a legend, we work on the premise that if a layer is in the canvas, it is also in the (fake) legend.

Parameters:layer (QgsMapLayer) – A QgsMapLayer that we want to determine if it is visible or not.
Returns:Hard coded to always return true if the layer is in the canvas!
Return type:bool
layers()[source]

Fake implementation for QgisLegendInterface.layers.

Returns:A list of QgsMapLayers - one per layer present in the map layer list will be returned.
Return type:list
setLayerVisible(layer, visibility)[source]

Fake implementation for QgisLegendInterface.setLayerVisible.

Since this is a fake interface pretending to be a real running QGIS app and we do not have a legend, we work on the premise that the layer is in the fake legend and we toggle the visibility.

Parameters:
  • layer (QgsMapLayer) – A QgsMapLayer that we want to set visible.
  • visibility (bool) – A boolean to set the layer visible or not.

svir.test.utilities module

Helper module for gui test suite.

class svir.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 svir.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!”)

svir.test.utilities.assert_and_emit(signal, assertion, p1, p2, msg)[source]
svir.test.utilities.assert_hash_for_file(hash_string, filename)[source]

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

svir.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
svir.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
svir.test.utilities.compare_two_vector_layers(control_layer, test_layer)[source]

Compare two vector layers (same geometries and same attributes)

Parameters:
  • control_layer (QgsVectorLayer) – The control layer.
  • test_layer (QgsVectorLayer) – The layer being checked.
Returns:

Success or failure indicator, message providing notes.

Return type:

bool, str

svir.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

svir.test.utilities.dict_values_sorted(d)[source]

Make sure dict values are sorted when they are sortable. This also works for lists of dicts nd discts of lists

svir.test.utilities.get_control_text(file_name)[source]

Helper to get control text for string compares.

Parameters:file_name (str) – filename
Returns:A string containing the contents of the file.
svir.test.utilities.get_dock()[source]

Get a dock for testing.

If you call this function from a QGIS Desktop, you will get the real dock, however, you use a fake QGIS interface, it will create a fake dock for you.

Returns:A dock.
Return type:QDockWidget
svir.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.

svir.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’, ‘Run Button Enabled’: False}
svir.test.utilities.hash_for_file(filename)[source]

Return an md5 checksum for a file :param filename:

svir.test.utilities.qgis_iface()[source]

Helper method to get the iface for testing.

Returns:The QGIS interface.
Return type:QgsInterface
svir.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.
svir.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.
svir.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.
svir.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.
svir.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.
svir.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.
svir.test.utilities.setup_scenario(dock, hazard, exposure, 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)
svir.test.utilities.standard_data_path(*args)[source]

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

New in version 3.0.

Parameters:*args

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

Module contents