broker_utils.testing

Classes and functions used for Pitt-Google broker testing.

class broker.broker_utils.broker_utils.testing.AlertPaths(alert_dir='ztf')[source]

Paths to alerts stored locally.

Load path to alerts directory and initialize variables.

Parameters

alert_dir (Union[str, Path]) – Either the path to a directory containing alerts or one of the following strings. “ALERT_DIR” or “alert_dir”: The path will be obtained from the environment variable ALERT_DIR. “ztf” or “elasticc”: The path will be obtained from the environment variable given by f"ALERT_DIR_{alert_dir.upper()}"

gen()[source]

Return a new generator of paths to Avro files in self.alert_dir.

property path

Return a path to an alert Avro file.

This can be used to obtain a random path to an alert, or within a loop to continuously iterate over paths in the alert directory.

The returned path is the next one in self._mygen (a path generator maintained internally). If you want more control (e.g., to ensure that you iterate over all files in the directory and/or do not recieve duplicate paths), use AlertPaths().gen() instead.

class broker.broker_utils.broker_utils.testing.IntegrationTestValidator(subscrip, published_alert_ids, schema_map, **kwargs)[source]

Functions to validate an integration test.

Initialize attributes.

kwargs can include keys:
max_pulls (int):

maximum number of times to pull the Pub/Sub subscription before quitting.

run()[source]

Pull the subscription and validate that ids match the published alerts.

class broker.broker_utils.broker_utils.testing.Mock(**kwargs)[source]

Mock data and objects useful for testing the broker.

Initialize attributes.

kwargs may contain keys:
modules:

List of module names to mock.

test_alert:

An instance of TestAlert. This will be used to generate appropriate mock data (e.g., Pub/Sub message attributes containing the TestAlert’s alert IDs).

args and kwargs for TestAlert:

These will be used to create a new instance of TestAlert.

property attrs

Mock attributes (dict) for a Pub/Sub message.

property cfinput

Mock input to a Cloud Function.

property id_tuples

Tuples of alert ID keys and IDs.

property module_results

Mock results for the pipeline module(s) given by self.modules.

property modules

List of modules for which mock results should be created.

If this is set manually, self._module_results will be recreated to accommodate.

property my_test_alert

Instance of TestAlert used by self to generate appropriate data.

class broker.broker_utils.broker_utils.testing.TestAlert(path, schema_map, **kwargs)[source]

An alert packet and related functions useful for testing the broker.

Load the alert from path and initialize attributes.

kwargs may contain keys:
drop_cutouts (bool):

Wether to drop the image cutouts from the alert.

serialize (str):

“avro” or “json”. Serialization format of the Pub/Sub message.

mock (Mock):

An instance of Mock containing mocked data which should be attached to the message.

mock_modules (List[str]):

List of modules for which data should be mocked. This will be passed to Mock() as keyword argument “modules”. This is ignored if an instance of Mock is passed using the “mock” keyword argument.

static guess_serializer(topic)[source]

Use the topic to kguess the format that the message is expected to be in.

property mock

Mock data.

If this is set manually, this will also set self.mock_modules = mock.modules

property mock_modules

List of modules for which results should be mocked.

If this is set manually, self._mock will be recreated to accommodate.

property msg_payload

Pub/Sub message payload containing the alert and any mocked results.

property serialize

One of “json” or “avro”. Determines the format of the published message.

If this is set manually, self._msg_payload will be recreated to accommodate. If this is manually deleted, it will be set to the default (“json”) and self._msg_payload will be recreated to accommodate.