Python Modules

This section details the public API for using the Pact testing package.

Public API Documentation

class ska_pact_tango.pact.Pact(providers, consumer_name, consumer_class=None, pact_dir=None, pact_file_name='')[source]

Bases: object

Represents a contract between a consumer and provider.

Provides Python context handlers to configure the Pact mock service to perform tests with a Tango device (consumer).

Also generates and writes a pact file.

classmethod from_dict(pact_dict: dict)[source]

Generate an instance of Pact from a dictionary

Parameters:

pact_dict (dict) – Pact in a dictionary format

Returns:

an instance of Pact

Return type:

Pact

classmethod from_file(pact_file_path: str)[source]

Parses a pact file and returns an instance of Pact

Parameters:

pact_file_path (str) – Path to a Pact file

Returns:

an instance of Pact

Return type:

Pact

get_interactions_for_provider(provider_name: str) list[source]

Fetch all the interactions associated with the device name passed in.

Parameters:

provider_name (str) – The provider name

Returns:

Interactions for the provider

Return type:

list

to_dict()[source]

Construct a pact dictionary

Returns:

A dictionary of the Pact

Return type:

dict

write_pact()[source]

Write the pact to disk

class ska_pact_tango.consumer.Consumer(name, consumer_cls=None)[source]

Bases: object

A Pact consumer.

Use this class to describe the consumer executing commands on the provider and then use has_pact_with to create a contract with a specific provider.

has_pact_with(providers=[], pact_dir='', pact_file_name='')[source]

Create a contract between the provider and this consumer.

Parameters:
  • providers – A list of providers that this contract has

  • pact_dir (str) – Directory where the resulting pact files will be written. Defaults to the current directory.

  • pact_file_name (str) – The name of the pact file for this interaction. Defaults to <consumer_name>-pact.json

Return type:

pact.Pact

class ska_pact_tango.provider.Interaction[source]

Bases: object

Define an interaction between a consumer and provider

and_given(provider_state_description: str, *args)[source]

Add a description of what state the provider should be in

Parameters:
  • provider_state (str) – Description of the provider state, defaults to “”

  • params (List[str], optional) – A list of string paramters, defaults to []

Returns:

self

Return type:

Provider

execute_request(request: dict, device_proxy: <Mock name='mock.DeviceProxy' id='139680465216960'>)[source]

Execute a request against a Tango device

Parameters:
  • request (dict) – The request to execute

  • device_proxy (tango.DeviceProxy) – The proxy to the provider device

Returns:

The result of the request

Return type:

Any

classmethod from_dict(interaction_dict: dict)[source]

Returns an instance of Interaction

Parameters:

interaction_dict (dict) – Interaction in a dictionary format

Returns:

instance of Interaction

Return type:

Interaction

given(provider_state_description: str, *args)[source]

Description of what state the provider should be in

Parameters:
  • provider_state (str) – Description of the provider state, defaults to “”

  • params (List[str], optional) – A list of string paramters, defaults to []

Returns:

self

Return type:

Provider

setup_provider(device_proxy: <Mock name='mock.DeviceProxy' id='139680465216960'>)[source]

Run through the provider states as defined in the Pact and execute them on the provider

Parameters:

device_proxy (tango.DeviceProxy) – The proxy to the provider

to_dict() dict[source]

Return a dictionary of the Interaction

Returns:

A dictionary of the Interaction

Return type:

dict

upon_receiving(scenario: str = '')[source]

Describe the interaction

Parameters:

scenario (str, optional) – Description of the interaction, defaults to “”

verify_interaction(device_proxy: <Mock name='mock.DeviceProxy' id='139680465216960'>)[source]

Verify the request against the Provider

Parameters:

device_proxy (tango.DeviceProxy) – proxy to the provider device

Raises:

AssertionError – If the request and response from the proxy fails

will_respond_with(response_type, response=None)[source]

Define what the provider should return with.

Parameters:
  • response – The type of the response from the provider

  • response – The provider response

Return type:

Interaction

with_request(*args)[source]

Define the request from the consumer

Reference examples

  • Tango attribute write
    • Shortform
      E.g
      >>> provider_device.random_float = 5.0
      
      Use
      >>> with_request("attribute", "random_float", 5.0)
      
    • Longform
      E.g
      >>> provider_device.write_attribute("random_float", 5.0)
      
      Use
      >>> with_request("method", "write_attribute", "random_float", 5.0)
      OR
      >>> with_request("write_attribute", "random_float", 5.0)
      
  • Tango attribute read
    • Shortform
      E.g
      >>> provider_device.random_float
      
      Use
      >>> with_request("attribute", "random_float")
      
    • Longform
      E.g
      >>> provider_device.read_attribute("random_float")
      
      Use
      >>> with_request("read_attribute", "random_float")
      
  • Tango commands
    • Shortform
      E.g
      >>> provider_device.SomeCommand()
      
      Use
      >>> with_request("command", "SomeCommand")
      
      E.g
      >>> provider_device.SomeCommand(1.0)
      
      Use
      >>> with_request("command", "SomeCommand", 1.0)
      
    • Longform
      E.g
      >>> provider_device.command_inout("SomeCommand")
      
      Use
      >>> with_request("method", "command_inout", "SomeCommand")
      OR
      >>> with_request("command_inout", "SomeCommand")
      
      E.g
      >>> provider_device.command_inout("SomeCommand", 1.0)
      
      Use
      >>> with_request("method", "command_inout", "SomeCommand", 1.0)
      OR
      >>> with_request("command_inout", "SomeCommand", 1.0)
      
Parameters:
  • req_type (string) – read_attribute or command

  • name (string) – The name of the command or attribute

  • arg (Any) – Any argument to be used in the command

Return type:

Interaction

class ska_pact_tango.provider.Provider(device_name: str)[source]

Bases: object

A Pact provider.

add_interaction(interaction: Interaction)[source]

Add an Interaction

Parameters:

interaction (Interaction) – An interaction between the consumer and provider

Returns:

self

Return type:

Provider

classmethod from_dict(provider_dict: dict)[source]

Return an instance of Provider

Parameters:

provider_dict (dict) – Provider in a dictionary format

Returns:

instance of Provider

Return type:

Provider

get_interaction_from_description(description) Optional[Interaction][source]

Returns an interaction that matches the description

Parameters:

description ([type]) – [description]

Returns:

[description]

Return type:

Optional[ska_pact_tango.Interaction]

to_dict() dict[source]

Return a dictionary of the Provider

Returns:

A dictionary of the Provider and it’s interactions

Return type:

dict

ska_pact_tango.verifier.verifier(pact_file_path, description)[source]
A convenience decorator that adds the interaction and device_name to kwargs.

The relevant interaction is looked up from the description.

Parameters:
  • pact_file_path (str) – The path to the Pact file

  • description (str) – The interaction description, as defined in the Pact file